How does property-based testing differ from example-based?
While they're from different concepts, property-based tests share many characteristics with example-based tests. This is illustrated in the following comparison of steps you'd take to write a given test:
|Example based||Property based|
|1. Set up some example data||1. Define data type matching a specification|
|2. Perform some operations on the data||2. Perform some operations on the data|
|3. Assert something about the result||3. Assert properties about the result|
Episode #178 Build a PyPI package from a Jupyter notebook - [Python Bytes Podcast]
- Carolyn Stransky, @carolynstran
- Excellent intro to property based testing and hypothesis
- Starts with a unit test that uses example based testing.
- Before showing similar test using hypothesis, she talks about the different mindset of testing for properties instead of exact examples.
- Like not the exact sorted list you should
- but instead,
- the length should be the same
- the contents should contain the same things, for instance, using set for that assertion
- you could element-wise walk the list and make sure i <= i+1
- She walks through the hypothesis decorators to come up with input and shows how to use some.lists and some.integers and max_examples
- Goes on to discuss coming up with properties to test for, which really is the hard part of property based testing.
- Checking for expected exceptions
- Using a naive method technique, useful in property based testing, to compare two versions of a method. This is super useful for refactoring and testing new vs old versions on tons of input data.
- json5 lib