Xcode 10 and super.tearDown

11

自从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.  
        }  
    }  
}  
1个回答

27

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

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

什么情况下才会有多个级别?有两种情况:

  • 当您想为不同的场景重复使用相同的测试时。
  • 当您子类化XCTestCase以制作自定义帮助器时。

这些情况并不经常发生。 但是它们确实发生。 决定“我需要它在这里,但我不需要它在那里”是危险的。 所以我只会一直调用super


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接