Git Product home page Git Product logo

tasty-th's Introduction

tasty-th

Automatically generate tasty TestTrees from functions of the current module, using TemplateHaskell. This is a fork the original test-framework-th package, modified to work with tasty instead of test-framework. Usage is exactly the same as for the original package.

Usage

To use this package, you need to enable TH and import the package plus the required tasty modules:

{-# LANGUAGE TemplateHaskell #-}
module Main where

import Test.Tasty
import Test.Tasty.TH
import Test.Tasty.QuickCheck
import Test.Tasty.HUnit

Then, write a few unit tests and QuickCheck properties:

prop_length_append :: [Int] -> [Int] -> Bool
prop_length_append as bs = length (as ++ bs) == length as + length bs

case_length_1 :: Assertion
case_length_1 = 1 @=? length [()]

tasty-th assumes that all QuickCheck properties are named prop_something and HUnit tests are named case_something. You can also group tests under a test tree and have them be discovered by tasty-th so they are added to the main test tree. In that case, you can define a test_something definition:

test_plus :: [TestTree]
test_plus = 
  [ testCase "3 + 4" (7 @=? (3 + 4))
    -- ...
  ]

After you've defined all your tests in this way, you can now automatically generate a main function using the defaultMainGenerator TH macro provided by tasty-th:

main :: IO ()
main = $(defaultMainGenerator)

It is important that you place this at the end of the file, since GHC only sees the definitions prior to the TH call.

If you don't wish to generate a main function, but instead just want a TestTree, you can use the testGroupGenerator macro for that:

tests :: TestTree
tests = $(testGroupGenerator)

Running the example, we get the following output:

./example
Main
  length append: OK (0.01s)
    +++ OK, passed 100 tests.
  length 1:      OK
  plus
    3 + 4:       OK

All 3 tests passed (0.01s)

You can find this whole example in the file example.hs at the root of the source code tree.

Contributing

If you have any questions or issues with the package, feel free to open an issue on the repo. Pull requests adding new features are also welcome if the features make sense. You can also find me in the #haskell IRC channel with the nick bennofs to ask a question about this package.

tasty-th's People

Contributors

bennofs avatar finnsson avatar kirelagin avatar sol avatar dag avatar edwardbetts avatar hvriedel avatar jship avatar kazu-yamamoto avatar

Stargazers

Ilmari Vacklin avatar  avatar Lau avatar Leandro Lisboa Penz avatar Doug Wilson avatar Daniel Kahlenberg avatar Masahiro Sakai avatar

Watchers

 avatar James Cloos avatar  avatar  avatar

tasty-th's Issues

Does not work with Literate Haskell

testGroupGenerator does not find any tests in an lhs file.


{-# LANGUAGE TemplateHaskell #-}

import Test.Tasty
import Test.Tasty.HUnit
import Test.Tasty.TH (testGroupGenerator)


test_foo = [ testCase "t1" $ 2 @?= 2
           ]

main :: IO ()
main = defaultMain $(testGroupGenerator)
› runhaskell test.hs                                                                                                                                                                                <146>
Main
  foo
    t1: OK

All 1 tests passed (0.00s)

> {-# LANGUAGE TemplateHaskell #-}
>
> import Test.Tasty
> import Test.Tasty.HUnit
> import Test.Tasty.TH (testGroupGenerator)
>
>
> test_foo = [ testCase "t1" $ 2 @?= 2
>            ]
>
> main :: IO ()
> main = defaultMain $(testGroupGenerator)
› runhaskell test.lhs

All 0 tests passed (0.00s)

Doesn't discover multiple test suites

I have 2 test suites, both of them use testGroupGenerator. One of them executes tests, the other does not. Have a look at my cabal package:

https://github.com/sgillespie/elocrypt/blob/6ea4f0c1ab3550e069427c804527c0fb0fbd0f40/elocrypt.cabal

You can see the output of the test here:

https://travis-ci.org/sgillespie/elocrypt/builds/220181604#L210

You can see that it's not executing any tests for the second suite (ui-tests). However, if I downgrade to version 0.1.4, it executes both suites.

$(defaultMainGenerator) and imports

In the following snippet:

module Main where

import Test.Tasty
import Test.Tasty.TH
import Test.Tasty.Quickcheck

import AllMyPropTests

main :: IO ()
main = $(defaultMainGenerator)

If I have all my quickcheck properties defined (as prop_*) in test/AllMyPropTests, should the properties be picked up and run?

It isn't working for me so far, but I have a strange use case. I am trying to create a new Tasty plugin which does test discovery, and I think the API would be greatly improved by using tasty-th. The implementation is 'borrowed' from the awesome hspec and their discover implementation.

The Tasty plugins pre processed output will be the following code which will auto import all test modules under the users test directory. Then, the main generator would generate the boilerplate to run the tests.

I'd really like to get this working. Is there some TemplateHaskell limitation here? Any help really appreciated.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.