Skip to content

Commit ad94456

Browse files
authored
Merge pull request #7976 from symonk/7942-refactor-stepwise-to-use-pytester
#7942 refactor stepwise tests to utilize pytester
2 parents aa84374 + c58abf7 commit ad94456

File tree

1 file changed

+41
-38
lines changed

1 file changed

+41
-38
lines changed

testing/test_stepwise.py

Lines changed: 41 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import pytest
2+
from _pytest.monkeypatch import MonkeyPatch
3+
from _pytest.pytester import Pytester
24

35

46
@pytest.fixture
5-
def stepwise_testdir(testdir):
7+
def stepwise_pytester(pytester: Pytester) -> Pytester:
68
# Rather than having to modify our testfile between tests, we introduce
79
# a flag for whether or not the second test should fail.
8-
testdir.makeconftest(
10+
pytester.makeconftest(
911
"""
1012
def pytest_addoption(parser):
1113
group = parser.getgroup('general')
@@ -15,7 +17,7 @@ def pytest_addoption(parser):
1517
)
1618

1719
# Create a simple test suite.
18-
testdir.makepyfile(
20+
pytester.makepyfile(
1921
test_a="""
2022
def test_success_before_fail():
2123
assert 1
@@ -34,27 +36,27 @@ def test_success_after_last_fail():
3436
"""
3537
)
3638

37-
testdir.makepyfile(
39+
pytester.makepyfile(
3840
test_b="""
3941
def test_success():
4042
assert 1
4143
"""
4244
)
4345

4446
# customize cache directory so we don't use the tox's cache directory, which makes tests in this module flaky
45-
testdir.makeini(
47+
pytester.makeini(
4648
"""
4749
[pytest]
4850
cache_dir = .cache
4951
"""
5052
)
5153

52-
return testdir
54+
return pytester
5355

5456

5557
@pytest.fixture
56-
def error_testdir(testdir):
57-
testdir.makepyfile(
58+
def error_pytester(pytester: Pytester) -> Pytester:
59+
pytester.makepyfile(
5860
test_a="""
5961
def test_error(nonexisting_fixture):
6062
assert 1
@@ -64,15 +66,15 @@ def test_success_after_fail():
6466
"""
6567
)
6668

67-
return testdir
69+
return pytester
6870

6971

7072
@pytest.fixture
71-
def broken_testdir(testdir):
72-
testdir.makepyfile(
73+
def broken_pytester(pytester: Pytester) -> Pytester:
74+
pytester.makepyfile(
7375
working_testfile="def test_proper(): assert 1", broken_testfile="foobar"
7476
)
75-
return testdir
77+
return pytester
7678

7779

7880
def _strip_resource_warnings(lines):
@@ -85,34 +87,33 @@ def _strip_resource_warnings(lines):
8587
]
8688

8789

88-
def test_run_without_stepwise(stepwise_testdir):
89-
result = stepwise_testdir.runpytest("-v", "--strict-markers", "--fail")
90-
90+
def test_run_without_stepwise(stepwise_pytester: Pytester) -> None:
91+
result = stepwise_pytester.runpytest("-v", "--strict-markers", "--fail")
9192
result.stdout.fnmatch_lines(["*test_success_before_fail PASSED*"])
9293
result.stdout.fnmatch_lines(["*test_fail_on_flag FAILED*"])
9394
result.stdout.fnmatch_lines(["*test_success_after_fail PASSED*"])
9495

9596

96-
def test_stepwise_output_summary(testdir):
97-
testdir.makepyfile(
97+
def test_stepwise_output_summary(pytester: Pytester) -> None:
98+
pytester.makepyfile(
9899
"""
99100
import pytest
100101
@pytest.mark.parametrize("expected", [True, True, True, True, False])
101102
def test_data(expected):
102103
assert expected
103104
"""
104105
)
105-
result = testdir.runpytest("-v", "--stepwise")
106+
result = pytester.runpytest("-v", "--stepwise")
106107
result.stdout.fnmatch_lines(["stepwise: no previously failed tests, not skipping."])
107-
result = testdir.runpytest("-v", "--stepwise")
108+
result = pytester.runpytest("-v", "--stepwise")
108109
result.stdout.fnmatch_lines(
109110
["stepwise: skipping 4 already passed items.", "*1 failed, 4 deselected*"]
110111
)
111112

112113

113-
def test_fail_and_continue_with_stepwise(stepwise_testdir):
114+
def test_fail_and_continue_with_stepwise(stepwise_pytester: Pytester) -> None:
114115
# Run the tests with a failing second test.
115-
result = stepwise_testdir.runpytest(
116+
result = stepwise_pytester.runpytest(
116117
"-v", "--strict-markers", "--stepwise", "--fail"
117118
)
118119
assert _strip_resource_warnings(result.stderr.lines) == []
@@ -124,7 +125,7 @@ def test_fail_and_continue_with_stepwise(stepwise_testdir):
124125
assert "test_success_after_fail" not in stdout
125126

126127
# "Fix" the test that failed in the last run and run it again.
127-
result = stepwise_testdir.runpytest("-v", "--strict-markers", "--stepwise")
128+
result = stepwise_pytester.runpytest("-v", "--strict-markers", "--stepwise")
128129
assert _strip_resource_warnings(result.stderr.lines) == []
129130

130131
stdout = result.stdout.str()
@@ -135,8 +136,8 @@ def test_fail_and_continue_with_stepwise(stepwise_testdir):
135136

136137

137138
@pytest.mark.parametrize("stepwise_skip", ["--stepwise-skip", "--sw-skip"])
138-
def test_run_with_skip_option(stepwise_testdir, stepwise_skip):
139-
result = stepwise_testdir.runpytest(
139+
def test_run_with_skip_option(stepwise_pytester: Pytester, stepwise_skip: str) -> None:
140+
result = stepwise_pytester.runpytest(
140141
"-v", "--strict-markers", "--stepwise", stepwise_skip, "--fail", "--fail-last",
141142
)
142143
assert _strip_resource_warnings(result.stderr.lines) == []
@@ -149,8 +150,8 @@ def test_run_with_skip_option(stepwise_testdir, stepwise_skip):
149150
assert "test_success_after_last_fail" not in stdout
150151

151152

152-
def test_fail_on_errors(error_testdir):
153-
result = error_testdir.runpytest("-v", "--strict-markers", "--stepwise")
153+
def test_fail_on_errors(error_pytester: Pytester) -> None:
154+
result = error_pytester.runpytest("-v", "--strict-markers", "--stepwise")
154155

155156
assert _strip_resource_warnings(result.stderr.lines) == []
156157
stdout = result.stdout.str()
@@ -159,8 +160,8 @@ def test_fail_on_errors(error_testdir):
159160
assert "test_success_after_fail" not in stdout
160161

161162

162-
def test_change_testfile(stepwise_testdir):
163-
result = stepwise_testdir.runpytest(
163+
def test_change_testfile(stepwise_pytester: Pytester) -> None:
164+
result = stepwise_pytester.runpytest(
164165
"-v", "--strict-markers", "--stepwise", "--fail", "test_a.py"
165166
)
166167
assert _strip_resource_warnings(result.stderr.lines) == []
@@ -170,7 +171,7 @@ def test_change_testfile(stepwise_testdir):
170171

171172
# Make sure the second test run starts from the beginning, since the
172173
# test to continue from does not exist in testfile_b.
173-
result = stepwise_testdir.runpytest(
174+
result = stepwise_pytester.runpytest(
174175
"-v", "--strict-markers", "--stepwise", "test_b.py"
175176
)
176177
assert _strip_resource_warnings(result.stderr.lines) == []
@@ -180,17 +181,19 @@ def test_change_testfile(stepwise_testdir):
180181

181182

182183
@pytest.mark.parametrize("broken_first", [True, False])
183-
def test_stop_on_collection_errors(broken_testdir, broken_first):
184+
def test_stop_on_collection_errors(
185+
broken_pytester: Pytester, broken_first: bool
186+
) -> None:
184187
"""Stop during collection errors. Broken test first or broken test last
185188
actually surfaced a bug (#5444), so we test both situations."""
186189
files = ["working_testfile.py", "broken_testfile.py"]
187190
if broken_first:
188191
files.reverse()
189-
result = broken_testdir.runpytest("-v", "--strict-markers", "--stepwise", *files)
192+
result = broken_pytester.runpytest("-v", "--strict-markers", "--stepwise", *files)
190193
result.stdout.fnmatch_lines("*error during collection*")
191194

192195

193-
def test_xfail_handling(testdir, monkeypatch):
196+
def test_xfail_handling(pytester: Pytester, monkeypatch: MonkeyPatch) -> None:
194197
"""Ensure normal xfail is ignored, and strict xfail interrupts the session in sw mode
195198
196199
(#5547)
@@ -207,8 +210,8 @@ def test_b(): assert {assert_value}
207210
def test_c(): pass
208211
def test_d(): pass
209212
"""
210-
testdir.makepyfile(contents.format(assert_value="0", strict="False"))
211-
result = testdir.runpytest("--sw", "-v")
213+
pytester.makepyfile(contents.format(assert_value="0", strict="False"))
214+
result = pytester.runpytest("--sw", "-v")
212215
result.stdout.fnmatch_lines(
213216
[
214217
"*::test_a PASSED *",
@@ -219,8 +222,8 @@ def test_d(): pass
219222
]
220223
)
221224

222-
testdir.makepyfile(contents.format(assert_value="1", strict="True"))
223-
result = testdir.runpytest("--sw", "-v")
225+
pytester.makepyfile(contents.format(assert_value="1", strict="True"))
226+
result = pytester.runpytest("--sw", "-v")
224227
result.stdout.fnmatch_lines(
225228
[
226229
"*::test_a PASSED *",
@@ -230,8 +233,8 @@ def test_d(): pass
230233
]
231234
)
232235

233-
testdir.makepyfile(contents.format(assert_value="0", strict="True"))
234-
result = testdir.runpytest("--sw", "-v")
236+
pytester.makepyfile(contents.format(assert_value="0", strict="True"))
237+
result = pytester.runpytest("--sw", "-v")
235238
result.stdout.fnmatch_lines(
236239
[
237240
"*::test_b XFAIL *",

0 commit comments

Comments
 (0)