Xcode UI Test example
Asked Answered
P

2

20

I have just recently learned about Unit Testing in Xcode. Now I am trying out Xcode 7 and I see there is a new group for UI Tests when I create a new project.

enter image description here

I watched the WWDC 2015 video and it was pretty good, but do you have a super simple example that I could go through myself? The video examples were a little too complex for me.

Notes

Polyethylene answered 24/7, 2015 at 1:33 Comment(0)
P
49

Use Unit Tests to test the validity of methods in your classes. You use them to test the code you have written. (See my other example for setting up a simple Unit Test in Xcode.)

Use UI Tests to check the validity of the User Interface. Think of it like having your own robot to go through and do all the normal interactions with your app that a normal user would. This saves you the time of doing it yourself.

At the time of this writing, it is difficult to access many of the properties of the UI components, but just having a test go through tapping them and swiping them confirms that they are there.

Example

This is about the simplest setup and UI test that I could think of: a button that when pressed changes the text of a label.

Set up

  • Create a new project in Xcode 7+ for iOS 9.0+.
  • Make sure that Include UI Tests is checked

enter image description here

  • If you are adding UI tests to a project created before Xcode 7, see this answer. (File > New > Target > Test > Cocoa Touch UI Testing Bundle)

  • Add a UILabel and a UIButton to the storyboard

enter image description here

  • Create an @IBOutlet and @IBAction in the ViewController and make the label text change when the button is pressed.

    import UIKit
    class ViewController: UIViewController {
        @IBOutlet weak var label: UILabel!
        @IBAction func button(sender: AnyObject) {
            label.text = "Hello"
        }
    }
    

Do the test

  • Open the YourProjectUITests file.

enter image description here

  • Put your curser in the testExample() method. (You can delete the comments)

enter image description here

  • Press the red Record button

enter image description here

  • In the app, (1) tap the label, (2) tap the button, and then (3) tap the label again. (4) Press the Record button again to stop recording. The following code should have been automatically generated for you:

    func testExample() {
    
        let app = XCUIApplication()
        app.staticTexts["Label"].tap()
        app.buttons["Button"].tap()
        app.staticTexts["Hello"].tap()
    }
    
  • Use the staticText lines as a starting point for making an XCTAssert. Now you should have:

    func testExample() {
    
        let app = XCUIApplication()
        XCTAssert(app.staticTexts["Label"].exists)
        app.buttons["Button"].tap()
        XCTAssert(app.staticTexts["Hello"].exists)
    }
    
  • Press the diamond on the left to run the UI Test. It should turn green when it passes.

enter image description here

  • That's it! This showed that the UIButton and UILabel exist and that the text of the label changed. If you want to see it fail (a good idea), you can change "Hello" to something else.

Further study

Polyethylene answered 24/7, 2015 at 1:33 Comment(4)
How is it possible to make sure that it was the label that changed? If it is a button what is changed to "Hello" the test will also pass. One option would be to check that the label and button exists, but this is not generic. What if we want to change the label to empty string instead "Hello"? app.staticTexts[""].exists is almost always true.Shulins
Why can't we go for accessibilityIdentifer to identify elements instead of hardcoded text like "Hello". This always helps, even though the text is changed in future.Potentate
I'm sure that would probably be better. The above answer was something I made as I was trying to figure out UI Testing in the absence of any other simple examples. Feel free to add another answer to this question that improves on my answer.Polyethylene
Is there really no official documentation for how to write UI tests in iOS? I found this page developer.apple.com/documentation/xctest/user_interface_tests but there's no examples and it's certainly not obvious that you would say something like XCTAssert(app.staticTexts["Label"].exists). Where's the official guide?Monaxial
B
7

@Suragch +1 for the answer. One thing I observed and want to share that every function inside the UI Test case must start with "test". You can append extra name after that. Only this way the button(for clicking to start the test) appears.

Bushnell answered 16/11, 2015 at 10:0 Comment(1)
You can suggest for edit or comment. A separate answer does make sense.Lambrecht

© 2022 - 2024 — McMap. All rights reserved.