How to extend XCTest to create custom test framework

Viewed 656

I'm experimenting with creating a XCTest class using the following code:

class NarfTest:XCTest {

    override var testCaseCount: Int {
        return 2
    }

    override var name: String {
        return "abcdef"
    }

    override var testRunClass: AnyClass? {
        return XCTestRun.self
    }


    override func perform(_ run:XCTestRun) {
        run.start()
        run.recordFailure(withDescription: "abc", inFile: "def", atLine: 1, expected: true)
        run.stop()
    }
}

class NarfTestRunner: XCTestCase {

    override class var defaultTestSuite:XCTestSuite {
        let suite = XCTestSuite(name: "hello suite")
        suite.addTest(NarfTest())
        return suite
    }
}

When I run this I get this log:

Test Suite 'Selected tests' started at 2017-11-11 00:30:17.081
Test Suite 'NarfTests.xctest' started at 2017-11-11 00:30:17.082
Test Suite 'hello suite' started at 2017-11-11 00:30:17.083
2017-11-11 00:30:17.082894+1100 xctest[11264:894722] *** Assertion failure in -[NarfTests.NarfTest testRun], /Library/Caches/com.apple.xbs/Sources/XCTest/XCTest-13763/Sources/XCTestFramework/Core/XCAbstractTest.m:98
<unknown>:0: error: hello suite : Invalid test run class: XCTestRun. Must be a subclass of: (null)
(
    0   CoreFoundation                      0x00007fff3230613b __exceptionPreprocess + 171
    1   libobjc.A.dylib                     0x00007fff58ed3c76 objc_exception_throw + 48
    2   CoreFoundation                      0x00007fff3230bed2 +[NSException raise:format:arguments:] + 98
    3   Foundation                          0x00007fff34417260 -[NSAssertionHandler handleFailureInMethod:object:file:lineNumber:description:] + 193
    4   XCTest                              0x00000001003979ad -[XCTest testRun] + 327
    5   XCTest                              0x00000001003979d7 -[XCTest runTest] + 27
    6   XCTest                              0x000000010035ffa6 __27-[XCTestSuite performTest:]_block_invoke + 363
    7   XCTest                              0x000000010035f90d -[XCTestSuite _performProtectedSectionForTest:testSection:] + 26
    8   XCTest                              0x000000010035fb0a -[XCTestSuite performTest:] + 239
....

I've been trying to work out why this code isn't working and studying the code at https://github.com/apple/swift-corelibs-xctest but the code there appears to be different to the XCTest being run by Xcode.

For example, the GitHub version run auto instantiate the class returned from var testRunClass:AnyClass? where as the Xcode version throws the error above.

Then there are the method names. The GitHub project has a method called run() where as the trace above shows a method called runTest().

Has anyone tried creating a custom extension of the 'XCTest' class? How did you get it to work?

P.S. and before anyone asks, I'm not extending XCTestCase because I'm playing around with the idea of creating a test from something other than a method :-)

0 Answers
Related