我知道类是 future 对象的蓝图,并且我正在尝试使用 Swift 更好地掌握 OOP 架构。所以我的问题是,当您从类和实例的角度运行测试时会发生什么过程。我认为我实际上不一定创建了 XCTestCase 子类的实例,但 Xcode 似乎会自动执行此操作。当我构建更多激情项目应用程序时,我通常必须创建一个实例才能使用它,但在测试中我没有这种感觉,它只需按(Command + U)即可工作。我想了解是否创建了一个实例,如果是的话,如何创建?
这是蓝图 XCTestCase 子类的一些示例代码,但我从来不需要实际实例化此类:
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.
}
}
}
最佳答案
当您运行应用程序时,您的代码不负责创建应用程序委托(delegate):UIKit 框架承担此责任。
同样,当您运行测试时,测试运行程序负责实例化您的测试用例。它通过在所有已加载类的列表中搜索属于 XCTestCase 的类来发现它们。然后它询问每个类的测试调用。然后它可以为这些测试方法创建测试用例实例并运行测试。
其工作原理的关键是 Objective-C 运行时提供的丰富元数据以及它提供的用于查询和操作该信息的元编程接口(interface)。
关于ios - XCTestCase子类实例化的过程是怎样的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43208018/