4 ms·
I'm a big fan of property-based testing. When I'm writing property-based tests, I usually follow a process like this: - Think of a simple unit test example, e.
by chriswarbo 4y ago
I'm a big fan of property-based testing. When I'm writing property-based tests, I usually follow a process like this:
- Think of a simple unit test example, e.g. `assert 1 in my_sort([1, 2, 3])`
- Turn constants into arguments, e.g. `assert x in my_sort([x, y, z])`
- Generalise any irrelevant details, e.g. `assert x in my_sort(prefix + [x] + suffix)`
Property-based testing also complements static typing: if we find ourselves restricting a test generator, like avoiding zero in the article, we can instead try to strengthen our function's argument types (e.g. giving one argument a `PositiveInteger` type). That way, not only have we avoided a crash in our tests; we've also eliminated it from all real usage sites too :)