Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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?

like image 727
Dwilson Avatar asked Aug 08 '12 23:08

Dwilson


2 Answers

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.

like image 103
Dwilson Avatar answered Nov 13 '22 09:11

Dwilson


Although Dwilson's answer is good, detailed is currently not well supported. You can intergrate HUnit with cabal using exitcode-stdio-1.0 and Test.Framework.

It will output all successful and failed tests to stdout as well as fail building if tests fail. See my gist.

like image 2
risto Avatar answered Nov 13 '22 08:11

risto