| 1 |
from typing import List |
| 2 |
from typing import Optional |
| 3 |
from typing import TYPE_CHECKING |
| 4 |
|
| 5 |
from _pytest import nodes |
| 6 |
from _pytest.config import Config |
| 7 |
from _pytest.config.argparsing import Parser |
| 8 |
from _pytest.main import Session |
| 9 |
from _pytest.reports import TestReport |
| 10 |
import pytest |
| 11 |
|
| 12 |
|
| 13 |
if TYPE_CHECKING: |
| 14 |
from _pytest.cacheprovider import Cache |
| 15 |
|
| 16 |
STEPWISE_CACHE_DIR = "cache/stepwise" |
| 17 |
|
| 18 |
|
| 19 |
def pytest_addoption(parser: Parser) -> None: |
| 20 |
group = parser.getgroup("general") |
| 21 |
group.addoption( |
| 22 |
"--sw", |
| 23 |
"--stepwise", |
| 24 |
action="store_true", |
| 25 |
default=False, |
| 26 |
dest="stepwise", |
| 27 |
help="Exit on test failure and continue from last failing test next time", |
| 28 |
) |
| 29 |
group.addoption( |
| 30 |
"--sw-skip", |
| 31 |
"--stepwise-skip", |
| 32 |
action="store_true", |
| 33 |
default=False, |
| 34 |
dest="stepwise_skip", |
| 35 |
help="Ignore the first failing test but stop on the next failing test. " |
| 36 |
"Implicitly enables --stepwise.", |
| 37 |
) |
| 38 |
|
| 39 |
|
| 40 |
@pytest.hookimpl |
| 41 |
def pytest_configure(config: Config) -> None: |
| 42 |
if config.option.stepwise_skip: |
| 43 |
# allow --stepwise-skip to work on its own merits. |
| 44 |
config.option.stepwise = True |
| 45 |
if config.getoption("stepwise"): |
| 46 |
config.pluginmanager.register(StepwisePlugin(config), "stepwiseplugin") |
| 47 |
|
| 48 |
|
| 49 |
def pytest_sessionfinish(session: Session) -> None: |
| 50 |
if not session.config.getoption("stepwise"): |
| 51 |
assert session.config.cache is not None |
| 52 |
if hasattr(session.config, "workerinput"): |
| 53 |
# Do not update cache if this process is a xdist worker to prevent |
| 54 |
# race conditions (#10641). |
| 55 |
return |
| 56 |
# Clear the list of failing tests if the plugin is not active. |
| 57 |
session.config.cache.set(STEPWISE_CACHE_DIR, []) |
| 58 |
|
| 59 |
|
| 60 |
class StepwisePlugin: |
| 61 |
def __init__(self, config: Config) -> None: |
| 62 |
self.config = config |
| 63 |
self.session: Optional[Session] = None |
| 64 |
self.report_status = "" |
| 65 |
assert config.cache is not None |
| 66 |
self.cache: Cache = config.cache |
| 67 |
self.lastfailed: Optional[str] = self.cache.get(STEPWISE_CACHE_DIR, None) |
| 68 |
self.skip: bool = config.getoption("stepwise_skip") |
| 69 |
|
| 70 |
def pytest_sessionstart(self, session: Session) -> None: |
| 71 |
self.session = session |
| 72 |
|
| 73 |
def pytest_collection_modifyitems( |
| 74 |
self, config: Config, items: List[nodes.Item] |
| 75 |
) -> None: |
| 76 |
if not self.lastfailed: |
| 77 |
self.report_status = "no previously failed tests, not skipping." |
| 78 |
return |
| 79 |
|
| 80 |
# check all item nodes until we find a match on last failed |
| 81 |
failed_index = None |
| 82 |
for index, item in enumerate(items): |
| 83 |
if item.nodeid == self.lastfailed: |
| 84 |
failed_index = index |
| 85 |
break |
| 86 |
|
| 87 |
# If the previously failed test was not found among the test items, |
| 88 |
# do not skip any tests. |
| 89 |
if failed_index is None: |
| 90 |
self.report_status = "previously failed test not found, not skipping." |
| 91 |
else: |
| 92 |
self.report_status = f"skipping {failed_index} already passed items." |
| 93 |
deselected = items[:failed_index] |
| 94 |
del items[:failed_index] |
| 95 |
config.hook.pytest_deselected(items=deselected) |
| 96 |
|
| 97 |
def pytest_runtest_logreport(self, report: TestReport) -> None: |
| 98 |
if report.failed: |
| 99 |
if self.skip: |
| 100 |
# Remove test from the failed ones (if it exists) and unset the skip option |
| 101 |
# to make sure the following tests will not be skipped. |
| 102 |
if report.nodeid == self.lastfailed: |
| 103 |
self.lastfailed = None |
| 104 |
|
| 105 |
self.skip = False |
| 106 |
else: |
| 107 |
# Mark test as the last failing and interrupt the test session. |
| 108 |
self.lastfailed = report.nodeid |
| 109 |
assert self.session is not None |
| 110 |
self.session.shouldstop = ( |
| 111 |
"Test failed, continuing from this test next run." |
| 112 |
) |
| 113 |
|
| 114 |
else: |
| 115 |
# If the test was actually run and did pass. |
| 116 |
if report.when == "call": |
| 117 |
# Remove test from the failed ones, if exists. |
| 118 |
if report.nodeid == self.lastfailed: |
| 119 |
self.lastfailed = None |
| 120 |
|
| 121 |
def pytest_report_collectionfinish(self) -> Optional[str]: |
| 122 |
if self.config.getoption("verbose") >= 0 and self.report_status: |
| 123 |
return f"stepwise: {self.report_status}" |
| 124 |
return None |
| 125 |
|
| 126 |
def pytest_sessionfinish(self) -> None: |
| 127 |
if hasattr(self.config, "workerinput"): |
| 128 |
# Do not update cache if this process is a xdist worker to prevent |
| 129 |
# race conditions (#10641). |
| 130 |
return |
| 131 |
self.cache.set(STEPWISE_CACHE_DIR, self.lastfailed) |