Enable javascript in your browser for better experience. Need to know to enable it? Go here.
Published : Oct 27, 2021
This blip is not on the current edition of the Radar. If it was on one of the last few editions, it is likely that it is still relevant. If the blip is older, it might no longer be relevant and our assessment might be different today. Unfortunately, we simply don't have the bandwidth to continuously review blips from previous editions of the Radar. Understand more
Oct 2021
Trial ?

When creating a user interface with SwiftUI, the idea is to build a view model that can be mapped easily to the elements of the user interface. In such cases, most of the testing can be done on the model, using the standard unit testing frameworks which makes these tests straightforward to write and fast to run. To test the bindings between the model and the views, developers usually reach for XCUITest, a test automation framework that launches the full application and remote controls the interface. It works, tests are reasonably stable, but they take a long time to run.

For a faster approach to writing unit tests for SwiftUI, try ViewInspector, an open-source framework that uses Swift's public reflection API to access the underlying views created by SwiftUI. With ViewInspector, a test simply instantiates a SwiftUI view, locates the interface elements that need to be tested and then makes assertions against them. Basic interactions such as taps can be tested, too. Like many UI testing frameworks, it provides an API to locate interface elements, either by specifying a path through the view hierarchy or by using a set of finder methods. These tests are usually simpler than XCUITests, and they run much faster. As a word of caution, though, given the ease with which tests can be written using ViewInspector, you might be tempted to over-test the interface. Testing simple one-to-one mappings is just double-entry bookkeeping. And even though ViewInspector makes it easier to test the SwiftUI code, remember to keep most of the logic in the model.

Download the PDF



English | Español | Português | 中文

Sign up for the Technology Radar newsletter


Subscribe now

Visit our archive to read previous volumes