ios - setUp() 方法在调用每个测试方法之前是如何调用的?

标签 ios swift oop xctest xctestcase

我正在阅读一本关于 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 和第三方测试框架的源代码:

https://github.com/apple/swift-corelibs-xctest

https://github.com/google/googletest

关于ios - setUp() 方法在调用每个测试方法之前是如何调用的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43208384/

相关文章:

php - OOP PHP undefined variable 和空查询

ios - 如何使用位置管理器获得准确的速度

arrays - 字符串数组顺序错误

ios - UITabBarController 和 UINavigationController 中的编辑按钮

oop - 使用什么设计模式?

java - 面向对象 : new class implementing requires additional parameter

ios - 我在共享中看不到选项 'Create NAT64 Network'

ios - Admob 智能横幅广告不会在设备旋转时更新

iOS - NSArray indexOfObject 不调用自定义的 isEqual

ios - SwiftUI - 详细 View 更改为 @fetchRequest 弹出导航链接 View