{"id":2726,"library":"pytest-random-order","title":"pytest-random-order","description":"pytest-random-order is a pytest plugin that randomises the order of tests. This can be useful to detect a test that passes just because it happens to run after an unrelated test that leaves the system in a favourable state. The plugin allows users to control the level of randomness they want to introduce and to disable reordering on subsets of tests. Tests can be rerun in a specific order by passing a seed value reported in a previous test run. Current version is 1.2.0.","status":"active","version":"1.2.0","language":"en","source_language":"en","source_url":"https://github.com/jbasko/pytest-random-order","tags":["pytest","testing","random","order","plugin","test-runner"],"install":[{"cmd":"pip install pytest-random-order","lang":"bash","label":"Install with pip"}],"dependencies":[{"reason":"This is a pytest plugin and requires pytest to function.","package":"pytest","optional":false}],"imports":[{"note":"pytest-random-order is a pytest plugin that is automatically discovered and loaded by pytest. Direct Python imports are not used to activate the plugin's core functionality.","symbol":"pytest_random_order","correct":"No direct Python import is typically used for plugin activation; it's managed by pytest. For specific test control, markers are used."},{"note":"The `random_order` marker is exposed via the `pytest.mark` namespace for controlling test randomization at the module or class level. It is not imported directly from the plugin package.","wrong":"from pytest_random_order import random_order","symbol":"pytest.mark.random_order","correct":"import pytest\n\n@pytest.mark.random_order(disabled=True)\ndef test_something_not_randomized():\n    pass"}],"quickstart":{"code":"# test_example.py\ndef test_alpha():\n    assert True\n\ndef test_beta():\n    assert True\n\ndef test_gamma():\n    assert True\n\n# Run from your terminal:\n# pip install pytest pytest-random-order\n# pytest --random-order\n#\n# To always enable it, add to pytest.ini (or pyproject.toml):\n# [pytest]\n# addopts = --random-order","lang":"python","description":"Install the plugin, then run pytest with the `--random-order` command-line flag. For persistent randomization, configure `addopts` in your `pytest.ini` or `pyproject.toml` file. This example demonstrates basic test functions which will be randomized when the plugin is active."},"warnings":[{"fix":"Add `addopts = --random-order` to your `pytest.ini` or always pass `--random-order` when invoking `pytest`.","message":"From v1.0.0 onwards, `pytest-random-order` no longer randomizes tests by default. You must explicitly enable it using `--random-order`, `--random-order-bucket=<bucket_type>`, or `--random-order-seed=<seed>` command-line options, or by setting `addopts` in your pytest configuration file (e.g., `pytest.ini`).","severity":"breaking","affected_versions":">=1.0.0"},{"fix":"Run `pytest` with `--dist=loadfile --random-order`.","message":"When using `pytest-random-order` with `pytest-xdist` for parallel test execution, the order configured by `pytest-random-order` might not be preserved. To make them work together effectively, use `pytest-xdist` with the `--dist=loadfile` option. This ensures all tests from one file are run in the same worker, allowing `pytest-random-order` to manage ordering within those files.","severity":"gotcha","affected_versions":"All versions when used with pytest-xdist"},{"fix":"Start with smaller bucket types like `class` or `module` and only switch to `package` or `global` if necessary and after ensuring fixtures handle repeated setup/teardown efficiently.","message":"Using the `--random-order-bucket=global` or `--random-order-bucket=package` options can significantly increase test execution time, especially if your tests rely on module- or session-scoped fixtures. Higher levels of randomization can cause these fixtures to be set up and torn down multiple times across a test run, leading to performance degradation.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Avoid using multiple test-ordering plugins simultaneously unless their compatibility is explicitly documented. Prioritize `pytest-random-order` if its functionality is desired.","message":"Coexistence with other pytest plugins that actively modify test order (e.g., `pytest-ordering`, `pytest-randomly`) can lead to unpredictable test execution sequences, as plugins may conflict or partially revert each other's effects.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-10T00:00:00.000Z","next_check":"2026-07-09T00:00:00.000Z"}