iOS UI 单元测试 (XCode7) [英] iOS UI Unit Testing (XCode7)

查看:34
本文介绍了iOS UI 单元测试 (XCode7)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对苹果在其 XCode7 Beta 中发布的新 UI 单元测试方案感到有些困惑.我认为这是一个很棒的主意,但我有几个问题.

I'm a bit confused with the new UI Unit Testing scheme that apple released in their XCode7 Beta. I think it's an awesome idea, but I have a couple questions.

这是我拥有的一种测试方法...

this is one testing method I have...

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, "");
}

然而,在接下来的测试方法中,我似乎必须再次加载整个应用程序,

However, in the next testing method, it seems that I have to load the entire app again,

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()
}

无论如何我可以避免这种情况吗?如果我想在整个套件上运行完整的测试,这可能会很麻烦.

Is there anyway I can avoid this? This can be troublesome if i'm trying to run a full test on an entire suite.

推荐答案

我相信您正在寻找的是使用 setUp()tearDown() 方法.setUp() 在每个测试方法之前被调用,而 tearDown() 在每个类的测试方法之后被调用.

I believe what you are looking for is using the setUp() and tearDown() methods. setUp() gets called before each test method and tearDown() gets called after each test method for a class.

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()
  }

使用这些在测试方法之间清理回到应用的原始状态.

Use these to clean up between testing methods back to the app's original state.

这篇关于iOS UI 单元测试 (XCode7)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