haskell-template
does not include tests by default (see Philosophy), but you may add them as follows:
-
Split any logic code out of
Main.hs
into, say, aLib.hs
. -
Correspondingly, add
other-modules: Lib
to the βsharedβ section of your cabal file. -
Add
tests/Spec.hs
(example below):module Main where import Lib qualified import Test.Hspec (describe, hspec, it, shouldContain) main :: IO () main = hspec $ do describe "Lib.hello" $ do it "contains the world emoji" $ do toString Lib.hello `shouldContain` "π"
-
Add the tests stanza to the cabal file:
test-suite tests import: shared main-is: Spec.hs type: exitcode-stdio-1.0 hs-source-dirs: tests build-depends: hspec
-
Update
hie.yaml
accordingly; for example, by adding,- path: "tests" component: "test:tests"
-
Add the test command to the mission-control scripts section of the flake file
mission-control.scripts = { test = { description = "Run all tests"; exec = '' ghcid -c "cabal repl test:tests" -T :main ''; }; };
-
Commit your changes to Git, and test it out by running
, test
from the (reloaded) dev shell.