iOS Simulator agnostic snapshot testing - Tooploox

Przemyslaw Stasiak

Jun 13, 2017 - 

2 min read - 

iOS Simulator agnostic snapshot testing

With a debut of each shiny new device equipped with higher resolution screen, developing and testing user interface of iOS applications becomes more and more demanding and time-consuming. For each view, that represents only a small part of an application, there could be several edge cases and states depending on data it works with. In contrast to domain logic code, it’s not obvious how to properly test view’s visual aspects.

It’s been a while since Facebook engineers published an internal tool for UIView/ CALayer snapshot testing – FBSnapshotTestCase (if you’re not familiar with this tool, I encourage you to check it out!). The presented approach simplifies testing multiple configurations of view, as well as prevents from introducing unexpected changes in user interface.

Size matters 😉

When it comes to view controllers, we usually want to ensure they keep visual quality on a set of various existing devices in each supported orientation. This can be achieved by running a test suite on numerous variants of iOS simulators. Such approach will result in extending total test suite’s execution time with multiple simulator’s restarts. Paying so high price for each UI code change is definitely not acceptable during a development process. To tackle this inconvenience we developed FBSnapshotTestCase extension, with a special love for testing view controllers in their natural habitats.

Meet SnappyTestCase

Snapshot tests built with SnappyTestCase don’t depend on a context of simulator or device they run on. Instead, context is defined for each verification statement you use to validate or record a snapshot. Context is specified by passing a set of objects adopting Snap protocol, that defines test environment parameters like device identifier, frame size, screen scale and orientation. For convenience, we prepared a set of objects defining real life iOS devices, ready to be used in your tests. Here we’re testing ExampleViewController with Snap representing the context of the 4″, portrait mode iPhone:

As a result of running the following test, reference image named testExample_iPhone 4,0"_Portrait@2x.png will be recorded and stored on a disk.

Virtual device rack

We gathered and grouped by device type all existing variants of iOS devices into a struct called DeviceRack. It simplifies defining sets of devices you want to simulate in your tests. You can compose device sets by picking test devices one by one, or by using simple chainable API:

  • portrait, landscape – define orientation of single, or sequence of devices,
  • uniqueWidths, uniqueHeights – filter sequence of devices to not contain duplicated widths or heights.

Some real-life usage examples:

Expression Device set description Snapshot count
DeviceRack.iPhone.all.landscape All iPhones, in landscape orientation 4
DeviceRack.iPhone.all.portrait.uniqueWidths Set of iPhones covering all possible screen widths in portrait orientation 3
DeviceRack.iPad.retina.landscape Retina 9.7″ / 7.9″ iPad, landscape 1

Try it yourself

Described tool is, of course, open sourced 🤓 – code and installation information can be found on our GitHub. We’ve prepared a demo application with some example tests and corresponding reference images. Feedback and contributions are welcome!

Read also about Apple app clips, new feature for iOS 14.

Join our newsletter

By clicking "Submit" you will subscribe to our newsletter. See our Privacy Policy for more information on how Tooploox uses your personal data and what your rights are.

Comments are closed.

Similar posts

How to Choose a Mobile App Development Company

Weronika Szota

Aug 18, 2021 - 

5 min read - 
Mental Health App Market

Weronika Szota

Jun 24, 2021 - 

5 min read - 
Mobile wallet apps - a promising business niche

Weronika Szota

May 19, 2021 - 

5 min read - 

Let’s work together

Tooploox Sp. z o.o.

EU: + 48 733 888 088

U.S.: + 1 415 800 2835

Business Partnerships

Marketing & PR


Office Management

Wrocław (HQ)

ul. Tęczowa 7
53-601 Wrocław
See on the map


ul. Foksal 18
00-372 Warszawa
See on the map

We use cookies for analytics and to improve our site - more info in our privacy policy