pytest-rng provides fixtures for
ensuring “randomness” in your tests is reproducible
from one run to the next.
It also allows the seed for all tests to be changed if requested,
to help ensure that test successes are not dependent on
particular random number seeds.
rngfixture to get a pre-seeded random number generator (RNG) that exposes NumPy’s
seedfixture to get an integer seed that can be used to initialize your own RNG.
The following example prints the same four random numbers every time the test is run.
import numpy as np def test_rectification(rng, seed): print(rng.uniform(-1, 1, size=3)) print(seed)
To use these fixtures, install with
pip install pytest-rng
Once installed, you can use these fixtures like any other fixture:
seed to the arguments of a test function or class.
seed fixture, we generate a seed by doing the following:
Concatenate the test’s
saltvalue, if provided.
Hash that string to yield an integer seed.
rng fixture, we also add the string
"rng" to the
value before generating the seed as above.
The seed is used to instantiate a
which is returned.
"rng" to the salt to ensure that random numbers
are different when using the
and when manually instantiating a
salt is a string that is added to the test’s
in order to change the seed for all tests.
It is advantageous to change seeds regularly to ensure that
your test suite is robust to different seeds.
The salt value can be specified in a configuration file
[tool:pytest] rng_salt = v0.3.0
The salt value can also be specified through the command line.
pytest --rng-salt "v0.4.0"
The salt value passed through the command line takes precedence over the value set in the configuration file so that you can change seeds on-the-fly.
A seeded random number generator (RNG).
An instance of
RandomState. It is preferable to the unseeded
numpy.randombecause it is consistent from one test run to the next when using the same salt value, and preferable to a fixed seed RNG because changing the salt checks that tests are not dependent on a specific seed.
An integer random number generator seed in the range [0, 2**32 - 1].
The seed is consistent from one test run to the next when using the same salt value. It is preferable to a completely fixed seed because changing the salt checks that tests are not dependent on a specific seed.