Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 is a subclass of XCTest but after reading the Apple docs, it doesn't look any different between the two.

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.
        }
    }

}
like image 787
Laurence Wingo Avatar asked Jan 05 '23 06:01

Laurence Wingo


1 Answers

XCTest is a base class, with empty setUp() and tearDown() methods.

XCTestCase inherits from XCTest, so it inherits those same methods. 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? The test runner can work with anything that subclasses from XCTest, and knows nothing about XCTestCase. This potentially allows us to define new ways of defining test suites other than XCTestCase subclasses, while still integrating with the XCTest framework.

For more about xUnit architecture, see JUnit: A Cook's Tour

like image 158
Jon Reid Avatar answered Jan 06 '23 20:01

Jon Reid