Skip to main content

Fail tests that take too long to run

Project description

Project Status: Active — The project has reached a stable, usable state and is being actively developed. CI Status https://codecov.io/gh/jwodder/pytest-fail-slow/branch/master/graph/badge.svg https://img.shields.io/pypi/pyversions/pytest-fail-slow.svg Conda Version MIT License

GitHub | PyPI | Issues | Changelog

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.

Installation

pytest-fail-slow requires Python 3.7 or higher and pytest 6.0 or higher. Just use pip 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:

import pytest

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

In addition, the --fail-slow DURATION option can be passed to the pytest command to affect all tests in that run. If --fail-slow is given and a test has the fail_slow marker, the duration given by the marker takes precedence for that test.

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

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:

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.

In addition, the --fail-slow-setup DURATION option can be passed to the pytest command to affect all tests in that run. If --fail-slow-setup is given and a test has the fail_slow_setupresou marker, the duration given by the marker takes precedence for that test.

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

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pytest-fail-slow-0.4.0.tar.gz (8.3 kB view details)

Uploaded Source

Built Distribution

pytest_fail_slow-0.4.0-py3-none-any.whl (5.7 kB view details)

Uploaded Python 3

File details

Details for the file pytest-fail-slow-0.4.0.tar.gz.

File metadata

  • Download URL: pytest-fail-slow-0.4.0.tar.gz
  • Upload date:
  • Size: 8.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.6

File hashes

Hashes for pytest-fail-slow-0.4.0.tar.gz
Algorithm Hash digest
SHA256 5cfe9fb63b1b55193277db3a0e7f9d01c25112932bb9063ec7035beed6533b56
MD5 765dc810fd0698f386d0e7bd86467e31
BLAKE2b-256 9d230a57254fc4e8c48a0c9a0ca0fc74279fe158aa667dfa6ec26b05a41832c4

See more details on using hashes here.

File details

Details for the file pytest_fail_slow-0.4.0-py3-none-any.whl.

File metadata

File hashes

Hashes for pytest_fail_slow-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 83a83492b9e5bcb4b3b19d86e8c5159a15edfec44098fe64d2a82507ca546ac0
MD5 fb0d99d3e3a999811f8bb3c8a6ecaee7
BLAKE2b-256 2a8b0525fa75a1b8ad7fe2fa296fdbf7754c3778e55b8642cc310b894c33da11

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page