Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Organizing Haskell Tests

So I'm trying to follow the suggested structure of a Haskell project, and I'm having a couple problems organizing my tests.

For simplicity, let's start with:

src/Clue/Cards.hs # defines Clue.Cards module testsuite/tests/Clue/Cards.hs # tests Clue.Cards module 

For one, I'm not sure what to name the module in testsuite/tests/Clue/Cards.hs that contains the test code, and for another, I'm no sure how to compile my test code so that I can link to my source:

% ghc -c testsuite/tests/Clue/Cards.hs -L src testsuite/tests/Clue/Cards.hs:5:0:     Failed to load interface for `Clue.Cards':       Use -v to see a list of the files searched for. 
like image 248
rampion Avatar asked Jan 14 '11 02:01

rampion


2 Answers

I use myself the approach taken by Snap Framework for their test-suites, which basically boils down to:

  1. Use a test-framework such as haskell-test-framework or HTF
  2. Name the modules containing tests by appending .Tests to the module-name containing the IUT, e.g.:

    module Clue.Cards where ... -- module containing IUT  module Clue.Cards.Tests where ... -- module containing tests for IUT 
  3. By using separate namespaces, you can put your tests in a separate source-folder tests/, you can then use a separate Cabal build-target (see also cabal test-build-target support in recent Cabal versions) for the test-suite which includes the additional source folder in its hs-source-dirs setting, e.g.:

    Executable clue   hs-source-dirs: src   ...  Executable clue-testsuite   hs-source-dirs: src tests   ... 

    This works, since there's no namespace collision between the modules in your IUT and the test-suite anymore.

like image 164
hvr Avatar answered Sep 19 '22 23:09

hvr


Personally I feel that an extra ./src/ directory doesn't make much sense for small Haskell projects. Of coarse there's source, I downloaded the source code.

Either way (with or without src), I'd suggest you refactor and have a Clue directory and a Test directory:

./Clue/Cards.hs   -- module Clude.Cards where ... ./Test/Cards.hs   -- module Test.Cards where ... 

This allows GHCi + Test.Cards to see Clue.Cards without any extra args or using cabal. On that note, if you don't use cabal + flags for optionally building your test modules then you should look into it.

Another option, which I use in many of my projects, is to have:

./Some/Module/Hierarchy/File.hs ./tests/someTests.hs 

And I cabal install the package then run the tests/someTests.hs stuff. I guess this would be annoying if my packages were particularly large and too a long time to install.

like image 36
Thomas M. DuBuisson Avatar answered Sep 20 '22 23:09

Thomas M. DuBuisson