{"id":10150,"library":"pytest-rng","title":"pytest-rng: Reproducible Randomness for Pytest Tests","description":"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.","status":"active","version":"1.0.0","language":"en","source_language":"en","source_url":"https://github.com/pytest-dev/pytest-rng","tags":["pytest","testing","randomness","reproducibility","plugin"],"install":[{"cmd":"pip install pytest-rng","lang":"bash","label":"Install pytest-rng"}],"dependencies":[{"reason":"pytest-rng is a pytest plugin and requires pytest (>=3.5) to function.","package":"pytest","optional":false}],"imports":[{"note":"`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.","wrong":"from pytest_rng import rng","symbol":"rng","correct":"def test_my_feature(rng):\n    value = rng.randint(1, 100)"}],"quickstart":{"code":"# test_example.py\nimport os\n\ndef test_random_sequence(rng):\n    \"\"\"\n    Demonstrates using the rng fixture for reproducible randomness.\n    Run with: pytest test_example.py --rng-seed=123\n    With --rng-seed=123, rng.randint(1, 100) will consistently be 42\n    on the first call. (This is for demonstration and might vary with versions).\n    \"\"\"\n    # Use the rng fixture, which is a seeded random.Random instance\n    assert isinstance(rng.randint(1, 100), int)\n    assert isinstance(rng.random(), float)\n    # Example of running the test with a specific seed to get reproducible results:\n    # To run this specific test with a consistent seed:\n    # pytest test_example.py --rng-seed=123","lang":"python","description":"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`."},"warnings":[{"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.","message":"The configuration option for setting the seed changed significantly in v1.0.0.","severity":"breaking","affected_versions":"<1.0.0"},{"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.","message":"`pytest-rng` does not automatically patch the global `random` module or other randomness sources.","severity":"gotcha","affected_versions":"All versions"},{"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.","message":"Interactions with other randomness libraries (e.g., NumPy's `numpy.random`, `os.urandom`) are not managed by `pytest-rng`.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-17T00:00:00.000Z","next_check":"2026-07-16T00:00:00.000Z","problems":[{"fix":"`rng` is a pytest fixture. Do not import it. Instead, declare it as an argument in your test function: `def test_my_feature(rng): ...`","cause":"Attempting to import `rng` directly from the `pytest_rng` package.","error":"AttributeError: 'module' object has no attribute 'rng'"},{"fix":"Ensure `pytest-rng` is installed in your testing environment: `pip install pytest-rng`. If running in a virtual environment, make sure it's active. This error can also occur if pytest itself is not installed.","cause":"The `pytest-rng` plugin is not installed or not active in the pytest environment.","error":"pytest: error: unrecognized arguments: --rng-seed"},{"fix":"Ensure all calls to generate randomness within your tests explicitly use the `rng` fixture (e.g., `rng.randint()`, `rng.random()`) instead of `random.randint()` or other global or external randomness functions.","cause":"The global `random` module or other unseeded random sources are being used instead of the `rng` fixture.","error":"Tests are still producing non-reproducible random numbers despite `pytest-rng` being installed."}]}