I'd like to be able to take a file with declarations such as:
test_1 = assert $ 1 == 1
test_2 = assert $ 2 == 1
and generate a basic run function like
main = runTests [test_1, test2]
The goal is to get something like Python's nosetest.
Can I do this with template Haskell? I cannot find a lot of documentation on it (there are many broken links in the Wiki).
You might want to look into the test-framework family of packages. In particular, the test-framework-th package provides the Template Haskell function defaultMainGenerator
which does exactly what you want for both QuickCheck and HUnit tests, as long as you follow the convention of prefixing HUnit test cases with case_
and QuickCheck properties with prop_
.
{-# LANGUAGE TemplateHaskell #-}
import Test.Framework.Providers.HUnit
import Test.Framework.Providers.QuickCheck2
import Test.Framework.TH
import Test.HUnit
import Test.QuickCheck
main = $(defaultMainGenerator)
case_checkThatHUnitWorks =
assert $ 1 == 1
prop_checkThatQuickCheckWorks =
(1 == 1)
There is another way, you don't have to use template haskell. haskell-src-exts can parse Haskell, and you could extract from that.
Or if your purpose is practical, you can make like quickcheck
and do a simple-minded parse, i.e. looking for identifiers that start with prop_
in column 0. This is a perfectly adequate solution for real work, though it may be theoretically unsatisfying.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With