Adding tests

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, a Lib.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 bin/test and chmod a+x it:
    #!/usr/bin/env bash
    set -xe
    exec nix develop -i -c ghcid -c "cabal repl test:tests" -T :main
  • Commit your changes to Git, and test it out by running bin/test.
Links to this page
  • haskell-template
  • Philosophy

    Things like tests are not in the template repo, because I personally do not use it in every project created off this repo. Instead, a workflow like “How to add tests” should be documented (eg.: Adding tests). The same goes for project documentation (which normally would use Emanote).