tom
tom

Reputation: 1152

Access test resources within Haskell tests

This is probably a basic question but I've been Googling for a while on it... I have a Cabal-ized Haskell project and I'm in the process of writing integration tests for it. I want to be able to include test resources for my project in the same repo and access them in tests. For example, here are a couple things I want to accomplish:

1) Check a dummy database instance into my repo, including a shell script that spins up a database process. I want to write an Hspec integration test that spins up the database process, makes some calls to it, and then shuts it down. So I need to be able to find the shell script so I can use System.Process.createProcess on it.

2) Check in paired "input" and "output" files. My test should process each of the input files and compare them to a corresponding output file to make sure they match. (I've read about "golden" but it doesn't seem to solve the problem of finding/reading the input files in the first place?)

In short, how can I go about creating a "resources" folder in the root folder of my Haskell project and find the path to it inside tests?

Upvotes: 4

Views: 883

Answers (2)

Abhijit Sarkar
Abhijit Sarkar

Reputation: 24593

I don't believe the the question asked has been answered satisfactorily. Specifically, we want to answer the following questions:

  1. Is there any standard practice/convention for putting test data files? For JVM projects following Maven directory structure, the location is src/test/resources, but AFAICT, there's no such convention in Haskell. So, putting the files inside the test directory, presumably under the same module path, makes sense to me. For example, If the code under test is in the module A.B, then putting the test data in the directory test/A/B is fine. If using HSpec, the test module is A.BSpec.
  2. How to read the test data files? If the tests are run from the top folder, there's no need to list these files in the cabal file, because these are't going to be shipped with the package. Simple readFile test/A/B/file.txt from the test works. Otherwise, these files can be listed in the data-files field of cabal. See Accessing data files from package code. This, however, means that the data files will be shipped with the code.

Upvotes: -1

JP Moresmau
JP Moresmau

Reputation: 7403

Have a look at an existing project that uses input and output file.

For example, take haddock, the source code is at https://github.com/haskell/haddock. They have the test files under a folder (https://github.com/haskell/haddock/tree/master/html-test/ref) and they are referenced as extra-source-files in the cabal file (https://github.com/haskell/haddock/blob/master/haddock.cabal). Then the test code (https://github.com/haskell/haddock/blob/master/html-test/run.lhs) uses some CPP macro (__FILE__) to get the current directory, and can then resolve the files relative to that folder.

Upvotes: 3

Related Questions