Snapshot tests compare your UI using two images generated from your views. Generated images of your UI can be stored in your Git repository, representing the current state of your UI. You can run these tests on CI to ensure that your UI looks like you expect in all states that are important for you. Auto-generated snapshot tests over SwiftUI previews
AT SNAPSHOT TESTS Auto-generated snapshot tests over SwiftUI previews • iPhone 12 Pro Max vs. iPhone SE • iPhone vs. iPad • Light vs. dark mode • Major iOS updates • Accessibility settings • Split view or floating mode • Rare cases (e.g., app clips, app extensions) These tests allow you to examine what your UI looks like not only after updating or refactoring your code but also after changing the environment. Examples:
LOOK AT SNAPSHOT TESTS Auto-generated snapshot tests over SwiftUI previews Usually, people think a keyboard is just a set of keys arranged in a few rows.
LOOK AT SNAPSHOT TESTS Auto-generated snapshot tests over SwiftUI previews There are different layouts, keyboard types, return keys, user settings, regions, and much more—without even including the variation of devices and iOS versions. Examples of snapshot tests for our keyboard
LOOK AT SNAPSHOT TESTS Auto-generated snapshot tests over SwiftUI previews We have five iPad types that have completely different setups. You can compare the most notable snapshots of the same keyboard with the same configuration, but on different devices. iPad 9 iPad Pro 11 iPad Pro 12.9
SNAPSHOT TESTS Auto-generated snapshot tests over SwiftUI previews Different configurations can also make notable changes—like on iPad Pro 12.9, where we use an alternative keyboard layout that has a large return button. QWERTY AZERTY
• ios-snapshot-test-case by Uber Set up unit tests bundle Configure Git LFS Store your images in a large file storage for faster interactions with your Git. SNAPSHOT TESTS FLOW Auto-generated snapshot tests over SwiftUI previews • Target scheme • Test plan • swift-snapshot-testing by Point-Free
Auto-generated snapshot tests over SwiftUI previews SNAPSHOT TESTS FLOW Design Create a SwiftUI, AppKit, or UIKit view that uses plain models instead of mocked services. Development Use SwiftUI previews to quickly develop UI without launching the application. Testing Write snapshot test cases that cover the most important states of the application. Ready to go! Record snapshots and push all your new changes to your remote repository.
Auto-generated snapshot tests over SwiftUI previews SNAPSHOT TESTS FLOW Design Create a SwiftUI, AppKit, or UIKit view that uses plain models instead of mocked services. Development Use SwiftUI previews to quickly develop UI without launching the application. Testing Write snapshot test cases that cover the most important states of the application. Ready to go! Record snapshots and push all your new changes to your remote repository. 2 3
example? Before we start Auto-generated snapshot tests over SwiftUI previews Snapshotting swift-snapshot-testing by Point-Free generates and compares images. It doesn’t require any subclasses and provides a rich set of functionalities. Inspecting ViewInspector by Nalexn allows for traversing a view hierarchy at runtime for testing. For example, to find some specific view in the hierarchy. TEST CASES BASED ON SWIFTUI PREVIEWS
UIKit code? Before we start Visit our engineering blog to learn how we applied SwiftUI previews for our existing UIKit code and improved our UI development process. Auto-generated snapshot tests over SwiftUI previews https://www.grammarly.com/blog/engineering/swiftui-uikit/ TEST CASES BASED ON SWIFTUI PREVIEWS
Auto-generated snapshot tests over SwiftUI previews TEST CASES BASED ON SWIFTUI PREVIEWS Design Create a SwiftUI, AppKit, or UIKit view that uses plain models instead of mocked services. Development Use SwiftUI previews to quickly develop UI without launching the application. Testing Define the snapshot test class using test cases from SwiftUI previews. Ready to go! Record snapshots and push all your new changes to your remote repository.
we redefine them? SwiftUI previews display our view in all sizes, traits, and model states that we’re interested in. So why don’t we use something that will find these previews and transform them in snapshot tests? Presentation title TEST CASES BASED ON SWIFTUI PREVIEWS Auto-generated snapshot tests over SwiftUI previews
create a template, where we will generate a new test-case class for all types that conform to this protocol. Auto-generated snapshot tests over SwiftUI previews A configuration-of-generation step for all targets Slow integration time Using Stencil or Swift to create a template Template to maintain Git conflicts if you store generated files Third-party dependency that may stop working TESTS AUTO-GENERATION
an option where our test cases won’t be generated in compile time, but will be added to our test suite when we launch tests. Basically, we’re looking for our test cases among the types available in runtime. Auto-generated snapshot tests over SwiftUI previews No configuration-for-generation step in Xcode project Medium integration time Using Swift to define a dynamic base class No templates to maintain No Git conflicts since we don’t generate any code No third-party dependencies 32 TESTS AUTO-GENERATION
SwiftUI, AppKit, or UIKit view that uses plain models instead of mocked services. Development Create SwiftUI previews marked as SnapshotTestCase for a quick UI development and tests definition. Ready to go! Record snapshots and push all your new changes to your remote repository. TESTS AUTO-GENERATION Auto-generated snapshot tests over SwiftUI previews
to validate your UI after you change your code or your environment. Use SwiftUI previews to quickly develop UIKit and AppKit views. SwiftUI previews are perfect test-case candidates for your snapshot tests. You can use runtime tests generation to reduce the amount of code in your merge requests.