DriversRecommendedOutdated drivers can make a good PC feel brokenScan driver issues before chasing fixes manually.Scan NowOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsSlow PC?RecommendedPC slow today? Run a repair scan before it gets worseResolve common Windows issues and optimize system performance.Scan Now×
Skip to content
World desk4 min

How to Set Timeouts in Pytest

Use pytest-timeout to add global or per-test limits in seconds, with options for fixture coverage and timeout handling.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Pytest’s core does not provide the timeout mechanism described here. Install the pytest-timeout plugin, then set a default with pytest --timeout=30 or the plugin’s timeout configuration setting. To give one test a different limit, use @pytest.mark.timeout(5). These limits are in seconds and are intended to stop hangs, not measure performance precisely. pytest-timeout documentation

Install pytest-timeout and set a default

Install the plugin in the same Python environment used to run your tests:

python -m pip install pytest-timeout
pytest --timeout=30

The number is a limit in seconds. The example uses 30 seconds as an illustration, not as a universal recommendation; choose a limit that fits the expected runtime and environment of your tests. Pytest automatically discovers installed plugins.

To apply a default to the project, add this to a pytest configuration file using the INI-style syntax shown:

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
[pytest]
timeout = 30

Pytest supports several configuration-file formats. Keep the setting in the format already used by your project, following that format’s corresponding pytest configuration syntax.

Set a timeout for one test

Use the plugin’s marker to override the default for an individual test:

import pytest

@pytest.mark.timeout(5)
def test_may_hang():
    ...

The marker takes seconds. A value of zero disables the timeout for that item. The plugin’s documented precedence is configuration setting, PYTEST_TIMEOUT environment variable, command-line option, then item marker; a marker can therefore set a test-specific value over the configured defaults.

Understand what the timeout covers

By default, the timeout covers test setup, execution, and relevant finalizers. This means time spent in fixtures can count toward the limit, not just the lines inside the test function. If fixture setup is intentionally slow and you want to limit only the test body, set timeout_func_only in configuration or pass func_only=True to the marker:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
[pytest]
timeout = 30
timeout_func_only = true
@pytest.mark.timeout(5, func_only=True)
def test_may_hang():
    ...

Use function-only timing deliberately: it excludes setup time from the test’s timeout, so a hang during fixture setup will not be caught by that per-test limit.

Choose how pytest handles a timeout

pytest-timeout offers signal and thread methods. Set the method through configuration, a command-line option, or a marker, as supported by the installed plugin version.

Method Behavior and trade-off
signal Uses SIGALRM where supported and is the default on POSIX systems that support it. It can interrupt the test and allow pytest to continue, but may conflict with application or test code that also uses SIGALRM.
thread More portable and the documented safer choice when the plugin is not called from the main thread. A timeout can terminate the whole process, which may prevent normal fixture teardown and JUnit XML output.

Neither method guarantees graceful recovery in every case. Select based on your platform and whether continuing the test process matters; check the plugin documentation for the exact option syntax supported by your installed version.

Use a session timeout only for suite-level limits

The plugin also supports --session-timeout and session_timeout. This checks the overall expiration between tests; it does not interrupt a test that is currently running. Use a per-test timeout when the goal is to guard against one test hanging.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Troubleshoot common timeout problems

  • The option or marker is unknown: confirm pytest-timeout is installed in the environment that runs pytest, then check the installed plugin version’s documentation for supported settings.
  • A fixture setup hang is not stopped: check whether timeout_func_only or func_only=True narrows the timeout to the test function body. Remove that narrowing if setup should be covered.
  • The whole test run exits after a timeout: this can happen with the thread method, which may terminate the process. If your environment supports it and your code does not use SIGALRM, consider whether signal better fits your needs.
  • Timeout handling conflicts with application code: check whether that code uses SIGALRM; the signal method may conflict with it. Consider the thread method while accounting for its process-termination consequences.
  • The suite limit does not stop a slow current test: that is expected for a session timeout, which is checked between tests. Add a per-test timeout for individual hangs.

Keep timeout limits in perspective

The plugin describes timeouts as a last resort for excessively long or deadlocked tests, not a tool for precise timing or performance regressions. For performance tracking, use a measurement approach designed for that purpose rather than treating a timeout threshold as a benchmark. Timeout behavior and available options can change between plugin releases, so verify details against the version installed in your environment.

Or skip the browser setup

ScreenshotNeo is a website screenshot API and MCP server for developers, unrelated to configuring pytest timeouts. If you also need to capture web pages from code, one GET request can return an image or PDF. See the ScreenshotNeo API documentation.

curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp

It removes cookie banners, newsletter popups, and chat widgets before capture; bot checks, blank pages, and failed loads are never billed. Its MCP server lets AI agents take screenshots. The free plan includes 1,000 screenshots a month with no card, and paid plans start at $5 for 3,000.

Sign up for ScreenshotNeo’s free plan.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Shenzhen desk3 min
    HONOR Expands Beyond Smartphones With Humanoid Robot RevealHONOR said it unveiled its first humanoid robot at MWC 2026 and named shopping assistance, workplace inspections, and supportive companionship as intended uses. Later Robotics D1 claims and a reported…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Crashes, No Sound, or Screen Glitches?Free driver scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.