【问题标题】:Swift UITest handle starting of all tests and finishing of all test casesSwift UITest 处理所有测试的开始和所有测试用例的结束
【发布时间】:2019-06-10 09:04:41
【问题描述】:

使用 XCTest 编写 UITest 时,我想处理“开始所有测试”和“完成所有测试”。我想在测试用例之前“注册”用户并在所有测试用例之后删除帐户。我不能使用计数器值整数,因为在所有测试用例之后它都会重置。我该如何处理这个“开始-完成”?

【问题讨论】:

  • 如果我的回答对您有帮助,请采纳吗?

标签: ios swift xcode xctest uitest


【解决方案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()
    }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-10-16
    • 2011-10-04
    • 2016-08-07
    • 2015-12-25
    • 1970-01-01
    • 1970-01-01
    • 2017-09-06
    • 1970-01-01
    相关资源
    最近更新 更多