NorfairKing / sydtest

A modern testing framework for Haskell with good defaults and advanced testing features.
115 stars 25 forks source link

Clarify conventions needed to use sydtest-discover #13

Closed ornamentist closed 3 years ago

ornamentist commented 3 years ago

Could I summarize and clarify the file names and conventions needed to use sydtest-discover?

With this stack setup:

  foobar-test:
    main: Spec.hs
    source-dirs: test/
    build-tools: sydtest-discover
    ghc-options:
    - -threaded
    - -rtsopts
    - -with-rtsopts=-N
    dependencies:
    - sydtest

I need to add this line to test/Spec.hs:

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

Then when I run stack test (or in my case cabal test) sydtest-discover will find source files in the test directory tree matching *Spec.hs and include them in the test run?

if I need a custom test main, then I need to add to each test file in test/**.hs the line:

{-# OPTIONS_GHC -F -pgmF sydtest-discover -optF --no-main #-}

And also change main: Spec.hs in the stack/cabal settings to say Main.hs. In that file I can do

module Main where

import Spec
import Test.Syd

main :: IO ()
main = sydTest spec

Where Spec.hs is a file in the test directory. If I need multiple test files in the test directory then I need to combine their top-level exported spec values in the Main module in some way?

And many also thanks for creating a such a comprehensive testing framework!

NorfairKing commented 3 years ago

Hi there!

To "get started", you just need the one file called Spec.hs with this line and nothing else:

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

That'll give you a main = sydTest spec in that file, where spec is also defined in that file as the collection of all test suites in the test dir.

IF you want to use sydtest with your own options (sydTestWith), you can instead put this in Spec.hs but still nothing else:

{-# OPTIONS_GHC -F -pgmF sydtest-discover -optF --no-main #-}

This will then only generate a spec, but no main so that you can write your own main in a separate module.

The point of sydtest-discover is that you don't have to combine top-level specs anywhere, you can just use the one that's generated in the Spec module.

You can find examples here: https://github.com/NorfairKing/smos/blob/release/smos-data-gen/test/Spec.hs

Does that answer your question?

ornamentist commented 3 years ago

Yep -- now I see my misunderstandings.

ornamentist commented 3 years ago

That example was very useful thanks.

Once I have a conforming Spec.hs and project.cabal file I should be able to do a cabal test {test suite name} and sydtest-discover will do the discovery/generate stage and cabal will run those generated tests?

NorfairKing commented 3 years ago

@ornamentist I have no idea how cabal works but stack just does it all correctly for you yes.

ornamentist commented 3 years ago

I might have to move to Stack, as Cabal is not finding the tests.

ornamentist commented 3 years ago

I'd like to clone and build that example test suite ( https://github.com/NorfairKing/smos/blob/release/smos-data-gen) to see a working package in operation. Do I need to use nix or some other tool before doing a stack test on it?

NorfairKing commented 3 years ago

Do I need to use nix or some other tool before doing a stack test on it?

You can follow this guide: https://docs.smos.online/installation/stack