Member-only story

How To Test SwiftUI Views Smartly

Unit test the behavior and connection of your SwiftUI

Riccardo Cipolleschi
Better Programming
10 min readNov 5, 2021

Photo by Towfiqu barbhuiya on Unsplash

Properly testing the UI is hard to achieve. The UI is the most volatile part of our app: the product manager and their team like to experiment with it. It is also hard to test due to technical factors: different screen factors, orientations, and localizations.

Testing the whole UI is also expensive: tests are hard to write, they require more time to be written and they are easily invalidated by changes in the product. To face these difficulties, strategies different from unit tests were born: snapshot testing and UITesting.

These strategies are effective but still expensive. They are very good to automate regression checking and use case testing, but not that good to support the development cycle. A good development cycle is quick, and only unit tests can provide that for us.

In today’s article, I’d like to explore a way to unit test our SwiftUI views in a smart way. We need first to understand what does it makes sense to test in a view. Secondly, we will discuss a tool that makes this possible. Finally, we will implement a simple view using a Test Driven Development (TDD) approach.

What To Test?

Riccardo Cipolleschi
Riccardo Cipolleschi

Written by Riccardo Cipolleschi

Hey there, I’m Riccardo. Software engineer at Meta. I have a passion for iOS and I love to share my knowledge with others.

No responses yet

What are your thoughts?