Simple Quickcheck implementation for Python

It’s hard to compete with Haskell’s QuickCheck when it comes to having random test cases generated for you from properties. QuickCheck in Haskell is aided by the fact that Haskell has a very rich type system. I wanted something similar when writing tests in Python, but Python is dynamically typed. Fortunately it’s not too much trouble, using decorators, to annotate methods in Python. Here’s an example using the simple library qc.py that I wrote:

The “tries” argument defines the number of test cases that will be generated (when not specified it defaults to 100). a_unicode, a_list and a_character are all functions that return thunks (which, now that I think about it, is just a fancy word for a closure with no arguments) that are forced to evaluate each time a test is performed.

qc.py should be test framework neutral (since it uses asserts and just wraps the test method in a loop). I’ve only tested it with nose. I found peckcheck when searching for a Python QuickCheck implementation but thought it could be better done in a test framework agnostic way with decorators. I later found Paycheck (another QuickCheck implementation in Python that uses wrappers) after I had written qc. After a brief look it seems like the way that qc uses thunks might make it easier to support injecting arbitrary data types.