我正在尝试用HUnit编写一个简单的单元测试。
我将测试放入的模块名为"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]我可以在cabal repl中很好地运行这个模块:
λ> run
Cases: 1 Tried: 1 Errors: 0 Failures: 0
Counts {cases = 1, tried = 1, errors = 0, failures = 0}我想把这些测试和Cabal集成起来,这样我就可以运行cabal test了。
通过几个小时的googling搜索,我发现我应该能够使用以下序列测试我的应用程序:
cabal configure --enable-tests && cabal build tests && cabal test我在我的.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在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
]
]这显然会返回一个错误:
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我真的很喜欢我到目前为止写的HUnit测试(这是一个MWE),我想知道我可以将这些测试相互集成吗?
发布于 2016-02-27 06:23:29
问题是AG.gamma的类型是Test,因为TestCase :: Assertion -> Test。
因此,HUnit有一种创建测试树的方法,而test-framework有另一种使用函数testCase :: TestName -> Assertion -> Test创建测试树的方法。
因此,您不能接受HUnit.Test并将其传递给testCase。但事实证明,您可以将HUnit.Test转换为test-framework测试(或者更确切地说,是它们的列表)。
使用test-framework模块中的另一个函数:
hUnitTestToTests :: HUnit.Test -> [TestFramework.Test](扩展签名以显示模块的来源)。
有关更多详细信息,请参阅此处:
https://stackoverflow.com/questions/33758125
复制相似问题