3

We have a Jenkins job that runs our iOS UI tests, and I need to be able to ignore some tests (or test classes) based on certain runtime information during a test run. According to the XCTestSuite documentation https://developer.apple.com/reference/xctest/xctestsuite you can create a custom test suite programmatically, but I haven't found any examples of how to actually do so. So how exactly does one go about it?

I've toyed with this with the code below as a starting point, but haven't really gotten anywhere. When the IgnoreThisClass is run in Xcode, the test method in it gets run even though the testCaseCount in BaseTestClass' setUp() reports 0 tests in suite. Do I need to override some init() method instead or do something else? Any tips would be greatly appreciated!

import XCTest

class BaseTestClass: XCTestCase {
    var runThis: Bool?

    override func setUp() {
        if runThis != nil {
            let suite = XCTestSuite(name: "Custom suite")

            if runThis! {
                suite.addTest(self)
            }
            print("Testcases in suite \(suite.testCaseCount)")
            // This causes an error that the suite has already started,
            // even though this call is present in Apple's example documentation.
            //suite.run()
        }
        XCUIApplication().launch()
    }

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

// Run this.
class RunThisClass: BaseTestClass {

    override func setUp() {
        super.runThis = true
        super.setUp()
    }

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

    func testThis() {
        print("Test class was run.")
    }
}

// Don't run this.
class IgnoreThisClass: BaseTestClass {

    override func setUp() {
        super.runThis = false
        super.setUp()
    }

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

    func testIgnoreThis() {
        print("Ignored class was run.")
    }
}
4

1 回答 1

0

要实现您想要的,您将无法从 XCTestCase 子类中初始化 XCTestSuite。这是因为为了让 XCTestCase 运行其测试(并因此设置),它必须已经是 XCTestSuite 的一部分 - 因此在已经初始化的测试中初始化自定义测试套件将不起作用。

Xcode 会自动初始化必要的测试类,包括 XCTestSuite,因此您无法控制它的初始化时间,也无法向它发送任何自定义参数。

您最好的选择是extension为 XCTestSuite 添加一个,并在此处添加是否运行套件中包含的测试用例的评估。

于 2017-01-12T08:26:49.253 回答