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.

Senseful
  • 86,719
  • 67
  • 308
  • 465
Varun Varahabotla
  • 548
  • 2
  • 7
  • 16
  • i bet you can solve this too http://stackoverflow.com/questions/31534903/performance-testing-of-for-loop-in-swift-using-tdd – LC 웃 Jul 21 '15 at 17:15
  • 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 Jul 21 '15 at 17:16
  • Is there a more efficient way than creating helper methods inside of another class? – Varun Varahabotla Jul 22 '15 at 17:48

1 Answers1

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.

Drmorgan
  • 529
  • 4
  • 10
  • can you help me here http://stackoverflow.com/questions/31534903/performance-testing-of-for-loop-in-swift-using-tdd – LC 웃 Jul 21 '15 at 17:15
  • No. This is not what i'm looking for, because either way you'd have to restart the app.. which is troublesome – Varun Varahabotla Jul 31 '15 at 20:19
  • 1
    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. – Drmorgan Aug 03 '15 at 20:53