-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TST: be smart about tests to run after a failure
Use pytest incremental marker to skip running full length simulation if reduced simulations fail.
- Loading branch information
Showing
4 changed files
with
165 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
""" Example from pytest documentation | ||
https://pytest.org/en/stable/example/simple.html#incremental-testing-test-steps | ||
""" | ||
|
||
from typing import Dict, Tuple | ||
import pytest | ||
|
||
# store history of failures per test class name and per index in parametrize | ||
# (if parametrize used) | ||
_test_failed_incremental: Dict[str, Dict[Tuple[int, ...], str]] = {} | ||
|
||
|
||
def pytest_runtest_makereport(item, call): | ||
|
||
if "incremental" in item.keywords: | ||
# incremental marker is used | ||
if call.excinfo is not None and not call.excinfo.typename == "Skipped": | ||
# the test has failed, but was not skiped | ||
|
||
# retrieve the class name of the test | ||
cls_name = str(item.cls) | ||
# retrieve the index of the test (if parametrize is used in | ||
# combination with incremental) | ||
parametrize_index = ( | ||
tuple(item.callspec.indices.values()) | ||
if hasattr(item, "callspec") | ||
else () | ||
) | ||
# retrieve the name of the test function | ||
test_name = item.originalname or item.name | ||
# store in _test_failed_incremental the original name of the | ||
# failed test | ||
_test_failed_incremental.setdefault(cls_name, {}).setdefault( | ||
parametrize_index, test_name | ||
) | ||
|
||
|
||
def pytest_runtest_setup(item): | ||
if "incremental" in item.keywords: | ||
# retrieve the class name of the test | ||
cls_name = str(item.cls) | ||
# check if a previous test has failed for this class | ||
if cls_name in _test_failed_incremental: | ||
# retrieve the index of the test (if parametrize is used in | ||
# combination with incremental) | ||
parametrize_index = ( | ||
tuple(item.callspec.indices.values()) | ||
if hasattr(item, "callspec") | ||
else () | ||
) | ||
# retrieve the name of the first test function to fail for this | ||
# class name and index | ||
test_name = _test_failed_incremental[cls_name].get( | ||
parametrize_index, None) | ||
# if name found, test has failed for the combination of class name | ||
# and test name | ||
if test_name is not None: | ||
pytest.xfail("previous test failed ({})".format(test_name)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[pytest] | ||
markers = | ||
incremental: run tests with prerequisites in incremental order |