1
1
import pytest
2
+ from _pytest .monkeypatch import MonkeyPatch
3
+ from _pytest .pytester import Pytester
2
4
3
5
4
6
@pytest .fixture
5
- def stepwise_testdir ( testdir ) :
7
+ def stepwise_pytester ( pytester : Pytester ) -> Pytester :
6
8
# Rather than having to modify our testfile between tests, we introduce
7
9
# a flag for whether or not the second test should fail.
8
- testdir .makeconftest (
10
+ pytester .makeconftest (
9
11
"""
10
12
def pytest_addoption(parser):
11
13
group = parser.getgroup('general')
@@ -15,7 +17,7 @@ def pytest_addoption(parser):
15
17
)
16
18
17
19
# Create a simple test suite.
18
- testdir .makepyfile (
20
+ pytester .makepyfile (
19
21
test_a = """
20
22
def test_success_before_fail():
21
23
assert 1
@@ -34,27 +36,27 @@ def test_success_after_last_fail():
34
36
"""
35
37
)
36
38
37
- testdir .makepyfile (
39
+ pytester .makepyfile (
38
40
test_b = """
39
41
def test_success():
40
42
assert 1
41
43
"""
42
44
)
43
45
44
46
# 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 (
46
48
"""
47
49
[pytest]
48
50
cache_dir = .cache
49
51
"""
50
52
)
51
53
52
- return testdir
54
+ return pytester
53
55
54
56
55
57
@pytest .fixture
56
- def error_testdir ( testdir ) :
57
- testdir .makepyfile (
58
+ def error_pytester ( pytester : Pytester ) -> Pytester :
59
+ pytester .makepyfile (
58
60
test_a = """
59
61
def test_error(nonexisting_fixture):
60
62
assert 1
@@ -64,15 +66,15 @@ def test_success_after_fail():
64
66
"""
65
67
)
66
68
67
- return testdir
69
+ return pytester
68
70
69
71
70
72
@pytest .fixture
71
- def broken_testdir ( testdir ) :
72
- testdir .makepyfile (
73
+ def broken_pytester ( pytester : Pytester ) -> Pytester :
74
+ pytester .makepyfile (
73
75
working_testfile = "def test_proper(): assert 1" , broken_testfile = "foobar"
74
76
)
75
- return testdir
77
+ return pytester
76
78
77
79
78
80
def _strip_resource_warnings (lines ):
@@ -85,34 +87,33 @@ def _strip_resource_warnings(lines):
85
87
]
86
88
87
89
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" )
91
92
result .stdout .fnmatch_lines (["*test_success_before_fail PASSED*" ])
92
93
result .stdout .fnmatch_lines (["*test_fail_on_flag FAILED*" ])
93
94
result .stdout .fnmatch_lines (["*test_success_after_fail PASSED*" ])
94
95
95
96
96
- def test_stepwise_output_summary (testdir ) :
97
- testdir .makepyfile (
97
+ def test_stepwise_output_summary (pytester : Pytester ) -> None :
98
+ pytester .makepyfile (
98
99
"""
99
100
import pytest
100
101
@pytest.mark.parametrize("expected", [True, True, True, True, False])
101
102
def test_data(expected):
102
103
assert expected
103
104
"""
104
105
)
105
- result = testdir .runpytest ("-v" , "--stepwise" )
106
+ result = pytester .runpytest ("-v" , "--stepwise" )
106
107
result .stdout .fnmatch_lines (["stepwise: no previously failed tests, not skipping." ])
107
- result = testdir .runpytest ("-v" , "--stepwise" )
108
+ result = pytester .runpytest ("-v" , "--stepwise" )
108
109
result .stdout .fnmatch_lines (
109
110
["stepwise: skipping 4 already passed items." , "*1 failed, 4 deselected*" ]
110
111
)
111
112
112
113
113
- def test_fail_and_continue_with_stepwise (stepwise_testdir ) :
114
+ def test_fail_and_continue_with_stepwise (stepwise_pytester : Pytester ) -> None :
114
115
# Run the tests with a failing second test.
115
- result = stepwise_testdir .runpytest (
116
+ result = stepwise_pytester .runpytest (
116
117
"-v" , "--strict-markers" , "--stepwise" , "--fail"
117
118
)
118
119
assert _strip_resource_warnings (result .stderr .lines ) == []
@@ -124,7 +125,7 @@ def test_fail_and_continue_with_stepwise(stepwise_testdir):
124
125
assert "test_success_after_fail" not in stdout
125
126
126
127
# "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" )
128
129
assert _strip_resource_warnings (result .stderr .lines ) == []
129
130
130
131
stdout = result .stdout .str ()
@@ -135,8 +136,8 @@ def test_fail_and_continue_with_stepwise(stepwise_testdir):
135
136
136
137
137
138
@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 (
140
141
"-v" , "--strict-markers" , "--stepwise" , stepwise_skip , "--fail" , "--fail-last" ,
141
142
)
142
143
assert _strip_resource_warnings (result .stderr .lines ) == []
@@ -149,8 +150,8 @@ def test_run_with_skip_option(stepwise_testdir, stepwise_skip):
149
150
assert "test_success_after_last_fail" not in stdout
150
151
151
152
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" )
154
155
155
156
assert _strip_resource_warnings (result .stderr .lines ) == []
156
157
stdout = result .stdout .str ()
@@ -159,8 +160,8 @@ def test_fail_on_errors(error_testdir):
159
160
assert "test_success_after_fail" not in stdout
160
161
161
162
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 (
164
165
"-v" , "--strict-markers" , "--stepwise" , "--fail" , "test_a.py"
165
166
)
166
167
assert _strip_resource_warnings (result .stderr .lines ) == []
@@ -170,7 +171,7 @@ def test_change_testfile(stepwise_testdir):
170
171
171
172
# Make sure the second test run starts from the beginning, since the
172
173
# test to continue from does not exist in testfile_b.
173
- result = stepwise_testdir .runpytest (
174
+ result = stepwise_pytester .runpytest (
174
175
"-v" , "--strict-markers" , "--stepwise" , "test_b.py"
175
176
)
176
177
assert _strip_resource_warnings (result .stderr .lines ) == []
@@ -180,17 +181,19 @@ def test_change_testfile(stepwise_testdir):
180
181
181
182
182
183
@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 :
184
187
"""Stop during collection errors. Broken test first or broken test last
185
188
actually surfaced a bug (#5444), so we test both situations."""
186
189
files = ["working_testfile.py" , "broken_testfile.py" ]
187
190
if broken_first :
188
191
files .reverse ()
189
- result = broken_testdir .runpytest ("-v" , "--strict-markers" , "--stepwise" , * files )
192
+ result = broken_pytester .runpytest ("-v" , "--strict-markers" , "--stepwise" , * files )
190
193
result .stdout .fnmatch_lines ("*error during collection*" )
191
194
192
195
193
- def test_xfail_handling (testdir , monkeypatch ) :
196
+ def test_xfail_handling (pytester : Pytester , monkeypatch : MonkeyPatch ) -> None :
194
197
"""Ensure normal xfail is ignored, and strict xfail interrupts the session in sw mode
195
198
196
199
(#5547)
@@ -207,8 +210,8 @@ def test_b(): assert {assert_value}
207
210
def test_c(): pass
208
211
def test_d(): pass
209
212
"""
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" )
212
215
result .stdout .fnmatch_lines (
213
216
[
214
217
"*::test_a PASSED *" ,
@@ -219,8 +222,8 @@ def test_d(): pass
219
222
]
220
223
)
221
224
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" )
224
227
result .stdout .fnmatch_lines (
225
228
[
226
229
"*::test_a PASSED *" ,
@@ -230,8 +233,8 @@ def test_d(): pass
230
233
]
231
234
)
232
235
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" )
235
238
result .stdout .fnmatch_lines (
236
239
[
237
240
"*::test_b XFAIL *" ,
0 commit comments