Live data from Hacker News

Hypothesis 1.0: A property-based testing library for Python

lists.idyll.org

11–20 of 31 posts

Re: Hypothesis 1.0: A property-based testing library for Python

#11
post #8

This library is awesome, I am a massive fan of randomized and quick-check style testing. This type of testing has for me found so many bugs its surreal. I love the fact that hypothesis is a proper quickcheck and not just the fuzzing part, and that the author has a crazy stack-based-vm-language-testing thing project that might be useful. I have been using this library with py.test for 6 months now and its been a godse…

Thanks so much for the kind words! The best part of this message is that if you were someone I knew and thus could trust to only say nice things about me you would offer to buy me gin instead of beer. ;-)

Gin is fine, prehaps meet at a pycon one day :P

Re: Hypothesis 1.0: A property-based testing library for Python

#13
post #6

Is this similar to Mutation Testing, where the code being tested is mutated, in order to identify unspecified behavior? Mutant is a Ruby library for this: https://github.com/mbj/mutant

No, it's much closer to classic fuzz testing, where the test is held constant and the data fed to it is varied. I've been meaning to see if I can figure out a way to use mutation testing to feed into Hypothesis, but it's on the long list of things I'll try "at some point"

could you use something like recently open sourced z3 to assist in finding minimal examples? (http://en.wikipedia.org/wiki/Concolic_testing)

Re: Hypothesis 1.0: A property-based testing library for Python

#14
post #13

Earlier quoted context omitted.

No, it's much closer to classic fuzz testing, where the test is held constant and the data fed to it is varied. I've been meaning to see if I can figure out a way to use mutation testing to feed into Hypothesis, but it's on the long list of things I'll try "at some point"

could you use something like recently open sourced z3 to assist in finding minimal examples? ( http://en.wikipedia.org/wiki/Concolic_testing )

This is also on the "at some point" list. :-)

Concolic testing is quite hard in Python because of its extremely flexible semantics. I will probably look into doing something with z3 at some point when I need an interesting problem to entertain me, but I don't hold out a massive amount of hope for it being useful.

Re: Hypothesis 1.0: A property-based testing library for Python

#15
Interesting stuff. Can't wait to play with.

One immediate reaction from reading about things like "@given(float)... def testFun(x): ... assume(not isnan(x))..." -- I immediately would prefer to decorate/document my functions (that could be test functions, but more generally my "real" functions) with these pre-/post- conditions and assumtions, and generate the tests, something like:

    @assert(idempotent) #(inv(inv(x))==x)
    def inv(x):
      "Return -x"
      @given(float):
        @assume(not isnan(x))
      return -x
Typing this out, I can see why they go in the tests (avoids going down the rabbithole of creating a partially typed version of python...) -- which is why @assume(float) isn't on top; it'd imply inv didn't work for integers. Maybe a weaker:

    @assume(not isnan(x)) #implies x is number, but not nan
    @idempotent # inv(inv(x)) == x
    def inv(x):
      return -x
Now it should be possible to infer that inv(x) should be checked with numbers, and that it is idempotent. Might be a weak test unless one can specfify a few other things - but maybe such pre/post "hints" could be combined to make better short tests? Eg that one could give a different expression to check against rather than the function itself: def test_inv(): assert(inv(x), lambda y: -y)?

At any rate, interesting framework.

Re: Hypothesis 1.0: A property-based testing library for Python

#16
post #15

Interesting stuff. Can't wait to play with. One immediate reaction from reading about things like "@given(float)... def testFun(x): ... assume(not isnan(x))..." -- I immediately would prefer to decorate/document my functions (that could be test functions, but more generally my "real" functions) with these pre-/post- conditions and assumtions, and generate the tests, something like: @assert(idempotent) #(inv(inv(x))==…

I don't think that's what idempotent means. The only way f can be idempotent when f(f(x)) == x is if f(x) == x. (This implies that f is the identity function).

The property that makes the function idempotent is that successive application produces the same result as initial application: f(f(x)) == f(x).

Re: Hypothesis 1.0: A property-based testing library for Python

#17
This is awesome! And it comes right as I've been getting my feet wet with Haskell (and property-based testing).

It was really easy to get up and running with a simple test: https://github.com/ecordell/pymacaroons/blob/property-tests/...

and I'm excited to use it in more places. Though I'll have to configure tox/travis not to run hypothesis tests on Python 2.6.

Re: Hypothesis 1.0: A property-based testing library for Python

#19
post #15

Interesting stuff. Can't wait to play with. One immediate reaction from reading about things like "@given(float)... def testFun(x): ... assume(not isnan(x))..." -- I immediately would prefer to decorate/document my functions (that could be test functions, but more generally my "real" functions) with these pre-/post- conditions and assumtions, and generate the tests, something like: @assert(idempotent) #(inv(inv(x))==…

I don't think that's what idempotent means. The only way f can be idempotent when f(f(x)) == x is if f(x) == x. (This implies that f is the identity function). The property that makes the function idempotent is that successive application produces the same result as initial application: f(f(x)) == f(x).

You're right, of course. I typed that on my cellphone, and were a little quick looking up alternatives for @double_application_turns_this_into_an_identity_function -- that made more sense, and was a little more succinct.

Maybe @left_inverse would be more appropriate? It's been a while since I had to classify functions.

Re: Hypothesis 1.0: A property-based testing library for Python

#20
post #19

Earlier quoted context omitted.

I don't think that's what idempotent means. The only way f can be idempotent when f(f(x)) == x is if f(x) == x. (This implies that f is the identity function). The property that makes the function idempotent is that successive application produces the same result as initial application: f(f(x)) == f(x).

You're right, of course. I typed that on my cellphone, and were a little quick looking up alternatives for @double_application_turns_this_into_an_identity_function -- that made more sense, and was a little more succinct. Maybe @left_inverse would be more appropriate? It's been a while since I had to classify functions.

If you're curious, the math-y term for an operation that is its own inverse is involution. I don't know that I've ever heard the word in a programming context, but there it is! :)
Post reply on HN