Skip to content

Commit 43064f5

Browse files
committed
Improve stepwise to not forget failed tests
Now `--stepwise` will remember the last failed test, even if the previous pytest invocations did not pass `--stepwise`. Previously it would always clear the cache if not active, which hinders certain interactive workflows, which is the prime use cases for the flag.
1 parent 517b006 commit 43064f5

File tree

4 files changed

+51
-4
lines changed

4 files changed

+51
-4
lines changed

changelog/13122.improvement.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
Improve the ``--stepwise`` flag to not forget the last failed test in case pytest is executed later without the flag.
2+
3+
This enables the following workflow:
4+
5+
1. Execute pytest with ``--stepwise``, pytest then stops at the first failing test;
6+
2. User iteratively updates the code and runs the test in isolation, without the ``--stepwise`` flag
7+
(for example in an IDE), until it is fixed.
8+
3. At this point, the user can execute pytest with ``--stepwise`` again and pytest will continue from the previously
9+
failed test, and if it passes, continue with the next tests.
10+
11+
Previously, at step 3, pytest would start from the beginning, forgetting the failed tests.

src/_pytest/stepwise.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,6 @@ def pytest_sessionfinish(session: Session) -> None:
4747
# Do not update cache if this process is a xdist worker to prevent
4848
# race conditions (#10641).
4949
return
50-
# Clear the list of failing tests if the plugin is not active.
51-
session.config.cache.set(STEPWISE_CACHE_DIR, [])
5250

5351

5452
class StepwisePlugin:

testing/test_cacheprovider.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ def test_cache_failure_warns(
104104
pytester.makepyfile("def test_error(): raise Exception")
105105
result = pytester.runpytest()
106106
assert result.ret == 1
107-
# warnings from nodeids, lastfailed, and stepwise
107+
# warnings from nodeids and lastfailed
108108
result.stdout.fnmatch_lines(
109109
[
110110
# Validate location/stacklevel of warning from cacheprovider.
@@ -113,7 +113,7 @@ def test_cache_failure_warns(
113113
" */cacheprovider.py:*: PytestCacheWarning: could not create cache path "
114114
f"{unwritable_cache_dir}/v/cache/nodeids: *",
115115
' config.cache.set("cache/nodeids", sorted(self.cached_nodeids))',
116-
"*1 failed, 3 warnings in*",
116+
"*1 failed, 2 warnings in*",
117117
]
118118
)
119119

testing/test_stepwise.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,3 +358,41 @@ def test_one():
358358
with stepwise_cache_file.open(encoding="utf-8") as file_handle:
359359
observed_value = file_handle.readlines()
360360
assert [expected_value] == observed_value
361+
362+
363+
def test_do_not_clear_cache_if_disabled(pytester: Pytester) -> None:
364+
"""
365+
If pytest is run without --step-wise, do not clear the stepwise cache.
366+
367+
Keeping the cache around is important for this workflow:
368+
369+
1. Run tests with --stepwise
370+
2. Stop at the failing test, and iterate over it changing the code and running it in isolation
371+
(in the IDE for example).
372+
3. Run tests with --stepwise again - at this point we expect to start from the failing test, which should now pass,
373+
and continue with the next tests.
374+
"""
375+
pytester.makepyfile(
376+
"""
377+
def test_1():
378+
pass
379+
def test_2():
380+
assert False
381+
def test_3():
382+
pass
383+
"""
384+
)
385+
result = pytester.runpytest("--stepwise")
386+
result.stdout.fnmatch_lines(
387+
["*::test_2 - assert False*", "*failed, continuing from this test next run*"]
388+
)
389+
390+
# Run a specific test without passing `--stepwise`.
391+
result = pytester.runpytest("-k", "test_1")
392+
result.stdout.fnmatch_lines(["*1 passed*"])
393+
394+
# Running with `--stepwise` should continue from the last failing test.
395+
result = pytester.runpytest("--stepwise")
396+
result.stdout.fnmatch_lines(
397+
["*::test_2 - assert False*", "*failed, continuing from this test next run*"]
398+
)

0 commit comments

Comments
 (0)