Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Idiomatic way to run test-suites in stack

Haskell beginner here.

I am struggling to find a nice way to run my test-suites that I have defined in my .cabal file. Given that you add test-suite sections in the .cabal file I would expect that you can run all of them with a single command like stack runtests.

The best answer I found is this one: Haskell Stack Ghci test-suite suggesting that you have to run

stack ghci --test module:test:libtests

However, there are two things that irritate me and I think that there must be a better way doing it.

  1. It is cumbersome to call stack ghci --test module:test:libtests explicitly. I don't want to do that if I have more test-suites when projects get larger.
  2. Even worse, I end up in an interactive session and have to call main myself. This does not scale.

Isn't there a better way to run your test-suites for a project in stack? Of course I could do some shell scripting, but hey stack should know how to run my tests, I specified everything in the .cabal file.

I tried stack runghc --test but that doesn't help.

Project setup:

.
├── app
│   └── Main.hs
├── LICENSE
├── README.md
├── Setup.hs
├── src
│   ├── Lib.hs
│   └── WordNumber.hs
├── stack.yaml
├── test
│   └── Spec.hs
└── WordNumber.cabal

WordNumber.cabal

name:                WordNumber
version:             0.1.0.0
-- synopsis:
-- description:
homepage:            https://github.com/githubuser/WordNumber#readme
license:             BSD3
license-file:        LICENSE
author:              Author name here
maintainer:          [email protected]
copyright:           2017 Author name here
category:            Web
build-type:          Simple
extra-source-files:  README.md
cabal-version:       >=1.10

library
  hs-source-dirs:      src
  exposed-modules:     Lib, WordNumber
  build-depends:       base >= 4.7 && < 5
  default-language:    Haskell2010

executable wordnumber
  hs-source-dirs:      app
  main-is:             Main.hs
  ghc-options:         -threaded -rtsopts -with-rtsopts=-N
  build-depends:       base
                     , WordNumber
  default-language:    Haskell2010

test-suite wordnumber-test
  type:                exitcode-stdio-1.0
  hs-source-dirs:      test
  main-is:             Spec.hs
  build-depends:       base
                     , WordNumber
                     , hspec
  ghc-options:         -threaded -rtsopts -with-rtsopts=-N
  default-language:    Haskell2010

Update: Actually I feel a bit stupid for not finding the answer myself. It is either stack test or stack build --test and also documented.

However, for people following the HaskellBook it might not be that obvious. For some reason all tests in the testing chapter are executed in the cumbersome way and running tests with stack test is never mentioned.

like image 449
StarSheriff Avatar asked Nov 10 '17 12:11

StarSheriff


1 Answers

stack test runs tests located in a .cabal file. It runs stack build if required, so you don't need to build manually before testing.

See further: https://docs.haskellstack.org/en/stable/GUIDE/#stack-test

like image 163
Zpalmtree Avatar answered Nov 18 '22 10:11

Zpalmtree