Background
Using stack, and its preset file Spec.hs
, as far as I know you need to import the following test framework modules in order to execute a proper test:
import qualified Test.Framework as TF
import qualified Test.Framework.Providers.HUnit as FHU
import qualified Test.Framework.Providers.QuickCheck2 as QC2
import qualified Test.HUnit as HU
import qualified Test.QuickCheck as QC
Hence, you also need to add the add dependecies into the package.yaml
file, as follows:
tests:
XYZ-test:
main: Spec.hs
source-dirs: test
ghc-options:
- -threaded
- -rtsopts
- -with-rtsopts=-N
dependencies:
- Test4
- test-framework
- test-framework-hunit
- test-framework-quickcheck2
- HUnit
- QuickCheck
If you import the subject to test (calling it MyModule
) and implement test cases in Spec.hs
for that module then you cannot test the functions that are internally used in the module (MyModule
).
To test the internal functions you could implement the tests inside the module (MyModule
) and export the tests.
module MyModule
(
...
testCases, -- exported test cases
-- fun1 -- internal function not exported
) where
...
import qualified Test.Framework as TF
import qualified Test.Framework.Providers.HUnit as FHU
import qualified Test.HUnit as HU
fun1 :: [Bool] -> Integer -- internal function not exported
fun1 ...
testCases =
(FHU.testCase "MyModule.fun1 #1" ((fun1 []) HU.@?= 0)) :
(FHU.testCase "MyModule.fun1 #2" ((fun1 [True]) HU.@?= 0)) :
(FHU.testCase "MyModule.fun1 #2" ((fun1 [True, True]) HU.@?= 2)) :
[]
But then you also need to import the test framework (at least, Test.Framework, Test.Framework.Providers.HUnit, and Test.HUnit) and need to add additional dependencies also to the library of (MyModule
). Hence, package.yaml would look like this:
...
dependencies:
- ...
- test-framework
- test-framework-hunit
- HUnit
library:
source-dirs: src
...
Question
Is there a more lean aproach to export the unit test of module MyModule
?