hspec-golden: Golden tests for hspec

[ library, mit, program, testing ] [ Propose Tags ] [ Report a vulnerability ]

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.

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

Please see the README on GitHub for more information.


[Skip to Readme]

Modules

[Index] [Quick Jump]

Downloads

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees

Candidates

Versions [RSS] 0.1.0.0, 0.1.0.1, 0.1.0.2, 0.1.0.3, 0.2.0.0, 0.2.0.1, 0.2.1.0, 0.2.2.0
Change log ChangeLog.md
Dependencies ansi-terminal (>=1.0 && <2.0), base (>=4.6 && <5), directory (>=1.3.6 && <1.4), filepath (>=1.0 && <2.0), hspec-core (>=2.10 && <3.0), hspec-golden, optparse-applicative (>=0.18.1 && <0.19) [details]
Tested with ghc ==8.8, ghc ==8.10, ghc ==9.0
License MIT
Copyright 2019-Present Stack Builders Inc
Author Stack Builders
Maintainer Cristhian Motoche <cmotoche@stackbuilders.com>
Category Testing
Home page https://github.com/stackbuilders/hspec-golden#readme
Bug tracker https://github.com/stackbuilders/hspec-golden/issues
Source repo head: git clone https://github.com/stackbuilders/hspec-golden
Uploaded by camm at 2024-04-12T21:36:30Z
Distributions Arch:0.2.1.0, LTSHaskell:0.2.2.0, NixOS:0.2.2.0, Stackage:0.2.2.0
Executables hgold
Downloads 4929 total (66 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs available [build log]
Last success reported on 2024-04-12 [all 1 reports]

Readme for hspec-golden-0.2.2.0

[back to package description]

hspec-golden

Build and Test Hackage

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.

You can write golden tests using golden helper:

describe "myFunc" $
  golden "generates the right output with the right params" $
     let output = show $ myFunc params
       in return output

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
  }

spec :: Spec
spec =
  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

Contributors ✨

Thanks goes to these wonderful people (emoji key):

Cristhian Motoche
Cristhian Motoche

💻
Christian Wünsche (ehem Heyn)
Christian Wünsche (ehem Heyn)

💻
Bit Connor
Bit Connor

💻
Diego Balseiro
Diego Balseiro

💻
Jorge Guerra Landázuri
Jorge Guerra Landázuri

💻
Phil Hazelden
Phil Hazelden

💻
Alexander Vieth
Alexander Vieth

💻
Guillaume Bouchard
Guillaume Bouchard

💻
Stefani Castellanos
Stefani Castellanos

💻
Add your contributions

This project follows the all-contributors specification. Contributions of any kind welcome!

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.


Stack Builders Check out our libraries | Join our team