pytest-randomly

4.0.1 · active · verified Thu Apr 09

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.

Warnings

Install

Imports

Quickstart

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.

import random

def test_first_random():
    print(f"\nTest 1 random: {random.randint(1, 100)}")
    assert True

def test_second_random():
    print(f"\nTest 2 random: {random.randint(1, 100)}")
    assert True

# To run this, save as test_example.py and execute in your terminal:
# pytest -s test_example.py
# To reproduce a run, use the seed printed in the output, e.g.:
# pytest -s test_example.py --randomly-seed=123456789

view raw JSON →