如何使用HUnit和Cabal进行自动测试? [英] How to use HUnit and Cabal to do Automated Testing?

查看:177
本文介绍了如何使用HUnit和Cabal进行自动测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在与Cabal争斗一天中的很大一部分,试图使其自动化测试功能与HUnit一起工作。我已阅读此处的文档和< a href =http://hackage.haskell.org/packages/archive/Cabal/1.10.1.0/doc/html/Distribution-TestSuite.html =noreferrer>这里,我有我的Test-Suite部分设置就像它显示的那样,但是每当我尝试使用 cabal build 构建软件包时,Cabal表示支持的唯一Test-Suite类型是退出码-标准输入输出-1.0 。什么给了?

解决方案

背景



cabal网站上的文档是未来文档,也就是说,并非所有这些功能都已经实现并发布。 Cabal-install 0.14.0附带了详细资料-0.9 界面,该界面是文档中指定的版本( detailed-1.0 ),但我还没有遇到任何与此相关的问题。如果您有cabal-install 0.10.2附带的Haskell平台版本2011.4,您将无法使用 detailed-0.9 界面。您需要升级到cabal-install 0.14.0附带的Haskell Platform 2012.2。你也可以单独升级cabal-install,这是我做的,因为在Fedora 17上,Haskell Platform仅在2011.4上安装。



安装



在文档 here ,你会看到一个如何使用QuickCheck的 detailed-0.9 界面的例子。它提到了一些与HUnit,QuickCheck1和QuickCheck2有接口的软件包,但只有QuickCheck2的软件包可用于hackage。如果你想要其他框架的包,你需要使用darcs(一个VCS)从这个位置。你想为HUnit接口运行的命令是这样的: darcs获取http://community.haskell.org/~ttuegel/cabal-test-hunit/ 。您可能需要调整.cabal文件才能生成,具体取决于 ghc 3。* cabal 1.10 。我把它改成了我的版本( ghc 4。* cabal 1.14 ),并且它构建得很好。



测试



一旦建立了界面,您需要在测试模块中做一些事情,以便Cabal可以运行它。具体来说,您需要导入 Distribution.TestSuite Distribution.TestSuite.HUnit 。之后,您需要使用HUnit界面中提供的功能将您的HUnit测试转换为Cabal测试。以下是相关的代码行:

 输入限定的Distribution.TestSuite为Cabal 
导入合格的Distribution.TestSuite.HUnit为CabalHUnit

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

就是这样!你应该可以运行 cabal configure --enable-tests&& cabal build&& cabal test 并查看您的单元测试通过(或失败)。



编辑

编辑澄清 detailed-0.9 界面包含在cabal-install 0.14.0中,而不是 detailed-1.0

I've been fighting with Cabal for a good portion of a day trying to make its automated testing features work with 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. 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-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. 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. You'll need to upgrade to Haskell Platform 2012.2 which comes with cabal-install 0.14.0. 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.

Installation

In the documentation here you'll see an example of how to use the detailed-0.9 interface with QuickCheck. It mentions some packages that have interfaces to HUnit, QuickCheck1, and QuickCheck2, but only the package for QuickCheck2 is available on 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. The command you want to run for the HUnit interface is this: 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. I changed this to my versions (ghc 4.* and cabal 1.14) and it built fine.

Testing

Once you have the interface built you need to do some stuff in your test module so Cabal can run it. Specifically you'll need to import both Distribution.TestSuite and Distribution.TestSuite.HUnit. After that you'll need to convert your HUnit Tests to Cabal Tests, using a function provided in the HUnit interface. 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).

Edit
Edited to clarify that the detailed-0.9 interface is included in cabal-install 0.14.0, not detailed-1.0.

这篇关于如何使用HUnit和Cabal进行自动测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