Swift Snapshot Testing

Screenshotbot integrates with the popular swift-snapshot-testing library. In this document we’ll walk you through the steps required to set up the integration.

TL;DR Add your screenshot directory to .gitignore. In CI, run your tests in record mode and call the Screenshotbot CLI.

Prerequisite: patching the library

We need a few patches to the library. We have a pending pull request that hasn’t been merged at time of writing. So for the moment we recommend you use our patched version at https://github.com/tdrhq/swift-snapshot-testing.

For instance, if you use Carthage, you can specify this in your Cartfile as:

      github "tdrhq/swift-snapshot-testing" "c15b659d6d4ad2490486fe47bab910c4f394cc2e"

Once you have this, you need to set isFailOnNewRecording = false. There are many ways to do this, but if you have a common parent test class, you can set up in there:

    internal class BaseTestCase : TestCase {
      override func setUp {
      isFailOnNewRecording = false
    }

    override func tearDown{
      // Not required, but good cleanup habbit
      isFailOnNewRecording = true
      }
    }

At this point your tests will never fail when snapshots change, and after every run your __Snapshots__ directory will always be updated after every run. But we don’t want to commit the changes in this directory, so let’s also add it to __Snapshots__ to your .gitignore.

CI configuration

After you run your tests, whether on CircleCI or Jenkins or any other CI, you’ll need to upload your screenshots to Screenshotbot. This would be the simplest incantation in most case:

    # Install the screenshotbot recorder script to
    # ~/screenshotbot/recorder
    curl https://cdn.screenshotbot.io/recorder.sh | sh

    # Run the script from the root git directory of the project
    ~/screenshotbot/recorder --channel channel-name \
      --api-key "${SCREENSHOTBOT_API_KEY}" \
      --api-secret "${SCREENSHOTBOT_API_SECRET}" \
      --directory path/to/__Snapshots__ \
      --production
      --repo-url https://github.com/path-to/repo.git

Next Steps

In order to get the full benefits of Screenshotbot we encourage you to integrate the scripts from above into your CI jobs, for example CircleCI or Jenkins.

You can then integration with your Code Review platform of you choice (for example, GitHub), to get build statuses.

Ready to get started?

Sign up or contact us.