Skip to main content
https://img.shields.io/travis/pytest-dev/pytest-randomly.svg https://img.shields.io/pypi/v/pytest-randomly.svg
Randomness power.

Pytest plugin to randomly order tests and control random.seed. (Also available for nose).

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.

  • Resets random.seed() at the start of every test case and test to a fixed number - this defaults to time.time() from the start of your test run, but you can pass in --randomly-seed to repeat a randomness-induced failure.

  • If factory boy is installed, its random state is reset at the start of every test. This allows for repeatable use of its random ‘fuzzy’ features.

  • If faker is installed, its random state is reset at the start of every test. This is also for repeatable fuzzy data in tests - factory boy uses faker for lots of data.

  • If numpy is installed, its random state is reset at the start of every test.

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.

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.

This plugin is a Pytest port of my plugin for nose, nose-randomly. I’ve written a blog post on its history.

Usage

Install from pip with:

pip install pytest-randomly

Python 3.4+ supported.

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 2.7.11, pytest-2.9.1, py-1.4.31, pluggy-0.3.1
Using --randomly-seed=1460130750
...

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

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

History

Pending Release

2.1.1 (2019-03-26)

  • Fix including tests in sdist after re-arrangement in 2.1.0.

2.1.0 (2019-03-01)

  • Add the option --randomly-seed=last to reuse the last used value for the seed.

2.0.0 (2019-02-28)

  • Drop Python 2 support, only Python 3.4+ is supported now.

1.2.3 (2017-12-06)

  • Fix DeprecationWarning with recent versions of factory_boy.

1.2.2 (2017-11-03)

  • Fix collection to not sometimes crash when encoutering pytest Items without a module.

1.2.1 (2017-06-17)

  • Fix collection to be deterministically shuffled again, regression in 1.2.0.

1.2.0 (2017-06-16)

  • Dropped Python 2.6 compatibility, as upstream dependency NumPy did.

  • Reset and output the seed at the start of the test run when --randomly-dont-reset-seed is set, to allow the reorganization of tests to be reproducible.

1.1.2 (2016-10-27)

  • Reset the random state for NumPy too.

1.1.1 (2016-09-16)

  • Add Python 2.6 compatibility

1.1.0 (2016-09-12)

  • Offset the random seed during test setup and teardown. This is to avoid the awkward situation where test setup generates a random object, then the test generates a second one, but due to the re-seeding, they end up being always the same object. Thanks @rouge8 for the report.

1.0.0 (2016-04-15)

  • First release on PyPI.

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-2.1.1.tar.gz (9.1 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-2.1.1-py3-none-any.whl (6.3 kB view details)

Uploaded Python 3

File details

Details for the file pytest-randomly-2.1.1.tar.gz.

File metadata

  • Download URL: pytest-randomly-2.1.1.tar.gz
  • Upload date:
  • Size: 9.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.8.0 tqdm/4.29.1 CPython/3.7.2

File hashes

Hashes for pytest-randomly-2.1.1.tar.gz
Algorithm Hash digest
SHA256 0ff564fabb638ab52d535d8ce1da71353c7ecd54edcc5dc117f6f21f8269cfef
MD5 16d65243971e8c9d03161d2b0c8a2504
BLAKE2b-256 d1380169857245bf3a7d66c6439c74955d8512d08bc28ea4084ab66dd20884f2

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pytest_randomly-2.1.1-py3-none-any.whl
  • Upload date:
  • Size: 6.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.8.0 tqdm/4.29.1 CPython/3.7.2

File hashes

Hashes for pytest_randomly-2.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f76a068dcadb4ffbd4f6c55dd447cf95fb78cfe4f3f1445c3bee5657fdf5b3d1
MD5 d4f6cd6dd0cae54098be4bf9bc1b2ac3
BLAKE2b-256 7223a28109af5c93dad33ad0d357e592524927918c407fcf3e3e54aee37c037c

See more details on using hashes here.

Release history Release notifications | RSS feed

5.0.0

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

This release

2.1.1 This release

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