从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.
}
}
}
最佳答案
对于XCTestCase的直接子类,从未调用super.setUp()
不会改变任何行为。这是因为setUp
和tearDown
是模板方法,其顶层实现为空。
尽管行为没有改变,但省略对super
的调用意味着,如果您创建一个具有多个级别的测试层次结构,则必须将其重新添加。
您什么时候可以拥有多个级别?有两种情况:
这些并非每天都会发生。但是它们确实发生了。确定“我在这里需要它,但在那儿不需要它”是危险的。因此,我一直都会调用
super
。
关于Xcode 10和super.tearDown,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53448538/