Tools for sorting test cases
Project description
Project description
pytest-sort is a pytest plugin to automatically change the execution order of test cases. Changing the order of execution can help find test cases that only succeed because of a favorable state.
This plugin provides several options for controlling how the test cases are reordered.
Quick Start
Installation:
pip install pytest-sort -upgrade
Running with pytest-sort:
pytest --sort-mode=random
By default, all tests from the same module or class will run together. This command will randomize the execution order of the tests within the module/class.
pytest --help
In the pytest-sort section, all currently avaialable options will be listed.
You may also set options in any pytest configruation file.
Usage
Ideally, all test cases should be small and independant. And should cleanup any changes to the application state. This package provides several options for validating if test cases can really be run independantly and cleanly.
Deterministic but Unpredictable
Java developers will be farmilliar with JUnit. The JUnit package automatically runs test cases in a deterministic, but unpredictable order. This package can provide similar functionality in pytest several ways.
- Using sort mode "md5" will produce the same order of tests every time as long as the test case names don't change.
sort_mode="md5"
- Using sort mode "random" with a fixed sort seed value will produce the same order of tests every time unles the list of test cases is changed (new test case, or name changes)
sort_mode="random" sort_seed=100
Random
By running in random mode, the test case order will change every time. If a test case fails unexpectedly, you can use the seed from the failed run to determine what order is causing the tests to fail.
sort_mode="random"
Fail Fast
When you have a test suite that includes long running test cases, it can be helpful to delay the longer running test cases till later. The 'fastest' mode allows you to track how long different test cases take to run. Then always run the fastest test cases first, and the slow test cases last.
pytest --exitfirst --sort-mode=fastest
Mutation Testing
Mutuation testing is slow. The 'diffcov' mode can help accelerate mutation testing.
The mutuation testing tool 'mutmut' works by modifying the source code, then running pytest to see if any tests catch the change. Using 'git diff' and 'coverage.py' the 'diffcov' mode combines the differences detected by Git with the coverage information recorded by coverage.py to prioritize test cases. That way the test cases most likely to catch the change are run as early as possible.
Steps to use 'diffcov' mode:
- Run pytest with coverage and context. This records the coverage for each test case as a separate context.
pytest --cov= --cov-context=test
- Run mutmut with runner options.
mutmut run --runner "pytest --exitfirst --assert=plain --sort-mode=diffcov"
You can also combine with 'fastest' to run tests with same coverage from fastest to slowest:
pytest --cov= --cov-context=test --sort-mode=fastest
mutmut run --runner "pytest --exitfirst --assert=plain --sort-bucket-mode=diffcov --sort-bucket=function --sort-mode=fastest"
How it Works
Pytest gathers a list of test cases to be executed, by default it orders test cases from top to bottom of each file. This plugin takes that list and creates two sort keys. The first is a sort key for each bucket, and the second is a sort key for each test. It then sorts the list of test cases using the two sort keys.
Bucket is simply a group of test cases. Depending on the sitation it can be better to keep test cases grouped together. For example, if the test suite includes fixtures with a scope of module, it would be best to use a bucket type of module, class, or parent. This is becasue pytest will create that fixture every time testing eneters the module, and destroy it every time thesting leaves the module.
You can also separately control the sorting method for buckets and for test cases. For example, if you wanted to always run the modules in order provided by pytest, but wanted to randomize test order within each module. This can be achived by setting the Sort Mode to 'random', and the Bucket Sort Mode to 'ordered'.
Pytest Markers
sort(mode,bucket)
sort(mode: str, bucket: str = "self")
The 'sort' marker allows you to change the sort settings for the marked module or class.
- 'mode' argument is required, and changes the Sort Mode setting for all tests within the marked module or class.
- 'bucket' argument is optional. By default, the sort marker will set the bucket for all tests in the marked scope to be the marked module or class. But any other valid Sort Bucket can be set instead.
Usage Examples:
import pytest
pytestmark = pytest.mark.sort("random", bucket="package")
def test_that_cba_works(): # This test case is grouped with other tests in the package bucket, and sorted randomly.
...
@pytest.mark.sort("ordered")
class TestClassAbc:
def test_that_abc_works(self): # This test case is grouped with other tests in class TestClassAbc and kept in order listed.
...
order(item_sort_key)
def order(item_sort_key: Any)
The 'order' marker sets the sort key for the marked test, class, or module to the provided value.
- The 'sort_key' value is required, and can be any value that can be used as a list sort key
When used to mark a test function, the sort key for that item is set to the priovided value.
When used to mark a test Class or Module, the Class or Module is used as a bucket for all test within it, and the sort key for the bucket is set to the provided value.
Usage Example:
import pytest
pytestmark = pytest.mark.order("my_tests")
@pytest.mark.order("test_group_1")
class TestClass:
@pytest.mark.order(1)
def test_create_the_data(): # bucket_key="test_group_1", item_key=1
...
@pytest.mark.order(2)
def test_modify_the_data(): # bucket_key="test_group_1", item_key=2
...
@pytest.mark.order(3)
def test_validate_the_data(): # bucket_key="test_group_1", item_key=3
...
@pytest.mark.order("ZZZ")
def test_delete_the_data(): # bucket_key="my_tests", item_key="ZZZ"
...
Options
For all options, the priority of options is:
- Pytest Markers (closest to the test case)
- Command Line Option
- Pytest Config Option
- Default values
Sort Mode
This option controlls how the order is modified within each bucket.
Command Line: --sort-mode
Pytest Config: sort_mode
Default: ordered
Option | Definition |
---|---|
ordered | (default) pytest_sort will keep the default order of tests. |
reverse | pytest_sort will reverse the default order of tests. |
md5 | This mode creates an md5 of each test case id, then sorts on those values. This runs test cases in a deterministicly shuffled order. |
random | Test cases are shuffled randomly. Sort Seed is used to control random sorting. |
fastest | In each run in "fastest" mode, any previously recorded runtimes in ".pytest_sort_data" file will be used to sort the fastest tests to run first. Also, by default, it will record the longest execution time for each test case to that file for future usage. See Record Test Runtimes |
diffcov | Uses 'git diff' and data from 'coverage.py' to determine which test cases likey cover the changed lines of code, and runs them first. |
Sort Bucket
Sort test order within specified test buckets.
For example, if the sort bucket is "module", then all tests from the same module will be completed before running tests for the next module.
Command Line: --sort-bucket
Pytest Config: sort_bucket
Default: parent
Option | Definition |
---|---|
session | Apply sort to all items together. |
package | Group together test cases that are in the same package (folder). |
module | Group together test cases that are in the same module (py file). |
class | Group together test cases that are members of a class. All other (non-class) test cases are grouped by module. |
function | Each test function is assigned a separate bucket. |
parent | Group together test cases by their immediate parent. |
grandparent | Group together test cases by their parent's parent. |
Sort buckets higher than class or parent can cause fixtures to be teardown and setup more than expected.
For example: If sort bucket is "package" and fixture scope is "module", Pytest may setup the fixture every time the testing enters the module, and teardown each time testing leaves the module.
The option --setup-show can reveal when fixtures are created and destroyed.
Bucket Sort Mode
This option controlls how the order is modified within each bucket.
Command Line: --sort-bucket-mode
Pytest Config: sort_bucket_mode
Default: sort_mode
Option | Definition |
---|---|
sort_mode | (default) Use the same sort mode for buckets as used for tests. See Sort Mode |
ordered | pytest_sort will keep the default order of buckets. |
reverse | pytest_sort will reverse the default order of buckets. |
md5 | This mode creates an md5 of each bucket's id, then sorts on those values. |
random | Buckets are shuffled randomly. Sort Seed is used to control random sorting. |
fastest | The total of all runtimes for tests in the bucket is used as the sort key for the bucket. |
diffcov | Uses 'git diff' and data from 'coverage.py' to determine which test cases likey cover the changed lines of code, and runs them first. |
Sort Seed
Only used in "random" sort mode. This field allows you to manually set the seed used by random package when sorting tests.
Tip: Seed is printed in the pytest header
Command Line: --sort-seed
Pytest Config: sort_seed
Default: A random integer between 0 and 1,000,000 generated at runtime.
Record Test Runtimes
When this option is enabled, this plugin with collect runtime information for all tests. If the recorded values are higher than values already stored in ".pytest_sort_data" file, the values in the file are updated.
When Sort Mode or Bucket Sort Mode is 'fastest' this option is enabled by default.
For any othe Sort Mode, this option is disabled by default.
Command Line: --sort-record-times/--sort-no-record-times
Pytest Config: sort_record_times
Default: false
Option | Definition |
---|---|
--sort-record-times sort_record_times=true |
Enable recording the maximum test case runtimes. |
--sort-no-record-times sort_record_times=false |
Disable recording test case runtimes. |
Reset Recorded Test Runtimes
Clear all recorded runtimes before sorting and running the next test.
When recording runtimes, only the highest runtime values for each test case are retained. After significant changes to test cases that can change how fast they run, it is advisable to reset the runtimes. This can help maintain more accurate sorting in "fastest" mode.
Runtimes can also be deleted by deleting the '.pytest_sort_data' file.
Command Line: --sort-reset-times
Pytest Config: N/A
Report Recorded Test Runtimes
At the end of the test run, print out the currently saved test runtimes.
Command Line: --sort-reset-times
Pytest Config: N/A
Recorded Test Runtimes Datafile
Change the location and/or name of the datafile used to store the test runtimes.
Command Line: --sort-datafile
Pytest Config: sort_datafile
Default: .pytest_sort_data
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file pytest-sort-1.2.0.tar.gz
.
File metadata
- Download URL: pytest-sort-1.2.0.tar.gz
- Upload date:
- Size: 27.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/4.0.2 CPython/3.11.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 50840a6231ec341ae398b444bd0f94b49e5dc8b7d9b11c41ebb46cc0bc0e1c7a |
|
MD5 | e7706dc58ba30b67fbd31ea5cad61951 |
|
BLAKE2b-256 | 0e27d3aa38c4f8ad2686f3c7c9c72a2124bb0def91fcf4b5ae45de3e720c375a |
File details
Details for the file pytest_sort-1.2.0-py3-none-any.whl
.
File metadata
- Download URL: pytest_sort-1.2.0-py3-none-any.whl
- Upload date:
- Size: 15.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/4.0.2 CPython/3.11.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a9be77044272987dabbef507126f12840f474e4d4538a37f471e76c7884c6284 |
|
MD5 | 329b7d3a95b9062c92055b0a8980c23f |
|
BLAKE2b-256 | 9bf588ec27b62c68abc7689afc4c92a8b1cb167a2b5eab6777a1d1d601d4ee6d |