| 1 |
# mypy: allow-untyped-defs |
| 2 |
from __future__ import annotations |
| 3 |
|
| 4 |
from _pytest.config import ExitCode |
| 5 |
from _pytest.monkeypatch import MonkeyPatch |
| 6 |
from _pytest.pytester import Pytester |
| 7 |
import pytest |
| 8 |
|
| 9 |
|
| 10 |
class SessionTests: |
| 11 |
def test_basic_testitem_events(self, pytester: Pytester) -> None: |
| 12 |
tfile = pytester.makepyfile( |
| 13 |
""" |
| 14 |
def test_one(): |
| 15 |
pass |
| 16 |
def test_one_one(): |
| 17 |
assert 0 |
| 18 |
def test_other(): |
| 19 |
raise ValueError(23) |
| 20 |
class TestClass(object): |
| 21 |
def test_two(self, someargs): |
| 22 |
pass |
| 23 |
""" |
| 24 |
) |
| 25 |
reprec = pytester.inline_run(tfile) |
| 26 |
passed, skipped, failed = reprec.listoutcomes() |
| 27 |
assert len(skipped) == 0 |
| 28 |
assert len(passed) == 1 |
| 29 |
assert len(failed) == 3 |
| 30 |
|
| 31 |
def end(x): |
| 32 |
return x.nodeid.split("::")[-1] |
| 33 |
|
| 34 |
assert end(failed[0]) == "test_one_one" |
| 35 |
assert end(failed[1]) == "test_other" |
| 36 |
itemstarted = reprec.getcalls("pytest_itemcollected") |
| 37 |
assert len(itemstarted) == 4 |
| 38 |
# XXX check for failing funcarg setup |
| 39 |
# colreports = reprec.getcalls("pytest_collectreport") |
| 40 |
# assert len(colreports) == 4 |
| 41 |
# assert colreports[1].report.failed |
| 42 |
|
| 43 |
def test_nested_import_error(self, pytester: Pytester) -> None: |
| 44 |
tfile = pytester.makepyfile( |
| 45 |
""" |
| 46 |
import import_fails |
| 47 |
def test_this(): |
| 48 |
assert import_fails.a == 1 |
| 49 |
""", |
| 50 |
import_fails=""" |
| 51 |
import does_not_work |
| 52 |
a = 1 |
| 53 |
""", |
| 54 |
) |
| 55 |
reprec = pytester.inline_run(tfile) |
| 56 |
values = reprec.getfailedcollections() |
| 57 |
assert len(values) == 1 |
| 58 |
out = str(values[0].longrepr) |
| 59 |
assert out.find("does_not_work") != -1 |
| 60 |
|
| 61 |
def test_raises_output(self, pytester: Pytester) -> None: |
| 62 |
reprec = pytester.inline_runsource( |
| 63 |
""" |
| 64 |
import pytest |
| 65 |
def test_raises_doesnt(): |
| 66 |
with pytest.raises(ValueError): |
| 67 |
int("3") |
| 68 |
""" |
| 69 |
) |
| 70 |
_passed, _skipped, failed = reprec.listoutcomes() |
| 71 |
assert len(failed) == 1 |
| 72 |
out = failed[0].longrepr.reprcrash.message # type: ignore[union-attr] |
| 73 |
assert "DID NOT RAISE" in out |
| 74 |
|
| 75 |
def test_syntax_error_module(self, pytester: Pytester) -> None: |
| 76 |
reprec = pytester.inline_runsource("this is really not python") |
| 77 |
values = reprec.getfailedcollections() |
| 78 |
assert len(values) == 1 |
| 79 |
out = str(values[0].longrepr) |
| 80 |
assert out.find("not python") != -1 |
| 81 |
|
| 82 |
def test_exit_first_problem(self, pytester: Pytester) -> None: |
| 83 |
reprec = pytester.inline_runsource( |
| 84 |
""" |
| 85 |
def test_one(): assert 0 |
| 86 |
def test_two(): assert 0 |
| 87 |
""", |
| 88 |
"--exitfirst", |
| 89 |
) |
| 90 |
passed, skipped, failed = reprec.countoutcomes() |
| 91 |
assert failed == 1 |
| 92 |
assert passed == skipped == 0 |
| 93 |
|
| 94 |
def test_maxfail(self, pytester: Pytester) -> None: |
| 95 |
reprec = pytester.inline_runsource( |
| 96 |
""" |
| 97 |
def test_one(): assert 0 |
| 98 |
def test_two(): assert 0 |
| 99 |
def test_three(): assert 0 |
| 100 |
""", |
| 101 |
"--maxfail=2", |
| 102 |
) |
| 103 |
passed, skipped, failed = reprec.countoutcomes() |
| 104 |
assert failed == 2 |
| 105 |
assert passed == skipped == 0 |
| 106 |
|
| 107 |
def test_broken_repr(self, pytester: Pytester) -> None: |
| 108 |
p = pytester.makepyfile( |
| 109 |
""" |
| 110 |
import pytest |
| 111 |
|
| 112 |
class reprexc(BaseException): |
| 113 |
def __str__(self): |
| 114 |
return "Ha Ha fooled you, I'm a broken repr()." |
| 115 |
|
| 116 |
class BrokenRepr1(object): |
| 117 |
foo=0 |
| 118 |
def __repr__(self): |
| 119 |
raise reprexc |
| 120 |
|
| 121 |
class TestBrokenClass(object): |
| 122 |
def test_explicit_bad_repr(self): |
| 123 |
t = BrokenRepr1() |
| 124 |
with pytest.raises(BaseException, match="broken repr"): |
| 125 |
repr(t) |
| 126 |
|
| 127 |
def test_implicit_bad_repr1(self): |
| 128 |
t = BrokenRepr1() |
| 129 |
assert t.foo == 1 |
| 130 |
|
| 131 |
""" |
| 132 |
) |
| 133 |
reprec = pytester.inline_run(p) |
| 134 |
passed, skipped, failed = reprec.listoutcomes() |
| 135 |
assert (len(passed), len(skipped), len(failed)) == (1, 0, 1) |
| 136 |
out = failed[0].longrepr.reprcrash.message # type: ignore[union-attr] |
| 137 |
assert out.find("<[reprexc() raised in repr()] BrokenRepr1") != -1 |
| 138 |
|
| 139 |
def test_broken_repr_with_showlocals_verbose(self, pytester: Pytester) -> None: |
| 140 |
p = pytester.makepyfile( |
| 141 |
""" |
| 142 |
class ObjWithErrorInRepr: |
| 143 |
def __repr__(self): |
| 144 |
raise NotImplementedError |
| 145 |
|
| 146 |
def test_repr_error(): |
| 147 |
x = ObjWithErrorInRepr() |
| 148 |
assert x == "value" |
| 149 |
""" |
| 150 |
) |
| 151 |
reprec = pytester.inline_run("--showlocals", "-vv", p) |
| 152 |
passed, skipped, failed = reprec.listoutcomes() |
| 153 |
assert (len(passed), len(skipped), len(failed)) == (0, 0, 1) |
| 154 |
entries = failed[0].longrepr.reprtraceback.reprentries # type: ignore[union-attr] |
| 155 |
assert len(entries) == 1 |
| 156 |
repr_locals = entries[0].reprlocals |
| 157 |
assert repr_locals.lines |
| 158 |
assert len(repr_locals.lines) == 1 |
| 159 |
assert repr_locals.lines[0].startswith( |
| 160 |
"x = <[NotImplementedError() raised in repr()] ObjWithErrorInRepr" |
| 161 |
) |
| 162 |
|
| 163 |
def test_skip_file_by_conftest(self, pytester: Pytester) -> None: |
| 164 |
pytester.makepyfile( |
| 165 |
conftest=""" |
| 166 |
import pytest |
| 167 |
def pytest_collect_file(): |
| 168 |
pytest.skip("intentional") |
| 169 |
""", |
| 170 |
test_file=""" |
| 171 |
def test_one(): pass |
| 172 |
""", |
| 173 |
) |
| 174 |
try: |
| 175 |
reprec = pytester.inline_run(pytester.path) |
| 176 |
except pytest.skip.Exception: # pragma: no cover |
| 177 |
pytest.fail("wrong skipped caught") |
| 178 |
reports = reprec.getreports("pytest_collectreport") |
| 179 |
# Session, Dir |
| 180 |
assert len(reports) == 2 |
| 181 |
assert reports[1].skipped |
| 182 |
|
| 183 |
|
| 184 |
class TestNewSession(SessionTests): |
| 185 |
def test_order_of_execution(self, pytester: Pytester) -> None: |
| 186 |
reprec = pytester.inline_runsource( |
| 187 |
""" |
| 188 |
values = [] |
| 189 |
def test_1(): |
| 190 |
values.append(1) |
| 191 |
def test_2(): |
| 192 |
values.append(2) |
| 193 |
def test_3(): |
| 194 |
assert values == [1,2] |
| 195 |
class Testmygroup(object): |
| 196 |
reslist = values |
| 197 |
def test_1(self): |
| 198 |
self.reslist.append(1) |
| 199 |
def test_2(self): |
| 200 |
self.reslist.append(2) |
| 201 |
def test_3(self): |
| 202 |
self.reslist.append(3) |
| 203 |
def test_4(self): |
| 204 |
assert self.reslist == [1,2,1,2,3] |
| 205 |
""" |
| 206 |
) |
| 207 |
passed, skipped, failed = reprec.countoutcomes() |
| 208 |
assert failed == skipped == 0 |
| 209 |
assert passed == 7 |
| 210 |
|
| 211 |
def test_collect_only_with_various_situations(self, pytester: Pytester) -> None: |
| 212 |
p = pytester.makepyfile( |
| 213 |
test_one=""" |
| 214 |
def test_one(): |
| 215 |
raise ValueError() |
| 216 |
|
| 217 |
class TestX(object): |
| 218 |
def test_method_one(self): |
| 219 |
pass |
| 220 |
|
| 221 |
class TestY(TestX): |
| 222 |
pass |
| 223 |
""", |
| 224 |
test_three="xxxdsadsadsadsa", |
| 225 |
__init__="", |
| 226 |
) |
| 227 |
reprec = pytester.inline_run("--collect-only", p.parent) |
| 228 |
|
| 229 |
itemstarted = reprec.getcalls("pytest_itemcollected") |
| 230 |
assert len(itemstarted) == 3 |
| 231 |
assert not reprec.getreports("pytest_runtest_logreport") |
| 232 |
started = reprec.getcalls("pytest_collectstart") |
| 233 |
finished = reprec.getreports("pytest_collectreport") |
| 234 |
assert len(started) == len(finished) |
| 235 |
assert len(started) == 6 |
| 236 |
colfail = [x for x in finished if x.failed] |
| 237 |
assert len(colfail) == 1 |
| 238 |
|
| 239 |
def test_minus_x_import_error(self, pytester: Pytester) -> None: |
| 240 |
pytester.makepyfile(__init__="") |
| 241 |
pytester.makepyfile(test_one="xxxx", test_two="yyyy") |
| 242 |
reprec = pytester.inline_run("-x", pytester.path) |
| 243 |
finished = reprec.getreports("pytest_collectreport") |
| 244 |
colfail = [x for x in finished if x.failed] |
| 245 |
assert len(colfail) == 1 |
| 246 |
|
| 247 |
def test_minus_x_overridden_by_maxfail(self, pytester: Pytester) -> None: |
| 248 |
pytester.makepyfile(__init__="") |
| 249 |
pytester.makepyfile(test_one="xxxx", test_two="yyyy", test_third="zzz") |
| 250 |
reprec = pytester.inline_run("-x", "--maxfail=2", pytester.path) |
| 251 |
finished = reprec.getreports("pytest_collectreport") |
| 252 |
colfail = [x for x in finished if x.failed] |
| 253 |
assert len(colfail) == 2 |
| 254 |
|
| 255 |
|
| 256 |
def test_plugin_specify(pytester: Pytester) -> None: |
| 257 |
with pytest.raises(ImportError): |
| 258 |
pytester.parseconfig("-p", "nqweotexistent") |
| 259 |
# pytest.raises(ImportError, |
| 260 |
# "config.do_configure(config)" |
| 261 |
# ) |
| 262 |
|
| 263 |
|
| 264 |
def test_plugin_already_exists(pytester: Pytester) -> None: |
| 265 |
config = pytester.parseconfig("-p", "terminal") |
| 266 |
assert config.option.plugins == ["terminal"] |
| 267 |
config._do_configure() |
| 268 |
config._ensure_unconfigure() |
| 269 |
|
| 270 |
|
| 271 |
def test_exclude(pytester: Pytester) -> None: |
| 272 |
hellodir = pytester.mkdir("hello") |
| 273 |
hellodir.joinpath("test_hello.py").write_text("x y syntaxerror", encoding="utf-8") |
| 274 |
hello2dir = pytester.mkdir("hello2") |
| 275 |
hello2dir.joinpath("test_hello2.py").write_text("x y syntaxerror", encoding="utf-8") |
| 276 |
pytester.makepyfile(test_ok="def test_pass(): pass") |
| 277 |
result = pytester.runpytest("--ignore=hello", "--ignore=hello2") |
| 278 |
assert result.ret == 0 |
| 279 |
result.stdout.fnmatch_lines(["*1 passed*"]) |
| 280 |
|
| 281 |
|
| 282 |
def test_exclude_glob(pytester: Pytester) -> None: |
| 283 |
hellodir = pytester.mkdir("hello") |
| 284 |
hellodir.joinpath("test_hello.py").write_text("x y syntaxerror", encoding="utf-8") |
| 285 |
hello2dir = pytester.mkdir("hello2") |
| 286 |
hello2dir.joinpath("test_hello2.py").write_text("x y syntaxerror", encoding="utf-8") |
| 287 |
hello3dir = pytester.mkdir("hallo3") |
| 288 |
hello3dir.joinpath("test_hello3.py").write_text("x y syntaxerror", encoding="utf-8") |
| 289 |
subdir = pytester.mkdir("sub") |
| 290 |
subdir.joinpath("test_hello4.py").write_text("x y syntaxerror", encoding="utf-8") |
| 291 |
pytester.makepyfile(test_ok="def test_pass(): pass") |
| 292 |
result = pytester.runpytest("--ignore-glob=*h[ea]llo*") |
| 293 |
assert result.ret == 0 |
| 294 |
result.stdout.fnmatch_lines(["*1 passed*"]) |
| 295 |
|
| 296 |
|
| 297 |
def test_deselect(pytester: Pytester) -> None: |
| 298 |
pytester.makepyfile( |
| 299 |
test_a=""" |
| 300 |
import pytest |
| 301 |
|
| 302 |
def test_a1(): pass |
| 303 |
|
| 304 |
@pytest.mark.parametrize('b', range(3)) |
| 305 |
def test_a2(b): pass |
| 306 |
|
| 307 |
class TestClass: |
| 308 |
def test_c1(self): pass |
| 309 |
|
| 310 |
def test_c2(self): pass |
| 311 |
""" |
| 312 |
) |
| 313 |
result = pytester.runpytest( |
| 314 |
"-v", |
| 315 |
"--deselect=test_a.py::test_a2[1]", |
| 316 |
"--deselect=test_a.py::test_a2[2]", |
| 317 |
"--deselect=test_a.py::TestClass::test_c1", |
| 318 |
) |
| 319 |
assert result.ret == 0 |
| 320 |
result.stdout.fnmatch_lines(["*3 passed, 3 deselected*"]) |
| 321 |
for line in result.stdout.lines: |
| 322 |
assert not line.startswith(("test_a.py::test_a2[1]", "test_a.py::test_a2[2]")) |
| 323 |
|
| 324 |
|
| 325 |
def test_sessionfinish_with_start(pytester: Pytester) -> None: |
| 326 |
pytester.makeconftest( |
| 327 |
""" |
| 328 |
import os |
| 329 |
values = [] |
| 330 |
def pytest_sessionstart(): |
| 331 |
values.append(os.getcwd()) |
| 332 |
os.chdir("..") |
| 333 |
|
| 334 |
def pytest_sessionfinish(): |
| 335 |
assert values[0] == os.getcwd() |
| 336 |
|
| 337 |
""" |
| 338 |
) |
| 339 |
res = pytester.runpytest("--collect-only") |
| 340 |
assert res.ret == ExitCode.NO_TESTS_COLLECTED |
| 341 |
|
| 342 |
|
| 343 |
def test_collection_args_do_not_duplicate_modules(pytester: Pytester) -> None: |
| 344 |
"""Test that when multiple collection args are specified on the command line |
| 345 |
for the same module, only a single Module collector is created. |
| 346 |
|
| 347 |
Regression test for #723, #3358. |
| 348 |
""" |
| 349 |
pytester.makepyfile( |
| 350 |
**{ |
| 351 |
"d/test_it": """ |
| 352 |
def test_1(): pass |
| 353 |
def test_2(): pass |
| 354 |
""" |
| 355 |
} |
| 356 |
) |
| 357 |
|
| 358 |
result = pytester.runpytest( |
| 359 |
"--collect-only", |
| 360 |
"d/test_it.py::test_1", |
| 361 |
"d/test_it.py::test_2", |
| 362 |
) |
| 363 |
result.stdout.fnmatch_lines( |
| 364 |
[ |
| 365 |
" <Dir d>", |
| 366 |
" <Module test_it.py>", |
| 367 |
" <Function test_1>", |
| 368 |
" <Function test_2>", |
| 369 |
], |
| 370 |
consecutive=True, |
| 371 |
) |
| 372 |
|
| 373 |
# Different, but related case. |
| 374 |
result = pytester.runpytest( |
| 375 |
"--collect-only", |
| 376 |
"--keep-duplicates", |
| 377 |
"d", |
| 378 |
"d", |
| 379 |
) |
| 380 |
result.stdout.fnmatch_lines( |
| 381 |
[ |
| 382 |
" <Dir d>", |
| 383 |
" <Module test_it.py>", |
| 384 |
" <Function test_1>", |
| 385 |
" <Function test_2>", |
| 386 |
" <Function test_1>", |
| 387 |
" <Function test_2>", |
| 388 |
], |
| 389 |
consecutive=True, |
| 390 |
) |
| 391 |
|
| 392 |
|
| 393 |
@pytest.mark.parametrize("path", ["root", "{relative}/root", "{environment}/root"]) |
| 394 |
def test_rootdir_option_arg( |
| 395 |
pytester: Pytester, monkeypatch: MonkeyPatch, path: str |
| 396 |
) -> None: |
| 397 |
monkeypatch.setenv("PY_ROOTDIR_PATH", str(pytester.path)) |
| 398 |
path = path.format(relative=str(pytester.path), environment="$PY_ROOTDIR_PATH") |
| 399 |
|
| 400 |
rootdir = pytester.path / "root" / "tests" |
| 401 |
rootdir.mkdir(parents=True) |
| 402 |
pytester.makepyfile( |
| 403 |
""" |
| 404 |
import os |
| 405 |
def test_one(): |
| 406 |
assert 1 |
| 407 |
""" |
| 408 |
) |
| 409 |
|
| 410 |
result = pytester.runpytest(f"--rootdir={path}") |
| 411 |
result.stdout.fnmatch_lines( |
| 412 |
[ |
| 413 |
f"*rootdir: {pytester.path}/root", |
| 414 |
"root/test_rootdir_option_arg.py *", |
| 415 |
"*1 passed*", |
| 416 |
] |
| 417 |
) |
| 418 |
|
| 419 |
|
| 420 |
def test_rootdir_wrong_option_arg(pytester: Pytester) -> None: |
| 421 |
result = pytester.runpytest("--rootdir=wrong_dir") |
| 422 |
result.stderr.fnmatch_lines( |
| 423 |
["*Directory *wrong_dir* not found. Check your '--rootdir' option.*"] |
| 424 |
) |
| 425 |
|
| 426 |
|
| 427 |
def test_shouldfail_is_sticky(pytester: Pytester) -> None: |
| 428 |
"""Test that session.shouldfail cannot be reset to False after being set. |
| 429 |
|
| 430 |
Issue #11706. |
| 431 |
""" |
| 432 |
pytester.makeconftest( |
| 433 |
""" |
| 434 |
def pytest_sessionfinish(session): |
| 435 |
assert session.shouldfail |
| 436 |
session.shouldfail = False |
| 437 |
assert session.shouldfail |
| 438 |
""" |
| 439 |
) |
| 440 |
pytester.makepyfile( |
| 441 |
""" |
| 442 |
import pytest |
| 443 |
|
| 444 |
def test_foo(): |
| 445 |
pytest.fail("This is a failing test") |
| 446 |
|
| 447 |
def test_bar(): pass |
| 448 |
""" |
| 449 |
) |
| 450 |
|
| 451 |
result = pytester.runpytest("--maxfail=1", "-Wall") |
| 452 |
|
| 453 |
result.assert_outcomes(failed=1, warnings=1) |
| 454 |
result.stdout.fnmatch_lines("*session.shouldfail cannot be unset*") |
| 455 |
|
| 456 |
|
| 457 |
def test_shouldstop_is_sticky(pytester: Pytester) -> None: |
| 458 |
"""Test that session.shouldstop cannot be reset to False after being set. |
| 459 |
|
| 460 |
Issue #11706. |
| 461 |
""" |
| 462 |
pytester.makeconftest( |
| 463 |
""" |
| 464 |
def pytest_sessionfinish(session): |
| 465 |
assert session.shouldstop |
| 466 |
session.shouldstop = False |
| 467 |
assert session.shouldstop |
| 468 |
""" |
| 469 |
) |
| 470 |
pytester.makepyfile( |
| 471 |
""" |
| 472 |
import pytest |
| 473 |
|
| 474 |
def test_foo(): |
| 475 |
pytest.fail("This is a failing test") |
| 476 |
|
| 477 |
def test_bar(): pass |
| 478 |
""" |
| 479 |
) |
| 480 |
|
| 481 |
result = pytester.runpytest("--stepwise", "-Wall") |
| 482 |
|
| 483 |
result.assert_outcomes(failed=1, warnings=1) |
| 484 |
result.stdout.fnmatch_lines("*session.shouldstop cannot be unset*") |