【问题标题】:iOS UI Unit Testing (XCode7)iOS UI 单元测试 (XCode7)
【发布时间】:2015-07-20 16:43:55
【问题描述】:

我对苹果在其 XCode7 Beta 中发布的新 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()
}

无论如何我可以避免这种情况吗?如果我尝试对整个套件进行完整测试,这可能会很麻烦。

【问题讨论】:

  • 我打赌你也能解决这个问题stackoverflow.com/questions/31534903/…
  • 我建议您使用方法,例如创建一个对象mainController 并添加方法openMetricstapRevenue。然后,您的代码将类似于 mainController.openMetrics() mainController.tapRevenue() XCTAssert(...)。你会发现一切都会变得更易读、更简单。
  • 有没有比在另一个类中创建辅助方法更有效的方法?

标签: ios xcode7 ui-testing xcode-ui-testing


【解决方案1】:

我相信您正在寻找的是使用 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()
  }

使用这些将测试方法之间的清理工作恢复到应用的原始状态。

【讨论】:

  • 没有。这不是我要找的,因为无论哪种方式你都必须重新启动应用程序.. 这很麻烦
  • 除非我对您的要求感到困惑 - 这就是单元测试的工作方式。如果您基于假设他们首先做了其他事情来测试代码,那么您将在测试逻辑中遇到问题和错误。另外,如果发生剧烈变化,将很难重构。为了方便您在应用程序中围绕该功能编写测试,我建议您在其他评论线程中提到的帮助方法中添加您始终必须运行以达到当前状态的代码块。
猜你喜欢
  • 1970-01-01
  • 2011-07-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多