Swift: UI Testing with SnapshotTesting
Testing user interfaces (UI) in iOS applications can be a challenging task. The default tool provided by Apple, XCTestUI, is mainly focused on functional tests and user interaction from the end-user’s perspective. This means that to verify if the elements of the screen are exactly as expected in terms of visuals and layout, we need to write complex code.
While it’s important to ensure that UI components respond correctly to interactions and events, many times we want to validate the complete visual aspect of the entire screen. This need led to the creation of libraries like SnapshotTesting developed by Point-Free.
SnapshotTesting is a powerful tool that simplifies capturing and comparing screenshots or JSON representations of your user interfaces. The main advantage is that, instead of writing detailed tests for each individual element on the screen, you can create a single test that validates the entire screen at once.
assertSnapshot(of:as:)
The central method in SnapshotTesting is assertSnapshot(of:as:)
. This method receives two parameters:
1. of:
: The UI object or component you want to test, typically a UIViewController.
2. as:
: The format in which the snapshot should be generated and stored.
For example, here’s how we could test a simple screen using SnapshotTesting:
This code is quite self-explanatory. It creates an instance of MainViewController
, calls the assertSnapshot
method passing this controller as a parameter, and stores the screenshot in image format to be compared on the next run.
⚠️ Delay in Capture
Currently, the SnapshotTesting library (version 1.18.3) presents a specific issue: it’s not possible to capture a screenshot after a certain period of time without waiting for the user interface to fully update.
For instance, the method
assertSnapshot(of: viewController, as: .wait(for: 2, on: .image))
should allow the test to wait for 2 seconds before taking the screen shot. However, it doesn’t work as intended.This issue is recognized by the community and there’s an ongoing pull request aiming to resolve this problem for the next version of the library.
Conclusion
Testing user interfaces is essential to ensure that your application works as expected both visually and functionally. Tools like SnapshotTesting significantly simplify this task, making tests more efficient and less prone to human error.
By using SnapshotTesting, you can instantly verify if a screen has been altered without having to inspect each individual component. This approach not only saves time and effort when writing tests but also improves the reliability and consistency of test results.
Therefore, despite current limitations related to timing synchronization for capture, SnapshotTesting remains a valuable tool that greatly simplifies the implementation of appropriate tests for user interfaces in iOS applications.
If you would like to contribute so that I can continue producing more technical content, please feel free to buy me a coffee ☕️ through the Buy me a Coffee platform.
Your support is essential to maintain my work and contribute to the development community.