Skip to content

Commit 1d4cc7e

Browse files
authored
Merge pull request #7997 from duthades/master
#7942 test_session.py migrate from testdir to Pytester
2 parents 3adece9 + b815f43 commit 1d4cc7e

File tree

2 files changed

+83
-78
lines changed

2 files changed

+83
-78
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,7 @@ Ryan Wooden
261261
Samuel Dion-Girardeau
262262
Samuel Searles-Bryant
263263
Samuele Pedroni
264+
Sanket Duthade
264265
Sankt Petersbug
265266
Segev Finer
266267
Serhii Mozghovyi

testing/test_session.py

Lines changed: 82 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import pytest
22
from _pytest.config import ExitCode
3+
from _pytest.monkeypatch import MonkeyPatch
4+
from _pytest.pytester import Pytester
35

46

57
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(
810
"""
911
def test_one():
1012
pass
@@ -17,7 +19,7 @@ def test_two(self, someargs):
1719
pass
1820
"""
1921
)
20-
reprec = testdir.inline_run(tfile)
22+
reprec = pytester.inline_run(tfile)
2123
passed, skipped, failed = reprec.listoutcomes()
2224
assert len(skipped) == 0
2325
assert len(passed) == 1
@@ -35,8 +37,8 @@ def end(x):
3537
# assert len(colreports) == 4
3638
# assert colreports[1].report.failed
3739

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(
4042
"""
4143
import import_fails
4244
def test_this():
@@ -47,14 +49,14 @@ def test_this():
4749
a = 1
4850
""",
4951
)
50-
reprec = testdir.inline_run(tfile)
52+
reprec = pytester.inline_run(tfile)
5153
values = reprec.getfailedcollections()
5254
assert len(values) == 1
5355
out = str(values[0].longrepr)
5456
assert out.find("does_not_work") != -1
5557

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(
5860
"""
5961
import pytest
6062
def test_raises_doesnt():
@@ -63,18 +65,18 @@ def test_raises_doesnt():
6365
)
6466
passed, skipped, failed = reprec.listoutcomes()
6567
assert len(failed) == 1
66-
out = failed[0].longrepr.reprcrash.message
68+
out = failed[0].longrepr.reprcrash.message # type: ignore[union-attr]
6769
assert "DID NOT RAISE" in out
6870

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")
7173
values = reprec.getfailedcollections()
7274
assert len(values) == 1
7375
out = str(values[0].longrepr)
7476
assert out.find("not python") != -1
7577

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(
7880
"""
7981
def test_one(): assert 0
8082
def test_two(): assert 0
@@ -85,8 +87,8 @@ def test_two(): assert 0
8587
assert failed == 1
8688
assert passed == skipped == 0
8789

88-
def test_maxfail(self, testdir):
89-
reprec = testdir.inline_runsource(
90+
def test_maxfail(self, pytester: Pytester) -> None:
91+
reprec = pytester.inline_runsource(
9092
"""
9193
def test_one(): assert 0
9294
def test_two(): assert 0
@@ -98,8 +100,8 @@ def test_three(): assert 0
98100
assert failed == 2
99101
assert passed == skipped == 0
100102

101-
def test_broken_repr(self, testdir):
102-
p = testdir.makepyfile(
103+
def test_broken_repr(self, pytester: Pytester) -> None:
104+
p = pytester.makepyfile(
103105
"""
104106
import pytest
105107
@@ -124,14 +126,14 @@ def test_implicit_bad_repr1(self):
124126
125127
"""
126128
)
127-
reprec = testdir.inline_run(p)
129+
reprec = pytester.inline_run(p)
128130
passed, skipped, failed = reprec.listoutcomes()
129131
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]
131133
assert out.find("<[reprexc() raised in repr()] BrokenRepr1") != -1
132134

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(
135137
"""
136138
class ObjWithErrorInRepr:
137139
def __repr__(self):
@@ -142,10 +144,10 @@ def test_repr_error():
142144
assert x == "value"
143145
"""
144146
)
145-
reprec = testdir.inline_run("--showlocals", "-vv", p)
147+
reprec = pytester.inline_run("--showlocals", "-vv", p)
146148
passed, skipped, failed = reprec.listoutcomes()
147149
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]
149151
assert len(entries) == 1
150152
repr_locals = entries[0].reprlocals
151153
assert repr_locals.lines
@@ -154,8 +156,8 @@ def test_repr_error():
154156
"x = <[NotImplementedError() raised in repr()] ObjWithErrorInRepr"
155157
)
156158

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(
159161
conftest="""
160162
import pytest
161163
def pytest_collect_file():
@@ -166,7 +168,7 @@ def test_one(): pass
166168
""",
167169
)
168170
try:
169-
reprec = testdir.inline_run(testdir.tmpdir)
171+
reprec = pytester.inline_run(pytester.path)
170172
except pytest.skip.Exception: # pragma: no cover
171173
pytest.fail("wrong skipped caught")
172174
reports = reprec.getreports("pytest_collectreport")
@@ -175,8 +177,8 @@ def test_one(): pass
175177

176178

177179
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(
180182
"""
181183
values = []
182184
def test_1():
@@ -201,8 +203,8 @@ def test_4(self):
201203
assert failed == skipped == 0
202204
assert passed == 7
203205

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(
206208
test_one="""
207209
def test_one():
208210
raise ValueError()
@@ -217,7 +219,7 @@ class TestY(TestX):
217219
test_three="xxxdsadsadsadsa",
218220
__init__="",
219221
)
220-
reprec = testdir.inline_run("--collect-only", p.dirpath())
222+
reprec = pytester.inline_run("--collect-only", p.parent)
221223

222224
itemstarted = reprec.getcalls("pytest_itemcollected")
223225
assert len(itemstarted) == 3
@@ -229,66 +231,66 @@ class TestY(TestX):
229231
colfail = [x for x in finished if x.failed]
230232
assert len(colfail) == 1
231233

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)
236238
finished = reprec.getreports("pytest_collectreport")
237239
colfail = [x for x in finished if x.failed]
238240
assert len(colfail) == 1
239241

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)
244246
finished = reprec.getreports("pytest_collectreport")
245247
colfail = [x for x in finished if x.failed]
246248
assert len(colfail) == 2
247249

248250

249-
def test_plugin_specify(testdir):
251+
def test_plugin_specify(pytester: Pytester) -> None:
250252
with pytest.raises(ImportError):
251-
testdir.parseconfig("-p", "nqweotexistent")
253+
pytester.parseconfig("-p", "nqweotexistent")
252254
# pytest.raises(ImportError,
253255
# "config.do_configure(config)"
254256
# )
255257

256258

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")
259261
assert config.option.plugins == ["terminal"]
260262
config._do_configure()
261263
config._ensure_unconfigure()
262264

263265

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")
271273
assert result.ret == 0
272274
result.stdout.fnmatch_lines(["*1 passed*"])
273275

274276

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*")
286288
assert result.ret == 0
287289
result.stdout.fnmatch_lines(["*1 passed*"])
288290

289291

290-
def test_deselect(testdir):
291-
testdir.makepyfile(
292+
def test_deselect(pytester: Pytester) -> None:
293+
pytester.makepyfile(
292294
test_a="""
293295
import pytest
294296
@@ -303,7 +305,7 @@ def test_c1(self): pass
303305
def test_c2(self): pass
304306
"""
305307
)
306-
result = testdir.runpytest(
308+
result = pytester.runpytest(
307309
"-v",
308310
"--deselect=test_a.py::test_a2[1]",
309311
"--deselect=test_a.py::test_a2[2]",
@@ -315,8 +317,8 @@ def test_c2(self): pass
315317
assert not line.startswith(("test_a.py::test_a2[1]", "test_a.py::test_a2[2]"))
316318

317319

318-
def test_sessionfinish_with_start(testdir):
319-
testdir.makeconftest(
320+
def test_sessionfinish_with_start(pytester: Pytester) -> None:
321+
pytester.makeconftest(
320322
"""
321323
import os
322324
values = []
@@ -329,37 +331,39 @@ def pytest_sessionfinish():
329331
330332
"""
331333
)
332-
res = testdir.runpytest("--collect-only")
334+
res = pytester.runpytest("--collect-only")
333335
assert res.ret == ExitCode.NO_TESTS_COLLECTED
334336

335337

336338
@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(
344348
"""
345349
import os
346350
def test_one():
347351
assert 1
348352
"""
349353
)
350354

351-
result = testdir.runpytest(f"--rootdir={path}")
355+
result = pytester.runpytest(f"--rootdir={path}")
352356
result.stdout.fnmatch_lines(
353357
[
354-
f"*rootdir: {testdir.tmpdir}/root",
358+
f"*rootdir: {pytester.path}/root",
355359
"root/test_rootdir_option_arg.py *",
356360
"*1 passed*",
357361
]
358362
)
359363

360364

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")
363367
result.stderr.fnmatch_lines(
364368
["*Directory *wrong_dir* not found. Check your '--rootdir' option.*"]
365369
)

0 commit comments

Comments
 (0)