Skip to content

Instantly share code, notes, and snippets.

@wjlroe
Forked from mbbx6spp/ExitCodeTestsExample.hs
Last active August 29, 2015 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wjlroe/60f74b61980ce6b4c9d3 to your computer and use it in GitHub Desktop.
Save wjlroe/60f74b61980ce6b4c9d3 to your computer and use it in GitHub Desktop.

Integrating Test Suites with Cabal Haskell Projects

Basics

Add a Test-Suite block to your project Cabal file like below:

Test-Suite projectname-testsuite
  Type:                     exitcode-stdio-1.0
  Main-is:                  TestSuite.hs
  Build-depends:            base, ....your test framework deps here...

Note: you might need to run: cabal configure --enable-tests and cabal build again.

Now you should be able to just run: cabal test and it should integrate.

Other options

There are two built in test suite Cabal interfaces:

  • exitcode-stdio-1.0
  • detailed-1.0

The first test suite interface built into Cabal, exitcode-stdio-1.0, depends on the block specifying a Main-is: attribute which is the path of a "main" Haskell file (i.e. a runnable file with a main defined).

The second test suite interface built into Cabal, detailed-1.0, depends on the test-module attribute inside the Test-Suite Cabal block being set to the name of the module to run. This module should export a function named tests with type signature: tests :: [Test]. The Test type can be found in module Distribution.TestSuite. It has two wrapper functions in this module: pure and impure.

Written an internal test library or framework?

Any makers of test frameworks will need to (at a minimum) define instances of the TestOptions and ImpureTestable classes (also exported from Distribution.TestSuite). If the test framework also has a pure runner, you could also define PureTestable class instances for your framework.

QuickCheck & HUnit Support

You will find cabal packages with name prefixes of cabal-test-XXX if your favorite open source Haskell test library or framework (e.g. QuickCheck, HUnit) is supported already.

Test & Have Fun!

-- Say you have a Cabal-ized Haskell project called 'awesomesauce'
Name: awesomesauce
Version: 0.1
Synopsis: The awesomesauce
Description: Awesomesauceness for Haskell (as if it needs more?)
License: BSD3
License-file: LICENSE
Author: Susan Potter
Maintainer: me@susanpotter.net
Copyright: 2011
Category: Development
Build-type: Simple
Extra-source-files: README.md
Cabal-version: >=1.2
Test-Suite awesomesauce-testsuite
Type: exitcode-stdio-1.0
Main-is: ExitCodeTestsExample.hs
Build-depends: base, QuickCheck, HUnit
-- Then your Executable or Library Cabal sections/blocks under here.
module Main (main) where
import Test.Framework
import Test.Framework.Providers.QuickCheck2 (testProperty)
import Your.Module (encrypt, decrypt)
prop_reverseReverse :: [Char] -> Bool
prop_reverseReverse s = (reverse . reverse) s == s
prop_encryptDecrypt :: [Char] -> Bool
prop_encryptDecrypt s = (encrypt . decrypt) s == s
tests =
[ testProperty "prop_reverseReverse" prop_reverseReverse
, testProperty "prop_encryptDecrypt" prop_encryptDecrypt
]
main = defaultMain tests
@wjlroe
Copy link
Author

wjlroe commented Nov 29, 2014

I've removed the detailed-1.0 stuff because it doesn't work - the types aren't compatible between Test.Framework and what it expects. It seems to be abandonware also.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment