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 the random module with something much less random.
How can we unit test algorithms that involve randomness?