4

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'
Niklas B.
  • 92,950
  • 18
  • 194
  • 224
sergeyz
  • 1,308
  • 2
  • 14
  • 23

1 Answers1

3

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.

Niklas B.
  • 92,950
  • 18
  • 194
  • 224
  • 2
    For the time being, this is a good workaround. But I think HTF should simply add all importedTests to thisModulesTests. That's what your code does by hand and that's what HTF should do automatically in the future. I've opened an issue, see https://github.com/skogsbaer/HTF/issues/15 – stefanwehr Dec 27 '12 at 11:34
  • @stefanwehr: Indeed, simply reexporting imported tests seems like the most elegant and intuitive solution – Niklas B. Dec 27 '12 at 14:56