Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple test cases in Haskell modules

I want to write my own test cases in Haskell modules. Lets say I made a "range" module. I might want to check:

(range 1 2) + (range 3 5) == (range 1 5)
(range 1 4) + empty == (range 1 4)

etc.

I'd like to put these tests in modules, and perhaps some way to turn them on/off using a compiler flag.

I'm not currently really interested in some framework generating test cases for me, I'm happy to do that myself.

like image 836
Clinton Avatar asked Dec 28 '25 20:12

Clinton


1 Answers

Why not try the new hotness that is Tasty. Tasty can be integrated with cabal for integrated building/testing.

import Test.Tasty
import Test.Tasty.HUnit

import My.Range.Module

main = defaultMain unitTests

unitTests = testGroup "Unit tests" [
  testCase "Adding Continuous Ranges" $
    (range 1 2) + (range 3 5) @?= (range 1 5),
  testCase "Adding an empty Range" $
    (range 1 4) + empty @?= (range 1 4)]

There are command line options

% ./test --help
Mmm... tasty test suite

Usage: ex [-p|--pattern ARG] [-l|--list-tests] [-j|--num-threads ARG]
          [-q|--quiet] [--hide-successes] [--smallcheck-depth ARG]
          [--quickcheck-tests ARG] [--quickcheck-replay ARG]
          [--quickcheck-max-size ARG] [--quickcheck-max-ratio ARG]

Available options:
  -h,--help                Show this help text
  -p,--pattern ARG         Select only tests that match pattern
  -l,--list-tests          Do not run the tests; just print their names
  -j,--num-threads ARG     Number of threads to use for tests execution
  -q,--quiet               Do not produce any output; indicate success only by
                           the exit code
  --hide-successes         Do not print tests that passed successfully
  --smallcheck-depth ARG   Depth to use for smallcheck tests
  --quickcheck-tests ARG   Number of test cases for QuickCheck to generate
  --quickcheck-replay ARG  Replay token to use for replaying a previous test run
  --quickcheck-max-size ARG
                           Size of the biggest test cases quickcheck generates
  --quickcheck-max-ratio ARG
                           Maximum number of discared tests per successful test
                           before giving up

You can use the --pattern option to select only specific tests to run

like image 147
Thomas Avatar answered Dec 31 '25 17:12

Thomas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!