The Nengo team at ABR is excited to announce the first release of pytest-rng
. pytest-rng
is made available under the open source MIT license so that all Python projects can make use of it to better test their numerical code.
What is pytest-rng?
pytest-rng
ensures that “randomness” in your tests is reproducible from one run to the next by providing rng
and seed
fixtures for pytest.
Use the rng
fixture to get a pre-seeded random number generator (RNG) that exposes NumPy’s RandomState
interface.
import numpy as np
def test_with_randomness(rng):
"""Print the same three numbers every time."""
print(rng.uniform(-1, 1), size=3))
Use the seed
fixture to get an integer seed that can be used to initialize your own RNG.
import numpy as np
def test_with_randomness(seed):
"""Print the same four numbers every time."""
print(seed)
rng = np.random.RandomState(seed)
print(rng.uniform(-1, 1), size=3))
In order to ensure that your tests are robust to different seeds, you can change all of the seeds across all of your tests by specifying a salt
value. You can do this either in a configuration file like setup.cfg
or pytest.ini
[tool:pytest]
rng_salt = v0.3.0
or via the command line
pytest --rng-salt "v0.4.0"
How do I use it?
To use the fixture, first install it with pip
:
pip install pytest-rng
then you can use it in your tests by putting rng
or seed
in the function signature, like other pytest fixtures. We recommend you add pytest-rng
your list of test requirements as well.
Where can I learn more?
For more details and advanced usage, see the full documentation.
Where can I get help?
If you have any questions, run into any bugs, or have suggestions for new features, please file an issue on Github.