Exploring SwiftUI — A Broad Overview 4/4: Testing in SwiftUI

This article is the 4th of a series of 4 aimed at exploring the capabilities of SwiftUI. Happy reading!

Razvan Bogdan
XapoLabs
4 min readMay 9, 2023

--

Photo by Ion (Ivan) Sipilov on Unsplash

Intro

Testing is an essential part of software development lifecycle because it helps ensure the UI looks as expected, the underlying business logic code does what it’s supposed to, and that the business requirements are fulfilled. Detecting bugs and defects early on during the development cycle is important as the cost of fixing these issues increases significantly if discovered later. As the saying goes, fixing a bug in production is 100x more expensive than fixing it during development.

Two of the most common testing techniques are unit testing and UI testing. While UI testing is focused on testing the app’s user interface, unit testing is aimed at testing independent pieces of code. There are other testing techniques like snapshot testing but I won’t go into that just now.

Both unit and UI testing processes can be automated and integrated with CI tools such as Bitrise, CircleCI, Jenkins. This has a number of benefits that help streamline the development process:

  • fast feedback: integrating tests in CI provides a fast feedback cycle. Rules can be setup in the CI, e.g. running the tests for every pull request. This allows the engineers to quickly make changes to the code and run the tests again;
  • identifying problems early: running tests in CI ensures that any code changes that break the business logic can be caught early on in the development;
  • increased code quality: having a higher test coverage should mean having a higher certainty that the codebase if free of errors. New code changes can be easily tested for desired functionality without the need for manual testing.

Unit Testing

The goal of unit testing is to ensure that individual units of code function correctly and produce the expected output when tested in isolation. They’re designed to validate the smallest possible piece of code and quickly identify flaws.

Unit testing has the most significant impact when done during the development phase of the product lifecycle as an ongoing process. Creating unit tests for each block of code is important to verify against valid or invalid input data. The Test Driven Development (TDD) involves creating unit tests before writing code, and using them to guide the functional implementation specifics.

Let’s see how we can unit-test our code here:

We will use this form for our practical example, consisting of two Textfields and a Button. The Button is bound to be enabled only when the contents of the two Textfields are not empty.

Let’s test that logic using unit tests. We we use the XCTest framework for that. XCTest provides a handful of classes and helper functions making it easy to create unit tests and UI tests in Swift.

In these trivial test cases we are testing that the Button gets enabled only after the text in the two TextFields is not empty. Nothing more simple, right?

UI Testing

UI Testing is a powerful way of testing that the visual components of the user interface interact as expected when certain actions are triggered. It is also used for validating that the UI elements are displayed correctly, that the navigation between screens operates correctly. A fully automated UI testing suite can replace manual testing and save teams a lot of time and money resources. There are various ways of running automated UI tests in CI, both in simulators or on real devices.

Following the example from above, containing 2 Textfields and one Button that is enabled only when the Textfields contain text, let’s write a UI test to check that:

When running the UI tests, we are actually interacting with the app’s user interface. We used the XCTest framework again for creating the UI tests.

What we’re doing here is querying the UI elements collection for our two Textfields and typing some text into them. At the end, we just check that the Button is enabled.

There is a possibility to record a UI test. That allows for manually interacting with the UI and having code automatically generated. Although the code is not always clean and requires some touching, this can be a good start for someone that just got into UI testing.

Full code is available on my Github https://github.com/bogdan-razvan/swiftui-demo

Conclusion

Unit testing and UI testing are integral parts of the software development lifecycle. By using frameworks like XCTest, implementing these tests becomes straightforward.

By creating tests and following best practices, we can ensure the app meets the required functionality and that the users have a seamless, error free experience.

Thank you for reading! Got a suggestion? Please leave it in the comments section, feedback is always appreciated!

Let’s connect on LinkedIn!

If you’re interested in learning more about Xapo Bank — Bitcoin Bank providing simple and safe usability to retail customers, explore the resources at xapobank.com.

--

--