haskelltestinghspec

Hspec - snapshot testing in Haskell?


Is there a build-in possibility to create snapshot tests in hspec testing framework? With snapshot, I mean, that the output of a function can be compared to an expected output stored in a file.

Or is there a hackage package to enable this feature?


Solution

  • The hspec-golden does what you want (the feature is called “golden tests”). Quoting their Haddocks (Test.Hspec.Golden):

    Golden tests store the expected output in a separated file. Each time a golden test is executed the output of the subject under test (SUT) is compared with the expected output. If the output of the SUT changes then the test will fail until the expected output is updated.

    The main function exported from that module is defaultGolden. Here's an example:

    describe "myFunc" $
      it "generates the right output with the right params" $
         let output = show $ myFunc params
           in defaultGolden "myFunc" output