Xcode 10和super.tearDown

And*_*kyi 4 xcode teardown xctest

从Xcode 10.1(也许是10)开始,当我创建单元测试文件时,我没有调用super.tearDown()和super.setUp()。

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

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

所以我的问题还是应该编写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.  
        }  
    }  
}  
Run Code Online (Sandbox Code Playgroud)

Jon*_*eid 9

对于XCTestCase的直接子类,从未调用不会改变任何行为super.setUp()。这是因为setUptearDown是在顶层具有空实现的模板方法。

尽管行为没有变化,但省略对的调用super意味着,如果您创建一个具有多个级别的测试层次结构,则必须将其重新添加。

您什么时候可以拥有多个级别?有两种情况:

  • 当您想针对不同的场景重用相同的测试时。
  • 当您将XCTestCase子类化为自定义帮助程序时。

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