简体   繁体   English

为什么XCTestCase会覆盖XCTest的设置方法?

[英]Why does XCTestCase override the setup method of XCTest?

I'd like to think that I understand the idea of inheritance but apparently I don't because I'm confused as to why there a setup method in XCTestCase if XCTest provides the setup method in its class? 我想我理解继承的想法,但显然我没有,因为我很困惑为什么XCTestCase中的设置方法,如果XCTest在其类中提供设置方法? XCTestCase is a subclass of XCTest but after reading the Apple docs, it doesn't look any different between the two. 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 is a base class, with empty setUp() and tearDown() methods. XCTest是一个基类,具有空的setUp()tearDown()方法。

XCTestCase inherits from XCTest, so it inherits those same methods. XCTestCase继承自XCTest,因此它继承了相同的方法。 It doesn't have its own implementation of them. 它没有自己的实现。 They're just do-nothing methods, anyway. 无论如何,他们只是无所事事的方法。 They're only defined so that we can override them. 它们只是定义为我们可以覆盖它们。 This is an example of the Template Method design pattern . 这是模板方法设计模式的一个示例。

Why define these methods in XCTest, or have XCTest at all? 为什么在XCTest中定义这些方法,或者根本没有XCTest? The test runner can work with anything that subclasses from XCTest, and knows nothing about XCTestCase. 测试运行器可以处理来自XCTest的子类,并且对XCTestCase一无所知。 This potentially allows us to define new ways of defining test suites other than XCTestCase subclasses, while still integrating with the XCTest framework. 这可能允许我们定义除XCTestCase子类之外的定义测试套件的新方法,同时仍然与XCTest框架集成。

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

You can override a method in a subclass to add more functionality to what the superclass has. 您可以覆盖子类中的方法,以向超类添加更多功能。

You can override the superclass's implementation completely or you can call super.setUp() in the overriding method to execute the superclass's implementation before anything that you add in the override. 您可以完全覆盖超类的实现,也可以在重写方法中调用super.setUp() ,以便在您在覆盖中添加的任何内容之前执行超类的实现。

In tests, it's common for an XCTestCase subclass to override setUp() to add common setup actions for that class, whereas the superclass's implementation will execute common setup actions for your whole suite. 在测试中,XCTestCase子类通常会覆盖setUp()以为该类添加常见的设置操作,而超类的实现将为整个套件执行常见的设置操作。 For example, one XCTestCase subclass will have a setUp() method which launches the app, and a subclass of that class will have a setUp() method which calls its superclass setup and then initializes the area under test in that class. 例如,一个XCTestCase子类将具有启动app的setUp()方法,该类的子类将具有setUp()方法,该方法调用其超类设置,然后初始化该类中的测试区域。 In a unit test, this could mean creating an object, or in a UI test, this could mean navigating to a particular page in your app. 在单元测试中,这可能意味着创建对象,或者在UI测试中,这可能意味着导航到应用中的特定页面。

You need to override XCTestCase's setUp() when you subclass it if you want something to happen during the setup stage of your tests as it has an empty implementation by default. 如果您希望在测试的设置阶段期间发生某些事情,则需要在子类化时重写XCTestCase的setUp() ,因为默认情况下它具有空实现。

The reason that XCTest has a setUp() method defined (even though it does nothing) is to enable other methods on XCTest to call setUp() , for example, invokeTest() on XCTestCase calls setUp() . 这XCTest有一个原因setUp()定义的方法(即使它什么都不做)是使上XCTest其他方法来调用setUp()例如, invokeTest()的调用XCTestCase setUp() This enables users of the framework to specify actions to be done at the beginning of each test by overriding the setUp() method, which does not require any test invocation logic, instead of having to override methods with other logic in them, which we wouldn't necessarily know about and may not implement correctly or at all when overriding the method. 这使得框架的用户可以通过覆盖setUp()方法来指定在每个测试开始时要完成的操作,该方法不需要任何测试调用逻辑,而不必覆盖其中包含其他逻辑的方法,我们不希望这样做在覆盖方法时,必须知道并且可能无法正确实现或根本无法实现。 This model makes setUp() a safe place for you to execute code entirely of your choosing, without needing to worry about whether you have broken the entire test framework. 这个模型使setUp()成为一个安全的地方,让您完全按照自己的选择执行代码,而无需担心是否已经破坏了整个测试框架。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM