Swift UITest 处理所有测试的开始和所有测试用例的结束

Swift UITest handle starting of all tests and finishing of all test cases

在使用 XCTest 编写 UITest 时,我想处理 "Starting of all tests" 和 "Finishing of all test"。我想在测试用例之前 "Register" 用户并在所有测试之后删除帐户 case.I 不能使用计数器值整数,因为在所有测试用例之后它会重置。我该如何处理 "Start-Finish" ?

Apple 文档中均有描述。您想专门使用 setUp 和 tearDown。

https://developer.apple.com/documentation/xctest/xctestcase/understanding_setup_and_teardown_for_test_methods

class SetUpAndTearDownExampleTestCase: XCTestCase {

    override class func setUp() { // 1.
        super.setUp()
        // This is the setUp() class method.
        // It is called before the first test method begins.
        // Set up any overall initial state here.
    }

    override func setUp() { // 2.
        super.setUp()
        // This is the setUp() instance method.
        // It is called before each test method begins.
        // Set up any per-test state here.
    }

    func testMethod1() { // 3.
        // This is the first test method.
        // Your testing code goes here.
        addTeardownBlock { // 4.
            // Called when testMethod1() ends.
        }
    }

    func testMethod2() { // 5.
        // This is the second test method.
        // Your testing code goes here.
        addTeardownBlock { // 6.
            // Called when testMethod2() ends.
        }
        addTeardownBlock { // 7.
            // Called when testMethod2() ends.
        }
    }

    override func tearDown() { // 8.
        // This is the tearDown() instance method.
        // It is called after each test method completes.
        // Perform any per-test cleanup here.
        super.tearDown()
    }

    override class func tearDown() { // 9.
        // This is the tearDown() class method.
        // It is called after all test methods complete.
        // Perform any overall cleanup here.
        super.tearDown()
    }

}