|
| 1 | +import pytest |
| 2 | +from pytest_cache import Cache |
| 3 | +from pytest import Session |
| 4 | + |
| 5 | +__version__ = '1.0' |
| 6 | + |
| 7 | + |
| 8 | +def pytest_addoption(parser): |
| 9 | + group = parser.getgroup('general') |
| 10 | + group.addoption('--stepwise', action='store_true', dest='stepwise', |
| 11 | + help='exit on test fail and continue from last failing test next time') |
| 12 | + |
| 13 | + |
| 14 | +@pytest.mark.tryfirst |
| 15 | +def pytest_configure(config): |
| 16 | + config.cache = Cache(config) |
| 17 | + config.pluginmanager.register(StepwisePlugin(config), 'stepwiseplugin') |
| 18 | + |
| 19 | + |
| 20 | +class StepwisePlugin: |
| 21 | + def __init__(self, config): |
| 22 | + self.config = config |
| 23 | + self.active = config.getvalue('stepwise') |
| 24 | + |
| 25 | + if self.active: |
| 26 | + self.lastfailed = config.cache.get('cache/stepwise', set()) |
| 27 | + |
| 28 | + def pytest_collection_modifyitems(self, session, config, items): |
| 29 | + if not self.active or not self.lastfailed: |
| 30 | + return |
| 31 | + |
| 32 | + already_passed = [] |
| 33 | + for item in items: |
| 34 | + if item.nodeid in self.lastfailed: |
| 35 | + break |
| 36 | + else: |
| 37 | + already_passed.append(item) |
| 38 | + |
| 39 | + for item in already_passed: |
| 40 | + items.remove(item) |
| 41 | + |
| 42 | + config.hook.pytest_deselected(items=already_passed) |
| 43 | + |
| 44 | + def pytest_runtest_logreport(self, report): |
| 45 | + if not self.active or 'xfail' in report.keywords: |
| 46 | + return |
| 47 | + |
| 48 | + if report.failed: |
| 49 | + self.lastfailed.add(report.nodeid) |
| 50 | + raise Session.Interrupted('Test failed, continuing from this test next run.') |
| 51 | + |
| 52 | + elif report.when == 'call': |
| 53 | + self.lastfailed.discard(report.nodeid) |
| 54 | + |
| 55 | + def pytest_sessionfinish(self, session): |
| 56 | + if self.active: |
| 57 | + self.config.cache.set('cache/stepwise', self.lastfailed) |
0 commit comments