Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Test First with Snapshot Tests

Test First with Snapshot Tests

I talked about our experiences using Snapshot Testing at Hopscotch.

Event: http://www.cocoaheadsnyc.org/2017/03/02/march-9th-triple-header/

Nina Baculinao

March 09, 2017
Tweet

More Decks by Nina Baculinao

Other Decks in Programming

Transcript

  1. 1. Examine sketch file 2. Create a view or view

    controller 3. Write a snapshot test 4. Repeat ( until view looks good ) a. Write view logic b. Run that single test c. Look at screenshots d. If ( self has questions ) i. Borrow designer 5. Write navigation logic 6. Run simulator A PROCESS
  2. 1 import Quick 2 import Nimble 3 import Nimble_Snapshots 4

    import HopscotchTestHelpers 5 class <classname> : QuickSpec { 6 override func spec() { 7 describe("appearance") { 8 it("has valid snapshots") { 9 DeviceOrientation.allOrientations.forEach { orientation in 10 let view = <view> 11 view.size = orientation.configuration.screenSize 12 view.configuration = orientation.configuration 13 view.layoutSubviews() 14 expect(view).to(recordSnapshot(named: orientation.name)) 15 // expect(view).to(haveValidSnapshot(named: orientation.name)) 16 } 17 } 18 } 19 } 20 } snapshot Autocomplete
  3. Property injection: configuration is a mutable property on the class

    Initializer injection: configuration is passed into the init 4a. WRITE VIEW LOGIC
  4. SNAPSHOTS > PLAYGROUNDS Readable pull requests Tested @testable imports Faster

    prototyping on different devices Reference for designers
  5. GOTCHAS Test must be in a folder with spec in

    it Cannot record WKWebView [1] Running all specs can be slow iOS 10: retook all snapshots & no device agnostic tests [2]
  6. LEARNINGS Start with the smallest view Use code snippets for

    autocompletion Pass through view configurations in the initializer Make a separate test target to run a single test file Stub view models and data sources for testing Audit the colors in your app