How to import modules for testing?

Viewed 88

I am trying to test some functions using QuickCheck for my Haskell project. My directory currently looks like this:

|-cswk2
  |-validCard.hs //THIS FILE CONTAINS MY HASKELL FUNCTIONS 
  |-tests
    |-testSuite.hs //THIS FILE CONTAINS MY QUICKCHECK FUNCTIONS 

Inside my Haskell file i have some functions that i want to test, this looks something like this...

validCard.hs:

module Cswk2 where

getCard :: Integer -> [Integer]
...

...while my test file looks like this.

testSuite.hs:

import Test.QuickCheck
import Test.HUnit
import Cswk2

prop_numb :: [Integer] -> Bool
...

Unfortunately, i keep getting an error saying 'Failed to load interface for Cswk2' and was wondering if it was a problem with the way i have structured my directory for testing. I initially tried using cabal and following the user manual to setup a test suite but that lead to many more problems than this and left me more confused. I am new to Haskell so if anyone could highlight what exactly i am doing wrong and advise me on ways to structure a project for testing that would be greatly appreciated?

1 Answers

As far as I can tell, most people who write Haskell code with any degree of complexity use a build system. The community seems to be roughly evenly split between Stack and Cabal.

In both cases, the most common way to organise code is to keep separate directories for production and test code:

|-cswk2
  |-src
    |-validCard.hs //THIS FILE CONTAINS MY HASKELL FUNCTIONS 
  |-tests
    |-testSuite.hs //THIS FILE CONTAINS MY QUICKCHECK FUNCTIONS 

With the build systems, you're also going to need a file that defines dependencies. That'll typically be a .cabal or package.yaml file.

I have various examples of code bases that use Stack on GitHub. Here's one of intermediate complexity: https://github.com/ploeh/reservation-api-slice-haskell

Related