freshcrate
Home > Frameworks > pytest-timeout

pytest-timeout

pytest plugin to abort hanging tests

Description

============== pytest-timeout ============== |python| |version| |anaconda| |ci| |pre-commit| .. |version| image:: https://img.shields.io/pypi/v/pytest-timeout.svg :target: https://pypi.python.org/pypi/pytest-timeout .. |anaconda| image:: https://img.shields.io/conda/vn/conda-forge/pytest-timeout.svg :target: https://anaconda.org/conda-forge/pytest-timeout .. |ci| image:: https://github.com/pytest-dev/pytest-timeout/workflows/build/badge.svg :target: https://github.com/pytest-dev/pytest-timeout/actions .. |python| image:: https://img.shields.io/pypi/pyversions/pytest-timeout.svg :target: https://pypi.python.org/pypi/pytest-timeout/ .. |pre-commit| image:: https://results.pre-commit.ci/badge/github/pytest-dev/pytest-timeout/master.svg :target: https://results.pre-commit.ci/latest/github/pytest-dev/pytest-timeout/master .. warning:: Please read this README carefully and only use this plugin if you understand the consequences. This plugin is designed to catch excessively long test durations like deadlocked or hanging tests, it is not designed for precise timings or performance regressions. Remember your test suite should aim to be **fast**, with timeouts being a last resort, not an expected failure mode. This plugin will time each test and terminate it when it takes too long. Termination may or may not be graceful, please see below, but when aborting it will show a stack dump of all thread running at the time. This is useful when running tests under a continuous integration server or simply if you don't know why the test suite hangs. .. note:: While by default on POSIX systems pytest will continue to execute the tests after a test has timed out this is not always possible. Often the only sure way to interrupt a hanging test is by terminating the entire process. As this is a hard termination (``os._exit()``) it will result in no teardown, JUnit XML output etc. But the plugin will ensure you will have the debugging output on stderr nevertheless, which is the most important part at this stage. See below for detailed information on the timeout methods and their side-effects. The pytest-timeout plugin has been tested on Python 3.6 and higher, including PyPy3. See tox.ini for currently tested versions. Usage ===== Install is as simple as e.g.:: pip install pytest-timeout Now you can run the test suite while setting a timeout in seconds, any individual test which takes longer than the given duration will be terminated:: pytest --timeout=300 Furthermore you can also use a decorator to set the timeout for an individual test. If combined with the ``--timeout`` flag this will override the timeout for this individual test: .. code:: python @pytest.mark.timeout(60) def test_foo(): pass By default the plugin will not time out any tests, you must specify a valid timeout for the plugin to interrupt long-running tests. A timeout is always specified as a number of seconds, and can be defined in a number of ways, from low to high priority: 1. You can set a global timeout in the `pytest configuration file`__ using the ``timeout`` option. E.g.: .. code:: ini [pytest] timeout = 300 2. The ``PYTEST_TIMEOUT`` environment variable sets a global timeout overriding a possible value in the configuration file. 3. The ``--timeout`` command line option sets a global timeout overriding both the environment variable and configuration option. 4. Using the ``timeout`` marker_ on test items you can specify timeouts on a per-item basis: .. code:: python @pytest.mark.timeout(300) def test_foo(): pass __ https://docs.pytest.org/en/latest/reference.html#ini-options-ref .. _marker: https://docs.pytest.org/en/latest/mark.html Setting a timeout to 0 seconds disables the timeout, so if you have a global timeout set you can still disable the timeout by using the mark. Timeout Methods =============== Interrupting tests which hang is not always as simple and can be platform dependent. Furthermore some methods of terminating a test might conflict with the code under test itself. The pytest-timeout plugin tries to pick the most suitable method based on your platform, but occasionally you may need to specify a specific timeout method explicitly. If a timeout method does not work your safest bet is to use the *thread* method. thread ------ This is the surest and most portable method. It is also the default on systems not supporting the *signal* method. For each test item the pytest-timeout plugin starts a timer thread which will terminate the whole process after the specified timeout. When a test item finishes this timer thread is cancelled and the test run continues. The downsides of this method are that there is a relatively large overhead for running each test and that test runs are not completed. This means that other pytest features, like e.g. JUnit XML output or fixture

Release History

VersionChangesUrgencyDate
2.4.0Imported from PyPI (2.4.0)Low4/21/2026
2.3.1Tag 2.3.1Low3/7/2024
2.3.0Tag 2.3.0Low3/7/2024
2.2.0Tag 2.2.0Low10/8/2023
2.1.0Tag 2.1.0Low1/18/2022
2.0.2Tag 2.0.2Low12/13/2021
2.0.1Tag 2.0.1Low10/11/2021
2.0.0Tag 2.0.0Low10/10/2021
1.4.2Tag 1.4.2Low7/15/2020
1.4.1Tag 1.4.1Low6/15/2020
1.4.0Tag 1.4.0Low6/14/2020
1.3.4Tag 1.3.4Low1/6/2020
1.3.3Tag 1.3.3Low11/16/2018
1.3.2Tag 1.3.2Low8/27/2018
1.3.1Tag 1.3.1Low7/23/2018
1.2.1Tag 1.2.1Low11/28/2017
1.2.0Tag 1.2.0Low11/10/2016
1.0.0Tag 1.0.0Low11/15/2015
0.5Tag 0.5Low8/14/2015
0.4Tag 0.4Low8/5/2014

Dependencies & License Audit

Loading dependencies...

Similar Packages

pre-commitA framework for managing and maintaining multi-language pre-commit hooks.v4.6.0
azure-core-tracing-opentelemetryMicrosoft Azure Azure Core OpenTelemetry plugin Library for Pythonazure-template_0.1.0b6187637
spdx-toolsSPDX parser and tools.0.8.5
lacesDjango components that know how to render themselves.0.1.2
django-tasksA backport of Django's built in Tasks framework0.12.0