Quickcheck style testing in python with hypothesis

Edit: Hypothesis has moved on a lot since this post. At this point if you want a QuickCheck for Python it’s really the only game in town. This post remains for historic purposes, but you should check out its new site at hypothesis.works.

So I’ve been tinkering a bit more with hypothesis. I would now cautiously label it “probably not too broken and I’m unlikely to completely change the API”. The version number is currently 0.0.4 though, so you should certainly regard it with some degree of suspicion. However I’m now reasonably confident that it’s good enough for simple usage in the sense that I expect bug reports from most people who use it in anger but probably not all people and probably not many bug reports from most. :-)

Since the initial version I’ve mostly been thinking about the API and fixing anything that was really obviously stupid. I’ve also cleaned up a bunch of internals. The implementation is still pretty far from perfect, but it’s no longer awful.

Most importantly, I’ve added a feature to it that makes it actually useful. Test integration! You can now use it for some fairly pretty quickcheck style testing:

def test_int_addition_is_commutative(x,y):
    assert x + y == y + x
def test_str_addition_is_commutative(x,y):
    assert x + y == y + x

The first test will pass, because int addition really is commutative, but of course string addition is not so what you’ll get is an error from the test suite:

    x = '0', y = '1'
    def test_str_addition_is_commutative(x,y):
>       assert x + y == y + x
E       assert '01' == '10'
E         - 01
E         + 10

This doesn’t currently support passing keyword arguments through to the tests – all arguments have to be positional. I think I know how to fix this, I just haven’t sorted out the details yet.

But yeah, quickcheck style testing with test case minimization (look at how nice the counter-example it produced was!). If that turns out to be exactly what you’ve always wanted, go wild.

This entry was posted in Hypothesis, programming on by .

2 thoughts on “Quickcheck style testing in python with hypothesis

  1. John Regehr

    This is great. I want to do another “how to fuzz an ADT” blog post, this time about Python. When I do that, I’ll try out your tool! I’ll have to make sure to choose a broken ADT this time so I can watch your minimizer work.

    1. david Post author

      Thanks! Glad you like it.

      Unfortunately right now it lacks probably the coolest feature from scalacheck that would really help for your fuzzing (the stateful testing part), but it’s probably still quite a useful thing for it. I’m thinking about how best to port the stateful testing over. I think I know what to do, but it’ll probably not appear in hypothesis all that soon.

Comments are closed.