【发布时间】:2021-04-24 17:02:14
【问题描述】:
Apple 有一个示例说明如何使用 setUp 和 TearDown 方法,如下所示:
class SetUpAndTearDownExampleTestCase: XCTestCase {
override class func setUp() { // 1.
// This is the setUp() class method.
// It is called before the first test method begins.
// Set up any overall initial state here.
}
override func setUpWithError() throws { // 2.
// This is the setUpWithError() instance method.
// It is called before each test method begins.
// Set up any per-test state here.
}
override func setUp() { // 3.
// This is the setUp() instance method.
// It is called before each test method begins.
// Use setUpWithError() to set up any per-test state,
// unless you have legacy tests using setUp().
}
func testMethod1() throws { // 4.
// This is the first test method.
// Your testing code goes here.
addTeardownBlock { // 5.
// Called when testMethod1() ends.
}
}
func testMethod2() throws { // 6.
// This is the second test method.
// Your testing code goes here.
addTeardownBlock { // 7.
// Called when testMethod2() ends.
}
addTeardownBlock { // 8.
// Called when testMethod2() ends.
}
}
override func tearDown() { // 9.
// This is the tearDown() instance method.
// It is called after each test method completes.
// Use tearDownWithError() for any per-test cleanup,
// unless you have legacy tests using tearDown().
}
override func tearDownWithError() throws { // 10.
// This is the tearDownWithError() instance method.
// It is called after each test method completes.
// Perform any per-test cleanup here.
}
override class func tearDown() { // 11.
// This is the tearDown() class method.
// It is called after all test methods complete.
// Perform any overall cleanup here.
}
}
正如您在第一种方法中看到的那样,override class func setUp() 应该设置整体初始状态。我有一个简单的应用程序设置阶段。但是,如果这以某种方式失败。然后测试就会被忽略,就像它从未运行过而不是标记为失败一样。
日志是这样写的:
:0: 错误: simpleUITest : 未能获得匹配的快照: 否 为第一个查询匹配序列找到匹配项:
Descendants matching type Cell->Elements matching predicate '"login_button" IN identifiers',给定输入 App 元素 pid:9849(无属性值 错误)测试套件“simpleUITest”在 2021-01-20 失败 10:49:42.684。执行了 0 次测试,其中 1 次失败(0 次意外) 0.000 (13.204) 秒
我怎样才能让它报告为测试失败,而不是让 XCTest 忽略它?
【问题讨论】:
标签: swift xctest xcodebuild