[Haskell-cafe] Controlling how test data is generated in, QuickCheck 2

Li-yao Xia li-yao.xia at ens.fr
Tue May 3 13:31:38 UTC 2016

Hi Michael,

> What I noticed was the answer suggested that one should write a `newtype`,
> wrapping a `[Int]` and making a new `Arbitrary` instance. However, I also
> noticed that the answer is from 2012, and `QuickCheck 2` has an `Args`
> `datatype` that appears to be able to do what I need. So, can I create a
> new `Args` to limit the range of tests, (just want the first 1000 Fibonacci
> numbers), and also to limit the number of tests run? If not is the solution
> from the above link the approach I would have to take?

The module Test.Hspec.QuickCheck has functions to modify the number of 
tests to be run (modifyMaxSuccess) and the maximum size passed to the 
generators (modifyMaxSize) (and indeed internally they modify the Args 
passed to QuickCheck's driver).

I believe their purpose is to modify values coming from outside the 
program, via the command line options --qc-max-success and 
--qc-max-size. You can certainly set them to constants:

     modifyMaxSuccess (const 1000) $ prop ...

But to specify the domains of your properties from within the program, 
the approaches mentioned in the answer you linked to are still current.

Defining a newtype wrapper with an Arbitrary instance is a somewhat 
heavyweight solution. Some already exist for common preconditions, for 
example you can use Positive instead of the first check in modfiz:

     modfiz :: Positive Int -> Bool
     modfiz (Positive int) = ...

The answer also mentions forAll and standalone generators, which I find 
to be most straightforward to set the domain of testfib:

     testfib :: Property
     testfib = forAll (choose (1, 1000)) $ \n ->


More information about the Haskell-Cafe mailing list