Skip to main content

pytest-slowtrace

pytest-slowtrace is a pytest plugin that reports what a test was doing when it ran too long. It is meant to catch tests that are slow because they are waiting — on sleep, I/O, locks, subprocesses, network calls, etc. — rather than tests that are simply doing CPU work.

There are more targeted pytest plugins that can nerf sleep() calls or block network access entirely. Slowtrace is a more holistic detector: it flags any mostly-idle test and points you at where to start looking, whether the wait is in the test itself, the code under test, a library call, or a subprocess invocation that makes the test "slow" in practice.

Enabling the plugin

Install the package in the environment where pytest runs:

pip install pytest-slowtrace

Pytest discovers the plugin automatically through its pytest11 entry point; there is no explicit activation step. Once installed, run pytest as usual:

pytest

If a test exceeds the slowtrace threshold and is mostly idle, the terminal summary includes a slow tests section with the test name, CPU percentage, and the most-sampled application frame.

Setting the threshold

By default, tests are reported when they take at least 0.2 seconds and are at least 50% idle. On a reasonably fast machine, well-mocked unit tests should normally be effectively instantaneous, so the default should be noisy only when a test is accidentally waiting on something.

Set a project-wide threshold in pytest config via addopts:

[tool.pytest.ini_options]
addopts = "--slowtrace-threshold=0.5"

Or pass it on the command line:

pytest --slowtrace-threshold=0.5

For one test, override the threshold with @pytest.mark.slowtrace:

import pytest

@pytest.mark.slowtrace(seconds=1.0)
def test_allowed_to_take_about_a_second():
    ...

Mark real integration tests with xslowtrace

Slowtrace is most useful for fast unit tests that should use mocks instead of real external systems. If you intentionally have integration tests that talk to real services, wait on subprocesses, or otherwise spend time doing real I/O, mark them with xslowtrace so they do not produce noise:

import pytest

@pytest.mark.xslowtrace
def test_real_service_integration():
    ...

Choosing the application package

When a test is slow and idle, slowtrace samples the stack and reports the application frame that most likely caused the wait. By default, "application code" means code under pytest's root directory.

If your virtualenv or test dependencies live under the project root — for example in .venv/ or .tox/ — that root-directory check can mistake library frames for application frames. In that case, set the app package explicitly:

[tool.pytest.ini_options]
addopts = "--slowtrace-app-packages=myapp"

For multiple top-level packages, use a comma-separated list:

pytest --slowtrace-app-packages=myapp,myotherapp

This matches frames by their module name, such as myapp.worker, instead of by file path.

Version compatibility

This library is compatible with Python 3.10+, but should be linted under the newest stable version.

Versioning

This library follows meanver, which is like semver with a promise to rename when the major version changes.

License

pytest-slowtrace is copyright Tim Hatch, and licensed under the MIT license. See the LICENSE file for details.

Metadata

Release files for pytest-slowtrace 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pytest-slowtrace 0.2.0
File Size Uploaded
pytest_slowtrace-0.2.0.tar.gz 59.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pytest-slowtrace 0.2.0
File Interpreter ABI Platform
pytest_slowtrace-0.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 68.1 kB

Release files / pytest_slowtrace-0.2.0.tar.gz

Download URL pytest_slowtrace-0.2.0.tar.gz
Size 59.4 kB
Tags Source
SHA-256 checksum
How to use checksums
a6d273b7840e7341f7b2564e62a366b2c63e01b28806b2472d8bef4a139860d7
BLAKE2b-256 checksum
How to use checksums
8b13ecdb64a3a60852c2332ee82afbe6052533880745bd07a4ed03f2c127b7bf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 10, 2026.

Transparency log

Release files / pytest_slowtrace-0.2.0-py3-none-any.whl

Download URL pytest_slowtrace-0.2.0-py3-none-any.whl
Size 8.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c0bba2c5b993eaea253b8060f54a41f394df95c14814ad8b5aed1ce06d2d814f
BLAKE2b-256 checksum
How to use checksums
722ae3760efdbd3964cb97aa8d716e394a6318aaa0b86ada033a80bea959ab64
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 10, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page