Skip to main content

pytest-slow-first

PyPI Version Python Versions

Overview

pytest-slow-first is a pytest plugin that optimizes test suite execution by sorting tests based on their duration from the last run. This ensures that slower tests run first, leading to more efficient utilization of resources when used in conjunction with pytest-xdist.

How It Works

When running your test suite with this plugin, it saves the duration of each test in a file. On subsequent runs, tests are sorted based on the time they took to execute in the last run. This sorting minimizes the overall test suite execution time.

Running tests with pytest-xdist with multiple workers, evenly distributing slower tests among workers becomes crucial. Without this plugin, the default order may lead to inefficient resource utilization. pytest-slow-first addresses this issue by ensuring that slower tests are evenly distributed, reducing the total execution time.

Example

Consider a test suite with six tests, each taking varying amounts of time to run: Suite with 6 tests and their times

Running this suite with two workers using only pytest-xdist might result in uneven distribution:

Suite running with xdist

However, with pytest-slow-first, the same suite will be executed more efficiently and takes less time to finish, as shown below:

Suite with 6 tests running with xdist + slow-first

Usage

To use pytest-slow-first alongside pytest-xdist, simply add the --slow-first option:

pytest tests --slow-first -n3  # using along side xdist

The plugin initializes by running your suite as usual the first time. On subsequent runs, it sorts tests based on the time taken in the previous run.

Multiple runs

The plugin's results file is cumulative. If a test run already exists in the file, its duration will be updated.

Custom File Location

By default, the plugin saves test durations in a file named pytest-slow-first.json in the current directory. You can change the location by setting the SLOW_FIRST_PATH environment variable:

export SLOW_FIRST_PATH=/tmp/pytest-slow-first.json pytest --slow-first

Installation

You can install "pytest-slow-first" via pip:

$ pip install pytest-slow-first

Contributing

Contributions are very welcome. Tests can be run with tox, please ensure the coverage at least stays the same before you submit a pull request.

License

Distributed under the terms of the MIT license, "pytest-slow-first" is free and open source software.

Issues

If you encounter any problems, please file an issue along with a detailed description.

Metadata

Release files for pytest-slow-first 1.0.3

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-slow-first 1.0.3
File Size Uploaded
pytest-slow-first-1.0.3.tar.gz 6.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pytest-slow-first 1.0.3
File Interpreter ABI Platform
pytest_slow_first-1.0.3-py3-none-any.whl Python 3 none any Details

Total release size: 11.6 kB

Release files / pytest-slow-first-1.0.3.tar.gz

Download URL pytest-slow-first-1.0.3.tar.gz
Size 6.0 kB
Tags Source
SHA-256 checksum
How to use checksums
49376a7fe8075f969faada421d25dfffea8e28695640102be160788ac4762899
BLAKE2b-256 checksum
How to use checksums
70f69d5ceb822f214231f85478d6d17b20db7b0f7cba0f229ff383ca1801487e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/4.0.2 CPython/3.11.7

Release files / pytest_slow_first-1.0.3-py3-none-any.whl

Download URL pytest_slow_first-1.0.3-py3-none-any.whl
Size 5.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6ccbf25fae8db7378771f22454f9a54af26e69cbd77e91a800fb1664f7133126
BLAKE2b-256 checksum
How to use checksums
774a07ec59756e97b3bbfa1aebc5793d3a2de930204c986d72ae336658c4c25f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/4.0.2 CPython/3.11.7

Release history Release notifications | RSS feed

This release

1.0.3 This release

2 release files

1.0.2

2 release files

1.0.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