hspec-golden: Golden tests for hspec

[ library, mit, program, testing ] [ Propose Tags ]

Please see the README on GitHub at https://github.com/stackbuilders/hspec-golden#README


[Skip to Readme]
Versions [faq] 0.1.0.0
Change log ChangeLog.md
Dependencies base (>=4.6 && <5), directory (>=1.2.5.0), hspec-core, hspec-golden, simple-get-opt [details]
License MIT
Copyright 2019 Stack Builders Inc
Author Stack Builders
Maintainer 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 stackbuilders at Thu Jun 20 16:48:08 UTC 2019
Distributions NixOS:0.1.0.0
Executables hgold
Downloads 36 total (36 in the last 30 days)
Rating (no votes yet) [estimated by rule of succession]
Your Rating
  • λ
  • λ
  • λ
Status Hackage Matrix CI
Docs available [build log]
Last success reported on 2019-06-20 [all 1 reports]

Modules

[Index] [Quick Jump]

Downloads

Maintainer's Corner

For package maintainers and hackage trustees


Readme for hspec-golden-0.1.0.0

[back to package description]

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.

Getting started

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,
    testName = name,
    directory = ".myGoldenTestDir"
  }

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

Installing 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

Parameters:
  DIR    The testing directory where you're dumping your results (default: .golden/)

Flags:
  -u[DIR]  --update[=DIR]  Replaces `golden` files with `actual` files
  -v       --version       Displays the version of hgold
  -h       --help          Displays help information

Update the golden tests under .golden directory:

$ hgold -u

Update the golden tests under .myGoldenTest directory:

$ hgold -u .myGoldenTest

Licensing

MIT, see the LICENSE file.

Contributing

Pull requests for modifications to this program are welcome! Fork and open a PR.

If you're looking for a place to start, you may want to check the open issue.