TE

Haskell Testing

Haskell testing conventions with HSpec, QuickCheck, and property-based testing

Details

Language / Topic
haskellHaskell
Category
Testing

Rules

balanced
- Use `hspec` as the primary test framework — structure tests with `describe` and `it` blocks that document behaviour as natural language specifications.
- Use `QuickCheck` with `forAll` and custom `Arbitrary` instances to generate random test data and find property violations automatically.
- Place test files in a `test/` directory mirroring `src/` — name spec files `FooSpec.hs` for module `Foo`.
- Write `Arbitrary` instances for domain types to enable property-based testing — use `shrink` to produce minimal failing counterexamples.
- Use `hspec-discover` to automatically discover and run all `*Spec.hs` files — add `{-# OPTIONS_GHC -F -pgmF hspec-discover #-}` to `Spec.hs`.
- Use `IORef` or `MVar` test doubles to verify that effectful functions call dependencies with the correct arguments.
- Use `tasty` with `tasty-hspec` and `tasty-quickcheck` for a unified test runner with parallel execution and structured output.