I'm relatively new to Haskell, so apologies in advance if my terminology is not quite correct.
I would like to implement some plain unit test for a very simple project, managed through cabal
. I noticed this very similar question, but it didn't really help. This one didn't either (and it mentions tasty
, see below).
I think I can accomplish this by using only HUnit
- however, I admit I am a bit confused by all other "things" that guides on the net talk about:
- I don't quite appreciate the difference between the interfaces
exitcode-stdio-1.0
anddetailed-0.9
- I am not sure about the differences (or mid- and long-terms) implication of using
HUnit
orQuickcheck
or others? - What's the role of
tasty
that the HUnit guide mentions.
So, I tried to leave all "additional" packages out of the equation and everything else as "default" as much as I could aside and did the following:
$ mkdir example ; mkdir example/test
$ cd example
$ cabal init
Then edited example.cabal
and added this section:
Test-Suite test-example
type: exitcode-stdio-1.0
hs-source-dirs: test, app
main-is: Main.hs
build-depends: base >=4.15.1.0,
HUnit
default-language: Haskell2010
Then I created test/Main.hs
with this content:
module Main where
import Test.HUnit
tests = TestList [
TestLabel "test2"
(TestCase $ assertBool "Why is this not running," False)
]
main :: IO ()
main = do
runTestTT tests
return ()
Finally, I tried to run the whole lot:
$ cabal configure --enable-tests && cabal build && cabal test
Up to date
Build profile: -w ghc-9.2.4 -O1
In order, the following will be built (use -v for more details):
- example-0.1.0.0 (test:test-example) (additional components to build)
Preprocessing test suite 'test-example' for example-0.1.0.0..
Building test suite 'test-example' for example-0.1.0.0..
Build profile: -w ghc-9.2.4 -O1
In order, the following will be built (use -v for more details):
- example-0.1.0.0 (test:test-example) (ephemeral targets)
Preprocessing test suite 'test-example' for example-0.1.0.0..
Building test suite 'test-example' for example-0.1.0.0..
Running 1 test suites...
Test suite test-example: RUNNING...
Test suite test-example: PASS
Test suite logged to:
/home/jir/workinprogress/haskell/example/dist-newstyle/build/x86_64-linux/ghc-9.2.4/example-0.1.0.0/t/test-example/test/example-0.1.0.0-test-example.log
1 of 1 test suites (1 of 1 test cases) passed.
And the output is not what I expected. I'm clearly doing something fundamentally wrong, but I don't know what it is.
CodePudding user response:
In order for the exitcode-stdio-1.0
test type to recognize a failed suite, you need to arrange for your test suite's main
function to exit with failure in case there are any test failures. Fortunately, there's a runTestTTAndExit
function to handle this, so if you replace your main
with:
main = runTestTTAndExit tests
it should work fine.