===============
pytest-randomly
===============
.. image:: https://img.shields.io/github/actions/workflow/status/pytest-dev/pytest-randomly/main.yml?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
.. figure:: https://raw.githubusercontent.com/pytest-dev/pytest-randomly/main/logo.png
:scale: 50%
:alt: Randomness power.
Pytest plugin to randomly order tests and control ``random.seed``.
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 the global ``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 <https://factoryboy.readthedocs.io/en/latest/reference.html>`_
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 <https://pypi.org/project/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. This is also done
if you're using the ``faker`` pytest fixture, by defining the ``faker_seed``
fixture
(`docs <https://faker.readthedocs.io/en/master/pytest-fixtures.html#seeding-configuration>`__).
* If
`Model Bakery <https://model-bakery.readthedocs.io/en/latest/>`_
is installed, its random state is reset at the start of every test. This
allows for repeatable use of its random fixture field values.
* If `numpy <http://www.numpy.org/>`_ is installed, its legacy global random state in |numpy.random|__ is reset at the start of every test.
.. |numpy.random| replace:: ``numpy.random``
__ https://numpy.org/doc/stable/reference/random/index.html
* 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.8 to 3.12 supported.
----
**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.
----
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:
.. code-block:: bash
$ 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:
.. code-block:: bash
pytest --randomly-seed=1234
Or more conveniently, use the special value ``last``:
.. code-block:: bash
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:
.. code-block:: bash
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:
.. code-block:: sh
pytest -p no: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``:
.. code-block:: ini
[options.entry_points]
pytest_randomly.random_seeder =
mypackage = mypackage.reseed
Then implement ``reseed(new_seed)``.
References
==========
.. [1] Sai Zhang, Darioush Jalali, Jochen Wuttke, Kıvanç Muşlu, Wing Lam, Michael D. Ernst, and David Notkin. 2014. Empirically revisiting the test independence assumption. In Proceedings of the 2014 International Symposium on Software Testing and Analysis (ISSTA 2014). Association for Computing Machinery, New York, NY, USA, 385–396. doi:https://doi.org/10.1145/2610384.2610404
Raw data
{
"_id": null,
"home_page": "https://github.com/pytest-dev/pytest-randomly",
"name": "pytest-randomly",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.8",
"maintainer_email": "",
"keywords": "pytest,random,randomize,randomise,randomly",
"author": "Adam Johnson",
"author_email": "me@adamj.eu",
"download_url": "https://files.pythonhosted.org/packages/c9/d4/6e924a0b2855736d942703dec88dfc98b4fe0881c8fa849b6b0fbb9182fa/pytest_randomly-3.15.0.tar.gz",
"platform": null,
"description": "===============\npytest-randomly\n===============\n\n.. image:: https://img.shields.io/github/actions/workflow/status/pytest-dev/pytest-randomly/main.yml?branch=main&style=for-the-badge\n :target: https://github.com/pytest-dev/pytest-randomly/actions?workflow=CI\n\n.. image:: https://img.shields.io/badge/Coverage-100%25-success?style=for-the-badge\n :target: https://github.com/pytest-dev/pytest-randomly/actions?workflow=CI\n\n.. image:: https://img.shields.io/pypi/v/pytest-randomly.svg?style=for-the-badge\n :target: https://pypi.org/project/pytest-randomly/\n\n.. image:: https://img.shields.io/badge/code%20style-black-000000.svg?style=for-the-badge\n :target: https://github.com/psf/black\n\n.. image:: https://img.shields.io/badge/pre--commit-enabled-brightgreen?logo=pre-commit&logoColor=white&style=for-the-badge\n :target: https://github.com/pre-commit/pre-commit\n :alt: pre-commit\n\n.. figure:: https://raw.githubusercontent.com/pytest-dev/pytest-randomly/main/logo.png\n :scale: 50%\n :alt: Randomness power.\n\nPytest plugin to randomly order tests and control ``random.seed``.\n\nFeatures\n========\n\nAll of these features are on by default but can be disabled with flags.\n\n* Randomly shuffles the order of test items. This is done first at the level of\n modules, then at the level of test classes (if you have them), then at the\n order of functions. This also works with things like doctests.\n\n* Resets the global ``random.seed()`` at the start of every test case and test\n to a fixed number - this defaults to ``time.time()`` from the start of your\n test run, but you can pass in ``--randomly-seed`` to repeat a\n randomness-induced failure.\n\n* If\n `factory boy <https://factoryboy.readthedocs.io/en/latest/reference.html>`_\n is installed, its random state is reset at the start of every test. This\n allows for repeatable use of its random 'fuzzy' features.\n\n* If `faker <https://pypi.org/project/faker>`_ is installed, its random\n state is reset at the start of every test. This is also for repeatable fuzzy\n data in tests - factory boy uses faker for lots of data. This is also done\n if you're using the ``faker`` pytest fixture, by defining the ``faker_seed``\n fixture\n (`docs <https://faker.readthedocs.io/en/master/pytest-fixtures.html#seeding-configuration>`__).\n\n* If\n `Model Bakery <https://model-bakery.readthedocs.io/en/latest/>`_\n is installed, its random state is reset at the start of every test. This\n allows for repeatable use of its random fixture field values.\n\n* If `numpy <http://www.numpy.org/>`_ is installed, its legacy global random state in |numpy.random|__ is reset at the start of every test.\n\n .. |numpy.random| replace:: ``numpy.random``\n __ https://numpy.org/doc/stable/reference/random/index.html\n\n* If additional random generators are used, they can be registered under the\n ``pytest_randomly.random_seeder``\n `entry point <https://packaging.python.org/specifications/entry-points/>`_ and\n will have their seed reset at the start of every test. Register a function\n that takes the current seed value.\n\n* Works with `pytest-xdist <https://pypi.org/project/pytest-xdist/>`__.\n\nAbout\n=====\n\nRandomness in testing can be quite powerful to discover hidden flaws in the\ntests themselves, as well as giving a little more coverage to your system.\n\nBy randomly ordering the tests, the risk of surprising inter-test dependencies\nis reduced - a technique used in many places, for example Google's C++ test\nrunner `googletest\n<https://code.google.com/p/googletest/wiki/V1_5_AdvancedGuide#Shuffling_the_Tests>`_.\nResearch suggests that \"dependent tests do exist in practice\" and a random\norder of test executions can effectively detect such dependencies [1]_.\nAlternatively, a reverse order of test executions, as provided by `pytest-reverse\n<https://github.com/adamchainz/pytest-reverse>`__, may find less dependent\ntests but can achieve a better benefit/cost ratio.\n\nBy resetting the random seed to a repeatable number for each test, tests can\ncreate data based on random numbers and yet remain repeatable, for example\nfactory boy's fuzzy values. This is good for ensuring that tests specify the\ndata they need and that the tested system is not affected by any data that is\nfilled in randomly due to not being specified.\n\nI have written a `blog post covering the history of\npytest-randomly <https://adamj.eu/tech/2018/01/08/pytest-randomly-history/>`__,\nincluding how it started life as the nose plugin\n`nose-randomly <https://github.com/adamchainz/nose-randomly>`__.\n\nAdditionally, I appeared on the Test and Code podcast to `talk about\npytest-randomly <https://testandcode.com/128>`__.\n\nInstallation\n============\n\nInstall with:\n\n.. code-block:: bash\n\n python -m pip install pytest-randomly\n\nPython 3.8 to 3.12 supported.\n\n----\n\n**Testing a Django project?**\nCheck 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.\n\n----\n\nUsage\n=====\n\nPytest will automatically find the plugin and use it when you run ``pytest``.\nThe output will start with an extra line that tells you the random seed that is\nbeing used:\n\n.. code-block:: bash\n\n $ pytest\n ...\n platform darwin -- Python ...\n Using --randomly-seed=1553614239\n ...\n\nIf the tests fail due to ordering or randomly created data, you can restart\nthem with that seed using the flag as suggested:\n\n.. code-block:: bash\n\n pytest --randomly-seed=1234\n\nOr more conveniently, use the special value ``last``:\n\n.. code-block:: bash\n\n pytest --randomly-seed=last\n\n(This only works if pytest\u2019s cacheprovider plugin has not been disabled.)\n\nSince the ordering is by module, then by class, you can debug inter-test\npollution failures by narrowing down which tests are being run to find the bad\ninteraction by rerunning just the module/class:\n\n.. code-block:: bash\n\n pytest --randomly-seed=1234 tests/module_that_failed/\n\nYou can disable behaviours you don't like with the following flags:\n\n* ``--randomly-dont-reset-seed`` - turn off the reset of ``random.seed()`` at\n the start of every test\n* ``--randomly-dont-reorganize`` - turn off the shuffling of the order of tests\n\nThe plugin appears to Pytest with the name 'randomly'. To disable it\naltogether, you can use the ``-p`` argument, for example:\n\n.. code-block:: sh\n\n pytest -p no:randomly\n\nEntry Point\n===========\n\nIf you're using a different randomness generator in your third party package,\nyou can register an entrypoint to be called every time ``pytest-randomly``\nreseeds. Implement the entrypoint ``pytest_randomly.random_seeder``, referring\nto a function/callable that takes one argument, the new seed (int).\n\nFor example in your ``setup.cfg``:\n\n.. code-block:: ini\n\n [options.entry_points]\n pytest_randomly.random_seeder =\n mypackage = mypackage.reseed\n\nThen implement ``reseed(new_seed)``.\n\nReferences\n==========\n\n.. [1] Sai Zhang, Darioush Jalali, Jochen Wuttke, K\u0131van\u00e7 Mu\u015flu, Wing Lam, Michael D. Ernst, and David Notkin. 2014. Empirically revisiting the test independence assumption. In Proceedings of the 2014 International Symposium on Software Testing and Analysis (ISSTA 2014). Association for Computing Machinery, New York, NY, USA, 385\u2013396. doi:https://doi.org/10.1145/2610384.2610404\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Pytest plugin to randomly order tests and control random.seed.",
"version": "3.15.0",
"project_urls": {
"Changelog": "https://github.com/pytest-dev/pytest-randomly/blob/main/CHANGELOG.rst",
"Homepage": "https://github.com/pytest-dev/pytest-randomly",
"Mastodon": "https://fosstodon.org/@adamchainz",
"Twitter": "https://twitter.com/adamchainz"
},
"split_keywords": [
"pytest",
"random",
"randomize",
"randomise",
"randomly"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "24d300e575657422055c4ea220b2f80e8cc6026ab7130372b7067444d1b0ac10",
"md5": "241c521bf787134c50ac3895350d26c8",
"sha256": "0516f4344b29f4e9cdae8bce31c4aeebf59d0b9ef05927c33354ff3859eeeca6"
},
"downloads": -1,
"filename": "pytest_randomly-3.15.0-py3-none-any.whl",
"has_sig": false,
"md5_digest": "241c521bf787134c50ac3895350d26c8",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.8",
"size": 8685,
"upload_time": "2023-08-15T18:04:57",
"upload_time_iso_8601": "2023-08-15T18:04:57.913911Z",
"url": "https://files.pythonhosted.org/packages/24/d3/00e575657422055c4ea220b2f80e8cc6026ab7130372b7067444d1b0ac10/pytest_randomly-3.15.0-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "c9d46e924a0b2855736d942703dec88dfc98b4fe0881c8fa849b6b0fbb9182fa",
"md5": "49ba7643e60ff56b2b913b2245bc5fe9",
"sha256": "b908529648667ba5e54723088edd6f82252f540cc340d748d1fa985539687047"
},
"downloads": -1,
"filename": "pytest_randomly-3.15.0.tar.gz",
"has_sig": false,
"md5_digest": "49ba7643e60ff56b2b913b2245bc5fe9",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.8",
"size": 21743,
"upload_time": "2023-08-15T18:04:59",
"upload_time_iso_8601": "2023-08-15T18:04:59.857990Z",
"url": "https://files.pythonhosted.org/packages/c9/d4/6e924a0b2855736d942703dec88dfc98b4fe0881c8fa849b6b0fbb9182fa/pytest_randomly-3.15.0.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-08-15 18:04:59",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "pytest-dev",
"github_project": "pytest-randomly",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"tox": true,
"lcname": "pytest-randomly"
}