Creating a Full Haskell Stack with Tests
Asked Answered
P

1

12

I'm new to Haskell and I'm trying to structure a program under test. I have decided to use HUnit and Cabal.

From what I have seen a well strucutred project looks the following:

src/
  AppName/
  Appname.hs
testsuite/
  tests/
    AppName/
  TestRunner.hs
AppName.cabal
Setup.hs

The parts that are a mystery to me are the TestRunner.hs and the AppName.cabal.

What would a testrunner look like that runs all of the test under the testsuite/tests directory and sub directories? And how can it be integrated with Cabal?

Also, how do you put the hackage dependencies in the AppName.cabal and build them from the command line?

I am having a hard time finding a full example building an application from scratch with tests and dependencies.

Thanks

Platysma answered 25/11, 2012 at 3:43 Comment(1)
Did you check out haskell.org/haskellwiki/How_to_write_a_Haskell_program? It might answer some of your questions.Devy
M
18

Here's a fragment of the .cabal file I used for one of my recent libraries.

...
Library
  Build-depends:        base >= 4 && < 5, bytestring, directory, filepath, hslogger,
                        SHA, zlib
  Ghc-options:          -Wall
  Exposed-modules:      Ltc.Store

Test-suite reference
  Hs-Source-Dirs:       Test, .
  Main-Is:              ReferenceProps.hs
  Type:                 exitcode-stdio-1.0

  Build-Depends:        base >= 4 && < 5, bytestring, directory, filepath, hslogger,
                        SHA, zlib
  Ghc-Options:          -Wall

  Build-Depends:        test-framework, test-framework-hunit, test-framework-quickcheck2,
                        HUnit, QuickCheck

As we can see the cabal file defines a library and a testsuite. The library defines the modules it exports, the packages it depends on, and sets some custom GHC options.

We can easily build and package the library for distribution with:

% cabal configure
% cabal build
% cabal sdist

The testsuite looks a lot like the the library: first off, it has the same dependencies as the library (see the first Build-Depends line), and it then adds some extra test dependencies (see the second Build-Depends line). The testsuite here is a combination of HUnit and QuickCheck tests, and it uses Test-Framework as the runner. The test proper is Test/ReferenceProps.hs. It's a exitcode-stdio type test. This means that cabal will say that the tests pass if ReferenceProps exits with code 0. Otherwise, it will say the tests failed.

The testsuite looks like this (but, here, we're going to use some simple tests for list reversals):

import Data.Monoid
import Test.Framework
import Test.Framework.Providers.HUnit
import Test.Framework.Providers.QuickCheck2
import Test.HUnit
import Test.QuickCheck

main :: IO ()
main = defaultMainWithOpts
       [ testCase "rev" testRev
       , testProperty "listRevRevId" propListRevRevId
       ] mempty

testRev :: Assertion
testRev = reverse [1, 2, 3] @?= [3, 2, 1]

propListRevRevId :: [Int] -> Property
propListRevRevId xs = not (null xs) ==> reverse (reverse xs) == xs

The main is just a harness. You can also set various options for test-framework by replacing the mempty. The function testRev is a HUnit test, and propListRevRevId is a QuickCheck test; see the relevant docs on how to write these.

Finally, we can run the tests:

% cabal configure --enable-tests
% cabal test
Milliner answered 25/11, 2012 at 9:54 Comment(2)
Thanks a lot this answers most my questions and I like how you linked to a github. Just one more thing, how would you run tests in subdirectories of Test/ ? I don't want to put all of my tests in one file and it would be great if when I create a test file the tests are run in the testrunner automatically.Platysma
There are two ways I can think of: 1) add more Test-Suite sections to the cabal file (this is going to get annoying quickly, but it's probably the simplest solution if you only have a few test suites), or 2) split your tests into multiple files, export the lists of tests in each file, then use a single runner to concat the lists of tests and run them.Milliner

© 2022 - 2024 — McMap. All rights reserved.