haskell-testing
Testing patterns for pragmatic Haskell. Use when writing unit tests, property tests, or effectful tests; when structuring test suites in cabal; or when deciding how to inject test doubles.
Unsigned, install at your own risk
UnverifiedThis skill has no cryptographic signature attached. We can't verify the contents match what the publisher intended.
Install this skill
No account required. Pick your tool, or use the command that detects it for you.
Run in your terminal. It detects your AI tool.
npx @skills-hub-ai/cli install pragmatic-haskell-haskell-testingManual setup by platform
Claude Code
~/.claude/skills/pragmatic-haskell-haskell-testing/SKILL.md
Instructions
Testing
Framework Choices
HSpec is the default test framework for unit and integration tests.
Use hspec for all describe/it style tests.
QuickCheck is the default for property-based tests when the codebase
already uses it. Use it directly or via HSpec's hspec-discover + property
integration.
Hedgehog is an excellent alternative to QuickCheck for property tests.
Prefer Hedgehog when:
- Integrated shrinking matters (no separate
Arbitrary/shrinkboilerplate —
shrinking is automatic and built in to every generator) - Generators need to be monadic or interleave IO
- You want
Range-based size control rather thanGen's implicit sizing
Do not mix QuickCheck and Hedgehog generators in the same test suite. If
starting a new project with no existing property tests, Hedgehog is the
recommended choice.
Tasty is an acceptable alternative runner, particularly for projects that
need multiple test providers under one runner (HUnit, QuickCheck, SmallCheck,
Hedgehog, golden tests). If the project already uses Tasty, stay consistent
with it. Do not mix HSpec and Tasty runners in the same package.
Test Suite Structure in Cabal
Structure test suites as libraries, not executables. This pattern is used
in large Haskell monorepos. The benefits:
- Other packages in the monorepo can depend on the test library to reuse
helpers, generators, and mocks. - The test library is independently buildable and checkable.
- It enforces a clean public API for test utilities.
-- Cabal stanza layout
library myapp-test-lib -- test helpers as a library
hs-source-dirs: test
exposed-modules: MyApp.Test.Generators
MyApp.Test.Fixtures
MyApp.Test.MockStore
build-depends: base, myapp, hspec, QuickCheck
test-suite myapp-test -- executable that runs the tests
type: exitcode-stdio-1.0
hs-source-dirs: test-main
main-is: Main.hs
build-depends: base, myapp-test-lib, hspec
See references/testing-examples.md for the complete cabal stanza.
Structure: Pure Core, Effectful Boundary
Keep as much logic as possible in pure functions. Pure functions are trivially
testable — no IO, no mocking, no resource setup.
Push effects to the boundary:
-- Easy to test (pure)
calculateInvoiceTotal :: [LineItem] -> Discount -> Amount
-- Hard to test (effectful)
createInvoice :: Env -> [LineItem] -> Discount -> IO Invoice
Test the pure core exhaustively. Test the effectful boundary with a small
number of integration tests using mock injection.
Mock Injection via Function Records
Do not use type classes to abstract over effects for testability. Use a record
of functions — the same pattern used for UserStore, EventStore, etc.
-- Define the interface as a record
data UserStore = UserStore
{ fetchUser :: UserId -> IO (Maybe User)
, saveUser :: User -> IO ()
, deleteUser :: UserId -> IO ()
}
-- Production implementation
postgresUserStore :: Pool Connection -> UserStore
postgresUserStore pool = UserStore { ... }
-- Test implementation: pure IORef-backed store
inMemoryUserStore :: IORef (Map UserId User) -> UserStore
inMemoryUserStore ref = UserStore
{ fetchUser = \uid -> Map.lookup uid <$> readIORef ref
, saveUser = \u -> modifyIORef ref (Map.insert (userId u) u)
, deleteUser = \uid -> modifyIORef ref (Map.delete uid)
}
Pass the record into functions that need it. Tests construct the in-memory
version; production constructs the postgres version. No type class, no m,
no MonadUserStore constraint needed.
Unit Tests with HSpec
Each module under test gets a corresponding Spec file. Use hspec-discover
to auto-collect specs.
-- MyApp/User/ValidationSpec.hs
module MyApp.User.ValidationSpec (spec) where
import Test.Hspec
import MyApp.User.Validation
spec :: Spec
spec = do
describe "validateCreateUser" $ do
it "rejects a name shorter than 2 characters" $ ...
it "rejects an email without @" $ ...
it "accepts a valid request" $ ...
Name test files <ModuleName>Spec.hs, not Test<ModuleName>.hs.
Property Tests with QuickCheck
Use QuickCheck for invariants, round-trips, and laws.
-- Inside a Spec
describe "UserId" $ do
it "round-trips through show/read" $
property $ \(n :: Int) ->
let uid = UserId n
in read (show uid) == uid
Write Arbitrary instances in the test library so they can be shared across
packages:
-- MyApp/Test/Generators.hs
instance Arbitrary UserId where
arbitrary = UserId <$> arbitrary
shrink (UserId n) = UserId <$> shrink n
Property Tests with Hedgehog
Use Hedgehog when you want integrated shrinking or monadic generators.
Shrinking is automatic — every Gen a knows how to shrink; no separate
shrink implementation is needed.
{-# LANGUAGE OverloadedStrings #-}
module MyApp.User.HedgehogSpec (spec) where
import Hedgehog
import Hedgehog.Gen qualified as Gen
import Hedgehog.Range qualified as Range
import Test.Hspec
import Test.Hspec.Hedgehog (hedgehog)
import MyApp.User.Types (UserId (..), mkEmail, emailText)
spec :: Spec
spec = do
describe "UserId" $
it "Ord is transitive" $ hedgehog $ do
a <- forAll $ UserId <$> Gen.int Range.constantBounded
b <- forAll $ UserId <$> Gen.int Range.constantBounded
c <- forAll $ UserId <$> Gen.int Range.constantBounded
when (a <= b && b <= c) $ a <= c === True
describe "mkEmail" $
it "round-trips for valid addresses" $ hedgehog $ do
local <- forAll $ Gen.text (Range.linear 1 20) Gen.alphaNum
domain <- forAll $ Gen.text (Range.linear 1 20) Gen.alphaNum
let addr = local <> "@" <> domain
fmap emailText (mkEmail addr) === Just addr
Use hspec-hedgehog to run Hedgehog properties inside an HSpec suite, or
tasty-hedgehog to run them inside Tasty.
Golden Tests
Use tasty-golden for tests that verify output against a reference file:
pretty-printer output, serialised formats, rendered templates, or generated
code. Golden tests catch unintended output changes without requiring you to
hard-code expected values in source.
import Test.Tasty.Golden (goldenVsString)
goldenPrettyPrint :: TestTree
goldenPrettyPrint =
goldenVsString
"pretty-print UserId"
"test/golden/pretty-UserId.txt" -- reference file
(pure (renderBS (pretty (UserId 42))))
Golden files live in test/golden/ and are committed to version control.
Regenerate them intentionally with --accept when output changes by design.
What to Test
| Candidate | Test type |
|---|---|
| Pure validation / computation | Unit + property |
| Smart constructors | Unit (valid and invalid inputs) |
| Domain logic (pure) | Unit + property |
| Store operations | Unit with in-memory mock |
| Integration (real DB/network) | Tagged integration test, not in the main CI unit suite |
Arbitrary instance laws | Property (e.g. shrink produces smaller values) |
Do not write tests that assert on show output — use Eq instances and
structured assertions.
Warning Flags for Test Code
Apply the same warning flags to test code as to production code. Tests with
-Wno-incomplete-uni-patterns or -Wno-partial-fields suppressed are hiding
bugs.
See references/testing-examples.md for complete worked examples.
Security
Reviews (0)
More from Pragmatic Haskell
View source →More Build skills
Browse category →Frequently asked questions about haskell-testing
What does the haskell-testing skill do?
Testing patterns for pragmatic Haskell. Use when writing unit tests, property tests, or effectful tests; when structuring test suites in cabal; or when deciding how to inject test doubles. It's a reusable SKILL.md instruction set that loads into your AI coding assistant on demand, no prompt engineering, no copy-pasting every session.
How do I install the haskell-testing skill?
Run `npx @skills-hub-ai/cli install pragmatic-haskell-haskell-testing` from your terminal. The CLI writes the SKILL.md to the correct location for your AI tool (e.g. ~/.claude/skills/pragmatic-haskell-haskell-testing/ for Claude Code or ~/.cursor/skills/ for Cursor with --target cursor) and adds it to your project's .skills.json lockfile.
Which AI tools does haskell-testing work with?
haskell-testing runs in Claude Code. It follows the open Agent Skills standard (SKILL.md), so the same skill works in every supported tool without modification.
Is the haskell-testing skill free?
Yes. Every skill on skills-hub.ai is free and open-source. There are no premium tiers, paywalls, or usage limits. You only pay for whatever AI assistant you're already using.
How do I use haskell-testing after installing it?
In Claude Code, type `/pragmatic-haskell-haskell-testing` (or whatever slash command the skill registers) and the AI follows the skill's instructions immediately. You can also reference it by name in natural language, your AI loads the skill into context when relevant.
Can I share the haskell-testing skill with my team?
Yes. Commit your project's .skills.json lockfile and teammates run `npx @skills-hub-ai/cli install` (no args) to install every skill at the exact version you pinned. Organization-scoped installs work via skills-hub.ai organizations.