Skip to main content

pytest-rng

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.

  • Use the rng fixture to get a pre-seeded random number generator (RNG) that exposes NumPy’s ~numpy.random.mtrand.RandomState interface.

  • Use the seed fixture 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: add rng or seed to the arguments of a test function or class.

Seed generation

For the seed fixture, we generate a seed by doing the following:

  1. Concatenate the test’s nodeid and a salt value, if provided.

  2. Hash that string to yield an integer seed.

For the rng fixture, we also add the string "rng" to the salt value before generating the seed as above. The seed is used to instantiate a ~numpy.random.mtrand.RandomState, which is returned.

salt

salt is a string that is added to the test’s nodeid 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 like setup.cfg or pytest.ini.

[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.


See the full documentation for more details.

Release History

1.0.0 (August 8, 2019)

Initial release of pytest-rng! Thanks to all of the contributors for making this possible!

Metadata

Release files for pytest-rng 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pytest-rng 1.0.0
File Size Uploaded
pytest-rng-1.0.0.tar.gz 14.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pytest-rng 1.0.0
File Interpreter ABI Platform
pytest_rng-1.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 22.8 kB

Release files / pytest-rng-1.0.0.tar.gz

Download URL pytest-rng-1.0.0.tar.gz
Size 14.8 kB
Tags Source
SHA-256 checksum
How to use checksums
9d9ee96557246756072133ff9b990588f28f12d3e80357cad959ef0b05aed9fa
BLAKE2b-256 checksum
How to use checksums
e1b711c928ffabbb79d86cc120b9b67b74c2ac0d132b3ea19f14024f7f0a0e4c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.33.0 CPython/3.6.3

Release files / pytest_rng-1.0.0-py3-none-any.whl

Download URL pytest_rng-1.0.0-py3-none-any.whl
Size 8.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
346e76a34f19c1f70e1059567460df9edf34aa6b41441c8707bf9ed40446b9c7
BLAKE2b-256 checksum
How to use checksums
ac6640c88e87c8731d96bb2f9a49329785d24236debb9685f6aaccc52536e697
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.33.0 CPython/3.6.3

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page