Image by Alex Ivanova

How to setup your Xcode project for testing with Unit and UI Tests in 4 simple steps

Alex Ivanova
5 min readOct 27, 2023

Having started my career in testing and ensuring the quality of various software applications, I have gained a deep understanding of the significance of testing in the development process. Through my experience, I have realized that testing plays a crucial role not only in identifying bugs but also in preventing them from occurring in the first place.

In development, I have observed a common tendency to overlook the importance of writing tests. This often results in difficulties in pinpointing and resolving bugs that arise after integrating a new feature.

Embracing a testing mindset not only ensures the smooth execution of projects but also instills a sense of confidence in the final product.

Let’s get to the point

Here is how to set up unit testing:

To perform unit tests, you can include the Unit Testing Bundle in your Xcode project in two ways:

#1 If you’re creating a brand new project

While creating your new project check Include Tests.

#2 If you want to add tests to an existing project

Go to File > New > Target....

And scroll down or search for test.

From here you can add the Unit Testing Bundle and the UI Testing Bundle one by one.

#3 Where to begin writing your tests

  1. In the Project navigator, you will find two new testing folders:
  • <YourProjectName>Tests: Add your unit tests here.
  • <YourProjectName>UITests: Add your UI tests here.

2. Inside these folders, you will find example XCTestCase subclasses:

  • <YourProjectName>Tests.swift: Add your unit tests here.
  • <YourProjectName>UITests.swift: Add your UI tests here.
  • <YourProjectName>UITestsLaunchTests.swift: Add your UI tests here, which will run after the app launch but before taking a screenshot.

If you open <YourProjectName>Tests.swift you will find some example code:

import XCTest

final class MyToDoListTests: XCTestCase {
override func setUpWithError() throws {
// Put setup code here. This method is called before the invocation of each test method in the class.
}
override func tearDownWithError() throws {
// Put teardown code here. This method is called after the invocation of each test method in the class.
}
func testExample() throws {
// This is an example of a functional test case.
// Use XCTAssert and related functions to verify your tests produce the correct results.
// Any test you write for XCTest can be annotated as throws and async.
// Mark your test throws to produce an unexpected failure when your test encounters an uncaught error.
// Mark your test async to allow awaiting for asynchronous code to complete. Check the results with assertions afterwards.
}
func testPerformanceExample() throws {
// This is an example of a performance test case.
measure {
// Put the code you want to measure the time of here.
}
}
}

It has three methods: setUpWithError() for the initial setup of each test, tearDownWithError() to perform cleanup after each test execution, a test method called testExample() to perform a test, and testPerformanceExample() to write a performance test. We will discuss each of these methods in another article.

#4 How to run your tests

Option 1:

The easiest way to run all of your tests is to go to Product > Test. There you will find also the fastest way to do this, which is by using the shortcut ⌘ U.

Option 2:

Another way is to navigate to your Swift tests file, such as <YourProjectName>Tests.swift, and click the Play button there. Simply hover over the diamond shape next to the class name, and you will find it. You will find the diamond shape next to the name of each of the tests as well.

To run only one of the tests, such as testExample(), click the Play button next to its name.

Option 3:

You can also follow these steps:

  1. Open the Test navigator.
  2. Locate the folder you want to test, either <YourProjectName>Tests or <YourProjectName>UITests.
  3. Hover over the folder name with your mouse.
  4. Click the Play button that appears.

This will execute all the tests in the selected folder.

You can also choose to run only one test class such as <YourProjectName>Tests.swift, as well as only one of the tests such as testExample() by clicking the Play button next to the name of the test or test class you wish to run.

Summary

Setting up your Xcode project to use tests is easy when you know the steps to follow. Automating the process of catching errors early in the development cycle provides confidence in the correctness of your code. When all your tests pass, you can be more certain that your code is functioning as intended.

Testing is an essential skill for becoming a good developer. Now that your project is ready, you can start adding tests. In the next article, we will explore how to write your first test.

--

--

Alex Ivanova
Alex Ivanova

Written by Alex Ivanova

💻 📱 ⌚️ 📸 🏔 🍫

No responses yet