Automatically gather all quickChecks
Asked Answered
E

2

8

Being a fan of quickCheck, I have a lot of

prop_something_something = ...

throughout my program.

For convenience, to easily run all of them, I define

runchecks = do
    quickCheck prop_something_something
    quickCheck prop_something_different

but is there a nice way to generate runchecks?

TL;DR: I want to easily run all quickChecks in a file. I guess one way is to prefix the runnable tests with test_ or something similar, but that might be too hacky.

Elytron answered 26/4, 2012 at 18:11 Comment(0)
L
7

You can do this with the test-framework-th package. Just do:

import Test.Framework.TH
import Test.Framework.Providers.QuickCheck2
runchecks = $(defaultMainGenerator)

This will use the test-framework way of running tests, i.e. you'll get slightly more information than what you'd get by simply running the tests one after the other, which oftentimes is a good thing.

You obviously need TemplateHaskell to be enabled for this to work; either add Default-extensions: TemplateHaskell to your Cabal file, or add {-# LANGUAGE TemplateHaskell #-} to the top of the file.

Litigation answered 26/4, 2012 at 18:27 Comment(3)
By just adding that, I get this error: hpaste.org/67672 - do I have to make other changes? I'll paste more code if you want to.Elytron
Apparently, you have to include the QuickCheck2 provider module as well, which makes sense I guess. See my updated answer for more info.Litigation
Yes, adding import Test.Framework.Providers.QuickCheck2 worked. Thanks. :)Elytron
L
8

An extra note: this functionality exists out of the box in QuickCheck 2 as well, see the function quickCheckAll, which requires an import of Test.QuickCheck.All as well as TemplateHaskell. quickCheckAll will test all functions in your module whose name starts with prop_.

Laban answered 27/4, 2012 at 5:20 Comment(0)
L
7

You can do this with the test-framework-th package. Just do:

import Test.Framework.TH
import Test.Framework.Providers.QuickCheck2
runchecks = $(defaultMainGenerator)

This will use the test-framework way of running tests, i.e. you'll get slightly more information than what you'd get by simply running the tests one after the other, which oftentimes is a good thing.

You obviously need TemplateHaskell to be enabled for this to work; either add Default-extensions: TemplateHaskell to your Cabal file, or add {-# LANGUAGE TemplateHaskell #-} to the top of the file.

Litigation answered 26/4, 2012 at 18:27 Comment(3)
By just adding that, I get this error: hpaste.org/67672 - do I have to make other changes? I'll paste more code if you want to.Elytron
Apparently, you have to include the QuickCheck2 provider module as well, which makes sense I guess. See my updated answer for more info.Litigation
Yes, adding import Test.Framework.Providers.QuickCheck2 worked. Thanks. :)Elytron

© 2022 - 2024 — McMap. All rights reserved.