Skip to content

[SR-460] API incompatibility of XCTestCase setUp() and tearDown() methods #407

Closed
@briancroom

Description

@briancroom
Previous ID SR-460
Radar None
Original Reporter @briancroom
Type Bug
Status Closed
Resolution Done
Additional Detail from JIRA
Votes 1
Component/s XCTest
Labels Bug
Assignee @briancroom
Priority Medium

md5: d4d2b61860ed289302e1fd98c142f4e9

Issue Description:

The following trivial test case, taken nearly verbatim from Xcode's "Unit Test Case Class" file template, builds and runs using Apple XCTest, but fails to compile using `swift-corelibs-xctest`:

import XCTest

class Test: XCTestCase {
    var allTests: [(String, () -> Void)] { return [("testExample", testExample)] }

    override func setUp() {
        super.setUp()
    }

    override func tearDown() {
        super.tearDown()
    }

    func testExample() {
        XCTAssertEqual(1, 1)
    }
}

The errors produced are:

/home/osboxes/swift/LetsTryXCTest/Sources/Test.swift:6:19: error: method does not override any method from its superclass
    override func setUp() {
    ~~~~~~~~      ^
/home/osboxes/swift/LetsTryXCTest/Sources/Test.swift:10:19: error: method does not override any method from its superclass
    override func tearDown() {
    ~~~~~~~~      ^
/home/osboxes/swift/LetsTryXCTest/Sources/Test.swift:7:9: error: 'super' members cannot be referenced in a root class
        super.setUp()
        ^
/home/osboxes/swift/LetsTryXCTest/Sources/Test.swift:11:9: error: 'super' members cannot be referenced in a root class
        super.tearDown()
        ^

The reason for this is that when building with Apple XCTest, the `setUp` and `tearDown` methods are defined on the `XCTest` base class and thus must include the `override` modifier when used in client code, whereas `swift-corelibs-xctest` defines them in the `XCTestCase` protocol (or should, anyway. See [this PR](#26

While I am generally a big fan of the idea of defining `XCTestCase` as a protocol instead of a class that must be derived from, I don't think there is any way to maintain source-level compatibility for users of the framework without converting `XCTestCase` to be a class, thus allowing these test lifecycle methods to be overridden.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions