我正在阅读一本关于 Swift 中测试驱动开发的好书。我的最终目标是更好地理解 OOP 体系结构。在我阅读这本书时,前面的一节指出,setUp() 方法在我理解的每个测试方法执行对象设置以运行测试以获得通过或失败结果之前被触发。我不确定的是,从架构的角度来看,这怎么可能? Apple 如何能够创建一个类,该类的一个方法在该类中的所有其他方法之前被触发?
下面是一些示例代码:
import XCTest
@testable import FirstDemo
class FirstDemoTests: XCTestCase {
override func setUp() {
super.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.
super.tearDown()
}
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.
}
}
}
最佳答案
我认为 XCTest
类有一个生命周期,例如 UIViewController
。
同viewDidLoad()
在init(coder:)
之后调用 View 加载到内存时,setUp()
方法也是在加载测试类后调用(在对象的生命周期内调用一次)。
您还可以在 github 上调查 native 和第三方测试框架的源代码:
关于ios - setUp() 方法在调用每个测试方法之前是如何调用的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43208384/