1
1
import pytest
2
2
from _pytest .config import ExitCode
3
+ from _pytest .monkeypatch import MonkeyPatch
4
+ from _pytest .pytester import Pytester
3
5
4
6
5
7
class SessionTests :
6
- def test_basic_testitem_events (self , testdir ) :
7
- tfile = testdir .makepyfile (
8
+ def test_basic_testitem_events (self , pytester : Pytester ) -> None :
9
+ tfile = pytester .makepyfile (
8
10
"""
9
11
def test_one():
10
12
pass
@@ -17,7 +19,7 @@ def test_two(self, someargs):
17
19
pass
18
20
"""
19
21
)
20
- reprec = testdir .inline_run (tfile )
22
+ reprec = pytester .inline_run (tfile )
21
23
passed , skipped , failed = reprec .listoutcomes ()
22
24
assert len (skipped ) == 0
23
25
assert len (passed ) == 1
@@ -35,8 +37,8 @@ def end(x):
35
37
# assert len(colreports) == 4
36
38
# assert colreports[1].report.failed
37
39
38
- def test_nested_import_error (self , testdir ) :
39
- tfile = testdir .makepyfile (
40
+ def test_nested_import_error (self , pytester : Pytester ) -> None :
41
+ tfile = pytester .makepyfile (
40
42
"""
41
43
import import_fails
42
44
def test_this():
@@ -47,14 +49,14 @@ def test_this():
47
49
a = 1
48
50
""" ,
49
51
)
50
- reprec = testdir .inline_run (tfile )
52
+ reprec = pytester .inline_run (tfile )
51
53
values = reprec .getfailedcollections ()
52
54
assert len (values ) == 1
53
55
out = str (values [0 ].longrepr )
54
56
assert out .find ("does_not_work" ) != - 1
55
57
56
- def test_raises_output (self , testdir ) :
57
- reprec = testdir .inline_runsource (
58
+ def test_raises_output (self , pytester : Pytester ) -> None :
59
+ reprec = pytester .inline_runsource (
58
60
"""
59
61
import pytest
60
62
def test_raises_doesnt():
@@ -63,18 +65,18 @@ def test_raises_doesnt():
63
65
)
64
66
passed , skipped , failed = reprec .listoutcomes ()
65
67
assert len (failed ) == 1
66
- out = failed [0 ].longrepr .reprcrash .message
68
+ out = failed [0 ].longrepr .reprcrash .message # type: ignore[union-attr]
67
69
assert "DID NOT RAISE" in out
68
70
69
- def test_syntax_error_module (self , testdir ) :
70
- reprec = testdir .inline_runsource ("this is really not python" )
71
+ def test_syntax_error_module (self , pytester : Pytester ) -> None :
72
+ reprec = pytester .inline_runsource ("this is really not python" )
71
73
values = reprec .getfailedcollections ()
72
74
assert len (values ) == 1
73
75
out = str (values [0 ].longrepr )
74
76
assert out .find ("not python" ) != - 1
75
77
76
- def test_exit_first_problem (self , testdir ) :
77
- reprec = testdir .inline_runsource (
78
+ def test_exit_first_problem (self , pytester : Pytester ) -> None :
79
+ reprec = pytester .inline_runsource (
78
80
"""
79
81
def test_one(): assert 0
80
82
def test_two(): assert 0
@@ -85,8 +87,8 @@ def test_two(): assert 0
85
87
assert failed == 1
86
88
assert passed == skipped == 0
87
89
88
- def test_maxfail (self , testdir ) :
89
- reprec = testdir .inline_runsource (
90
+ def test_maxfail (self , pytester : Pytester ) -> None :
91
+ reprec = pytester .inline_runsource (
90
92
"""
91
93
def test_one(): assert 0
92
94
def test_two(): assert 0
@@ -98,8 +100,8 @@ def test_three(): assert 0
98
100
assert failed == 2
99
101
assert passed == skipped == 0
100
102
101
- def test_broken_repr (self , testdir ) :
102
- p = testdir .makepyfile (
103
+ def test_broken_repr (self , pytester : Pytester ) -> None :
104
+ p = pytester .makepyfile (
103
105
"""
104
106
import pytest
105
107
@@ -124,14 +126,14 @@ def test_implicit_bad_repr1(self):
124
126
125
127
"""
126
128
)
127
- reprec = testdir .inline_run (p )
129
+ reprec = pytester .inline_run (p )
128
130
passed , skipped , failed = reprec .listoutcomes ()
129
131
assert (len (passed ), len (skipped ), len (failed )) == (1 , 0 , 1 )
130
- out = failed [0 ].longrepr .reprcrash .message
132
+ out = failed [0 ].longrepr .reprcrash .message # type: ignore[union-attr]
131
133
assert out .find ("<[reprexc() raised in repr()] BrokenRepr1" ) != - 1
132
134
133
- def test_broken_repr_with_showlocals_verbose (self , testdir ) :
134
- p = testdir .makepyfile (
135
+ def test_broken_repr_with_showlocals_verbose (self , pytester : Pytester ) -> None :
136
+ p = pytester .makepyfile (
135
137
"""
136
138
class ObjWithErrorInRepr:
137
139
def __repr__(self):
@@ -142,10 +144,10 @@ def test_repr_error():
142
144
assert x == "value"
143
145
"""
144
146
)
145
- reprec = testdir .inline_run ("--showlocals" , "-vv" , p )
147
+ reprec = pytester .inline_run ("--showlocals" , "-vv" , p )
146
148
passed , skipped , failed = reprec .listoutcomes ()
147
149
assert (len (passed ), len (skipped ), len (failed )) == (0 , 0 , 1 )
148
- entries = failed [0 ].longrepr .reprtraceback .reprentries
150
+ entries = failed [0 ].longrepr .reprtraceback .reprentries # type: ignore[union-attr]
149
151
assert len (entries ) == 1
150
152
repr_locals = entries [0 ].reprlocals
151
153
assert repr_locals .lines
@@ -154,8 +156,8 @@ def test_repr_error():
154
156
"x = <[NotImplementedError() raised in repr()] ObjWithErrorInRepr"
155
157
)
156
158
157
- def test_skip_file_by_conftest (self , testdir ) :
158
- testdir .makepyfile (
159
+ def test_skip_file_by_conftest (self , pytester : Pytester ) -> None :
160
+ pytester .makepyfile (
159
161
conftest = """
160
162
import pytest
161
163
def pytest_collect_file():
@@ -166,7 +168,7 @@ def test_one(): pass
166
168
""" ,
167
169
)
168
170
try :
169
- reprec = testdir .inline_run (testdir . tmpdir )
171
+ reprec = pytester .inline_run (pytester . path )
170
172
except pytest .skip .Exception : # pragma: no cover
171
173
pytest .fail ("wrong skipped caught" )
172
174
reports = reprec .getreports ("pytest_collectreport" )
@@ -175,8 +177,8 @@ def test_one(): pass
175
177
176
178
177
179
class TestNewSession (SessionTests ):
178
- def test_order_of_execution (self , testdir ) :
179
- reprec = testdir .inline_runsource (
180
+ def test_order_of_execution (self , pytester : Pytester ) -> None :
181
+ reprec = pytester .inline_runsource (
180
182
"""
181
183
values = []
182
184
def test_1():
@@ -201,8 +203,8 @@ def test_4(self):
201
203
assert failed == skipped == 0
202
204
assert passed == 7
203
205
204
- def test_collect_only_with_various_situations (self , testdir ) :
205
- p = testdir .makepyfile (
206
+ def test_collect_only_with_various_situations (self , pytester : Pytester ) -> None :
207
+ p = pytester .makepyfile (
206
208
test_one = """
207
209
def test_one():
208
210
raise ValueError()
@@ -217,7 +219,7 @@ class TestY(TestX):
217
219
test_three = "xxxdsadsadsadsa" ,
218
220
__init__ = "" ,
219
221
)
220
- reprec = testdir .inline_run ("--collect-only" , p .dirpath () )
222
+ reprec = pytester .inline_run ("--collect-only" , p .parent )
221
223
222
224
itemstarted = reprec .getcalls ("pytest_itemcollected" )
223
225
assert len (itemstarted ) == 3
@@ -229,66 +231,66 @@ class TestY(TestX):
229
231
colfail = [x for x in finished if x .failed ]
230
232
assert len (colfail ) == 1
231
233
232
- def test_minus_x_import_error (self , testdir ) :
233
- testdir .makepyfile (__init__ = "" )
234
- testdir .makepyfile (test_one = "xxxx" , test_two = "yyyy" )
235
- reprec = testdir .inline_run ("-x" , testdir . tmpdir )
234
+ def test_minus_x_import_error (self , pytester : Pytester ) -> None :
235
+ pytester .makepyfile (__init__ = "" )
236
+ pytester .makepyfile (test_one = "xxxx" , test_two = "yyyy" )
237
+ reprec = pytester .inline_run ("-x" , pytester . path )
236
238
finished = reprec .getreports ("pytest_collectreport" )
237
239
colfail = [x for x in finished if x .failed ]
238
240
assert len (colfail ) == 1
239
241
240
- def test_minus_x_overridden_by_maxfail (self , testdir ) :
241
- testdir .makepyfile (__init__ = "" )
242
- testdir .makepyfile (test_one = "xxxx" , test_two = "yyyy" , test_third = "zzz" )
243
- reprec = testdir .inline_run ("-x" , "--maxfail=2" , testdir . tmpdir )
242
+ def test_minus_x_overridden_by_maxfail (self , pytester : Pytester ) -> None :
243
+ pytester .makepyfile (__init__ = "" )
244
+ pytester .makepyfile (test_one = "xxxx" , test_two = "yyyy" , test_third = "zzz" )
245
+ reprec = pytester .inline_run ("-x" , "--maxfail=2" , pytester . path )
244
246
finished = reprec .getreports ("pytest_collectreport" )
245
247
colfail = [x for x in finished if x .failed ]
246
248
assert len (colfail ) == 2
247
249
248
250
249
- def test_plugin_specify (testdir ) :
251
+ def test_plugin_specify (pytester : Pytester ) -> None :
250
252
with pytest .raises (ImportError ):
251
- testdir .parseconfig ("-p" , "nqweotexistent" )
253
+ pytester .parseconfig ("-p" , "nqweotexistent" )
252
254
# pytest.raises(ImportError,
253
255
# "config.do_configure(config)"
254
256
# )
255
257
256
258
257
- def test_plugin_already_exists (testdir ) :
258
- config = testdir .parseconfig ("-p" , "terminal" )
259
+ def test_plugin_already_exists (pytester : Pytester ) -> None :
260
+ config = pytester .parseconfig ("-p" , "terminal" )
259
261
assert config .option .plugins == ["terminal" ]
260
262
config ._do_configure ()
261
263
config ._ensure_unconfigure ()
262
264
263
265
264
- def test_exclude (testdir ) :
265
- hellodir = testdir .mkdir ("hello" )
266
- hellodir .join ("test_hello.py" ).write ("x y syntaxerror" )
267
- hello2dir = testdir .mkdir ("hello2" )
268
- hello2dir .join ("test_hello2.py" ).write ("x y syntaxerror" )
269
- testdir .makepyfile (test_ok = "def test_pass(): pass" )
270
- result = testdir .runpytest ("--ignore=hello" , "--ignore=hello2" )
266
+ def test_exclude (pytester : Pytester ) -> None :
267
+ hellodir = pytester .mkdir ("hello" )
268
+ hellodir .joinpath ("test_hello.py" ).write_text ("x y syntaxerror" )
269
+ hello2dir = pytester .mkdir ("hello2" )
270
+ hello2dir .joinpath ("test_hello2.py" ).write_text ("x y syntaxerror" )
271
+ pytester .makepyfile (test_ok = "def test_pass(): pass" )
272
+ result = pytester .runpytest ("--ignore=hello" , "--ignore=hello2" )
271
273
assert result .ret == 0
272
274
result .stdout .fnmatch_lines (["*1 passed*" ])
273
275
274
276
275
- def test_exclude_glob (testdir ) :
276
- hellodir = testdir .mkdir ("hello" )
277
- hellodir .join ("test_hello.py" ).write ("x y syntaxerror" )
278
- hello2dir = testdir .mkdir ("hello2" )
279
- hello2dir .join ("test_hello2.py" ).write ("x y syntaxerror" )
280
- hello3dir = testdir .mkdir ("hallo3" )
281
- hello3dir .join ("test_hello3.py" ).write ("x y syntaxerror" )
282
- subdir = testdir .mkdir ("sub" )
283
- subdir .join ("test_hello4.py" ).write ("x y syntaxerror" )
284
- testdir .makepyfile (test_ok = "def test_pass(): pass" )
285
- result = testdir .runpytest ("--ignore-glob=*h[ea]llo*" )
277
+ def test_exclude_glob (pytester : Pytester ) -> None :
278
+ hellodir = pytester .mkdir ("hello" )
279
+ hellodir .joinpath ("test_hello.py" ).write_text ("x y syntaxerror" )
280
+ hello2dir = pytester .mkdir ("hello2" )
281
+ hello2dir .joinpath ("test_hello2.py" ).write_text ("x y syntaxerror" )
282
+ hello3dir = pytester .mkdir ("hallo3" )
283
+ hello3dir .joinpath ("test_hello3.py" ).write_text ("x y syntaxerror" )
284
+ subdir = pytester .mkdir ("sub" )
285
+ subdir .joinpath ("test_hello4.py" ).write_text ("x y syntaxerror" )
286
+ pytester .makepyfile (test_ok = "def test_pass(): pass" )
287
+ result = pytester .runpytest ("--ignore-glob=*h[ea]llo*" )
286
288
assert result .ret == 0
287
289
result .stdout .fnmatch_lines (["*1 passed*" ])
288
290
289
291
290
- def test_deselect (testdir ) :
291
- testdir .makepyfile (
292
+ def test_deselect (pytester : Pytester ) -> None :
293
+ pytester .makepyfile (
292
294
test_a = """
293
295
import pytest
294
296
@@ -303,7 +305,7 @@ def test_c1(self): pass
303
305
def test_c2(self): pass
304
306
"""
305
307
)
306
- result = testdir .runpytest (
308
+ result = pytester .runpytest (
307
309
"-v" ,
308
310
"--deselect=test_a.py::test_a2[1]" ,
309
311
"--deselect=test_a.py::test_a2[2]" ,
@@ -315,8 +317,8 @@ def test_c2(self): pass
315
317
assert not line .startswith (("test_a.py::test_a2[1]" , "test_a.py::test_a2[2]" ))
316
318
317
319
318
- def test_sessionfinish_with_start (testdir ) :
319
- testdir .makeconftest (
320
+ def test_sessionfinish_with_start (pytester : Pytester ) -> None :
321
+ pytester .makeconftest (
320
322
"""
321
323
import os
322
324
values = []
@@ -329,37 +331,39 @@ def pytest_sessionfinish():
329
331
330
332
"""
331
333
)
332
- res = testdir .runpytest ("--collect-only" )
334
+ res = pytester .runpytest ("--collect-only" )
333
335
assert res .ret == ExitCode .NO_TESTS_COLLECTED
334
336
335
337
336
338
@pytest .mark .parametrize ("path" , ["root" , "{relative}/root" , "{environment}/root" ])
337
- def test_rootdir_option_arg (testdir , monkeypatch , path ):
338
- monkeypatch .setenv ("PY_ROOTDIR_PATH" , str (testdir .tmpdir ))
339
- path = path .format (relative = str (testdir .tmpdir ), environment = "$PY_ROOTDIR_PATH" )
340
-
341
- rootdir = testdir .mkdir ("root" )
342
- rootdir .mkdir ("tests" )
343
- testdir .makepyfile (
339
+ def test_rootdir_option_arg (
340
+ pytester : Pytester , monkeypatch : MonkeyPatch , path : str
341
+ ) -> None :
342
+ monkeypatch .setenv ("PY_ROOTDIR_PATH" , str (pytester .path ))
343
+ path = path .format (relative = str (pytester .path ), environment = "$PY_ROOTDIR_PATH" )
344
+
345
+ rootdir = pytester .path / "root" / "tests"
346
+ rootdir .mkdir (parents = True )
347
+ pytester .makepyfile (
344
348
"""
345
349
import os
346
350
def test_one():
347
351
assert 1
348
352
"""
349
353
)
350
354
351
- result = testdir .runpytest (f"--rootdir={ path } " )
355
+ result = pytester .runpytest (f"--rootdir={ path } " )
352
356
result .stdout .fnmatch_lines (
353
357
[
354
- f"*rootdir: { testdir . tmpdir } /root" ,
358
+ f"*rootdir: { pytester . path } /root" ,
355
359
"root/test_rootdir_option_arg.py *" ,
356
360
"*1 passed*" ,
357
361
]
358
362
)
359
363
360
364
361
- def test_rootdir_wrong_option_arg (testdir ) :
362
- result = testdir .runpytest ("--rootdir=wrong_dir" )
365
+ def test_rootdir_wrong_option_arg (pytester : Pytester ) -> None :
366
+ result = pytester .runpytest ("--rootdir=wrong_dir" )
363
367
result .stderr .fnmatch_lines (
364
368
["*Directory *wrong_dir* not found. Check your '--rootdir' option.*" ]
365
369
)
0 commit comments