Generating tests with Hypothesis
The Hypothesis library is a third-party library for generating tests, or performing property-based testing. You create a strategy (an object that generates samples of data) and then run your code against the generated output of the strategy. You want to test an invariant, or something about your data that you presume to always hold true.
Again, there could be a book written solely about this type of testing, but in this section we will show an example of using the library.
We will show how to generate Kaggle survey data, then using that generated survey data, we will run it against the tweak_kag
function and validate that the function will work on new data.
We will leverage the testing code found in the previous section. The Hypothesis library works with pytest, so we can use the same layout.
How to do it…
- Create a project data layout. If you had the code from the previous section, add a
test_hypot.py
file...