MIT licensed by Stack Builders
Maintained by Cristhian Motoche
This version can be pinned in stack with:hspec-golden-0.2.1.0@sha256:b695ae72685bbb5acd04cdd79d07c43de5ab8867e28662dd1a0002296f2a4940,2635

Module documentation for 0.2.1.0

hspec-golden

Build Status

Description

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.

hspec-golden allows you to write golden tests using the popular hspec.

Usage

You can write golden tests using defaultGolden helper:

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

The first parameter of defaultGolden is the golden file name. I recommend you to use show and 'functionName (enable TemplateHaskellQuotes for the quote) to always have a unique name for your file. Example: show 'myFunc == MyModule.myFunc. Although, you can name it as you like.

In case your output isn’t a String you can define your own Golden test using the Golden data type:

{-# LANGUAGE TemplateHaskellQuotes #-}

import           Test.Hspec
import           Test.Hspec.Golden
import           Data.Text (Text)
import qualified Data.Text.IO as T

myGoldenTest :: String -> Text -> Golden Text
myGoldenTest name actualOutput =
  Golden {
    output = actualOutput,
    encodePretty = prettyText,
    writeToFile = T.writeFile,
    readFromFile = T.readFile,
    goldenFile = name,
    actualFile = Just (name </> "-actual"),
    failFirstTime = False
  }

describe "myTextFunc" $
  it "generates the right output with the right params" $
     let textOutput = myTextFunc params
       in myGoldenTest (show 'myTextFunc) textOutput

Install CLI

You can install the hspec-golden command line interface (CLI) with stack:

$ stack install hspec-golden

or cabal:

$ cabal install hspec-golden

The CLI is called hgold:

$ hgold -h

Update your golden files

Usage: hgold [-u|--update [DIR]] [-v|--version]

Available options:
  -u,--update [DIR]        The testing directory where you're dumping your
                           results. (default: ".golden")
  -v,--version             Show version
  -h,--help                Show this help text

Update the golden tests under .golden directory:

$ hgold

Update the golden tests under .myGoldenTest directory:

$ hgold -u .myGoldenTest

License

MIT, see the LICENSE file.

Contributing

Do you want to contribute to this project? Please take a look at our contributing guideline to know how you can help us build it.


Check out our libraries | Join our team

Changes

Changelog for hspec-golden

0.2.1.0

Add

  • Instance for IO Golden type. Kudos to @dbalseiro

0.2.0.1

Add

  • Update docs.

0.2.0.0

Add

  • More flexible file locations
  • Contributed by: @bitc and @christianheyn

0.1.0.2

Add

  • Lowerbound for hspec-core in hspec-golden library

0.1.0.2

Add

  • CLI updates tests recursively #19
  • Allow first execution to fail #16

0.1.0.1

Add

  • CLI changed to work with optparse-applicative.

0.1.0.0

Add

  • CLI to update actual files to golden
  • Basic functionality to have Golden tests in hspec.
  • Golden str type to write your own golden tests for the kind of type that you need and a variable output folder.
  • defaultGolden helper to create golden tests when your results are simple Strings.