Testing Haskell code

A Haskell test is a program that compares what a function returns with what it should return, and the run reports the answer through its exit status. Everything else a framework adds sits on top of that: a line per check, a message that names the values, a summary, and a status the build server reads. These pages build that base by hand, because the libraries that package it, HUnit, hspec, tasty and QuickCheck, are not in the image and there is no network here to fetch them.

Every page shows code that was compiled and run, with the real output and the real exit status. The last page is the exception and says so: it covers the five tools that are not installed, with the error the resolver prints for a missing package and the route to install each one on a machine with a network. Nothing on these pages is a guess.

The pages are independent of the Haskell course: read one when you need it. The lessons behind them are running a Cabal test suite, GHC flags and testing the capstone parser.

Assert Assertions and a test driver A hand-written check, a driver that reports every failure, and the exit status that makes a run pass or fail. Open Prop Property checks with a hand-written generator A seeded generator without the random package, three properties that hold, and the counterexample of one that does not. Open Cabal A test suite Cabal runs An exitcode-stdio-1.0 suite over a small library, cabal build and cabal test offline, and a suite that fails. Open Golden Golden files and diff A report compared with a fixed file, the diff of a changed output, and the status of the comparison in a script. Open hpc Coverage with hpc -fhpc on two modules, -hpcdir and -outputdir, hpc report per module, and the run that covers the missing branch. Open Lint hlint and ormolu Three hlint hints and an ormolu formatting diff on one file, then the same file after both tools accept it. Open Tools QuickCheck, HUnit, hspec, tasty and doctest What the image does not have, the real error cabal gives for a missing package, and the install route for each tool. Open

The programs on these pages were built with GHC 9.10.3 and cabal-install 3.16.0.0, and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.