Search code examples
testinghaskellhtf

Haskell Test Framework (HTF) and test definitions in multiple modules


HTF has sample project which shows how to use test framework. Module MyPkg.A defines some tests and MyPkg.B defines some tests. Is it possible to write new module MyPkg.C which aggregates tests from modules A and B (and does not define new tests itself) ?

Instead of importing tests from A and B (in my test runner Main module), I want to import tests from single C module.

I implement MyPkg.C like this

{-# OPTIONS_GHC -F -pgmF htfpp #-}
module MyPkg.C (htf_importedTests) where

import Test.Framework
import {-@ HTF_TESTS @-} MyPkg.A
import {-@ HTF_TESTS @-} MyPkg.B

And my main test runner module like this:

{-# OPTIONS_GHC -F -pgmF htfpp #-}
module Main where

import Test.Framework
import Test.Framework.BlackBoxTest
import {-@ HTF_TESTS @-} MyPkg.C

main = htfMain htf_importedTests

When I try to compile this code, I get an error:

TestMain.hs:23:5:
Not in scope: `htf_MyPkg_C_thisModulesTests'
Perhaps you meant `htf_Main_thisModulesTests'

Solution

  • HTF doesn't seem to directly support this mode of operation. You can however achieve the goal with some hackery:

    A.hs

    {-# OPTIONS_GHC -F -pgmF htfpp #-}
    module A where
    import Test.Framework
    
    funA :: Int -> Int
    funA x = x + 1
    
    test_funA = assertEqual (funA 41) 42
    

    B.hs

    {-# OPTIONS_GHC -F -pgmF htfpp #-}
    module B where
    import Test.Framework
    
    funB :: Int -> Int
    funB x = x + 1
    
    test_funB = assertEqual (funB 41) 42
    

    C.hs

    module C where
    import Test.Framework.TestManager
    
    import A  
    import B
    
    htf_C_thisModulesTests =
       makeAnonTestSuite $ map testSuiteAsTest                  
         [ htf_A_thisModulesTests
         , htf_B_thisModulesTests
         ]
    

    TestMain.hs:

    {-# OPTIONS_GHC -F -pgmF htfpp #-}
    module TestMain where
    import Test.Framework
    import {-@ HTF_TESTS @-} C
    
    main = htfMain htf_importedTests
    

    This works, but the problem is that we have to hardcode the module names in C.hs. I think a good idea would be to introduce a new delegate_ test definition statement that allows you to delegate a test to another TestableHTF, so that we can export htf_importedTests as a single test. Maybe you can open a feature request for that or different solution to the problem.