Find file
Fetching contributors…
Cannot retrieve contributors at this time
79 lines (49 sloc) 4.11 KB

QuickCheck.jl --- Specification-Based Randomized Testing

This package is a Julia implementation of QuickCheck by Koen Claessen and John Hughes. QuickCheck is a technique for testing software by specifying properties of a function, then randomly searching for inputs for which the property fails to hold.


Let's look at a property of zip. Zipping two iterables should produce a new iterable whose length is the same as the length of the shorter of the two iterables:

julia> using QuickCheck

julia> property((x::Vector{Int}, y::Vector{Int}) -> length(zip(x,y)) == min(length(x), length(y)))
OK, passed 100 tests.

QuickCheck supplied 100 random pairs of arguments to this function, all of which met the specification. What happens when the function does not meet the specification?:

julia> property((x::Vector{Int}, y::Vector{Int}) -> length(zip(x,y)) == max(length(x), length(y)))
ERROR: Falsifiable, after 1 tests:
{[2, 1], [2]}

QuickCheck not only points out that the specification is not met, but provides the test inputs that failed.



QuickCheck specifies properties for most of Julia's built in numeric types and strings, as well as arrays and composite types that are composed of those types. However, you might need to extend the specifications to new types for which a generator does not exist and cannot be automatically constructed, or for which the default generator might provide poor coverage. To do this, import QuickCheck.generator and extend the generator method, which takes a type and a "size" argument (a positive integer) and should return a new instance of that type scaled appropriately by the size. As a contrived example, consider the type:

type A

We can create a custom generator as follows:

import QuickCheck.generator
generator(::Type{A}, size) = A(rand(10000:(10000*size)))

Then we can test a property using the new type:

julia> property((x)->x.b > sqrt(x.b), [A])
OK, passed 100 tests.