Skip to main content
https://img.shields.io/github/actions/workflow/status/pytest-dev/pytest-randomly/main.yml.svg?branch=main&style=for-the-badge https://img.shields.io/badge/Coverage-100%25-success?style=for-the-badge https://img.shields.io/pypi/v/pytest-randomly.svg?style=for-the-badge https://img.shields.io/badge/code%20style-black-000000.svg?style=for-the-badge pre-commit

Pytest plugin to randomly order tests and control random.seed.


Testing a Django project? Check out my book Speed Up Your Django Tests which covers loads of ways to write faster, more accurate tests.


Features

All of these features are on by default but can be disabled with flags.

  • Randomly shuffles the order of test items. This is done first at the level of modules, then at the level of test classes (if you have them), then at the order of functions. This also works with things like doctests.

  • Generates a base random seed or accepts one for reproduction with --randomly-seed. The base random seed is printed at the start of the test run, and can be passed in to repeat a failure caused by test ordering or random data.

  • At the start of the test run, and before each test setup, run, and teardown, it resets Python’s global random seed to a fixed value, using random.seed(). The fixed value is derived from the base random seed, the pytest test ID, and an offset for setup or teardown. This ensures each test gets a different but repeatable random seed.

  • pytest-randomly also resets several libraries’ random states at the start of every test, if they are installed:

  • If additional random generators are used, they can be registered under the pytest_randomly.random_seeder entry point and will have their seed reset at the start of every test. Register a function that takes the current seed value.

  • Works with pytest-xdist.

About

Randomness in testing can be quite powerful to discover hidden flaws in the tests themselves, as well as giving a little more coverage to your system.

By randomly ordering the tests, the risk of surprising inter-test dependencies is reduced - a technique used in many places, for example Google’s C++ test runner googletest. Research suggests that “dependent tests do exist in practice” and a random order of test executions can effectively detect such dependencies [1]. Alternatively, a reverse order of test executions, as provided by pytest-reverse, may find less dependent tests but can achieve a better benefit/cost ratio.

By resetting the random seed to a repeatable number for each test, tests can create data based on random numbers and yet remain repeatable, for example factory boy’s fuzzy values. This is good for ensuring that tests specify the data they need and that the tested system is not affected by any data that is filled in randomly due to not being specified.

I have written a blog post covering the history of pytest-randomly, including how it started life as the nose plugin nose-randomly.

Additionally, I appeared on the Test and Code podcast to talk about pytest-randomly.

Installation

Install with:

python -m pip install pytest-randomly

Python 3.10 to 3.15 supported.

Usage

Pytest will automatically find the plugin and use it when you run pytest. The output will start with an extra line that tells you the random seed that is being used:

$ pytest
...
platform darwin -- Python ...
Using --randomly-seed=1553614239
...

If the tests fail due to ordering or randomly created data, you can restart them with that seed using the flag as suggested:

pytest --randomly-seed=1234

Or more conveniently, use the special value last:

pytest --randomly-seed=last

(This only works if pytest’s cacheprovider plugin has not been disabled.)

Since the ordering is by module, then by class, you can debug inter-test pollution failures by narrowing down which tests are being run to find the bad interaction by rerunning just the module/class:

pytest --randomly-seed=1234 tests/module_that_failed/

You can disable behaviours you don’t like with the following flags:

  • --randomly-dont-reset-seed - turn off the reset of random.seed() at the start of every test

  • --randomly-dont-reorganize - turn off the shuffling of the order of tests

The plugin appears to Pytest with the name ‘randomly’. To disable it altogether, you can use the -p argument, for example:

pytest -p no:randomly

Avoid reordering some tests

To fix the order of some tests, use the pytest-order plugin. See its documentation section on usage with pytest-randomly.

Entry Point

If you’re using a different randomness generator in your third party package, you can register an entrypoint to be called every time pytest-randomly reseeds. Implement the entrypoint pytest_randomly.random_seeder, referring to a function/callable that takes one argument, the new seed (int).

For example in your setup.cfg:

[options.entry_points]
pytest_randomly.random_seeder =
    mypackage = mypackage.reseed

Then implement reseed(new_seed).

References

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pytest_randomly-5.0.0.tar.gz (8.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pytest_randomly-5.0.0-py3-none-any.whl (8.9 kB view details)

Uploaded Python 3

File details

Details for the file pytest_randomly-5.0.0.tar.gz.

File metadata

  • Download URL: pytest_randomly-5.0.0.tar.gz
  • Upload date:
  • Size: 8.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.13

File hashes

Hashes for pytest_randomly-5.0.0.tar.gz
Algorithm Hash digest
SHA256 e9c575a5873ef168ddbe340ed9e97ce9edb4492ccc821e4b2ac6bb1f0ed515d2
MD5 f072ad7820d1e6f925feb370c1638ceb
BLAKE2b-256 013b6a40e1b9d925651e601e056a97f60d8a1daeddeac03d5609be60cb4362ce

See more details on using hashes here.

Provenance

The following attestation bundles were made for pytest_randomly-5.0.0.tar.gz:

Publisher: main.yml on pytest-dev/pytest-randomly

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pytest_randomly-5.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for pytest_randomly-5.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8a0d4703115c0c25b38b6e129fc16b1947b9643ff26a41bc1d185d7e5a7689c1
MD5 ad9d31985c05f798c90ec45c7cd8fae4
BLAKE2b-256 10b447e939285caad9a623d021512912ac08dc92a467ad075d179f43729d2934

See more details on using hashes here.

Provenance

The following attestation bundles were made for pytest_randomly-5.0.0-py3-none-any.whl:

Publisher: main.yml on pytest-dev/pytest-randomly

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

This release

5.0.0 This release

2 files

4.1.0

2 files

4.0.1

2 files

4.0.0

2 files

3.16.0

2 files

3.15.0

2 files

3.14.0

2 files

3.13.0

2 files

3.12.0

2 files

3.11.0

2 files

3.10.3

2 files

3.10.2

2 files

3.10.1

2 files

3.10.0

2 files

3.9.0

2 files

3.8.0

2 files

3.7.0

2 files

3.6.0

2 files

3.5.0

2 files

3.4.1

2 files

3.4.0

2 files

3.3.1

2 files

3.3.0

2 files

3.2.1

2 files

3.2.0

2 files

3.1.0

2 files

3.0.0

2 files

2.1.1

2 files

2.1.0

2 files

2.0.0

2 files

1.2.3

2 files

1.2.2

2 files

1.2.1

2 files

1.2.0

2 files

1.1.2

2 files

1.1.1

2 files

1.1.0

2 files

1.0.0

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page