25

I created a very simple project with stack. It contains: an executable, a library and test targets in the associated cabal file. When I load the code to ghci via stack ghci, I can't access test there, even if they are in separate module. Is there any way to use it in such a way?

Konstantin Solomatov
  • 10,252
  • 8
  • 58
  • 88
  • 1
    According to [this answer](https://stackoverflow.com/a/45295351/126014) you can use `stack ghci --test`. I've found that it works without problems. – Mark Seemann Dec 08 '21 at 15:12

1 Answers1

31

Try stack ghci (your project name):(the test suite name). Then you should be able to enter main and your tests will run.

Example:

If your .cabal project file had the following values:

name: ExampleProject
...
test-suite Example-test

Then the command to run would be stack ghci ExampleProject:Example-test

(edit suggested by @Chris Stryczynski)

To watch the test and src directories so they are updated when you reload with :r, run:

stack ghci --ghci-options -isrc --ghci-options -itest ExampleProduct:Example-test
Libby
  • 1,357
  • 12
  • 17
  • If you do this, `:r` won't reload changes to your lib. Do you know how to do it so that `:r` recompiles the lib and tests? – Sean Clark Hess Oct 11 '16 at 16:51
  • @SeanClarkHess There's a couple ways of setting up tests. You can set it up by adding the library you're testing to the test-suite build-depends, or you can just add the library source folder to the test-suite hs-source-dirs. I *believe* `:r` works with the latter and not the former, but I'm not certain. Sorry for taking so long to notice your question. – Libby Oct 26 '16 at 12:09
  • I think you need to do a `stack install` of the library under test prior to running the command above, don't you? – Damian Nadales Feb 23 '17 at 22:22