从 Xcode 10.1(可能是 10)开始,当我创建单元测试文件时,我没有调用 super.tearDown() 和 super.setUp() 。
我在发行说明中没有看到这样的变化。
在文档中https://developer.apple.com/documentation/xctest/xctestcase/understanding_setup_and_teardown_for_test_methods 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
and tearDown
是顶层具有空实现的模板方法。
尽管行为没有改变,但省略了对super
这意味着如果您创建具有多个级别的测试层次结构,则必须将它们添加回来。
你什么时候会拥有多个级别?有两种情况:
- 当您想针对不同场景重复使用相同的测试时。
- 当您子类化 XCTestCase 来制作自定义帮助程序时。
这些并不是每天都会发生。但它们确实发生了。决定“我在这里需要它,但我在那里不需要它”是危险的。所以我就打电话super
每时每刻。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)