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

An Introduction to Snapshot testing

An Introduction to Snapshot testing

A1b4b398630bbd32b77a82cbc37fae9a?s=128

Freek Van der Herten

September 01, 2021
Tweet

Transcript

  1. AN INTRODUCTION TO SNAPSHOT TESTING Freek Van der Herten

  2. About me Freek Van der Herten Developer at SPATIE @freekmurze

    freek.dev spatie.be/products ohdear.app
  3. spatie.be/open-source 300+ open source packages PHP & Laravel 175 million

    downloads total 9 million downloads per month
  4. LET’S TALK ABOUT 
 SNAPSHOT TESTING

  5. What? A file on disk – let’s call it a

    snapshot Test succeeds if the output matches the snapshot If not, the test will fail
  6. Where does it come from? Characterisation testing Popularized by Facebook

    Jest Testing interfaces
  7. How can we do it in PHP? github.com/spatie/phpunit-snapshot-assertions github.com/spatie/pest-plugin-snapshots assertMatchesSnapshot

    assertMatchesJsonSnapshot assertMatchesXmlSnapshot
  8. DEMO

  9. WRAPPING UP

  10. Advantages Very easy to get started with Can be used

    to validate complex output Easily update all the tests in one go
  11. Disadvantages Brittle Hard to pinpoint exact point of failure Bugs

    can be easily written in the snapshots
  12. Interesting links github.com/spatie/phpunit-snapshot-assertions github.com/spatie/pest-plugin-snapshots github.com/spatie/shiki-php github.com/spatie/laravel-markdown torchlight.dev

  13. ONE MORE THING

  14. testing-laravel.com Premium video course, 6+ hours of video Learn how

    to tests a Laravel app from scratch Tips and tricks to sharpen your skills Two courses in one: Pest & PHPUnit 10 % discount with SPATIE-LOVES-LARACON
  15. Thank you! spatie.be spatie.be/products spatie.be/open-source testing-laravel.com freek.dev