Skip to content

Commit 40b38f9

Browse files
committed
Add --stepwise-reset to restart the stepwise workflow
1 parent 43064f5 commit 40b38f9

File tree

3 files changed

+49
-2
lines changed

3 files changed

+49
-2
lines changed

changelog/13122.improvement.rst

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Improve the ``--stepwise`` flag to not forget the last failed test in case pytest is executed later without the flag.
1+
Improve the ``--stepwise``/``--sw`` flag to not forget the last failed test in case pytest is executed later without the flag.
22

33
This enables the following workflow:
44

@@ -9,3 +9,5 @@ This enables the following workflow:
99
failed test, and if it passes, continue with the next tests.
1010

1111
Previously, at step 3, pytest would start from the beginning, forgetting the failed tests.
12+
13+
Also added the new ``--stepwise-reset``/``--sw-reset``, allowing the user to explicitly reset the stepwise state and restart the workflow from the beginning.

src/_pytest/stepwise.py

+15-1
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,23 @@ def pytest_addoption(parser: Parser) -> None:
3030
help="Ignore the first failing test but stop on the next failing test. "
3131
"Implicitly enables --stepwise.",
3232
)
33+
group.addoption(
34+
"--sw-reset",
35+
"--stepwise-reset",
36+
action="store_true",
37+
default=False,
38+
dest="stepwise_reset",
39+
help="Resets stepwise state, restarting the stepwise workflow. "
40+
"Implicitly enables --stepwise.",
41+
)
3342

3443

3544
def pytest_configure(config: Config) -> None:
45+
# --stepwise-skip implies stepwise.
3646
if config.option.stepwise_skip:
37-
# allow --stepwise-skip to work on its own merits.
47+
config.option.stepwise = True
48+
# --stepwise-clear implies stepwise.
49+
if config.option.stepwise_reset:
3850
config.option.stepwise = True
3951
if config.getoption("stepwise"):
4052
config.pluginmanager.register(StepwisePlugin(config), "stepwiseplugin")
@@ -58,6 +70,8 @@ def __init__(self, config: Config) -> None:
5870
self.cache: Cache = config.cache
5971
self.lastfailed: str | None = self.cache.get(STEPWISE_CACHE_DIR, None)
6072
self.skip: bool = config.getoption("stepwise_skip")
73+
if config.getoption("stepwise_reset"):
74+
self.lastfailed = None
6175

6276
def pytest_sessionstart(self, session: Session) -> None:
6377
self.session = session

testing/test_stepwise.py

+31
Original file line numberDiff line numberDiff line change
@@ -396,3 +396,34 @@ def test_3():
396396
result.stdout.fnmatch_lines(
397397
["*::test_2 - assert False*", "*failed, continuing from this test next run*"]
398398
)
399+
400+
401+
def test_stepwise_reset(pytester: Pytester) -> None:
402+
pytester.makepyfile(
403+
"""
404+
def test_1():
405+
pass
406+
def test_2():
407+
assert False
408+
def test_3():
409+
pass
410+
"""
411+
)
412+
result = pytester.runpytest("--stepwise", "-v")
413+
result.stdout.fnmatch_lines(
414+
[
415+
"*::test_1 *PASSED*",
416+
"*::test_2 *FAILED*",
417+
"*failed, continuing from this test next run*",
418+
]
419+
)
420+
421+
# Running with --stepwise-reset restarts the stepwise workflow.
422+
result = pytester.runpytest("-v", "--stepwise-reset")
423+
result.stdout.fnmatch_lines(
424+
[
425+
"*::test_1 *PASSED*",
426+
"*::test_2 *FAILED*",
427+
"*failed, continuing from this test next run*",
428+
]
429+
)

0 commit comments

Comments
 (0)