describe Declare a test group
Example usage:
describe "addition" $ do
it "adds 3 to 5 to result in 8" $
3 + 5 `shouldBe` 8
it "adds 4 to 7 to result in 11" $
4 + 7 `shouldBe` 11:: a typeCtrl KGHC 9.10.3 · lts/ghc-9.10.x · 248f8f0 · 2026-10-05
Modulesydtest-0.22.0.0Haskell2010
This module defines all the functions you will use to define your test suite.
describe Declare a test group
describe "addition" $ do
it "adds 3 to 5 to result in 8" $
3 + 5 `shouldBe` 8
it "adds 4 to 7 to result in 11" $
4 + 7 `shouldBe` 11it Declare a test
Note: Don't look at the type signature unless you really have to, just follow the examples.
describe "addition" $
it "adds 3 to 5 to result in 8" $
3 + 5 == 8describe "readFile and writeFile" $
it "reads back what it wrote for this example" $ do
let cts = "hello world"
let fp = "test.txt"
writeFile fp cts
cts' <- readFile fp
cts' `shouldBe` ctsdescribe "sort" $
it "is idempotent" $
forAllValid $ \ls ->
sort (sort ls) `shouldBe` (sort (ls :: [Int]))describe "readFile and writeFile" $
it "reads back what it wrote for any example" $ do
forAllValid $ \fp ->
forAllValid $ \cts -> do
writeFile fp cts
cts' <- readFile fp
cts' `shouldBe` ctsThis is quite a rare use-case but here is an example anyway:
before (pure 3) $ describe "addition" $
it "adds 3 to 5 to result in 8" $ \i ->
i + 5 == 8This test sets up a temporary directory as an inner resource, and makes it available to each test in the group below.
let setUpTempDir func = withSystemTempDir $ \tempDir -> func tempDir
in around setUpTempDir $ describe "readFile and writeFile" $
it "reads back what it wrote for this example" $ \tempDir -> do
let cts = "hello world"
let fp = tempDir </> "test.txt"
writeFile fp cts
cts' <- readFile fp
cts' `shouldBe` ctsThis is quite a rare use-case but here is an example anyway:
before (pure 3) $ describe "multiplication" $
it "is commutative for 5" $ \i ->
i * 5 == 5 * 3let setUpTempDir func = withSystemTempDir $ \tempDir -> func tempDir
in around setUpTempDir $ describe "readFile and writeFile" $
it "reads back what it wrote for this example" $ \tempDir ->
property $ \cts -> do
let fp = tempDir </> "test.txt"
writeFile fp cts
cts' <- readFile fp
cts' `shouldBe` ctsDeclare a test that uses an outer resource
This is quite a rare use-case but here is an example anyway:
beforeAll (pure 3) $ describe "addition" $
itWithOuter "adds 3 to 5 to result in 8" $ \i ->
i + 5 == 8This test sets up a temporary directory as an inner resource, and makes it available to each test in the group below.
let setUpTempDir func = withSystemTempDir $ \tempDir -> func tempDir
in aroundAll setUpTempDir describe "readFile and writeFile" $
itWithOuter "reads back what it wrote for this example" $ \tempDir -> do
let cts = "hello world"
let fp = tempDir </> "test.txt"
writeFile fp cts
cts' <- readFile fp
cts' `shouldBe` ctsThis is quite a rare use-case but here is an example anyway:
beforeAll (pure 3) $ describe "multiplication" $
itWithOuter "is commutative for 5" $ \i ->
i * 5 == 5 * 3let setUpTempDir func = withSystemTempDir $ \tempDir -> func tempDir
in aroundAll setUpTempDir describe "readFile and writeFile" $
itWithouter "reads back what it wrote for this example" $ \tempDir ->
property $ \cts -> do
let fp = tempDir </> "test.txt"
writeFile fp cts
cts' <- readFile fp
cts' `shouldBe` ctsDeclare a test that uses both an inner and an outer resource
This is quite a rare use-case but here is an example anyway:
beforeAll (pure 3) $ before (pure 5) $ describe "addition" $
itWithBoth "adds 3 to 5 to result in 8" $ \i j ->
i + j == 8This test sets up a temporary directory as an inner resource, and makes it available to each test in the group below.
let setUpTempDir func = withSystemTempDir $ \tempDir -> func tempDir
in aroundAll setUpTempDir describe "readFile and writeFile" $ before (pure "hello world") $
itWithBoth "reads back what it wrote for this example" $ \tempDir cts -> do
let fp = tempDir </> "test.txt"
writeFile fp cts
cts' <- readFile fp
cts' `shouldBe` ctsThis is quite a rare use-case but here is an example anyway:
beforeAll (pure 3) $ before (pure 5) $ describe "multiplication" $
itWithBoth "is commutative" $ \i j ->
i * j == 5 * 3let setUpTempDir func = withSystemTempDir $ \tempDir -> func tempDir
in aroundAll setUpTempDir describe "readFile and writeFile" $ before (pure "test.txt") $
itWithBoth "reads back what it wrote for this example" $ \tempDir fileName ->
property $ \cts -> do
let fp = tempDir </> fileName
writeFile fp cts
cts' <- readFile fp
cts' `shouldBe` ctsDeclare a test that uses all outer resources
You will most likely never need this function, but in case you do:
Note that this will always require a type annotation, along with the GADTs and ScopedTypeVariables extensions.
beforeAll (pure 'a') $ beforeAll (pure 5) $
itWithAll "example" $
\(HCons c (HCons i HNil) :: HList '[Char, Int]) () ->
(c, i) `shouldeBe` ('a', 5)specify A synonym for it
A synonym for itWithOuter
A synonym for itWithBoth
A synonym for itWithAll
Convenience function for backwards compatibility with hspec
prop s p = it s $ property pxspecify A synonym for xit
A synonym for xitWithOuter
A synonym for xitWithBoth
A synonym for xitWithAll
Declare a test that has not been written yet.
Declare a test that has not been written yet for the given reason.