Screenshotbot

Swift Snapshot Testing

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

Using the patched version of 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://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

Powered by BetterDocs