How to run multiple test files using the Haskell Stack project

I want to set up an existing Haskell project with Stack . An existing project uses several files in a directory test; these individual test files by default, Stack (or cabal?) apparently uses one test/Spec.hsfor testing. How can I continue to use multiple files with this project?

NOTE. I am studying Haskell, and this project is approaching my study from the kata approach. Therefore, tests are isolated to focus on one aspect of the language at a time.

+6
source share
1 answer

Here is a setting for a directory structure like this

> tree                                                                                       
.
β”œβ”€β”€ example.cabal
β”œβ”€β”€ app
β”‚   └── Main.hs
β”œβ”€β”€ ChangeLog.md
β”œβ”€β”€ LICENSE
β”œβ”€β”€ Setup.hs
β”œβ”€β”€ src
β”‚   β”œβ”€β”€ A
β”‚   β”‚   └── C.hs
β”‚   β”œβ”€β”€ A.hs
β”‚   └── B.hs
β”œβ”€β”€ stack.yaml
└── tst
    β”œβ”€β”€ integration
    β”‚   └── Spec.hs
    └── unit
        β”œβ”€β”€ A
        β”‚   └── CSpec.hs
        β”œβ”€β”€ ASpec.hs
        β”œβ”€β”€ BSpec.hs
        └── Spec.hs

you want to have integration tests, which are separated from regular unit tests and several sub-modules, each module corresponding to src-pamyatke

first of all you need to add test packages to

example.cabal file

name:                example
...
-- copyright:
-- category:
build-type:          Simple
extra-source-files:  ChangeLog.md
cabal-version:       >=1.10

executable testmain
  main-is:       Main.hs
  hs-source-dirs: app
  build-depends: base
               , example

library
  exposed-modules:     A.C,A,B
  -- other-modules:
  -- other-extensions:
  build-depends:       base >=4.9 && <4.10
  hs-source-dirs:      src
  default-language:    Haskell2010

test-suite unit-tests
  type:          exitcode-stdio-1.0
  main-is:       Spec.hs
  hs-source-dirs: tst/unit
  build-depends: base
               , example
               , hspec
               , hspec-discover
               , ...

test-suite integration-tests
  type:          exitcode-stdio-1.0
  main-is:       Spec.hs
  hs-source-dirs: tst/integration
  build-depends: base
               , example
               , hspec
               , ...

put the following in tst/unit/Spec.hsfrom hspec-discoverand discovers (hence the name) all modules of the form ...Spec.hsand performs a function specfrom each of these modules.

tst/unit/Spec.hs

{-# OPTIONS_GHC -F -pgmF hspec-discover #-}

only this single line

Other test files

then add your unit tests to ASpec.hs, and others to BSpec.hs, CSpec.hsand yours Spec.hsto the foldertst/integration

module ASpec where

import Test.Hspec
import A

spec :: Spec
spec = do
  describe "Prelude.head" $ do
    it "returns the first element of a list" $ do
      head [23 ..] `shouldBe` (23 :: Int)

    it "returns the first element of an *arbitrary* list" $
      property $ \x xs -> head (x:xs) == (x :: Int)

    it "throws an exception if used with an empty list" $ do
      evaluate (head []) `shouldThrow` anyException

you can compile and run your tests with

$> stack test
# now all your tests are executed
$> stack test :unit-tests
# now only the unit tests run
$> stack test :integration-tests
# now only the integration tests run

Sources

https://hspec.imtqy.com, hspec-, , . - https://haskellstack.org - / - .

haskell . HUnit, QuickCheck, Smallcheck, doctests ( , - , ).

+9

Source: https://habr.com/ru/post/1016365/


All Articles