Giter Club home page Giter Club 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 dag avatar edwardbetts avatar finnsson avatar hvriedel avatar jship avatar kazu-yamamoto avatar kirelagin avatar sol avatar

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.