Release with confidence.

Hackage GitHub CI

Hedgehog automatically generates a comprehensive array of test cases, exercising your software in ways human testers would never imagine.

Generate hundreds of test cases automatically, exposing even the most insidious of corner cases. Failures are automatically simplified, giving developers coherent, intelligible error messages.

Features

  • Integrated shrinking, shrinks obey invariants by construction.
  • Abstract state machine testing.
  • Generators allow monadic effects.
  • Range combinators for full control over the scope of generated numbers and collections.
  • Equality and roundtrip assertions show a diff instead of the two inequal values.
  • Template Haskell test runner which executes properties concurrently.

Example

The main module, Hedgehog, includes almost everything you need to get started writing property tests with Hedgehog.

It is designed to be used alongside Hedgehog.Gen and Hedgehog.Range which should be imported qualified. You also need to enable Template Haskell so the Hedgehog test runner can find your properties.

{-# LANGUAGE TemplateHaskell #-}

import           Hedgehog
import qualified Hedgehog.Gen as Gen
import qualified Hedgehog.Range as Range

Once you have your imports set up, you can write a simple property:

prop_reverse :: Property
prop_reverse =
  property $ do
    xs <- forAll $ Gen.list (Range.linear 0 100) Gen.alpha
    reverse (reverse xs) === xs

And add the Template Haskell splice which will discover your properties:

tests :: IO Bool
tests =
  checkParallel $$(discover)

If you prefer to avoid macros, you can specify the group of properties to run manually instead:

{-# LANGUAGE OverloadedStrings #-}

tests :: IO Bool
tests =
  checkParallel $ Group "Test.Example" [
      ("prop_reverse", prop_reverse)
    ]

You can then load the module in GHCi, and run it:

λ tests
━━━ Test.Example ━━━
  ✓ prop_reverse passed 100 tests.

In Memory of Jacob Stanley

As we come to the end of our guide to haskell-hedgehog, we’d like to take a moment to remember and honor one of its key contributors, our dear friend, author and co-founder, Jacob Stanley.

Jacob’s passion for Haskell and his commitment to creating high-quality, reliable software was truly inspiring. His work has shaped haskell-hedgehog in countless ways, and without him, it wouldn’t be the project it is today.

Jacob passed away unexpectedly on April 9th. His absence is deeply felt, but his impact on this project, and on all of us who had the privilege to work with him, remains. We continue to maintain and develop haskell-hedgehog in his memory and in honor of his dedication to excellence in programming.

As you explore haskell-hedgehog, and possibly contribute to its ongoing development, we invite you to join us in remembering Jacob Stanley — a tremendous developer, collaborator, and friend.

Contributors

Changes

Version 1.4 (2023-08-07)

  • Fix skipping to tests/shrinks when tests have been discarded (#489, @ChickenProp)

Version 1.3 (2023-06-22)

Version 1.2 (2022-08-28)

Version 1.1.2 (2022-09-02)

Version 1.1.1 (2022-01-29)

Version 1.1 (2022-01-27)

Version 1.0.5 (2021-03-12)

Version 1.0.4 (2020-12-11)

Version 1.0.3 (2020-06-26)

Version 1.0.2 (2020-01-10)

Version 1.0.1 (2019-09-16)

  • Add compatibility with GHC 8.8 (#319, @erikd)
  • Include location of failed assertion in report. This enables editors to more easily parse the location of failed test assertions, and provide links/jump functionality (#308, @owickstrom)
  • Stop using filter to define unicode (#303, @ajmcmiddlin)
  • Export LabelName from main module (#299, @erikd)

Version 1.0 (2019-05-13)

Version 0.6.1 (2018-09-22)

Version 0.6 (2018-05-14)

Version 0.5.3 (2018-03-12)

Version 0.5.2 (2018-02-05)

Version 0.5.1 (2017-12-06)

Version 0.5 (2017-07-16)

  • Parallel state machine testing, allows detection of commands which are not-atomic (#98, @jacobstanley)
  • Easier to use variables for state machine testing (#94, @jacobstanley)
  • MonadGen class allows the use of transformers like ReaderT and StateT on the outside of generators (#99, @jacobstanley)
  • Better error messages for tests which throw exceptions (#95, @jacobstanley)
  • Separated test input generation and assertions in to PropertyT and TestT respectively, this allows TestT to have a MonadBaseControl instance (#96, @jacobstanley)
  • This document grew links to the pull requests which introduced various changes (#93, @moodmosaic)

Version 0.4.1 (2017-06-28)

  • Fixed runtime type error that could occur when shrinking state machine commands (#91, @jacobstanley)

Version 0.4 (2017-06-28)

Version 0.3 (2017-06-11)

Version 0.2.2 (2017-05-16)

Version 0.2.1 (2017-05-09)

Version 0.2 (2017-05-06)

  • Added a quiet test runner which can be activated by setting HEDGEHOG_VERBOSITY=0 (@jacobstanley)
  • Concurrent test runner does not display tests until they are executing (@jacobstanley)
  • Test runner now outputs a summary of how many successful / failed tests were run (@jacobstanley)
  • checkSequential and checkParallel now allow for tests to be run without Template Haskell (@jacobstanley)
  • Auto-discovery of properties is now available via discover instead of being baked in (@jacobstanley)
  • annotate allows source code to be annotated inline with extra information (@jacobstanley)
  • forAllWith can be used to generate values without a Show instance (@jacobstanley)
  • Removed uses of Typeable to allow for generating types which cannot implement it (@jacobstanley)