1
votes

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.

1
I would recommend you to use methods, e.g. create an object mainController and add methods openMetrics and tapRevenue. Your code will then look like mainController.openMetrics() mainController.tapRevenue() XCTAssert(...). You will see everything will get more readable and simpler. - Sulthan
Is there a more efficient way than creating helper methods inside of another class? - Varun Varahabotla

1 Answers

0
votes

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.