Repolode
pytest-dev

pytest-dev/pytest-randomly

A pytest plugin that randomly shuffles test order and resets random seeds so failures caused by ordering or random data can be reproduced.

Stars
724
Forks
39
Open issues
5
Last commit
4 days ago

What is it?

pytest-randomly is a plugin for the pytest Python testing framework. By default it shuffles test order at the level of modules, then classes, then individual functions, which helps surface hidden dependencies between tests.

Each test run prints a base random seed that can be passed back with --randomly-seed (or the value last) to repeat the same order and data. At the start of every test, setup, run and teardown, the plugin resets Python's global random seed from that base seed and the test ID, so tests that rely on random data stay repeatable. It also resets the random state of factory boy, Faker, Model Bakery, NumPy's legacy generator and Polyfactory.

Both behaviours can be turned off with flags, and the plugin supports custom random generators through the pytest_randomly.random_seeder entry point. It works with pytest-xdist.

Key features

  • 01Shuffles test order by module, class and function
  • 02Accepts a seed with --randomly-seed, including 'last' to reuse the previous one
  • 03Resets random seeds before each test setup, run and teardown
  • 04Resets random state for factory boy, Faker, Model Bakery, NumPy and Polyfactory
  • 05Supports custom random generators via an entry point
  • 06Works with pytest-xdist

Use cases

  • Finding tests that only pass because another test ran first
  • Reproducing a flaky failure caused by test order or random test data
  • Writing tests with factory-generated data that must remain repeatable
  • Debugging inter-test pollution by rerunning a single module with the same seed

Quick start

  1. 1

    Install the plugin with pip

    python -m pip install pytest-randomly
  2. 2

    Run pytest; the plugin shuffles tests automatically and prints the seed

    pytest

Things to know

  • Requires Python 3.11 to 3.15 and the pytest test runner.

In collections

Topics

#pytest#testing#python#test-ordering#random-seed#test-isolation

FAQ

What is pytest-randomly?
A pytest plugin that randomly shuffles test order and resets random seeds so failures caused by ordering or random data can be reproduced. pytest-randomly is a plugin for the pytest Python testing framework. By default it shuffles test order at the level of modules, then classes, then individual functions, which helps surface hidden dependencies between tests.
Is pytest-randomly free to use? What license does it have?
pytest-randomly is open source under the MIT license. Check the license terms before using it in commercial products.
Is pytest-randomly still maintained?
Actively maintained. The last commit to pytest-randomly was on Nov 2, 2026.
What can I use pytest-randomly for?
Finding tests that only pass because another test ran first Reproducing a flaky failure caused by test order or random test data Writing tests with factory-generated data that must remain repeatable Debugging inter-test pollution by rerunning a single module with the same seed
How do I install pytest-randomly?
Install the plugin with pip: python -m pip install pytest-randomly Run pytest; the plugin shuffles tests automatically and prints the seed: pytest
Where can I get pytest-randomly?
The source code and installation instructions are on GitHub: https://github.com/pytest-dev/pytest-randomly

Summary generated by AI from the project's README. GitHub stats refresh daily.

Related projects