我对苹果在其XCode7测试版中发布的新的UI单元测试方案感到有点困惑。我认为这是一个很棒的想法,但我有几个问题。
这是我有的一种测试方法。
func testMetricsProperties() {
// Used some of the metrics for testing for reference
let app = XCUIApplication()
app.scrollViews.descendantsMatchingType(.Unknown).containingType(.StaticText, identifier:"rim").childrenMatchingType(.Button).element.tap()
app.textFields["_XCUI:Secure"].typeText("")
app.typeText("\r")
app.buttons["dash metrics"].tap()
let element = app.descendantsMatchingType(.Unknown).containingType(.Image, identifier:"darkBackground.png").childrenMatchingType(.Unknown).element.childrenMatchingType(.Unknown).elementBoundByIndex(1).childrenMatchingType(.Unknown).element.childrenMatchingType(.Unknown).element
let offPlanRevenue = element.childrenMatchingType(.Unknown).elementBoundByIndex(0).staticTexts["OFF PLAN REVENUE"]
offPlanRevenue.tap()
XCTAssert(offPlanRevenue.exists);
XCTAssertEqual(offPlanRevenue.value as! String, "");
}然而,在接下来的测试方法中,似乎我必须重新加载整个应用程序,
let app = XCUIApplication()
app.scrollViews.descendantsMatchingType(.Unknown).containingType(.StaticText, identifier:"im").childrenMatchingType(.Button).element.tap()
app.textFields["_XCUI:Secure"].typeText("")
app.typeText("\r")
app.buttons["dash metrics"].tap()
}有什么我可以避免的吗?如果我试图在整个套件上运行完整的测试,这可能会很麻烦。
发布于 2015-07-21 03:02:28
我相信您正在寻找的是使用setUp()和tearDown()方法。在每个测试方法之前调用setUp(),在类的每个测试方法之后调用tearDown()。
override func setUp() {
super.setUp()
// Put setup code here. This method is called before the invocation of each test method in the class.
}
override func tearDown() {
// Put teardown code here. This method is called after the invocation of each test method in the class.
super.tearDown()
}使用这些方法可以在测试方法之间恢复到应用程序的原始状态。
https://stackoverflow.com/questions/31521916
复制相似问题