Registry / testing / pytest-rng

pytest-rng

JSON →
library1.0.0pypypiunverified

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-rng
INSTALL
IMPORT
SIG · PYTEST-RNG
P
pytest-rng
testingpythonv1.0.0
Install
4.7s avg
Import
Disk
102MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v1.0.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.910 runs
installs and imports cleanly · install 0.0s · import 0.000s · 102.3MB
glibc
py 3.103.910 runs
installs and imports cleanly · install 4.7s · import 0.000s · 99MB
102MB installed
● package 102MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

rng
def test_my_feature(rng): value = rng.randint(1, 100)
from pytest_rng import rng
`rng` is a pytest fixture provided by the plugin and is injected into test functions by pytest. It should not be imported directly from the `pytest_rng` module.

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

# test_example.py import os def test_random_sequence(rng): """ Demonstrates using the rng fixture for reproducible randomness. Run with: pytest test_example.py --rng-seed=123 With --rng-seed=123, rng.randint(1, 100) will consistently be 42 on the first call. (This is for demonstration and might vary with versions). """ # Use the rng fixture, which is a seeded random.Random instance assert isinstance(rng.randint(1, 100), int) assert isinstance(rng.random(), float) # Example of running the test with a specific seed to get reproducible results: # To run this specific test with a consistent seed: # pytest test_example.py --rng-seed=123
Debug
Known issues
breakingThe configuration option for setting the seed changed significantly in v1.0.0.
fix
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.
affects: <1.0.0
gotcha`pytest-rng` does not automatically patch the global `random` module or other randomness sources.
fix
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.
affects: All versions
gotchaInteractions with other randomness libraries (e.g., NumPy's `numpy.random`, `os.urandom`) are not managed by `pytest-rng`.
fix
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.
affects: All versions
Upgrade
Version history
1.0.0latest on PyPI · released Aug 8, 2019
Audit
Dependencies
pytestrequiredpytest-rng is a pytest plugin and requires pytest (>=3.5) to function.
Agent activity
15 hits · last 30 days
node
14
Amazon
1
Resources
pytest-rng — pip install pytest-rng · libregistry