【问题标题】:Xcode 10 and super.tearDownXcode 10 和 super.tearDown
【发布时间】:2019-04-26 04:12:32
【问题描述】:

自 Xcode 10.1(可能是 10)以来,当我创建单元测试文件时,我没有调用 super.tearDown() 和 super.setUp() 。

我没有在发行说明中看到这样的变化。

在文档中https://developer.apple.com/documentation/xctest/xctestcase/understanding_setup_and_teardown_for_test_methods 仍然在这里。

所以我的问题是我还应该写 super.tearDown() 和 super.setUp() 吗?

class SomethingTests: XCTestCase {  

    override func setUp() {  
        // Put setup code here. This method is called before the invocation of each test method in the class.  
    }  

    override func tearDown() {  
        // Put teardown code here. This method is called after the invocation of each test method in the class.  
    }  

    func testExample() {  
        // This is an example of a functional test case.  
        // Use XCTAssert and related functions to verify your tests produce the correct results.  
    }  

    func testPerformanceExample() {  
        // This is an example of a performance test case.  
        self.measure {  
            // Put the code you want to measure the time of here.  
        }  
    }  
}  

【问题讨论】:

    标签: xcode xctest teardown


    【解决方案1】:

    对于 XCTestCase 的直接子类,不调用 super.setUp() 的行为从未发生任何变化。那是因为setUptearDown 是模板方法,顶层实现为空。

    虽然行为没有变化,但省略对 super 的调用意味着如果您创建的测试层次结构具有多个级别,则必须将它们添加回来。

    你什么时候会拥有多个级别?有两种情况:

    • 当您希望针对不同场景重复使用相同的测试时。
    • 当您将 XCTestCase 子类化以制作自定义助手时。

    这些不会每天都发生。但它们确实发生了。决定“我在这里需要它,但我在那里不需要它”是危险的。所以我会一直打电话给super

    【讨论】:

      猜你喜欢
      • 2019-03-24
      • 2020-07-03
      • 2017-01-20
      • 1970-01-01
      • 2017-02-19
      • 2017-02-04
      • 2017-02-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多