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.
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
When you're ready to test, go to a test class and place the cursor inside the test method to record the interaction. From the debug bar, click the Record UI Test button. Xcode will launch the app and run it. You can interact with the element on-screen and perform a sequence of interactions for any UI test.
Enable Unit Tests in Xcode Project While creating a new Project, click the checkbox “Include Unit Tests”, “Include UI Tests”. Once created, you can able to see a folder in the project called “ProjectNameTests” and the XCode already creates a default test case class with a template generated to start working with.
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.
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.
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
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"
}
}
testExample()
method. (You can delete the comments)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.
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.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With