Skip to content

Learn

iOS Testing: XCTest & XCUITest guide with Swift examples

Struggling to start iOS testing? This guide covers XCTest and XCUITest side by side with Swift code examples, pros/cons, and a best practices checklist. Dive in.

iOS Testing: XCTest & XCUITest with Swift examples - Tricentis

In the iOS development pipeline, testing plays a vital role to ensure that our applications work as expected and meet user requirements. And the larger the project becomes, the more critical it is to have a robust testing workflow.

There are two types of testing in iOS development: unit testing and integration testing. Unit testing focuses on testing individual code units, such as classes and functions, ensuring they work as expected. Integration testing focuses on the interaction between different code units to confirm the application works correctly.

This article will cover both unit testing and integration testing in iOS development and provide code examples to help you get started.

Unit testing in iOS

As mentioned above, unit testing is a process of testing individual units of code, such as classes and functions, to ensure they work as expected in isolation.

In iOS development, you can do unit testing using XCTest, a framework provided by Apple for writing and executing unit tests.

To start unit testing in iOS, you need to create a new test target in your Xcode project. You can do this by going to File > New > Target and selecting Unit Testing Bundle. Once you create the test target, you can start writing your tests.

A test in XCTest consists of a series of assertions you can use to verify the behavior of your code. To write a test, you must create a subclass of XCTestCase and add your assertions.

Here’s an example of a simple test in XCTest:

import XCTest

class ExampleTests: XCTestCase {

func testExample() {
// Given
let firstNumber = 10
let secondNumber = 5

// When
let result = firstNumber + secondNumber

// Then
XCTAssertEqual(result, 15)
}
}

In this example, we have a simple unit test called testExample that tests adding two numbers. The test follows the “Arrange, Act, Assert” pattern, where we first arrange the inputs (firstNumber and secondNumber), then perform the operation we want to test (result = firstNumber + secondNumber), and finally assert that the result is what we expect (XCTAssertEqual(result, 15)). If the result is not 15, the test will fail, and Xcode will report the failure.

How to Run

To run your tests, select the test target in the Xcode project navigator and press the Run button. Xcode will run your tests and display the results in the test navigator.

Integration testing in iOS

Integration testing focuses on testing the interaction between different code units to ensure the application works correctly as a whole. In iOS development, you can perform integration testing using XCUITest, a framework provided by Apple for writing and executing integration tests.

Like unit testing, integration testing in iOS also uses XCTest. But unlike unit tests, which run in a simulated environment, integration tests run on a physical device or in a simulator, allowing you to test the application as a user would experience it. These tests focus on testing the user interface and interactions between different application parts.

To start integration testing in iOS, you need to create a new test target in your Xcode project, just like you would for unit testing. Once you create the test target, you can start writing your tests.

A test in XCUITest consists of a series of actions and assertions that you can use to verify the behavior of the user interface. To write a test, you must create a subclass of XCUITestCase and add your actions and assertions.

Here’s an example of a simple integration test in XCUITest:

import XCTest

class MyIntegrationTestCase: XCUITestCase {
func testExample() {
let app = XCUIApplication()
app.launch()

let textField = app.textFields["Enter Text Here"]
textField.tap()
textField.typeText("Hello World")

let submitButton = app.buttons["Submit"]
submitButton.tap()

let label = app.staticTexts["Hello World"]
XCTAssertTrue(label.exists)
}
}

As you can see, the testExample method launches the application and interacts with it by tapping on a text field, typing text, and tapping a submit button. It then asserts that the label displaying the text “Hello World” appears on the screen.

How to run

To run your integration tests, you must have a physical device or a simulator connected to your computer. Then, select the test target in the Xcode project navigator and press the Run button. Xcode will run your tests on the connected device or simulator and display the results in the test navigator.

A more in-depth integration testing example

Here’s another example of an integration test.

import XCTest

class LoginIntegrationTests: XCTestCase {

var app: XCUIApplication!

override func setUp() {
super.setUp()
continueAfterFailure = false
app = XCUIApplication()
app.launch()
}

override func tearDown() {
super.tearDown()
}

func testLoginSuccess() {
// Given
let usernameTextField = app.textFields["Username"]
let passwordTextField = app.secureTextFields["Password"]
let loginButton = app.buttons["Login"]

// When
usernameTextField.tap()
usernameTextField.typeText("testuser")
passwordTextField.tap()
passwordTextField.typeText("testpassword")
loginButton.tap()

// Then
XCTAssertTrue(app.isDisplayingMainView)
}
}

