Pytest-randomly is a pytest plugin that randomly orders tests and controls the random seed. Its primary purpose is to help identify and prevent inter-test dependencies and 'flaky' tests by ensuring tests do not rely on a specific execution order. It also resets the global `random.seed()` at the start of each test and can manage the random states of several other libraries. The current version is 4.0.1, and the project is actively maintained.
pip install pytest-randomlyVerified import paths — ran on the pinned version, not inferred.
Install `pytest-randomly` and `pytest`. Create a test file, e.g., `test_example.py`, with standard pytest functions. Run pytest from your terminal. The plugin will automatically randomize the test order and print the seed used. You can pass this seed back via `--randomly-seed` to reproduce a specific test run order and random number sequence.
To get different random numbers within a single test, manage `random.seed()` manually inside that test function, or use the `--randomly-dont-reset-seed` flag if you want less control over per-test random reproducibility. Understand that `pytest-randomly` aims for *test run* reproducibility, not unique random streams per `random` call across test runs.
For unsupported randomness generators, you can register an entry point (`pytest_randomly.random_seeder`) referring to a function that takes the new seed and resets the library's random state. This allows `pytest-randomly` to manage its state.
Consult both `pytest-randomly` and the other plugin's documentation for compatible usage patterns. `pytest-order` documentation, for instance, has a section on usage with `pytest-randomly`.
Evaluate if global randomization is strictly necessary. For most cases, the default randomization (at module and class level) is sufficient. If global randomization is required, ensure your fixtures are efficient and can handle multiple setup/teardown cycles gracefully, or consider adjusting fixture scopes.