How to setup Swift Testing in a Swift or SwiftUI project in Xcode

Oscar de la Hera Gomez
Three flowers that represent Swift, Swift Testing and XCode side by side. Beneath them sits the text "Setup".

A step by step guide for adding Unit Tests and UITests to a Swift or SwiftUI project.

The following tutorial can be found in the main branch of our Open Source Swift and SwiftUI projects linked below.

Step One: Add a New Target

A screenshot of XCode showing how if you select the App project file (i.e. the one with the Apple App Store Icon) you can add a new target through a plus (i.e. "+") on the bottom left.

Add a new target through project settings screen or through File > New > Target...

Step Two: Select a Test Bundle

A screenshot of XCode showing the modal that appears when you attempt to add a new target. Highlighted are how we have selected the "Multiplatform" tab, how we have searched for "tests", the UI Testing Bundle and the Unit Testing Bundle, as well as the next button on the bottom right.

In the modal that appears, select Multiplatform and search for test.

Select UI Testing Bundle or Unit Testing Bundle.

Click the Next button on the bottom left.

Step Three: Name & Finish

A screenshot of the modal in XCode highlighting how we have named the UI Testing Bundle "UITests", how the app target is selected under "Target to be Tested" as well as the Finish button on the bottom right.

Name the Target. We recommend UITests or Tests depending on whether they are UITests or Unit Tests.

Please note that the modal should show the app target selected. If you have a scenario like the image below, which may arise if you selected the MacOS tab, please go back and select "Multiplatform" from the target tab to ensure that you can add the target.

A screenshot of the modal that appears when you attempt to add a UI Testing Bundle or a Unit Testing Bundle. Highlighted in red is how you cannot add a target to be tested.

Step Four: Import the Project

A screenshot of the UITests.swift file highlighting how we have imported the project to allow us to test parts of it.

Select the relevant test swift file and at the top import your project (i.e. import SwiftUI_Starter_Project).

This import statement must match your app target name in order to be able to use elements (i.e. LanguageCoordinator) from within the target.

Step Five: Repeat if Necessary

A screenshot of the project showing the UI Testing Target and Unit Testing Target. The Unit Tests swift file is open and has also imported the project correctly.

Repeat steps one to four, to add more testing targets (i.e. Unit Testing or UI Testing).

Looking to learn more about SwiftUI, Swift, Swift Testing, Design and Technology?

Search our blog to learn more about Swift, SwiftUI, Swift Testing, design and technology.

Any Questions?

We are actively looking for feedback on how to improve this resource. Please send us a note to inquiries@delasign.com with any thoughts or feedback you may have.
delasign logo

Book a Free Consultation.

An icon of an email.

Click here to email us.

Fill in the details below to book a free consultation or to let us know about something else. Whatever it is, we are here to help.

How can we help you ?

Contact Details