pytest-fail-slow


Namepytest-fail-slow JSON
Version 0.6.0 PyPI version JSON
download
home_pageNone
SummaryFail tests that take too long to run
upload_time2024-06-01 22:21:24
maintainerNone
docs_urlNone
authorNone
requires_python>=3.8
licenseNone
keywords pytest slow tests timeout
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            |repostatus| |ci-status| |coverage| |pyversions| |conda| |license|

.. |repostatus| image:: https://www.repostatus.org/badges/latest/active.svg
    :target: https://www.repostatus.org/#active
    :alt: Project Status: Active — The project has reached a stable, usable
          state and is being actively developed.

.. |ci-status| image:: https://github.com/jwodder/pytest-fail-slow/actions/workflows/test.yml/badge.svg
    :target: https://github.com/jwodder/pytest-fail-slow/actions/workflows/test.yml
    :alt: CI Status

.. |coverage| image:: https://codecov.io/gh/jwodder/pytest-fail-slow/branch/master/graph/badge.svg
    :target: https://codecov.io/gh/jwodder/pytest-fail-slow

.. |pyversions| image:: https://img.shields.io/pypi/pyversions/pytest-fail-slow.svg
    :target: https://pypi.org/project/pytest-fail-slow/

.. |conda| image:: https://img.shields.io/conda/vn/conda-forge/pytest-fail-slow.svg
    :target: https://anaconda.org/conda-forge/pytest-fail-slow
    :alt: Conda Version

.. |license| image:: https://img.shields.io/github/license/jwodder/pytest-fail-slow.svg
    :target: https://opensource.org/licenses/MIT
    :alt: MIT License

`GitHub <https://github.com/jwodder/pytest-fail-slow>`_
| `PyPI <https://pypi.org/project/pytest-fail-slow/>`_
| `Issues <https://github.com/jwodder/pytest-fail-slow/issues>`_
| `Changelog <https://github.com/jwodder/pytest-fail-slow/blob/master/CHANGELOG.md>`_

``pytest-fail-slow`` is a pytest_ plugin for treating tests as failed if they
took too long to run.  It adds markers for failing tests if they or their setup
stages run for longer than a given duration, along with command-line options
for applying the same cutoff to all tests.

Note that slow tests will still be run to completion; if you want them to
instead be stopped early, use pytest-timeout_.

.. _pytest: https://docs.pytest.org
.. _pytest-timeout: https://github.com/pytest-dev/pytest-timeout


Installation
============
``pytest-fail-slow`` requires Python 3.8 or higher and pytest 7.0 or higher.
Just use `pip <https://pip.pypa.io>`_ for Python 3 (You have pip, right?) to
install it::

    python3 -m pip install pytest-fail-slow


Usage
=====

Failing Slow Tests
------------------

To cause a specific test to fail if it takes too long to run, apply the
``fail_slow`` marker to it, with the desired cutoff time as the argument:

.. code:: python

    import pytest

    @pytest.mark.fail_slow("5s")
    def test_something_sluggish():
        ...

If a test fails due to being slow, pytest's output will include the test's
duration and the duration threshold, like so::

    ________________________________ test_func ________________________________
    Test passed but took too long to run: Duration 123.0s > 5.0s

(*New in version 0.6.0*) If you only want a given test to fail for being slow
under certain conditions — say, when running under CI or on a certain platform
— supply the ``enabled`` keyword argument to the marker.  The value of
``enabled`` can be either a boolean expression or a `condition string`_.  When
the ``enabled`` value evaluates to ``True`` (the default), the test will fail
if its runtime exceeds the given duration; if it evaluates to ``False``, a
lengthy runtime will not cause the test to fail.  Example usage:

.. code:: python

    import os
    import pytest

    @pytest.mark.fail_slow("5s", enabled="CI" in os.environ)
    def test_something_that_needs_to_be_fast_in_ci():
        ...

An an alternative or in addition to the marker, the ``--fail-slow DURATION``
option can be passed to the ``pytest`` command to, in essence, apply the
``fail_slow`` marker with the given cutoff to all tests that don't already have
the marker.  (As far as ``pytest`` is concerned, the option does not actually
cause any markers to be added to any tests, in case your code cares about
that.)  If a test already has the ``fail_slow`` marker, the ``--fail-slow``
option will have no effect on it.

