iOS UI Unit Testing (XCode7)
Asked Answered
D

1

1

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.

Decigram answered 20/7, 2015 at 16:43 Comment(3)
i bet you can solve this too #31535403Barna
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.Burkhard
Is there a more efficient way than creating helper methods inside of another class?Decigram
A
0

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.

Assiut answered 20/7, 2015 at 19:2 Comment(3)
can you help me here #31535403Barna
No. This is not what i'm looking for, because either way you'd have to restart the app.. which is troublesomeDecigram
Unless I'm confused on what you're asking - this is how unit testing works. If you test code based off the assumption they did something else first you're going to run into issues and bugs in your testing logic. Plus it'll be hard to refactor if something dramatic changes. I'd recommend adding the chunk of code you always have to run to get to a current state in a helper method as mentioned in the other comment thread for convenience as you write tests around that functionality in your app.Assiut

© 2022 - 2024 — McMap. All rights reserved.