简体   繁体   中英

Cabal and HUnit interaction

I am trying to get a simple unit test to work, written in HUnit.

The module I have put the test in is named "MyTests".

module MyTests where
import qualified Test.HUnit    as H
gamma = H.TestCase (H.assertEqual "foo" 1 1)
-- Run the tests from the REPL
runTestTT $ H.TestList [H.TestLabel "foo" gamma]

I can run this module perfectly fine from the cabal repl:

λ> run
Cases: 1  Tried: 1  Errors: 0  Failures: 0
Counts {cases = 1, tried = 1, errors = 0, failures = 0}

I want to integrate these tests with Cabal such that I can run cabal test .

From a few hours of googling I found that I should be able to test my application using the following seqeuence:

cabal configure --enable-tests && cabal build tests && cabal test

I have inserted the following in my .cabal file:

Test-Suite tests
    type:           exitcode-stdio-1.0
    main-is:        Main.hs
    hs-source-dirs: test src
    test-module:    YourTestModule
    build-depends:  base
                  , HUnit
                  , Cabal
                  , QuickCheck
                  , test-framework
                  , test-framework-hunit
                  , test-framework-quickcheck2

In the Main.hs file under the test/ folder I have the following:

module Main where

import Test.Framework (defaultMain, testGroup)
import Test.Framework.Providers.HUnit
import Test.Framework.Providers.QuickCheck2 (testProperty)

import Test.QuickCheck
import Test.HUnit

import Data.List

import qualified MyTests as AG


main = defaultMain tests

tests = [
        testGroup "Some group" [
                testCase "foo" AG.gamma        
            ]
    ]

This obviously returns an error:

test/Main.hs:19:32:
    Couldn't match type ‘Test’ with ‘IO ()’
    Expected type: Assertion
      Actual type: Test
    In the second argument of ‘testCase’, namely ‘AG.gamma’
    In the expression: testCase "foo" AG.gamma

I really like the HUnit tests I have written so far (this is a MWE) and I wonder hw I can integrate these tests with eachother?

The problem is that AG.gamma is of type Test , since TestCase :: Assertion -> Test .

So HUnit has one way of creating trees of tests, and test-framework has another way of creating trees of tests, using the function testCase :: TestName -> Assertion -> Test .

Hence You can't take an HUnit.Test and pass it to testCase . But it turns out you can take an HUnit.Test and convert it to a test-framework test (or rather a list of them).

Use the other function from the test-framework module:

hUnitTestToTests :: HUnit.Test -> [TestFramework.Test]

(signature augmented to show where modules come from).

See here for more details:

https://hackage.haskell.org/package/test-framework-hunit-0.3.0.2/docs/Test-Framework-Providers-HUnit.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM