Tasty: Haskell's Composable Testing Framework
Tasty is a test framework for Haskell that treats test suites as composable trees. Unlike HSpec or HUnit alone, Tasty integrates multiple testing libraries—HUnit for unit tests, QuickCheck for property tests, SmallCheck for exhaustive small-scope tests—into a single structured suite with unified reporting and filtering.
Setup
test-suite my-app-tests
type: exitcode-stdio-1.0
main-is: Main.hs
build-depends:
, base
, tasty
, tasty-hunit
, tasty-quickcheck
, tasty-smallcheck
, tasty-hspec
, my-appBasic Test Tree
import Test.Tasty
import Test.Tasty.HUnit
import Test.Tasty.QuickCheck as QC
main :: IO ()
main = defaultMain tests
tests :: TestTree
tests = testGroup "My App Tests"
[ unitTests
, propertyTests
, integrationTests
]
unitTests :: TestTree
unitTests = testGroup "Unit Tests"
[ testCase "2+2 = 4" $ 2 + 2 @?= 4
, testCase "reverse of empty is empty" $ reverse [] @?= ([] :: [Int])
, testCase "head of non-empty list" $ head [1,2,3] @?= 1
]
propertyTests :: TestTree
propertyTests = testGroup "Property Tests"
[ QC.testProperty "sort idempotent" $
\(xs :: [Int]) -> sort (sort xs) == sort xs
, QC.testProperty "reverse involution" $
\(xs :: [Int]) -> reverse (reverse xs) == xs
]HUnit Assertions
import Test.Tasty.HUnit
testCase "user validation" $ do
validateEmail "alice@example.com" @?= Right "alice@example.com"
validateEmail "bad-email" @?= Left "Invalid email"
validateEmail "" @?= Left "Email cannot be empty"
-- @?= is assertEqual
-- @? is assertBool
-- assertFailure "message" unconditionally fails
testCase "asserting on IO" $ do
result <- fetchUser "user-1"
result @?= Just (User "user-1" "Alice")
testCase "exception testing" $ do
assertThrows "Expected ArithException" anyArithException $
evaluate (1 `div` 0 :: Int)Combining Providers
import qualified Test.Tasty.QuickCheck as QC
import qualified Test.Tasty.SmallCheck as SC
import Test.Tasty.HUnit
-- SmallCheck exhaustively tests all values up to a depth
-- QuickCheck randomly samples
-- Use both for important properties
tests :: TestTree
tests = testGroup "Sort"
[ testGroup "HUnit"
[ testCase "empty list" $ sort [] @?= ([] :: [Int])
, testCase "singleton" $ sort [1] @?= [1]
, testCase "already sorted" $ sort [1,2,3] @?= [1,2,3]
]
, testGroup "QuickCheck"
[ QC.testProperty "idempotent" $
\(xs :: [Int]) -> sort (sort xs) == sort xs
, QC.testProperty "length preserved" $
\(xs :: [Int]) -> length (sort xs) == length xs
]
, testGroup "SmallCheck"
[ SC.testProperty "output is sorted" $
\(xs :: [Int]) ->
let s = sort xs
in and $ zipWith (<=) s (tail s) || length s <= 1
]
]Resource Management
Tasty's withResource bracket allocates and tears down resources for a group of tests:
import Test.Tasty
databaseTests :: TestTree
databaseTests = withResource
(do
conn <- connectPostgreSQL testConnectionString
migrate conn
return conn
)
(\conn -> close conn)
$ \getConn ->
testGroup "Database Tests"
[ testCase "insert user" $ do
conn <- getConn
userId <- insertUser conn (NewUser "Alice" "alice@example.com")
userId `assertNotEqual` 0
, testCase "fetch user" $ do
conn <- getConn
insertUser conn (NewUser "Bob" "bob@example.com")
user <- fetchUserByEmail conn "bob@example.com"
fmap userName user @?= Just "Bob"
]Parallel Test Execution
Mark independent tests as parallel:
tests :: TestTree
tests = testGroup "Parallel Tests"
[ -- These run in parallel
testGroup "Independent Unit Tests" $
map (\n -> testCase ("test " ++ show n) $ threadDelay 100000)
[1..20 :: Int]
]
main :: IO ()
main = defaultMain $ localOption (NumThreads 4) testsOr set via command line:
cabal test --test-options="+RTS -N4 -RTS"Custom Ingredients and Reporters
Tasty's output can be customized with "ingredients":
import Test.Tasty.Ingredients.Basic
import Test.Tasty.Runners.Html -- tasty-html package
main :: IO ()
main = defaultMainWithIngredients
[ listingTests
, htmlRunner
, consoleTestReporter
]
testsGenerate an HTML report:
cabal test --test-options="--html=test-results.html"Filtering and Running Subsets
# Run tests matching a pattern
cabal test --test-options="--pattern /Sort"
# List all tests without running
cabal test --test-options="--list-tests"
# Run a specific test by exact name
cabal test --test-options="--pattern '$1 == \"insert user\"'"
# Timeout per test (seconds)
cabal test --test-options="--timeout 10s"QuickCheck Configuration in Tasty
import Test.Tasty.QuickCheck
tests :: TestTree
tests = testGroup "With Config"
[ localOption (QuickCheckTests 1000) $
localOption (QuickCheckMaxSize 100) $
testGroup "Intensive Tests"
[ QC.testProperty "important property" $
\(xs :: [Int]) -> sort (sort xs) == sort xs
]
]Integration with HSpec
Use tasty-hspec to include HSpec specs in Tasty:
import Test.Tasty.Hspec
hspecTests :: IO TestTree
hspecTests = testSpec "HSpec Tests" $ do
describe "User" $ do
it "validates email" $
validateEmail "alice@example.com" `shouldBe` Right "alice@example.com"
main :: IO ()
main = do
specs <- hspecTests
defaultMain $ testGroup "All Tests"
[ specs
, unitTests
, propertyTests
]Handling Test Dependencies
When tests must run in a specific order (e.g., create before read):
import Test.Tasty.Sequence -- tasty-sequence package
orderedTests :: TestTree
orderedTests = sequentialTestGroup "Ordered DB Tests" AllSucceed
[ testCase "1. Create schema" createSchema
, testCase "2. Insert test data" insertTestData
, testCase "3. Verify data" verifyData
, testCase "4. Cleanup" cleanup
]Golden Tests
Compare output against stored reference files:
import Test.Tasty.Golden
goldenTests :: TestTree
goldenTests = testGroup "Golden Tests"
[ goldenVsString "render user JSON" "test/golden/user.json" $ do
let user = User "1" "Alice" "alice@example.com"
return $ encode user
, goldenVsFile "generate report" "test/golden/report.txt" "test/output/report.txt" $
generateReport >>= writeFile "test/output/report.txt"
]Update golden files when output intentionally changes:
cabal test --test-options="--accept"Conclusion
Tasty's strength is composability: build a test tree from HUnit assertions, QuickCheck properties, SmallCheck exhaustive checks, and golden file comparisons, all in one suite with unified filtering and parallel execution. The withResource bracket and sequentialTestGroup cover resource lifecycle and ordering requirements. For new Haskell projects, Tasty is the recommended framework when you need to mix multiple test styles.