简体   繁体   English

如何使用HUnit和Cabal进行自动化测试?

[英]How to use HUnit and Cabal to do Automated Testing?

I've been fighting with Cabal for a good portion of a day trying to make its automated testing features work with HUnit. 我一直在与Cabal争夺一天的大部分时间,试图使其自动化测试功能与HUnit一起使用。 I've read the documentation here and here , and I have my Test-Suite section set up like it shows, but whenever I try and build the package using cabal build Cabal says that the only Test-Suite type supported is exitcode-stdio-1.0 . 我已经在这里这里阅读了文档,我的Test-Suite部分就像它显示的那样设置,但每当我尝试使用cabal build软件包时,Cabal说支持的唯一Test-Suite类型是exitcode-stdio-1.0 What gives? 是什么赋予了?

Background 背景

So here's the deal, the documentation on the cabal site is "future documentation," that is, not all of those features are implemented and released yet. 所以这是交易,cabal网站上的文档是“未来文档”,也就是说,并非所有这些功能都已实现和发布。 Cabal-install 0.14.0 comes with the detailed-0.9 interface, which is a version behind what's specified in the docs ( detailed-1.0 ), but I haven't encountered any problems related to this yet. Cabal-install 0.14.0附带detailed-0.9接口,这是一个落后于docs( detailed-1.0 )的版本,但我还没有遇到任何与此相关的问题。 If you have the Haskell Platform version 2011.4 which comes with cabal-install 0.10.2 you won't be able to use the detailed-0.9 interface. 如果你有cabal-install 0.10.2附带的Haskell平台版本2011.4,你将无法使用detailed-0.9接口。 You'll need to upgrade to Haskell Platform 2012.2 which comes with cabal-install 0.14.0. 您需要升级到装有cabal-install 0.14.0的Haskell Platform 2012.2。 You could also just upgrade cabal-install separately, which is what I did because on Fedora 17 the Haskell Platform is only on 2011.4. 您也可以单独升级cabal-install,这就是我所做的,因为在Fedora 17上,Haskell平台仅在2011.4上。

Installation 安装

In the documentation here you'll see an example of how to use the detailed-0.9 interface with QuickCheck. 这里的文档中您将看到如何使用QuickCheck的detailed-0.9接口的示例。 It mentions some packages that have interfaces to HUnit, QuickCheck1, and QuickCheck2, but only the package for QuickCheck2 is available on hackage. 它提到了一些包含HUnit,QuickCheck1和QuickCheck2接口的软件包,但只有QuickCheck2软件包可用于hackage。 If you want the packages for the rest of the frameworks you'll need to use darcs (a VCS) to download them from this location. 如果你想为框架,其余部分的包,你将需要使用的darcs(一个VCS)从下载他们这个位置。 The command you want to run for the HUnit interface is this: darcs get http://community.haskell.org/~ttuegel/cabal-test-hunit/ . 您要为HUnit接口运行的命令是: darcs get http://community.haskell.org/~ttuegel/cabal-test-hunit/ You may have to adjust the .cabal file in order to get it to build, specifically it relies on ghc 3.* and cabal 1.10 . 您可能必须调整.cabal文件才能使其生成,特别是它依赖于ghc 3.*cabal 1.10 I changed this to my versions ( ghc 4.* and cabal 1.14 ) and it built fine. 我把它更改为我的版本( ghc 4.*cabal 1.14 )并且它构建得很好。

Testing 测试

Once you have the interface built you need to do some stuff in your test module so Cabal can run it. 一旦你构建了界面,你需要在你的测试模块中做一些事情,以便Cabal可以运行它。 Specifically you'll need to import both Distribution.TestSuite and Distribution.TestSuite.HUnit . 具体来说,您需要导入Distribution.TestSuiteDistribution.TestSuite.HUnit After that you'll need to convert your HUnit Tests to Cabal Tests, using a function provided in the HUnit interface. 之后,您需要使用HUnit界面中提供的功能将HUnit Tests转换为Cabal Tests。 Here's the relevant lines of code: 这是相关的代码行:

import qualified Distribution.TestSuite as Cabal
import qualified Distribution.TestSuite.HUnit as CabalHUnit

tests = map (\(x,y) -> CabalHUnit.test x y) [("Login tests", loginTests)]

That's it! 而已! You should be able to run cabal configure --enable-tests && cabal build && cabal test and see your unit tests pass (or fail). 您应该能够运行cabal configure --enable-tests && cabal build && cabal test并查看您的单元测试通过(或失败)。

Edit 编辑
Edited to clarify that the detailed-0.9 interface is included in cabal-install 0.14.0, not detailed-1.0 . 编辑以澄清detailed-0.9接口包含在cabal-install 0.14.0中,而不是detailed-1.0

Although Dwilson's answer is good, detailed is currently not well supported. 虽然Dwilson的回答是不错的, detailed ,目前没有得到很好的支持。 You can intergrate HUnit with cabal using exitcode-stdio-1.0 and Test.Framework . 您可以使用HUnit exitcode-stdio-1.0Test.Framework HUnitcabal Test.Framework

It will output all successful and failed tests to stdout as well as fail building if tests fail. 如果测试失败,它会将所有成功和失败的测试stdoutstdout以及构建失败。 See my gist . 看看我的要点

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

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