gpt4 book ai didi

ios - 为什么XCTestCase会覆盖XCTest的setup方法?

转载 作者:可可西里 更新时间:2023-11-01 00:00:55 24 4
gpt4 key购买 nike

我想认为我理解继承的概念,但显然我不理解,因为我很困惑如果 XCTest 在其类中提供设置方法,为什么在 XCTestCase 中有设置方法? XCTestCase 是 XCTest 的子类,但在阅读 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 是一个基类,具有空的 setUp()tearDown() 方法。

XCTestCase 继承自 XCTest,因此它继承了那些相同的方法。它没有自己的实现。无论如何,它们只是无所事事的方法。它们的定义只是为了让我们可以覆盖它们。这是 Template Method design pattern 的示例.

为什么要在 XCTest 中定义这些方法,或者根本没有 XCTest?测试运行器可以使用 XCTest 的子类的任何东西,并且对 XCTestCase 一无所知。这可能允许我们定义定义测试套件的新方法,而不是 XCTestCase 子类,同时仍然与 XCTest 框架集成。

有关 xUnit 架构的更多信息,请参阅 JUnit: A Cook's Tour

关于ios - 为什么XCTestCase会覆盖XCTest的setup方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43104693/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com