If you want a test to fail for being slow only if the ``--fail-slow`` option is
given, but you also want a different cutoff for the test than that passed to
the option, you can give the test a ``fail_slow`` marker that sets the desired
cutoff and also sets ``enabled`` to a condition string that checks whether
``--fail-slow`` has been given, like so:

.. code:: python

    import pytest

    @pytest.mark.fail_slow(3, enabled="config.getoption('--fail-slow') is not None")
    def test_something_sometimes_sluggish():
        ...

**Note:** This feature only takes the durations for tests themselves into
consideration.  If a test passes in less than the specified duration, but one
or more fixture setups/teardowns take longer than the duration, the test will
still be marked as passing.  To fail a test if the setup takes too long, see
below.


Failing Slow Setups
-------------------

*New in version 0.4.0*

To cause a specific test to fail if the setup steps for all of its fixtures
combined take too long to run, apply the ``fail_slow_setup`` marker to it, with
the desired cutoff time as the argument:

.. code:: python

    import pytest

    @pytest.mark.fail_slow_setup("5s")
    def test_costly_resource(slow_to_create):
        ...

Do not apply the marker to the test's fixtures; markers have no effect on
fixtures.

If the setup for a test takes too long to run, the test will be marked as
"errored," the test itself will not be run, and pytest's output will include
the setup stage's duration and the duration threshold, like so::

    _______________________ ERROR at setup of test_func _______________________
    Setup passed but took too long to run: Duration 123.0s > 5.0s

Like ``fail_slow``, the ``fail_slow_setup`` marker takes an optional
``enabled`` keyword argument that can be used to conditionally enable or
disable failure for slow setups.  There is also a ``--fail-slow-setup
DURATION`` option that can be passed to ``pytest`` to, in essence, apply the
marker to all tests that don't already have it.

**Note:** If a test depends on multiple fixtures and just one of them exceeds
the given duration on its own, the remaining fixtures will still have their
setup steps run.  Also, all fixture teardowns will still be run after the test
would have run.


Specifying Durations
--------------------

A duration passed to a marker or command-line option can be either a bare
number of seconds or else a floating-point number followed by one of the
following units (case insensitive):

- ``h``, ``hour``, ``hours``
- ``m``, ``min``, ``mins``, ``minute``, ``minutes``
- ``s``, ``sec``, ``secs``, ``second``, ``seconds``
- ``ms``, ``milli``, ``millisec``, ``milliseconds``
- ``us``, ``μs``, ``micro``, ``microsec``, ``microseconds``

