Introduction to UI Testing in SwiftUI Using XCTest Framework

⏱ Reading Time: 2 mins

This is a post contributed to AppCoda. Here you can read only the introduction.

The process of making an app includes various parts, with the code implementation being the most important one. However, programming is not just about writing code; it’s also about testingusing the available tools each programming environment provides.

The first and foremost thing we all need to understand is the importance of testing. Writing automated tests should be considered a task equal to writing code, but unfortunately that’s quite often neglected. Lack of time, the need to meet a deadline, or the the fact that one person is the only developer of an app are common reasons behind that. Making extensive use of an app to verify that it’s working as expected can reveal issues, but not all, especially if the app’s logic or interface change over time. In the worst case, end users are becoming testers who report issues usually with negative feedback.

Obviously, not all problems or bugs can be detected by simply writing tests. But definitely most of them can be found early and eliminated before they reach at users’ hands. Edge or rare cases can be predicted that way much easier too. Automated testing is really important, and for that reason there is an entire programming philosophy around that, called TDD; test-driven development. The logic there lies on writing tests first, and then the code that will make those tests pass. Of course, I’m not suggesting to adopt TDD, unless you want to, or the usual way of coding is not correct. All I want is to stress out the importance of writing tests, even if you can’t cover the entire codebase but the most vital parts of your program.

Becoming more specific now, there are two kinds of automated tests we can write in Xcode. Unit tests, and UI (user interface) tests. The former help to verify that the logic implemented throughout the app is correct, and that will work properly when applied under different use cases.

The latter, UI testing, also the topic of this post, ensures that the interface that users will interact with actually contains all expected elements and controls, with the proper content, it is usable, and that the workflow is smooth and without any issues.

You may find all that a bit reduntant, given that we see UI behavior in action each time we run an app. However, consider the impact of one or more small or big changes to the UI; will the app still keep behaving and responding the way it’s supposed to be? Are you willing to manually test each and every single aspect of the app under all possible use cases to verify that there is nothing broken? And how many times will you repeat that until you feel bored, or time pressure appears? Here is the significance of automated UI testing; we need additional time in order to write tests, but we save time by not trying to manually make sure that everything is working fine.

I hope I make my point with this short introduction. In this tutorial, we will explore together the basics of UI testing in Xcode using the XCTest framework. If you have not written UI tests in the past, then you will definitely find new and interesting things to learn about. And most probably you will enjoy implementing tests and see them successfully pass.

→ Keep Reading at AppCoda

New Video on YouTube

Dear reader,

check out my new video and learn how to add video playback capabilities to SwiftUI based apps in seconds!