Getting Started with Xcode UI Testing

Getting Started with Xcode UI Testing

11 April 2024 Stephan Petzl Leave a comment Tech-Help

Transitioning from unit testing to UI testing in Xcode can be a significant leap for developers. With the introduction of UI Testing in Xcode 7, Apple has provided a powerful tool for ensuring the user interface of your app works as expected. In this guide, we will walk you through a simple example to get you started with UI Testing in Xcode.

Understanding UI Tests

Unlike Unit Tests, which verify the correctness of the code logic, UI Tests simulate user interactions with the application’s interface. This automation can be likened to a robot performing all the standard operations a user would, such as tapping buttons and entering text, thus saving you time and ensuring consistency in testing.

Setting Up Your UI Test

Here’s how to set up a basic UI Test environment:

  1. Create a new project in Xcode 7 or later, targeting iOS 9.0 or later. Ensure that the “Include UI Tests” option is selected.
  2. If you are adding UI Tests to an existing project, you can add a new UI Testing Bundle by navigating to File > New > Target > Test > Cocoa Touch UI Testing Bundle.
  3. Add a UILabel and a UIButton to your storyboard.
  4. Create corresponding @IBOutlet and @IBAction in your ViewController to update the label when the button is pressed. For example:
    import UIKit
    class ViewController: UIViewController {
        @IBOutlet weak var label: UILabel!
        @IBAction func button(sender: AnyObject) {
            label.text = "Hello"

Writing Your First UI Test

With your UI set up, it’s time to write the test:

  1. Open the YourProjectUITests.swift file in your project.
  2. Place your cursor inside the testExample() method and remove any existing comments.
  3. Press the red Record button at the bottom of the screen.
  4. Interact with your app in the simulator by tapping the label, then the button, and the label again to change its text.
  5. Stop the recording, and Xcode will generate code based on your interactions. You will see something similar to this:
    func testExample() {
        let app = XCUIApplication()
  6. Modify the generated code to include assertions that validate the UI elements’ existence and state, like so:
    func testExample() {
        let app = XCUIApplication()
  7. Run the test by clicking the diamond icon next to the test method. A green indicator signifies a passing test.

Additional Tips

Remember that all functions in your UI Test case class should start with ‘test’ followed by the name of the test. This naming convention is necessary for Xcode to recognize the method as a test and provide you with the option to run it.


By following the steps above, you have created a simple UI Test that verifies the presence of a button and label, and checks that the label’s text changes when the button is clicked. UI Testing is a powerful tool that, with practice, can significantly improve the quality and reliability of your app’s user interface.

Further Reading

To deepen your understanding of UI Testing in Xcode, consider exploring the following resources:

  • UI Testing in Xcode
  • Exploring the New UI Testing Features of Xcode 7
  • Xcode 7 UI testing, a first look
  • UI Testing in Xcode 7

Like this article? there’s more where that came from!