FBSnapshotTestCase

 FBSnapshotTestCase

Presented @ Swift Meetup Hamburg

Sample code: https://github.com/pietbrauer/Beverages

48f07655aeed5ca639b37b05e52d4cb3?s=128

Piet Brauer

July 09, 2015
Tweet

Transcript

  1. FBSnapshotTestCase @pietbrauer

  2. Who developed an app before?

  3. Who wrote tests for it?

  4. Why didn't you?

  5. You wanted to get your killer App idea on Kickstarter

    ASAP
  6. Winners don’t test

  7. Most is view code is hard to test

  8. What do I mean with that?

  9. Our app

  10. We have ... • a simple UITableViewController setup • a

    Beverage model • a BeverageCell cell that displays data
  11. We want to ... • test cell interactions • setting

    texts on labels • interacting with the stepper • view layout code
  12. Model tests func testBeer() { let subject = Beverage.Beer XCTAssertEqual(subject.amount,

    0.5) XCTAssertEqual(subject.description, "Beer (0.5 l)") XCTAssertEqual(subject.emoji, "!") }
  13. Model logic tests func testSettingAmountDrunk() { var subject = Beverage.Tea

    // reset units drunk subject.unitsDrunk = 0 XCTAssertEqual(subject.unitsDrunk, 0) // set new value subject.unitsDrunk = 5 XCTAssertEqual(subject.unitsDrunk, 5) // Take other instance of tea and check if same amount applies let secondSubject = Beverage.Tea XCTAssertEqual(subject.unitsDrunk, 5) }
  14. Cell View tests func testCellConfiguration() { var beverage = Beverage.Beer

    beverage.unitsDrunk = 3 let subject = BeverageCell(style: .Default, reuseIdentifier: nil) subject.configure(beverage) XCTAssertEqual(subject.beverageEmojiLabel.text!, "!") XCTAssertEqual(subject.beverageNameLabel.text!, "Beer (0.5 l)") XCTAssertEqual(subject.amountDrunkLabel.text!, "3 drunk today") XCTAssertEqual(subject.stepper.value, 3) }
  15. Cell Logic tests func testStepper() { var beverage = Beverage.Beers

    beverage.unitsDrunk = 0 let subject = BeverageCell(style: .Default, reuseIdentifier: nil) subject.configure(beverage) // Set stepper value subject.stepper.value = 5 subject.stepperStepped(subject.stepper) // Expect that the amount drunk label has the right value XCTAssertEqual(subject.amountDrunkLabel.text!, "5 drunk today") }
  16. None
  17. None
  18. Actual app

  19. What to do? Write snapshot tests

  20. Installation # Cartfile github "facebook/ios-snapshot-test-case" >= 2.0 # Podfile pod

    "FBSnapshotTestCase", ">= 2.0"
  21. Usage 1. import FBSnapshotTestCase 2. Inherit from FBSnapshotTestCase 3. In

    setUp() set recordMode = true 4. Run unit tests 5. Remove setUp()
  22. Demo

  23. None
  24. Links • FBSnapshotTestCase: https://github.com/facebook/ios- snapshot-test-case • Demo project: https://github.com/pietbrauer/Beverages •

    Snapshots Xcode plugin: https://github.com/orta/snapshots
  25. Thanks Twitter: @pietbrauer Blog: nerdishbynature.com/blog