.. _condition string: https://docs.pytest.org/en/8.2.x/historical-notes.html
                      #conditions-as-strings-instead-of-booleans

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "pytest-fail-slow",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": null,
    "keywords": "pytest, slow tests, timeout",
    "author": null,
    "author_email": "John Thorvald Wodder II <pytest-fail-slow@varonathe.org>",
    "download_url": "https://files.pythonhosted.org/packages/e4/ec/32f3a9cd3e7ffd50cb4c98413f5047338f3fbc2dc67012572bbe527279bb/pytest_fail_slow-0.6.0.tar.gz",
    "platform": null,
    "description": "|repostatus| |ci-status| |coverage| |pyversions| |conda| |license|\n\n.. |repostatus| image:: https://www.repostatus.org/badges/latest/active.svg\n    :target: https://www.repostatus.org/#active\n    :alt: Project Status: Active \u2014 The project has reached a stable, usable\n          state and is being actively developed.\n\n.. |ci-status| image:: https://github.com/jwodder/pytest-fail-slow/actions/workflows/test.yml/badge.svg\n    :target: https://github.com/jwodder/pytest-fail-slow/actions/workflows/test.yml\n    :alt: CI Status\n\n.. |coverage| image:: https://codecov.io/gh/jwodder/pytest-fail-slow/branch/master/graph/badge.svg\n    :target: https://codecov.io/gh/jwodder/pytest-fail-slow\n\n.. |pyversions| image:: https://img.shields.io/pypi/pyversions/pytest-fail-slow.svg\n    :target: https://pypi.org/project/pytest-fail-slow/\n\n.. |conda| image:: https://img.shields.io/conda/vn/conda-forge/pytest-fail-slow.svg\n    :target: https://anaconda.org/conda-forge/pytest-fail-slow\n    :alt: Conda Version\n\n.. |license| image:: https://img.shields.io/github/license/jwodder/pytest-fail-slow.svg\n    :target: https://opensource.org/licenses/MIT\n    :alt: MIT License\n\n`GitHub <https://github.com/jwodder/pytest-fail-slow>`_\n| `PyPI <https://pypi.org/project/pytest-fail-slow/>`_\n| `Issues <https://github.com/jwodder/pytest-fail-slow/issues>`_\n| `Changelog <https://github.com/jwodder/pytest-fail-slow/blob/master/CHANGELOG.md>`_\n\n``pytest-fail-slow`` is a pytest_ plugin for treating tests as failed if they\ntook too long to run.  It adds markers for failing tests if they or their setup\nstages run for longer than a given duration, along with command-line options\nfor applying the same cutoff to all tests.\n\nNote that slow tests will still be run to completion; if you want them to\ninstead be stopped early, use pytest-timeout_.\n\n.. _pytest: https://docs.pytest.org\n.. _pytest-timeout: https://github.com/pytest-dev/pytest-timeout\n\n\nInstallation\n============\n``pytest-fail-slow`` requires Python 3.8 or higher and pytest 7.0 or higher.\nJust use `pip <https://pip.pypa.io>`_ for Python 3 (You have pip, right?) to\ninstall it::\n\n    python3 -m pip install pytest-fail-slow\n\n\nUsage\n=====\n\nFailing Slow Tests\n------------------\n\nTo cause a specific test to fail if it takes too long to run, apply the\n``fail_slow`` marker to it, with the desired cutoff time as the argument:\n\n.. code:: python\n\n    import pytest\n\n    @pytest.mark.fail_slow(\"5s\")\n    def test_something_sluggish():\n        ...\n\nIf a test fails due to being slow, pytest's output will include the test's\nduration and the duration threshold, like so::\n\n    ________________________________ test_func ________________________________\n    Test passed but took too long to run: Duration 123.0s > 5.0s\n\n(*New in version 0.6.0*) If you only want a given test to fail for being slow\nunder certain conditions \u2014 say, when running under CI or on a certain platform\n\u2014 supply the ``enabled`` keyword argument to the marker.  The value of\n``enabled`` can be either a boolean expression or a `condition string`_.  When\nthe ``enabled`` value evaluates to ``True`` (the default), the test will fail\nif its runtime exceeds the given duration; if it evaluates to ``False``, a\nlengthy runtime will not cause the test to fail.  Example usage:\n\n.. code:: python\n\n    import os\n    import pytest\n\n    @pytest.mark.fail_slow(\"5s\", enabled=\"CI\" in os.environ)\n    def test_something_that_needs_to_be_fast_in_ci():\n        ...\n\nAn an alternative or in addition to the marker, the ``--fail-slow DURATION``\noption can be passed to the ``pytest`` command to, in essence, apply the\n``fail_slow`` marker with the given cutoff to all tests that don't already have\nthe marker.  (As far as ``pytest`` is concerned, the option does not actually\ncause any markers to be added to any tests, in case your code cares about\nthat.)  If a test already has the ``fail_slow`` marker, the ``--fail-slow``\noption will have no effect on it.\n\nIf you want a test to fail for being slow only if the ``--fail-slow`` option is\ngiven, but you also want a different cutoff for the test than that passed to\nthe option, you can give the test a ``fail_slow`` marker that sets the desired\ncutoff and also sets ``enabled`` to a condition string that checks whether\n``--fail-slow`` has been given, like so:\n\n.. code:: python\n\n    import pytest\n\n    @pytest.mark.fail_slow(3, enabled=\"config.getoption('--fail-slow') is not None\")\n    def test_something_sometimes_sluggish():\n        ...\n\n**Note:** This feature only takes the durations for tests themselves into\nconsideration.  If a test passes in less than the specified duration, but one\nor more fixture setups/teardowns take longer than the duration, the test will\nstill be marked as passing.  To fail a test if the setup takes too long, see\nbelow.\n\n\nFailing Slow Setups\n-------------------\n\n*New in version 0.4.0*\n\nTo cause a specific test to fail if the setup steps for all of its fixtures\ncombined take too long to run, apply the ``fail_slow_setup`` marker to it, with\nthe desired cutoff time as the argument:\n\n.. code:: python\n\n    import pytest\n\n    @pytest.mark.fail_slow_setup(\"5s\")\n    def test_costly_resource(slow_to_create):\n        ...\n\nDo not apply the marker to the test's fixtures; markers have no effect on\nfixtures.\n\nIf the setup for a test takes too long to run, the test will be marked as\n\"errored,\" the test itself will not be run, and pytest's output will include\nthe setup stage's duration and the duration threshold, like so::\n\n    _______________________ ERROR at setup of test_func _______________________\n    Setup passed but took too long to run: Duration 123.0s > 5.0s\n\nLike ``fail_slow``, the ``fail_slow_setup`` marker takes an optional\n``enabled`` keyword argument that can be used to conditionally enable or\ndisable failure for slow setups.  There is also a ``--fail-slow-setup\nDURATION`` option that can be passed to ``pytest`` to, in essence, apply the\nmarker to all tests that don't already have it.\n\n**Note:** If a test depends on multiple fixtures and just one of them exceeds\nthe given duration on its own, the remaining fixtures will still have their\nsetup steps run.  Also, all fixture teardowns will still be run after the test\nwould have run.\n\n\nSpecifying Durations\n--------------------\n\nA duration passed to a marker or command-line option can be either a bare\nnumber of seconds or else a floating-point number followed by one of the\nfollowing units (case insensitive):\n\n- ``h``, ``hour``, ``hours``\n- ``m``, ``min``, ``mins``, ``minute``, ``minutes``\n- ``s``, ``sec``, ``secs``, ``second``, ``seconds``\n- ``ms``, ``milli``, ``millisec``, ``milliseconds``\n- ``us``, ``\u03bcs``, ``micro``, ``microsec``, ``microseconds``\n\n.. _condition string: https://docs.pytest.org/en/8.2.x/historical-notes.html\n                      #conditions-as-strings-instead-of-booleans\n",
    "bugtrack_url": null,
    "license": null,
    "summary": "Fail tests that take too long to run",
    "version": "0.6.0",
    "project_urls": {
        "Bug Tracker": "https://github.com/jwodder/pytest-fail-slow/issues",
        "Source Code": "https://github.com/jwodder/pytest-fail-slow"
    },
    "split_keywords": [
        "pytest",
        " slow tests",
        " timeout"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "56f59fcebc75407e14e4e36bd26da0fc659ea585af256007937e3c355ce807cd",
                "md5": "472cfa9c9da0b6f67bb47ba33fe63198",
                "sha256": "1658ad93b19e54c25142540f2808640c418ba000be87dc0c9b7aac6662d493cc"
            },
            "downloads": -1,
            "filename": "pytest_fail_slow-0.6.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "472cfa9c9da0b6f67bb47ba33fe63198",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 7076,
            "upload_time": "2024-06-01T22:21:23",
            "upload_time_iso_8601": "2024-06-01T22:21:23.125819Z",
            "url": "https://files.pythonhosted.org/packages/56/f5/9fcebc75407e14e4e36bd26da0fc659ea585af256007937e3c355ce807cd/pytest_fail_slow-0.6.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "e4ec32f3a9cd3e7ffd50cb4c98413f5047338f3fbc2dc67012572bbe527279bb",
                "md5": "4e0cc72630a736376dad74257a588654",
                "sha256": "b367a5bdfadb0a4d35d4ef1c220737aa46bc8d6035256171004c67f7f2f5235c"
            },
            "downloads": -1,
            "filename": "pytest_fail_slow-0.6.0.tar.gz",
            "has_sig": false,
            "md5_digest": "4e0cc72630a736376dad74257a588654",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 9485,
            "upload_time": "2024-06-01T22:21:24",
            "upload_time_iso_8601": "2024-06-01T22:21:24.862293Z",
            "url": "https://files.pythonhosted.org/packages/e4/ec/32f3a9cd3e7ffd50cb4c98413f5047338f3fbc2dc67012572bbe527279bb/pytest_fail_slow-0.6.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-06-01 22:21:24",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "jwodder",
    "github_project": "pytest-fail-slow",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "tox": true,
    "lcname": "pytest-fail-slow"
}
        
Elapsed time: 1.54239s