Swift UITest处理所有测试的开始和所有测试用例的完成

时间:2019-01-16 09:50:20

标签: ios swift xcode xctest uitest

用XCTest编写UITest时,我要处理“所有测试的开始”和“所有测试的完成”。我想在测试用例之前“注册”用户,并在所有测试用例之后删除帐户。我不能使用计数器值整数,因为在所有测试用例复位之后,我无法使用该值。我该如何处理“开始完成”?

1 个答案:

答案 0 :(得分:1)

所有这些都在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()
    }

}
相关问题