ProductPromotion
Logo

Swift

made by https://0x3d.site

GitHub - parski/SnapshotTest: Snapshot testing tool for iOS and tvOS
Snapshot testing tool for iOS and tvOS. Contribute to parski/SnapshotTest development by creating an account on GitHub.
Visit Site

GitHub - parski/SnapshotTest: Snapshot testing tool for iOS and tvOS

GitHub - parski/SnapshotTest: Snapshot testing tool for iOS and tvOS

SnapshotTest is a simple view testing tool written completely in Swift to aid with development for Apple platforms. It's like unit testing for views.

How

When record mode is active a snapshot assertion will record an image of the view and save it to a specified directory. This will cause the test to fail. When record mode is deactivated the snapshot assertion will record an image of the view and compare it to the saved reference image. Should they differ the test will fail.

Setup

All SnapshotTest needs to know is where to save the reference images. This directory is specified using a test scheme environmental variable using the key:

REFERENCE_IMAGE_DIR

Recommended reference image directory path is:

$(SOURCE_ROOT)/$(PROJECT_NAME)Tests/ReferenceImages

It should look something like this:

Test scheme arguments.

Usage

If you are familiar with XCTest using SnapshotTest will be a breeze. Instead of subclassing XCTestCase you just need to subclass SnapshotTestCase and assert the view using AssertSnapshot() to test it.

class ViewTests: SnapshotTestCase {
    
    func testView_withAlteration() {
        // Given
        let view = View(frame: CGRect(x: 0, y: 0, width: 375, height: 100))
        
        // When
        view.alter()
        
        // Then
        AssertSnapshot(view)
    }
    
}

Currently UIView, UIViewController and CALayer are supported.

Record mode

Record mode records a snapshot of your view within the current scope and stores it in the reference directory to compare with any subsequent assertions.

Note that record mode will fail the test and output the reference image path to the console.

๐Ÿ”ด RECORD MODE: Reference image saved to /Users/snap/App/AppTests/ReferenceImages/View/testView_withAlteration.png

Test case

To set the test case to record mode simply change the recordMode property to true.

class ViewTests: SnapshotTestCase {
    
    override func setUp() {
        super.setUp()
        recordMode = true
    }
    
}

The assertion will then record and save a reference image.

Global

To set record mode globally and record all snapshots for every assertion, set the class variable recordMode instead.

SnapshotTestCase.recordMode = true

Note that you will probably need to set up a principal class to guarantee that record mode is activated before your test suite is run.

Statement

To explicitly record a single snapshot you can instead use the RecordSnapshot() function:

class ViewTests: SnapshotTestCase {
    
    func testView_withAlteration() {
        // Given
        let view = View(frame: CGRect(x: 0, y: 0, width: 375, height: 100))
        
        // When
        view.alter()
        
        // Then
        RecordSnapshot(view)
    }
    
}

Principal class

A principal class is automatically instantiated by XCTest when your test bundle is loaded. Think of it as a good place to put a global setUp() and tearDown(). A simple principal class might look like this:

import SnapshotTest

class TestObserver : NSObject {

    override init() {
        SnapshotTestCase.recordMode = true
    }
}

In your test bundle's Info.plist, add a key value pair:

<key>NSPrincipalClass</key>
<string>YourAppTests.TestObserver</string>

Where YourAppTests is the name of your test bundle.

This will activate record mode globally and is guaranteed to be run before your test suite.

Options

SnapshotTest provides different ways to compare snapshots using several options.

Option Description
device Compares snapshots specific to a certain device.
osVersion Compares snapshots specific to a certain OS version.

To use one or several options you just pass them to the options argument of the assertion:

AssertSnapshot(view, options: [.device, .osVersion])

Platforms

The following platforms and minimum versions are supported:

  • iOS 8.0
  • tvOS 9.0

Distribution

Use SnapshotTest by building it and integrating it into your project manually or by using a depencency manager. Currently only CocoaPods is supported with more to come.

CocoaPods

Just add the following line to your Podfile in the scope of your test target:

target "MyAppTests" do
  use_frameworks!
  pod 'SnapshotTest' ~> 'X.Y.Z'
end

Replace X.Y.Z with the starting version you wish to use. Breaking backward compatability will be a last resort kind of deal but specifying version is still recommended.

Contribute

SnapshotTest is licensed under the BSD 2-clause License and contributions are very welcome in the form of pull requests and issues.

More Resources
to explore the angular.

mail [email protected] to add your project or resources here ๐Ÿ”ฅ.

Related Articles
to learn about angular.

FAQ's
to learn more about Angular JS.

mail [email protected] to add more queries here ๐Ÿ”.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory