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

An Introduction to Snapshot testing

An Introduction to Snapshot testing

Freek Van der Herten

September 01, 2021
Tweet

More Decks by Freek Van der Herten

Other Decks in Technology

Transcript

  1. AN INTRODUCTION


    TO SNAPSHOT TESTING
    Freek Van der Herten

    View full-size slide

  2. About me


    Freek Van der Herten


    Developer at SPATIE


    @freekmurze


    freek.dev


    spatie.be/products


    ohdear.app

    View full-size slide

  3. spatie.be/open-source


    300+ open source packages


    PHP & Laravel


    175 million downloads total


    9 million downloads per month

    View full-size slide

  4. LET’S TALK ABOUT

    SNAPSHOT TESTING

    View full-size slide

  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

    View full-size slide

  6. Where does it come from?


    Characterisation testing


    Popularized by Facebook


    Jest


    Testing interfaces

    View full-size slide

  7. How can we do it in PHP?


    github.com/spatie/phpunit-snapshot-assertions


    github.com/spatie/pest-plugin-snapshots


    assertMatchesSnapshot

    assertMatchesJsonSnapshot

    assertMatchesXmlSnapshot

    View full-size slide

  8. Advantages


    Very easy to get started with


    Can be used to validate complex output


    Easily update all the tests in one go


    View full-size slide

  9. Disadvantages


    Brittle


    Hard to pinpoint exact point of failure


    Bugs can be easily written in the snapshots

    View full-size slide

  10. 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

    View full-size slide

  11. ONE MORE THING

    View full-size slide

  12. 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


    View full-size slide

  13. Thank you!


    spatie.be


    spatie.be/products


    spatie.be/open-source


    testing-laravel.com


    freek.dev

    View full-size slide