Many applications rely on the random
module to create random values or put values into random order. In many statistical tests, repeated random shuffling or random subset calculations are done. When we want to test one of the algorithms, the results are essentially impossible to predict.
We have two choices for trying to make the random
module predictable enough to write meaningful unit tests:
- Set a known seed value, this is common, and we've made heavy use of this in many other recipes.
- Use
unittest.mock
to replace therandom
module with something much less random.
How can we unit test algorithms that involve randomness?
Given a sample dataset, we can compute a statistical measure such as a mean or median. A common next step is to determine the likely values of these statistical measures for some overall population. This can done by a technique called bootstrapping.
The idea is to resample the initial set of data repeatedly. Each of the resamples...