Skip to main content

A Pytest plugin for automatically prioritizing/ranking tests to speed up failure detection

Project description

pytest-ranking

CI PyPI pre-commit.ci status

A Pytest plugin for reducing failure detection time of your Python test suite with automated test prioritization/ranking.

This pytest plugin allows you to find failures faster and receive sooner debugging feedback from CI. It speed up the failure detection of your test suite by prioritizing the execution of tests that are faster, recently failed and/or more textually related to code change.

Installation

To install pytest-ranking via pip from PyPI:

pip install pytest-ranking

Usage

Pytest will automatically find the plugin and use it when you run pytest. You can use the default prioritization heuristic, which runs tests that have shorter execution time first by passing the --rank option:

pytest --rank

The terminal output will tell you the current configurations and runtime overhead of this plugin. For example:

[pytest-ranking] Weights: 1-1-0
[pytest-ranking] History length: 30
[pytest-ranking] Number of files with new hashes: 0
[pytest-ranking] Change relatedness computation time (s): 0.0007872581481933594
[pytest-ranking] Test order computation time(s): 0.00020933151245117188

You can configure the weights of different prioritization heuristics by passing the optional --rank-weight flag with formatted values:

pytest --rank --rank-weight=0-1-0

Weights are separated by hyphens -. The 1st weight is for running faster tests, the 2nd weight is for running recently failed tests, and the 3rd weight is for running tests more textually similar to the changed *.py files in the codebase since the last run. All weights must be integers or floats, and their sum will be normalized to 1. A higher weight means that a corresponding heuristic is favored. The default value is 1-0-0, only prioritizes faster tests.

You can also configure the maximum window size for looking into previous test runs, which is used to compute the number of runs since a test had failed, by passing the optional --rank-hist-len flag (the default value is 50):

pytest --rank --rank-hist-len=30

You can always apply these options by adding them to the addopts setting in your pytest.ini.

For example, create pytest.ini in your codebase root folder as such:

[pytest]
addopts = --rank --rank-weight=0-1-0 --rank-hist-len=30

and run pytest on command line.

Alternatively, you can also create pytest.ini in your codebase root folder as such:

[pytest]
rank_weight=0-1-0
rank_hist_len=30

and run pytest --rank on command line.

Warning

Because pytest-ranking re-orders tests to speed up failure detection time, please disable other pytest plugins that enforeces other test orders, e.g., pytest-randomly, pytest-random-order, pytest-reverse.

Contributing

Contributions are very welcome. Tests can be run with tox.

License

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

Issues

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

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-ranking-0.2.6.tar.gz (10.7 kB view details)

Uploaded Source

Built Distribution

pytest_ranking-0.2.6-py3-none-any.whl (8.9 kB view details)

Uploaded Python 3

File details

Details for the file pytest-ranking-0.2.6.tar.gz.

File metadata

  • Download URL: pytest-ranking-0.2.6.tar.gz
  • Upload date:
  • Size: 10.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.12

File hashes

Hashes for pytest-ranking-0.2.6.tar.gz
Algorithm Hash digest
SHA256 559b214247a4a408ccc45e6a778f8ea0cd7eae2e464c1b492216e27e42183a9e
MD5 25044583d7f080acf0942eca756bf73f
BLAKE2b-256 fad646c0468eec48fde0a62788411980c770820a10947caf0a86374ed2e41c50

See more details on using hashes here.

File details

Details for the file pytest_ranking-0.2.6-py3-none-any.whl.

File metadata

File hashes

Hashes for pytest_ranking-0.2.6-py3-none-any.whl
Algorithm Hash digest
SHA256 487381f60d3fe82b3e6ffbc391252be95ca70bb91f088f981f822f8cc7bce2f3
MD5 bdca54484969aff54496376b2c03b671
BLAKE2b-256 89d4ef7a8a1f2ff0d90485a8a6f461d57f045f58216e4f0b1d24b516d176affa

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