Skip to content

Commit 7352154

Browse files
committed
initial
0 parents  commit 7352154

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed

pytest_stepwise.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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)

setup.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from setuptools import setup
2+
3+
setup(
4+
name='pytest-stepwise',
5+
version='1.0',
6+
author='Niclas Olofsson',
7+
description='Run pytest one failing test at a time.',
8+
py_modules=['pytest_stepwise'],
9+
install_requires=[
10+
'pytest-cache >= 1.0',
11+
],
12+
entry_points={
13+
'pytest11': [
14+
'stepwise = pytest_stepwise',
15+
]
16+
},
17+
)

0 commit comments

Comments
 (0)