Member-only story
How To Test SwiftUI Views Smartly
Unit test the behavior and connection of your SwiftUI
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.