Search code examples
unit-testinghaskelltest-framework

How to use test-framework-th with multiple modules containing tests?


I want to use test-framework-th for generating test groups.

I have this main test module:

module Main where

import           Test.Framework.TH

import           Foo.Test

main = $(defaultMainGenerator)

And this module containing the test:

module Foo.Test where

import           Test.HUnit

case_1 = do 1 @=? 2

However, defaultMainGenerator does not detect the test in the Foo.Test module. It only detects tests in the module in which it is called (in this case Main).

How can I split my tests across modules without duplicating boilerplate for every test?


Solution

  • Use testGroupGenerator in each module to generate a test group for that module, then use those from main, e.g.

    Foo.hs:

    module Foo where
    import Test.Framework.TH
    tests = $(testGroupGenerator)
    ...
    

    Bar.hs:

    module Bar where
    import Test.Framework.TH
    tests = $(testGroupGenerator)
    ...
    

    Main.hs:

    import Test.Framework
    
    import Foo
    import Bar
    
    main = defaultMain [Foo.tests, Bar.tests]