Here we have an integration test called testLoginSuccess that tests the login functionality of the application. The test uses the XCUIApplication class to interact with the UI elements of the application. First, in the setUp method, we initialize the XCUIApplication instance and launch the app. Then, in the tearDown method, we can add any cleanup logic that needs to be executed after the test has been completed.

In the testLoginSuccess method, we use the XCUIApplication instance to interact with the UI elements of the application, such as the text fields for username and password and the login button. We then use XCTAssertTrue to assert that the application’s main view is displayed after the login is successful.

These are simple examples, but they should give you a good idea of how XCTest and XCUITest work in practice.

Comparing testing methods

Like all technologies, there are some benefits and drawbacks involved in their use. Here are the pros and cons of each testing method we’ve described:

Unit testing

Pros:

  • Unit tests are fast and efficient, as they only test small, isolated code.
  • They’re easy to maintain because they’re independent of each other and can be updated or changed without affecting other tests.
  • They can help you catch bugs early: By testing individual parts of the code, they make it easier to identify the source of any problems.

Cons:

  • Unit tests only test individual pieces of code, so they may not catch all issues with the application.
  • Because they only test individual units, they may not catch issues that arise from the interaction between different parts of the code.

Integration testing

Pros:

  • Integration tests test the entire application, making it easier to catch issues that arise from the interaction between different parts of the code.
  • They can help ensure that the entire application works as expected, including the UI and back-end interaction.
  • They can be automated using tools like XCUITest, making testing the application on multiple devices and screen sizes easier.

Cons:

  • Because integration tests test the entire application, they may require more setup and configuration, making them can be more complex and time-consuming than unit tests.
  • They may be slower to execute because they test the whole application, including the UI and back-end.

Both unit testing and integration testing have pros and cons. Using both methods to ensure that your iOS applications are tested and deliver a good user experience is essential. As you can see, unit tests are great for testing small, isolated pieces of code. Meanwhile, integration tests will help ensure that the entire application works as expected.

IOS testing best practices

Here are some best practices for iOS testing:

  1. Test early, test often: It’s important to start testing your application early in the development process and to test regularly. This will help you catch issues early on, making them easier to fix before they become significant problems.
  2. Write tests for all code: Writing tests for all of your code guarantees that all of your application’s parts work as expected.
  3. Use meaningful test names: Give your tests meaningful names, such as “testLoginSuccess” or “testSearchResults,” so it’s easy to understand what each test is checking.
  4. Keep tests focused and straightforward: Make sure your tests are simple and focused on a specific aspect of the code. This will help make the tests easier to maintain and avoid tests that are too complex to understand.
  5. Use code coverage tools: Use these tools to track which parts of your code have been tested and which parts still need work. Doing this will allow you to have comprehensive test coverage for your code.
  6. Automate tests: Automating as many tests as possible using tools like Xcode or third-party tools saves time and reduces the risk of human error.
  7. Test with real data: Whenever possible, use real data in your tests rather than using fake or synthetic data. This will confirm that your tests are as realistic as possible and catch any issues with actual data.
  8. Test on multiple devices and simulators: Make sure to test your application widely, including different screen sizes and resolutions. By doing this, you can confirm that the application works correctly on all devices.

Following these best practices ensures that your work is thoroughly tested and delivers a high-quality user experience. Incorporating testing into your development process can also reduce the risk of bugs and issues, which makes it easier to maintain and update your application over time.

Conclusion

Testing is an essential part of software development and plays a crucial role in ensuring the quality of iOS applications. This article has covered unit and integration testing and provided code examples to help you get started.

By using XCTest and XCUITest, you can write and execute tests for your iOS applications to ensure that they work as expected and deliver a smooth user experience. Whether you’re a seasoned iOS developer or just starting, incorporating testing into your development workflow is a must for delivering high-quality iOS applications.

Author:

Guest Contributors

Date: Aug. 13, 2026

You may also be interested in...