pytest-rng is a pytest plugin that provides fixtures to manage and inject reproducible randomness into your tests. It offers a `random.Random` instance (`rng`), a factory to create new instances (`rng_factory`), and access to the current seed (`rng_seed`). Version `1.0.0` is current, and it maintains a stable release cadence for a core pytest plugin, focusing on test stability.
pip install pytest-rngVerified import paths — ran on the pinned version, not inferred.
Create a test file (e.g., `test_example.py`) and define a test function that accepts the `rng` fixture. To observe reproducibility, run `pytest` with the `--rng-seed` CLI option, for instance: `pytest --rng-seed=123`.
Prior to v1.0.0, the seed could be set via the `pytest_rng_seed` entry in `pytest.ini`. In v1.0.0 and later, this option was removed. Use the `--rng-seed` command-line option, or set the `PYTEST_RNG_SEED` environment variable instead.
You must explicitly use the `rng` fixture (e.g., `rng.randint(a, b)`) within your tests to benefit from `pytest-rng`'s reproducible seeding. Calls to `random.randint()` or other non-`pytest-rng` random sources will not be affected.
If your tests depend on other sources of randomness, you need to manage their seeding separately. `pytest-rng` only provides a seeded `random.Random` instance through its `rng` fixture.