freshcrate
Home > Frameworks > pytest-randomly

pytest-randomly

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

Description

=============== pytest-randomly =============== .. image:: https://img.shields.io/github/actions/workflow/status/pytest-dev/pytest-randomly/main.yml.svg?branch=main&style=for-the-badge :target: https://github.com/pytest-dev/pytest-randomly/actions?workflow=CI .. image:: https://img.shields.io/badge/Coverage-100%25-success?style=for-the-badge :target: https://github.com/pytest-dev/pytest-randomly/actions?workflow=CI .. image:: https://img.shields.io/pypi/v/pytest-randomly.svg?style=for-the-badge :target: https://pypi.org/project/pytest-randomly/ .. image:: https://img.shields.io/badge/code%20style-black-000000.svg?style=for-the-badge :target: https://github.com/psf/black .. image:: https://img.shields.io/badge/pre--commit-enabled-brightgreen?logo=pre-commit&logoColor=white&style=for-the-badge :target: https://github.com/pre-commit/pre-commit :alt: 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 <https://adamchainz.gumroad.com/l/suydt>`__ 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. .. |random.seed()| replace:: ``random.seed()`` __ https://docs.python.org/3/library/random.html#random.seed * pytest-randomly also resets several libraries’ random states at the start of every test, if they are installed: * `factory boy <https://factoryboy.readthedocs.io/en/latest/reference.html>`__ * `Faker <https://pypi.org/project/faker>`__ The ``faker`` pytest fixture is also affected, as pytest-randomly defines |the faker_seed fixture|__. .. |the faker_seed fixture| replace:: the ``faker_seed`` fixture __ https://faker.readthedocs.io/en/master/pytest-fixtures.html#seeding-configuration * `Model Bakery <https://model-bakery.readthedocs.io/en/latest/>`__ * `NumPy <https://www.numpy.org/>`_ Only its `legacy random state <https://numpy.org/doc/stable/reference/random/legacy.html>`__ is affected. * If additional random generators are used, they can be registered under the ``pytest_randomly.random_seeder`` `entry point <https://packaging.python.org/specifications/entry-points/>`_ 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 <https://pypi.org/project/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 <https://code.google.com/p/googletest/wiki/V1_5_AdvancedGuide#Shuffling_the_Tests>`_. 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 <https://github.com/adamchainz/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 <https://adamj.eu/tech/2018/01/08/pytest-randomly-history/>`__, including how it started life as the nose plugin `nose-randomly <https://github.com/adamchainz/nose-randomly>`__. Additionally, I appeared on the Test and Code podcast to `talk about pytest-randomly <https://testandcode.com/128>`__. Installation ============ Install with: .. code-block:: bash python -m pip install pytest-randomly Python 3.10 to 3.14 supported. Usage ===== Pytest will automatically find the plugin and use it when you r

Release History

VersionChangesUrgencyDate
4.1.0Imported from PyPI (4.1.0)Low4/21/2026
4.0.1Tag 4.0.1Low9/12/2025
4.0.0Tag 4.0.0Low9/10/2025
3.16.0Tag 3.16.0Low10/25/2024
3.15.0Tag 3.15.0Low8/15/2023
3.14.0Tag 3.14.0Low8/15/2023
3.13.0Tag 3.13.0Low7/10/2023
3.12.0Tag 3.12.0Low5/11/2022
3.11.0Tag 3.11.0Low1/10/2022
3.10.3Tag 3.10.3Low11/30/2021
3.10.2Tag 3.10.2Low11/10/2021
3.10.1Tag 3.10.1Low8/13/2021
3.10.0Tag 3.10.0Low8/13/2021
3.9.0Tag 3.9.0Low8/12/2021
3.8.0Tag 3.8.0Low5/10/2021
3.7.0Tag 3.7.0Low4/11/2021
3.6.0Tag 3.6.0Low4/1/2021
3.5.0Tag 3.5.0Low11/16/2020
3.4.1Tag 3.4.1Low7/10/2020
3.4.0Tag 3.4.0Low5/27/2020

Dependencies & License Audit

Loading dependencies...

Similar Packages

schemathesisProperty-based testing framework for Open API and GraphQL based apps4.15.2
pytest-subtestsunittest subTest() support and subtests fixture0.15.0
seleniumbaseA complete web automation framework for end-to-end testing.4.48.2
pytest-recordingA pytest plugin powered by VCR.py to record and replay HTTP traffic0.13.4
pytest-httpxSend responses to httpx.0.36.2