← Back to context

Comment by quickthrower2

3 years ago

Fibo would be excellent for property testing.

I.e. For all x, Fib(x+2)=Fib(x)+Fib(x+1)

A property based test produces values of x and tests this property (or invariant).

If it fails the framework usually then tries to find the simplest failing case.

I am always slightly uncomfortable about property based tests that just replicate the logic of the thing they're testing. I don't really know of an alternative in this case, and obviously there's rarely any harm in having regression tests. But it feels like a code smell somehow.

  • I suppose they're ideal for cases where there's a simple, obviously correct but slow implementation that you can use to test a more complex but faster implementation.

  • Your uneasiness is understandable, but often it is far simpler to check the correctness of a result than to find it in the first place. Then property-based testing really shines.

    Even outside that special case it is always possible to generate just some input data and check for the absence of runtime errors or exceptions. That approach can be improved by adding assertions, pre- and postconditions to your code under test.

    Additionally, there a different ways to come up with good properties, see: https://www.youtube.com/watch?v=zvRAyq5wj38

  • You say it replicates the logic, but I think it's more useful to say that it describes the meaning: fib is the function on natural numbers that has that property plus the base cases fib(0) = 1 and fib(1) = 1. That's what fib means.

    That said, there are plenty of cases where your reaction is justified, where the test is implemented the same way as the function it's testing when the correctness property could be specified more simply.

  • You can chuck some regular tests in at the same time. They attack different classes of bugs.

    Property based tests fuzz test values you may not have dreamed of. Given a string as the required type it could generate some exotic stuff.