Pytest Timeouts Plugin

1.2.1 · active · verified Sat Apr 11

pytest-timeouts is a Linux-only Pytest plugin (version 1.2.1) designed to precisely control the duration of various test case execution phases: setup, execution, and teardown. It's distinct from the `pytest-timeout` plugin and is specifically tailored for scenarios requiring SIGALRM-based test interruption on Linux systems. Releases are infrequent but active.

Warnings

Install

Imports

Quickstart

This example demonstrates how to apply timeouts to the setup, execution, and teardown phases of a test using markers. The `test_timeout_phases` function's execution phase is intentionally set to exceed its defined timeout, which should cause a failure. You can also configure timeouts globally via `pytest.ini` or command-line options like `--setup-timeout`.

import time
import pytest

# Configure global timeouts in pytest.ini (optional):
# [pytest]
# setup_timeout = 0.3
# execution_timeout = 0.5
# teardown_timeout = 0.4

@pytest.mark.setup_timeout(0.3)
@pytest.mark.execution_timeout(0.5)
@pytest.mark.teardown_timeout(0.4)
def test_timeout_phases():
    print("\nStarting setup (simulated)")
    time.sleep(0.1) # Simulate setup
    print("Starting execution (simulated)")
    time.sleep(0.6) # This will exceed execution_timeout(0.5)
    print("Starting teardown (simulated)")
    time.sleep(0.1) # Simulate teardown

def test_no_timeout():
    print("\nRunning test without explicit timeouts")
    time.sleep(0.1)

# To run this test, save as test_example.py and execute:
# pytest -v test_example.py

view raw JSON →