GithubHelp home page GithubHelp logo

squarefrog / ios-snapshot-test-case Goto Github PK

View Code? Open in Web Editor NEW

This project forked from facebookarchive/ios-snapshot-test-case

0.0 2.0 0.0 401 KB

Snapshot view unit tests for iOS

License: Other

Ruby 2.38% Objective-C 90.35% Swift 7.28%

ios-snapshot-test-case's Introduction

FBSnapshotTestCase

Build Status

What it does

A "snapshot test case" takes a configured UIView or CALayer and uses the renderInContext: method to get an image snapshot of its contents. It compares this snapshot to a "reference image" stored in your source code repository and fails the test if the two images don't match.

Why?

At Facebook we write a lot of UI code. As you might imagine, each type of feed story is rendered using a subclass of UIView. There are a lot of edge cases that we want to handle correctly:

  • What if there is more text than can fit in the space available?
  • What if an image doesn't match the size of an image view?
  • What should the highlighted state look like?

It's straightforward to test logic code, but less obvious how you should test views. You can do a lot of rectangle asserts, but these are hard to understand or visualize. Looking at an image diff shows you exactly what changed and how it will look to users.

We developed FBSnapshotTestCase to make snapshot tests easy.

Installation with CocoaPods

  1. Add the following lines to your Podfile:

    target "Tests" do
      pod 'FBSnapshotTestCase'
    end
    

    Replace "Tests" with the name of your test project.

  2. Define FB_REFERENCE_IMAGE_DIR in GCC_PREPROCESSOR_DEFINITIONS. This should point to the directory where you want reference images to be stored. At Facebook, we normally use this:

    GCC_PREPROCESSOR_DEFINITIONS = $(inherited) FB_REFERENCE_IMAGE_DIR="\"$(SOURCE_ROOT)/$(PROJECT_NAME)Tests/ReferenceImages\""

Creating a snapshot test

  1. Subclass FBSnapshotTestCase instead of XCTestCase.
  2. From within your test, use FBSnapshotVerifyView.
  3. Run the test once with self.recordMode = YES; in the test's -setUp method. (This creates the reference images on disk.)
  4. Remove the line enabling record mode and run the test.

Features

  • Automatically names reference images on disk according to test class and selector.
  • Prints a descriptive error message to the console on failure. (Bonus: failure message includes a one-line command to see an image diff if you have Kaleidoscope installed.)
  • Supply an optional "identifier" if you want to perform multiple snapshots in a single test method.
  • Support for CALayer via FBSnapshotVerifyLayer.

Notes

Your unit test must be an "application test", not a "logic test." (That is, it must be run within the Simulator so that it has access to UIKit.) In Xcode 5 and later new projects only offer application tests, but older projects will have separate targets for the two types.

Authors

FBSnapshotTestCase was written at Facebook by Jonathan Dann with significant contributions by Todd Krabach.

License

FBSnapshotTestCase is BSD-licensed. See LICENSE.

ios-snapshot-test-case's People

Contributors

adamjernst avatar ashfurrow avatar dasmer avatar dblock avatar dstnbrkr avatar fgoessler avatar grantjk avatar jamesgpearce avatar rbsgn avatar squarefrog avatar x2on avatar yas375 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.