简体   繁体   English

Cabal和HUnit的交互

[英]Cabal and HUnit interaction

I am trying to get a simple unit test to work, written in HUnit. 我正在尝试使用HUnit编写一个简单的单元测试。

The module I have put the test in is named "MyTests". 我放入测试的模块名为“ 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: 我可以从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 . 我想将这些测试与Cabal集成在一起,以便可以运行Cabal 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: 我在.cabal文件中插入了以下内容:

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: test/文件夹下的Main.hs文件中,我具有以下内容:

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? 我真的很喜欢到目前为止已经编写的HUnit测试(这是MWE),我不知道为什么我可以将这些测试相互集成?

The problem is that AG.gamma is of type Test , since TestCase :: Assertion -> Test . 问题在于AG.gammaTest类型的,因为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 . 因此,使用功能testCase :: TestName -> Assertion -> Test ,HUnit提供了一种创建测试树的方法,而test-framework提供了另一种创建测试树的方法。

Hence You can't take an HUnit.Test and pass it to testCase . 因此,您不能将HUnit.Test传递给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). 但事实证明,您可以使用HUnit.Test 并将转换test-framework测试(或更确切地说是它们的列表)。

Use the other function from the test-framework module: 使用test-framework模块中的其他功能:

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 https://hackage.haskell.org/package/test-framework-hunit-0.3.0.2/docs/Test-Framework-Providers-HUnit.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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