| 1 |
# mypy: allow-untyped-defs |
| 2 |
from __future__ import annotations |
| 3 |
|
| 4 |
from collections.abc import Sequence |
| 5 |
import os |
| 6 |
from pathlib import Path |
| 7 |
from pathlib import PurePath |
| 8 |
import pprint |
| 9 |
import shutil |
| 10 |
import sys |
| 11 |
import tempfile |
| 12 |
import textwrap |
| 13 |
|
| 14 |
from _pytest.compat import running_on_ci |
| 15 |
from _pytest.config import ExitCode |
| 16 |
from _pytest.fixtures import FixtureRequest |
| 17 |
from _pytest.main import _in_venv |
| 18 |
from _pytest.main import Session |
| 19 |
from _pytest.monkeypatch import MonkeyPatch |
| 20 |
from _pytest.nodes import Item |
| 21 |
from _pytest.pathlib import symlink_or_skip |
| 22 |
from _pytest.pytester import HookRecorder |
| 23 |
from _pytest.pytester import Pytester |
| 24 |
import pytest |
| 25 |
|
| 26 |
|
| 27 |
def ensure_file(file_path: Path) -> Path: |
| 28 |
"""Ensure that file exists""" |
| 29 |
file_path.parent.mkdir(parents=True, exist_ok=True) |
| 30 |
file_path.touch(exist_ok=True) |
| 31 |
return file_path |
| 32 |
|
| 33 |
|
| 34 |
class TestCollector: |
| 35 |
def test_collect_versus_item(self) -> None: |
| 36 |
from pytest import Collector |
| 37 |
from pytest import Item |
| 38 |
|
| 39 |
assert not issubclass(Collector, Item) |
| 40 |
assert not issubclass(Item, Collector) |
| 41 |
|
| 42 |
def test_check_equality(self, pytester: Pytester) -> None: |
| 43 |
modcol = pytester.getmodulecol( |
| 44 |
""" |
| 45 |
def test_pass(): pass |
| 46 |
def test_fail(): assert 0 |
| 47 |
""" |
| 48 |
) |
| 49 |
fn1 = pytester.collect_by_name(modcol, "test_pass") |
| 50 |
assert isinstance(fn1, pytest.Function) |
| 51 |
fn2 = pytester.collect_by_name(modcol, "test_pass") |
| 52 |
assert isinstance(fn2, pytest.Function) |
| 53 |
|
| 54 |
assert fn1 == fn2 |
| 55 |
assert fn1 != modcol |
| 56 |
assert hash(fn1) == hash(fn2) |
| 57 |
|
| 58 |
fn3 = pytester.collect_by_name(modcol, "test_fail") |
| 59 |
assert isinstance(fn3, pytest.Function) |
| 60 |
assert not (fn1 == fn3) |
| 61 |
assert fn1 != fn3 |
| 62 |
|
| 63 |
for fn in fn1, fn2, fn3: |
| 64 |
assert isinstance(fn, pytest.Function) |
| 65 |
assert fn != 3 # type: ignore[comparison-overlap] |
| 66 |
assert fn != modcol |
| 67 |
assert fn != [1, 2, 3] # type: ignore[comparison-overlap] |
| 68 |
assert [1, 2, 3] != fn # type: ignore[comparison-overlap] |
| 69 |
assert modcol != fn |
| 70 |
|
| 71 |
assert pytester.collect_by_name(modcol, "doesnotexist") is None |
| 72 |
|
| 73 |
def test_getparent_and_accessors(self, pytester: Pytester) -> None: |
| 74 |
modcol = pytester.getmodulecol( |
| 75 |
""" |
| 76 |
class TestClass: |
| 77 |
def test_foo(self): |
| 78 |
pass |
| 79 |
""" |
| 80 |
) |
| 81 |
cls = pytester.collect_by_name(modcol, "TestClass") |
| 82 |
assert isinstance(cls, pytest.Class) |
| 83 |
fn = pytester.collect_by_name(cls, "test_foo") |
| 84 |
assert isinstance(fn, pytest.Function) |
| 85 |
|
| 86 |
assert fn.getparent(pytest.Module) is modcol |
| 87 |
assert modcol is not None |
| 88 |
assert modcol.module is not None |
| 89 |
assert modcol.cls is None |
| 90 |
assert modcol.instance is None |
| 91 |
|
| 92 |
assert fn.getparent(pytest.Class) is cls |
| 93 |
assert cls.module is not None |
| 94 |
assert cls.cls is not None |
| 95 |
assert cls.instance is None |
| 96 |
|
| 97 |
assert fn.getparent(pytest.Function) is fn |
| 98 |
assert fn.module is not None |
| 99 |
assert fn.cls is not None |
| 100 |
assert fn.instance is not None |
| 101 |
assert fn.function is not None |
| 102 |
|
| 103 |
def test_getcustomfile_roundtrip(self, pytester: Pytester) -> None: |
| 104 |
hello = pytester.makefile(".xxx", hello="world") |
| 105 |
pytester.makepyfile( |
| 106 |
conftest=""" |
| 107 |
import pytest |
| 108 |
class CustomFile(pytest.File): |
| 109 |
def collect(self): |
| 110 |
return [] |
| 111 |
def pytest_collect_file(file_path, parent): |
| 112 |
if file_path.suffix == ".xxx": |
| 113 |
return CustomFile.from_parent(path=file_path, parent=parent) |
| 114 |
""" |
| 115 |
) |
| 116 |
node = pytester.getpathnode(hello) |
| 117 |
assert isinstance(node, pytest.File) |
| 118 |
assert node.name == "hello.xxx" |
| 119 |
nodes = node.session.perform_collect([node.nodeid], genitems=False) |
| 120 |
assert len(nodes) == 1 |
| 121 |
assert isinstance(nodes[0], pytest.File) |
| 122 |
|
| 123 |
def test_can_skip_class_with_test_attr(self, pytester: Pytester) -> None: |
| 124 |
"""Assure test class is skipped when using `__test__=False` (See #2007).""" |
| 125 |
pytester.makepyfile( |
| 126 |
""" |
| 127 |
class TestFoo(object): |
| 128 |
__test__ = False |
| 129 |
def __init__(self): |
| 130 |
pass |
| 131 |
def test_foo(): |
| 132 |
assert True |
| 133 |
""" |
| 134 |
) |
| 135 |
result = pytester.runpytest() |
| 136 |
result.stdout.fnmatch_lines(["collected 0 items", "*no tests ran in*"]) |
| 137 |
|
| 138 |
|
| 139 |
class TestCollectFS: |
| 140 |
def test_ignored_certain_directories(self, pytester: Pytester) -> None: |
| 141 |
tmp_path = pytester.path |
| 142 |
ensure_file(tmp_path / "build" / "test_notfound.py") |
| 143 |
ensure_file(tmp_path / "dist" / "test_notfound.py") |
| 144 |
ensure_file(tmp_path / "_darcs" / "test_notfound.py") |
| 145 |
ensure_file(tmp_path / "CVS" / "test_notfound.py") |
| 146 |
ensure_file(tmp_path / "{arch}" / "test_notfound.py") |
| 147 |
ensure_file(tmp_path / ".whatever" / "test_notfound.py") |
| 148 |
ensure_file(tmp_path / ".bzr" / "test_notfound.py") |
| 149 |
ensure_file(tmp_path / "normal" / "test_found.py") |
| 150 |
for x in tmp_path.rglob("test_*.py"): |
| 151 |
x.write_text("def test_hello(): pass", encoding="utf-8") |
| 152 |
|
| 153 |
result = pytester.runpytest("--collect-only") |
| 154 |
s = result.stdout.str() |
| 155 |
assert "test_notfound" not in s |
| 156 |
assert "test_found" in s |
| 157 |
|
| 158 |
known_environment_types = pytest.mark.parametrize( |
| 159 |
"env_path", |
| 160 |
[ |
| 161 |
pytest.param(PurePath("pyvenv.cfg"), id="venv"), |
| 162 |
pytest.param(PurePath("conda-meta", "history"), id="conda"), |
| 163 |
], |
| 164 |
) |
| 165 |
|
| 166 |
@known_environment_types |
| 167 |
def test_ignored_virtualenvs(self, pytester: Pytester, env_path: PurePath) -> None: |
| 168 |
ensure_file(pytester.path / "virtual" / env_path) |
| 169 |
testfile = ensure_file(pytester.path / "virtual" / "test_invenv.py") |
| 170 |
testfile.write_text("def test_hello(): pass", encoding="utf-8") |
| 171 |
|
| 172 |
# by default, ignore tests inside a virtualenv |
| 173 |
result = pytester.runpytest() |
| 174 |
result.stdout.no_fnmatch_line("*test_invenv*") |
| 175 |
# allow test collection if user insists |
| 176 |
result = pytester.runpytest("--collect-in-virtualenv") |
| 177 |
assert "test_invenv" in result.stdout.str() |
| 178 |
# allow test collection if user directly passes in the directory |
| 179 |
result = pytester.runpytest("virtual") |
| 180 |
assert "test_invenv" in result.stdout.str() |
| 181 |
|
| 182 |
@known_environment_types |
| 183 |
def test_ignored_virtualenvs_norecursedirs_precedence( |
| 184 |
self, pytester: Pytester, env_path |
| 185 |
) -> None: |
| 186 |
# norecursedirs takes priority |
| 187 |
ensure_file(pytester.path / ".virtual" / env_path) |
| 188 |
testfile = ensure_file(pytester.path / ".virtual" / "test_invenv.py") |
| 189 |
testfile.write_text("def test_hello(): pass", encoding="utf-8") |
| 190 |
result = pytester.runpytest("--collect-in-virtualenv") |
| 191 |
result.stdout.no_fnmatch_line("*test_invenv*") |
| 192 |
# ...unless the virtualenv is explicitly given on the CLI |
| 193 |
result = pytester.runpytest("--collect-in-virtualenv", ".virtual") |
| 194 |
assert "test_invenv" in result.stdout.str() |
| 195 |
|
| 196 |
@known_environment_types |
| 197 |
def test__in_venv(self, pytester: Pytester, env_path: PurePath) -> None: |
| 198 |
"""Directly test the virtual env detection function""" |
| 199 |
# no env path, not a env |
| 200 |
base_path = pytester.mkdir("venv") |
| 201 |
assert _in_venv(base_path) is False |
| 202 |
# with env path, totally a env |
| 203 |
ensure_file(base_path.joinpath(env_path)) |
| 204 |
assert _in_venv(base_path) is True |
| 205 |
|
| 206 |
def test_custom_norecursedirs(self, pytester: Pytester) -> None: |
| 207 |
pytester.makeini( |
| 208 |
""" |
| 209 |
[pytest] |
| 210 |
norecursedirs = mydir xyz* |
| 211 |
""" |
| 212 |
) |
| 213 |
tmp_path = pytester.path |
| 214 |
ensure_file(tmp_path / "mydir" / "test_hello.py").write_text( |
| 215 |
"def test_1(): pass", encoding="utf-8" |
| 216 |
) |
| 217 |
ensure_file(tmp_path / "xyz123" / "test_2.py").write_text( |
| 218 |
"def test_2(): 0/0", encoding="utf-8" |
| 219 |
) |
| 220 |
ensure_file(tmp_path / "xy" / "test_ok.py").write_text( |
| 221 |
"def test_3(): pass", encoding="utf-8" |
| 222 |
) |
| 223 |
rec = pytester.inline_run() |
| 224 |
rec.assertoutcome(passed=1) |
| 225 |
rec = pytester.inline_run("xyz123/test_2.py") |
| 226 |
rec.assertoutcome(failed=1) |
| 227 |
|
| 228 |
def test_testpaths_ini(self, pytester: Pytester, monkeypatch: MonkeyPatch) -> None: |
| 229 |
pytester.makeini( |
| 230 |
""" |
| 231 |
[pytest] |
| 232 |
testpaths = */tests |
| 233 |
""" |
| 234 |
) |
| 235 |
tmp_path = pytester.path |
| 236 |
ensure_file(tmp_path / "a" / "test_1.py").write_text( |
| 237 |
"def test_a(): pass", encoding="utf-8" |
| 238 |
) |
| 239 |
ensure_file(tmp_path / "b" / "tests" / "test_2.py").write_text( |
| 240 |
"def test_b(): pass", encoding="utf-8" |
| 241 |
) |
| 242 |
ensure_file(tmp_path / "c" / "tests" / "test_3.py").write_text( |
| 243 |
"def test_c(): pass", encoding="utf-8" |
| 244 |
) |
| 245 |
|
| 246 |
# executing from rootdir only tests from `testpaths` directories |
| 247 |
# are collected |
| 248 |
items, _reprec = pytester.inline_genitems("-v") |
| 249 |
assert [x.name for x in items] == ["test_b", "test_c"] |
| 250 |
|
| 251 |
# check that explicitly passing directories in the command-line |
| 252 |
# collects the tests |
| 253 |
for dirname in ("a", "b", "c"): |
| 254 |
items, _reprec = pytester.inline_genitems(tmp_path.joinpath(dirname)) |
| 255 |
assert [x.name for x in items] == [f"test_{dirname}"] |
| 256 |
|
| 257 |
# changing cwd to each subdirectory and running pytest without |
| 258 |
# arguments collects the tests in that directory normally |
| 259 |
for dirname in ("a", "b", "c"): |
| 260 |
monkeypatch.chdir(pytester.path.joinpath(dirname)) |
| 261 |
items, _reprec = pytester.inline_genitems() |
| 262 |
assert [x.name for x in items] == [f"test_{dirname}"] |
| 263 |
|
| 264 |
def test_missing_permissions_on_unselected_directory_doesnt_crash( |
| 265 |
self, pytester: Pytester |
| 266 |
) -> None: |
| 267 |
"""Regression test for #12120.""" |
| 268 |
test = pytester.makepyfile(test="def test(): pass") |
| 269 |
bad = pytester.mkdir("bad") |
| 270 |
try: |
| 271 |
bad.chmod(0) |
| 272 |
|
| 273 |
result = pytester.runpytest(test) |
| 274 |
finally: |
| 275 |
bad.chmod(750) |
| 276 |
bad.rmdir() |
| 277 |
|
| 278 |
assert result.ret == ExitCode.OK |
| 279 |
result.assert_outcomes(passed=1) |
| 280 |
|
| 281 |
|
| 282 |
class TestCollectPluginHookRelay: |
| 283 |
def test_pytest_collect_file(self, pytester: Pytester) -> None: |
| 284 |
wascalled = [] |
| 285 |
|
| 286 |
class Plugin: |
| 287 |
def pytest_collect_file(self, file_path: Path) -> None: |
| 288 |
if not file_path.name.startswith("."): |
| 289 |
# Ignore hidden files, e.g. .testmondata. |
| 290 |
wascalled.append(file_path) |
| 291 |
|
| 292 |
pytester.makefile(".abc", "xyz") |
| 293 |
pytest.main(pytester.path, plugins=[Plugin()]) |
| 294 |
assert len(wascalled) == 1 |
| 295 |
assert wascalled[0].suffix == ".abc" |
| 296 |
|
| 297 |
|
| 298 |
class TestPrunetraceback: |
| 299 |
def test_custom_repr_failure(self, pytester: Pytester) -> None: |
| 300 |
p = pytester.makepyfile( |
| 301 |
""" |
| 302 |
import not_exists |
| 303 |
""" |
| 304 |
) |
| 305 |
pytester.makeconftest( |
| 306 |
""" |
| 307 |
import pytest |
| 308 |
def pytest_collect_file(file_path, parent): |
| 309 |
return MyFile.from_parent(path=file_path, parent=parent) |
| 310 |
class MyError(Exception): |
| 311 |
pass |
| 312 |
class MyFile(pytest.File): |
| 313 |
def collect(self): |
| 314 |
raise MyError() |
| 315 |
def repr_failure(self, excinfo): |
| 316 |
if isinstance(excinfo.value, MyError): |
| 317 |
return "hello world" |
| 318 |
return pytest.File.repr_failure(self, excinfo) |
| 319 |
""" |
| 320 |
) |
| 321 |
|
| 322 |
result = pytester.runpytest(p) |
| 323 |
result.stdout.fnmatch_lines(["*ERROR collecting*", "*hello world*"]) |
| 324 |
|
| 325 |
@pytest.mark.xfail(reason="other mechanism for adding to reporting needed") |
| 326 |
def test_collect_report_postprocessing(self, pytester: Pytester) -> None: |
| 327 |
p = pytester.makepyfile( |
| 328 |
""" |
| 329 |
import not_exists |
| 330 |
""" |
| 331 |
) |
| 332 |
pytester.makeconftest( |
| 333 |
""" |
| 334 |
import pytest |
| 335 |
@pytest.hookimpl(wrapper=True) |
| 336 |
def pytest_make_collect_report(): |
| 337 |
rep = yield |
| 338 |
rep.headerlines += ["header1"] |
| 339 |
return rep |
| 340 |
""" |
| 341 |
) |
| 342 |
result = pytester.runpytest(p) |
| 343 |
result.stdout.fnmatch_lines(["*ERROR collecting*", "*header1*"]) |
| 344 |
|
| 345 |
def test_collection_error_traceback_is_clean(self, pytester: Pytester) -> None: |
| 346 |
"""When a collection error occurs, the report traceback doesn't contain |
| 347 |
internal pytest stack entries. |
| 348 |
|
| 349 |
Issue #11710. |
| 350 |
""" |
| 351 |
pytester.makepyfile( |
| 352 |
""" |
| 353 |
raise Exception("LOUSY") |
| 354 |
""" |
| 355 |
) |
| 356 |
result = pytester.runpytest() |
| 357 |
result.stdout.fnmatch_lines( |
| 358 |
[ |
| 359 |
"*ERROR collecting*", |
| 360 |
"test_*.py:1: in <module>", |
| 361 |
' raise Exception("LOUSY")', |
| 362 |
"E Exception: LOUSY", |
| 363 |
"*= short test summary info =*", |
| 364 |
], |
| 365 |
consecutive=True, |
| 366 |
) |
| 367 |
|
| 368 |
|
| 369 |
class TestCustomConftests: |
| 370 |
def test_ignore_collect_path(self, pytester: Pytester) -> None: |
| 371 |
pytester.makeconftest( |
| 372 |
""" |
| 373 |
def pytest_ignore_collect(collection_path, config): |
| 374 |
return collection_path.name.startswith("x") or collection_path.name == "test_one.py" |
| 375 |
""" |
| 376 |
) |
| 377 |
sub = pytester.mkdir("xy123") |
| 378 |
ensure_file(sub / "test_hello.py").write_text("syntax error", encoding="utf-8") |
| 379 |
sub.joinpath("conftest.py").write_text("syntax error", encoding="utf-8") |
| 380 |
pytester.makepyfile("def test_hello(): pass") |
| 381 |
pytester.makepyfile(test_one="syntax error") |
| 382 |
result = pytester.runpytest("--fulltrace") |
| 383 |
assert result.ret == 0 |
| 384 |
result.stdout.fnmatch_lines(["*1 passed*"]) |
| 385 |
|
| 386 |
def test_ignore_collect_not_called_on_argument(self, pytester: Pytester) -> None: |
| 387 |
pytester.makeconftest( |
| 388 |
""" |
| 389 |
def pytest_ignore_collect(collection_path, config): |
| 390 |
return True |
| 391 |
""" |
| 392 |
) |
| 393 |
p = pytester.makepyfile("def test_hello(): pass") |
| 394 |
result = pytester.runpytest(p) |
| 395 |
assert result.ret == 0 |
| 396 |
result.stdout.fnmatch_lines(["*1 passed*"]) |
| 397 |
result = pytester.runpytest() |
| 398 |
assert result.ret == ExitCode.NO_TESTS_COLLECTED |
| 399 |
result.stdout.fnmatch_lines(["*collected 0 items*"]) |
| 400 |
|
| 401 |
def test_collectignore_exclude_on_option(self, pytester: Pytester) -> None: |
| 402 |
pytester.makeconftest( |
| 403 |
""" |
| 404 |
from pathlib import Path |
| 405 |
|
| 406 |
class MyPathLike: |
| 407 |
def __init__(self, path): |
| 408 |
self.path = path |
| 409 |
def __fspath__(self): |
| 410 |
return "path" |
| 411 |
|
| 412 |
collect_ignore = [MyPathLike('hello'), 'test_world.py', Path('bye')] |
| 413 |
|
| 414 |
def pytest_addoption(parser): |
| 415 |
parser.addoption("--XX", action="store_true", default=False) |
| 416 |
|
| 417 |
def pytest_configure(config): |
| 418 |
if config.getvalue("XX"): |
| 419 |
collect_ignore[:] = [] |
| 420 |
""" |
| 421 |
) |
| 422 |
pytester.mkdir("hello") |
| 423 |
pytester.makepyfile(test_world="def test_hello(): pass") |
| 424 |
result = pytester.runpytest() |
| 425 |
assert result.ret == ExitCode.NO_TESTS_COLLECTED |
| 426 |
result.stdout.no_fnmatch_line("*passed*") |
| 427 |
result = pytester.runpytest("--XX") |
| 428 |
assert result.ret == 0 |
| 429 |
assert "passed" in result.stdout.str() |
| 430 |
|
| 431 |
def test_collectignoreglob_exclude_on_option(self, pytester: Pytester) -> None: |
| 432 |
pytester.makeconftest( |
| 433 |
""" |
| 434 |
collect_ignore_glob = ['*w*l[dt]*'] |
| 435 |
def pytest_addoption(parser): |
| 436 |
parser.addoption("--XX", action="store_true", default=False) |
| 437 |
def pytest_configure(config): |
| 438 |
if config.getvalue("XX"): |
| 439 |
collect_ignore_glob[:] = [] |
| 440 |
""" |
| 441 |
) |
| 442 |
pytester.makepyfile(test_world="def test_hello(): pass") |
| 443 |
pytester.makepyfile(test_welt="def test_hallo(): pass") |
| 444 |
result = pytester.runpytest() |
| 445 |
assert result.ret == ExitCode.NO_TESTS_COLLECTED |
| 446 |
result.stdout.fnmatch_lines(["*collected 0 items*"]) |
| 447 |
result = pytester.runpytest("--XX") |
| 448 |
assert result.ret == 0 |
| 449 |
result.stdout.fnmatch_lines(["*2 passed*"]) |
| 450 |
|
| 451 |
def test_pytest_fs_collect_hooks_are_seen(self, pytester: Pytester) -> None: |
| 452 |
pytester.makeconftest( |
| 453 |
""" |
| 454 |
import pytest |
| 455 |
class MyModule(pytest.Module): |
| 456 |
pass |
| 457 |
def pytest_collect_file(file_path, parent): |
| 458 |
if file_path.suffix == ".py": |
| 459 |
return MyModule.from_parent(path=file_path, parent=parent) |
| 460 |
""" |
| 461 |
) |
| 462 |
pytester.mkdir("sub") |
| 463 |
pytester.makepyfile("def test_x(): pass") |
| 464 |
result = pytester.runpytest("--co") |
| 465 |
result.stdout.fnmatch_lines(["*MyModule*", "*test_x*"]) |
| 466 |
|
| 467 |
def test_pytest_collect_file_from_sister_dir(self, pytester: Pytester) -> None: |
| 468 |
sub1 = pytester.mkpydir("sub1") |
| 469 |
sub2 = pytester.mkpydir("sub2") |
| 470 |
conf1 = pytester.makeconftest( |
| 471 |
""" |
| 472 |
import pytest |
| 473 |
class MyModule1(pytest.Module): |
| 474 |
pass |
| 475 |
def pytest_collect_file(file_path, parent): |
| 476 |
if file_path.suffix == ".py": |
| 477 |
return MyModule1.from_parent(path=file_path, parent=parent) |
| 478 |
""" |
| 479 |
) |
| 480 |
conf1.replace(sub1.joinpath(conf1.name)) |
| 481 |
conf2 = pytester.makeconftest( |
| 482 |
""" |
| 483 |
import pytest |
| 484 |
class MyModule2(pytest.Module): |
| 485 |
pass |
| 486 |
def pytest_collect_file(file_path, parent): |
| 487 |
if file_path.suffix == ".py": |
| 488 |
return MyModule2.from_parent(path=file_path, parent=parent) |
| 489 |
""" |
| 490 |
) |
| 491 |
conf2.replace(sub2.joinpath(conf2.name)) |
| 492 |
p = pytester.makepyfile("def test_x(): pass") |
| 493 |
shutil.copy(p, sub1.joinpath(p.name)) |
| 494 |
shutil.copy(p, sub2.joinpath(p.name)) |
| 495 |
result = pytester.runpytest("--co") |
| 496 |
result.stdout.fnmatch_lines(["*MyModule1*", "*MyModule2*", "*test_x*"]) |
| 497 |
|
| 498 |
|
| 499 |
class TestSession: |
| 500 |
def test_collect_topdir(self, pytester: Pytester) -> None: |
| 501 |
p = pytester.makepyfile("def test_func(): pass") |
| 502 |
id = "::".join([p.name, "test_func"]) |
| 503 |
# XXX migrate to collectonly? (see below) |
| 504 |
config = pytester.parseconfig(id) |
| 505 |
topdir = pytester.path |
| 506 |
rcol = Session.from_config(config) |
| 507 |
assert topdir == rcol.path |
| 508 |
# rootid = rcol.nodeid |
| 509 |
# root2 = rcol.perform_collect([rcol.nodeid], genitems=False)[0] |
| 510 |
# assert root2 == rcol, rootid |
| 511 |
colitems = rcol.perform_collect([rcol.nodeid], genitems=False) |
| 512 |
assert len(colitems) == 1 |
| 513 |
assert colitems[0].path == topdir |
| 514 |
|
| 515 |
def get_reported_items(self, hookrec: HookRecorder) -> list[Item]: |
| 516 |
"""Return pytest.Item instances reported by the pytest_collectreport hook""" |
| 517 |
calls = hookrec.getcalls("pytest_collectreport") |
| 518 |
return [ |
| 519 |
x |
| 520 |
for call in calls |
| 521 |
for x in call.report.result |
| 522 |
if isinstance(x, pytest.Item) |
| 523 |
] |
| 524 |
|
| 525 |
def test_collect_protocol_single_function(self, pytester: Pytester) -> None: |
| 526 |
p = pytester.makepyfile("def test_func(): pass") |
| 527 |
id = "::".join([p.name, "test_func"]) |
| 528 |
items, hookrec = pytester.inline_genitems(id) |
| 529 |
(item,) = items |
| 530 |
assert item.name == "test_func" |
| 531 |
newid = item.nodeid |
| 532 |
assert newid == id |
| 533 |
pprint.pprint(hookrec.calls) |
| 534 |
topdir = pytester.path # noqa: F841 |
| 535 |
hookrec.assert_contains( |
| 536 |
[ |
| 537 |
("pytest_collectstart", "collector.path == topdir"), |
| 538 |
("pytest_make_collect_report", "collector.path == topdir"), |
| 539 |
("pytest_collectstart", "collector.path == p"), |
| 540 |
("pytest_make_collect_report", "collector.path == p"), |
| 541 |
("pytest_pycollect_makeitem", "name == 'test_func'"), |
| 542 |
("pytest_collectreport", "report.result[0].name == 'test_func'"), |
| 543 |
] |
| 544 |
) |
| 545 |
# ensure we are reporting the collection of the single test item (#2464) |
| 546 |
assert [x.name for x in self.get_reported_items(hookrec)] == ["test_func"] |
| 547 |
|
| 548 |
def test_collect_protocol_method(self, pytester: Pytester) -> None: |
| 549 |
p = pytester.makepyfile( |
| 550 |
""" |
| 551 |
class TestClass(object): |
| 552 |
def test_method(self): |
| 553 |
pass |
| 554 |
""" |
| 555 |
) |
| 556 |
normid = p.name + "::TestClass::test_method" |
| 557 |
for id in [p.name, p.name + "::TestClass", normid]: |
| 558 |
items, hookrec = pytester.inline_genitems(id) |
| 559 |
assert len(items) == 1 |
| 560 |
assert items[0].name == "test_method" |
| 561 |
newid = items[0].nodeid |
| 562 |
assert newid == normid |
| 563 |
# ensure we are reporting the collection of the single test item (#2464) |
| 564 |
assert [x.name for x in self.get_reported_items(hookrec)] == ["test_method"] |
| 565 |
|
| 566 |
def test_collect_custom_nodes_multi_id(self, pytester: Pytester) -> None: |
| 567 |
p = pytester.makepyfile("def test_func(): pass") |
| 568 |
pytester.makeconftest( |
| 569 |
f""" |
| 570 |
import pytest |
| 571 |
class SpecialItem(pytest.Item): |
| 572 |
def runtest(self): |
| 573 |
return # ok |
| 574 |
class SpecialFile(pytest.File): |
| 575 |
def collect(self): |
| 576 |
return [SpecialItem.from_parent(name="check", parent=self)] |
| 577 |
def pytest_collect_file(file_path, parent): |
| 578 |
if file_path.name == {p.name!r}: |
| 579 |
return SpecialFile.from_parent(path=file_path, parent=parent) |
| 580 |
""" |
| 581 |
) |
| 582 |
id = p.name |
| 583 |
|
| 584 |
items, hookrec = pytester.inline_genitems(id) |
| 585 |
pprint.pprint(hookrec.calls) |
| 586 |
assert len(items) == 2 |
| 587 |
hookrec.assert_contains( |
| 588 |
[ |
| 589 |
("pytest_collectstart", "collector.path == collector.session.path"), |
| 590 |
( |
| 591 |
"pytest_collectstart", |
| 592 |
"collector.__class__.__name__ == 'SpecialFile'", |
| 593 |
), |
| 594 |
("pytest_collectstart", "collector.__class__.__name__ == 'Module'"), |
| 595 |
("pytest_pycollect_makeitem", "name == 'test_func'"), |
| 596 |
("pytest_collectreport", "report.nodeid.startswith(p.name)"), |
| 597 |
] |
| 598 |
) |
| 599 |
assert len(self.get_reported_items(hookrec)) == 2 |
| 600 |
|
| 601 |
def test_collect_subdir_event_ordering(self, pytester: Pytester) -> None: |
| 602 |
p = pytester.makepyfile("def test_func(): pass") |
| 603 |
aaa = pytester.mkpydir("aaa") |
| 604 |
test_aaa = aaa.joinpath("test_aaa.py") |
| 605 |
p.replace(test_aaa) |
| 606 |
|
| 607 |
items, hookrec = pytester.inline_genitems() |
| 608 |
assert len(items) == 1 |
| 609 |
pprint.pprint(hookrec.calls) |
| 610 |
hookrec.assert_contains( |
| 611 |
[ |
| 612 |
("pytest_collectstart", "collector.path == test_aaa"), |
| 613 |
("pytest_pycollect_makeitem", "name == 'test_func'"), |
| 614 |
("pytest_collectreport", "report.nodeid.startswith('aaa/test_aaa.py')"), |
| 615 |
] |
| 616 |
) |
| 617 |
|
| 618 |
def test_collect_two_commandline_args(self, pytester: Pytester) -> None: |
| 619 |
p = pytester.makepyfile("def test_func(): pass") |
| 620 |
aaa = pytester.mkpydir("aaa") |
| 621 |
bbb = pytester.mkpydir("bbb") |
| 622 |
test_aaa = aaa.joinpath("test_aaa.py") |
| 623 |
shutil.copy(p, test_aaa) |
| 624 |
test_bbb = bbb.joinpath("test_bbb.py") |
| 625 |
p.replace(test_bbb) |
| 626 |
|
| 627 |
id = "." |
| 628 |
|
| 629 |
items, hookrec = pytester.inline_genitems(id) |
| 630 |
assert len(items) == 2 |
| 631 |
pprint.pprint(hookrec.calls) |
| 632 |
hookrec.assert_contains( |
| 633 |
[ |
| 634 |
("pytest_collectstart", "collector.path == test_aaa"), |
| 635 |
("pytest_pycollect_makeitem", "name == 'test_func'"), |
| 636 |
("pytest_collectreport", "report.nodeid == 'aaa/test_aaa.py'"), |
| 637 |
("pytest_collectstart", "collector.path == test_bbb"), |
| 638 |
("pytest_pycollect_makeitem", "name == 'test_func'"), |
| 639 |
("pytest_collectreport", "report.nodeid == 'bbb/test_bbb.py'"), |
| 640 |
] |
| 641 |
) |
| 642 |
|
| 643 |
def test_serialization_byid(self, pytester: Pytester) -> None: |
| 644 |
pytester.makepyfile("def test_func(): pass") |
| 645 |
items, _hookrec = pytester.inline_genitems() |
| 646 |
assert len(items) == 1 |
| 647 |
(item,) = items |
| 648 |
items2, _hookrec = pytester.inline_genitems(item.nodeid) |
| 649 |
(item2,) = items2 |
| 650 |
assert item2.name == item.name |
| 651 |
assert item2.path == item.path |
| 652 |
|
| 653 |
def test_find_byid_without_instance_parents(self, pytester: Pytester) -> None: |
| 654 |
p = pytester.makepyfile( |
| 655 |
""" |
| 656 |
class TestClass(object): |
| 657 |
def test_method(self): |
| 658 |
pass |
| 659 |
""" |
| 660 |
) |
| 661 |
arg = p.name + "::TestClass::test_method" |
| 662 |
items, hookrec = pytester.inline_genitems(arg) |
| 663 |
assert len(items) == 1 |
| 664 |
(item,) = items |
| 665 |
assert item.nodeid.endswith("TestClass::test_method") |
| 666 |
# ensure we are reporting the collection of the single test item (#2464) |
| 667 |
assert [x.name for x in self.get_reported_items(hookrec)] == ["test_method"] |
| 668 |
|
| 669 |
def test_collect_parametrized_order(self, pytester: Pytester) -> None: |
| 670 |
p = pytester.makepyfile( |
| 671 |
""" |
| 672 |
import pytest |
| 673 |
|
| 674 |
@pytest.mark.parametrize('i', [0, 1, 2]) |
| 675 |
def test_param(i): ... |
| 676 |
""" |
| 677 |
) |
| 678 |
items, _hookrec = pytester.inline_genitems(f"{p}::test_param") |
| 679 |
assert len(items) == 3 |
| 680 |
assert [item.nodeid for item in items] == [ |
| 681 |
"test_collect_parametrized_order.py::test_param[0]", |
| 682 |
"test_collect_parametrized_order.py::test_param[1]", |
| 683 |
"test_collect_parametrized_order.py::test_param[2]", |
| 684 |
] |
| 685 |
|
| 686 |
|
| 687 |
class Test_getinitialnodes: |
| 688 |
def test_global_file(self, pytester: Pytester) -> None: |
| 689 |
tmp_path = pytester.path |
| 690 |
x = ensure_file(tmp_path / "x.py") |
| 691 |
config = pytester.parseconfigure(x) |
| 692 |
col = pytester.getnode(config, x) |
| 693 |
assert isinstance(col, pytest.Module) |
| 694 |
assert col.name == "x.py" |
| 695 |
assert col.parent is not None |
| 696 |
assert col.parent.parent is not None |
| 697 |
assert col.parent.parent.parent is None |
| 698 |
for parent in col.listchain(): |
| 699 |
assert parent.config is config |
| 700 |
|
| 701 |
def test_pkgfile(self, pytester: Pytester, monkeypatch: MonkeyPatch) -> None: |
| 702 |
"""Verify nesting when a module is within a package. |
| 703 |
The parent chain should match: Module<x.py> -> Package<subdir> -> Session. |
| 704 |
Session's parent should always be None. |
| 705 |
""" |
| 706 |
tmp_path = pytester.path |
| 707 |
subdir = tmp_path.joinpath("subdir") |
| 708 |
x = ensure_file(subdir / "x.py") |
| 709 |
ensure_file(subdir / "__init__.py") |
| 710 |
with monkeypatch.context() as mp: |
| 711 |
mp.chdir(subdir) |
| 712 |
config = pytester.parseconfigure(x) |
| 713 |
col = pytester.getnode(config, x) |
| 714 |
assert col is not None |
| 715 |
assert col.name == "x.py" |
| 716 |
assert isinstance(col, pytest.Module) |
| 717 |
assert isinstance(col.parent, pytest.Package) |
| 718 |
assert isinstance(col.parent.parent, pytest.Session) |
| 719 |
# session is batman (has no parents) |
| 720 |
assert col.parent.parent.parent is None |
| 721 |
for parent in col.listchain(): |
| 722 |
assert parent.config is config |
| 723 |
|
| 724 |
|
| 725 |
class Test_genitems: |
| 726 |
def test_check_collect_hashes(self, pytester: Pytester) -> None: |
| 727 |
p = pytester.makepyfile( |
| 728 |
""" |
| 729 |
def test_1(): |
| 730 |
pass |
| 731 |
|
| 732 |
def test_2(): |
| 733 |
pass |
| 734 |
""" |
| 735 |
) |
| 736 |
shutil.copy(p, p.parent / (p.stem + "2" + ".py")) |
| 737 |
items, _reprec = pytester.inline_genitems(p.parent) |
| 738 |
assert len(items) == 4 |
| 739 |
for numi, i in enumerate(items): |
| 740 |
for numj, j in enumerate(items): |
| 741 |
if numj != numi: |
| 742 |
assert hash(i) != hash(j) |
| 743 |
assert i != j |
| 744 |
|
| 745 |
def test_example_items1(self, pytester: Pytester) -> None: |
| 746 |
p = pytester.makepyfile( |
| 747 |
""" |
| 748 |
import pytest |
| 749 |
|
| 750 |
def testone(): |
| 751 |
pass |
| 752 |
|
| 753 |
class TestX(object): |
| 754 |
def testmethod_one(self): |
| 755 |
pass |
| 756 |
|
| 757 |
class TestY(TestX): |
| 758 |
@pytest.mark.parametrize("arg0", [".["]) |
| 759 |
def testmethod_two(self, arg0): |
| 760 |
pass |
| 761 |
""" |
| 762 |
) |
| 763 |
items, _reprec = pytester.inline_genitems(p) |
| 764 |
assert len(items) == 4 |
| 765 |
assert items[0].name == "testone" |
| 766 |
assert items[1].name == "testmethod_one" |
| 767 |
assert items[2].name == "testmethod_one" |
| 768 |
assert items[3].name == "testmethod_two[.[]" |
| 769 |
|
| 770 |
# let's also test getmodpath here |
| 771 |
assert items[0].getmodpath() == "testone" # type: ignore[attr-defined] |
| 772 |
assert items[1].getmodpath() == "TestX.testmethod_one" # type: ignore[attr-defined] |
| 773 |
assert items[2].getmodpath() == "TestY.testmethod_one" # type: ignore[attr-defined] |
| 774 |
# PR #6202: Fix incorrect result of getmodpath method. (Resolves issue #6189) |
| 775 |
assert items[3].getmodpath() == "TestY.testmethod_two[.[]" # type: ignore[attr-defined] |
| 776 |
|
| 777 |
s = items[0].getmodpath(stopatmodule=False) # type: ignore[attr-defined] |
| 778 |
assert s.endswith("test_example_items1.testone") |
| 779 |
print(s) |
| 780 |
|
| 781 |
def test_classmethod_is_discovered(self, pytester: Pytester) -> None: |
| 782 |
"""Test that classmethods are discovered""" |
| 783 |
p = pytester.makepyfile( |
| 784 |
""" |
| 785 |
class TestCase: |
| 786 |
@classmethod |
| 787 |
def test_classmethod(cls) -> None: |
| 788 |
pass |
| 789 |
""" |
| 790 |
) |
| 791 |
items, _reprec = pytester.inline_genitems(p) |
| 792 |
ids = [x.getmodpath() for x in items] # type: ignore[attr-defined] |
| 793 |
assert ids == ["TestCase.test_classmethod"] |
| 794 |
|
| 795 |
def test_class_and_functions_discovery_using_glob(self, pytester: Pytester) -> None: |
| 796 |
"""Test that Python_classes and Python_functions config options work |
| 797 |
as prefixes and glob-like patterns (#600).""" |
| 798 |
pytester.makeini( |
| 799 |
""" |
| 800 |
[pytest] |
| 801 |
python_classes = *Suite Test |
| 802 |
python_functions = *_test test |
| 803 |
""" |
| 804 |
) |
| 805 |
p = pytester.makepyfile( |
| 806 |
""" |
| 807 |
class MyTestSuite(object): |
| 808 |
def x_test(self): |
| 809 |
pass |
| 810 |
|
| 811 |
class TestCase(object): |
| 812 |
def test_y(self): |
| 813 |
pass |
| 814 |
""" |
| 815 |
) |
| 816 |
items, _reprec = pytester.inline_genitems(p) |
| 817 |
ids = [x.getmodpath() for x in items] # type: ignore[attr-defined] |
| 818 |
assert ids == ["MyTestSuite.x_test", "TestCase.test_y"] |
| 819 |
|
| 820 |
|
| 821 |
def test_matchnodes_two_collections_same_file(pytester: Pytester) -> None: |
| 822 |
pytester.makeconftest( |
| 823 |
""" |
| 824 |
import pytest |
| 825 |
def pytest_configure(config): |
| 826 |
config.pluginmanager.register(Plugin2()) |
| 827 |
|
| 828 |
class Plugin2(object): |
| 829 |
def pytest_collect_file(self, file_path, parent): |
| 830 |
if file_path.suffix == ".abc": |
| 831 |
return MyFile2.from_parent(path=file_path, parent=parent) |
| 832 |
|
| 833 |
def pytest_collect_file(file_path, parent): |
| 834 |
if file_path.suffix == ".abc": |
| 835 |
return MyFile1.from_parent(path=file_path, parent=parent) |
| 836 |
|
| 837 |
class MyFile1(pytest.File): |
| 838 |
def collect(self): |
| 839 |
yield Item1.from_parent(name="item1", parent=self) |
| 840 |
|
| 841 |
class MyFile2(pytest.File): |
| 842 |
def collect(self): |
| 843 |
yield Item2.from_parent(name="item2", parent=self) |
| 844 |
|
| 845 |
class Item1(pytest.Item): |
| 846 |
def runtest(self): |
| 847 |
pass |
| 848 |
|
| 849 |
class Item2(pytest.Item): |
| 850 |
def runtest(self): |
| 851 |
pass |
| 852 |
""" |
| 853 |
) |
| 854 |
p = pytester.makefile(".abc", "") |
| 855 |
result = pytester.runpytest() |
| 856 |
assert result.ret == 0 |
| 857 |
result.stdout.fnmatch_lines(["*2 passed*"]) |
| 858 |
res = pytester.runpytest(f"{p.name}::item2") |
| 859 |
res.stdout.fnmatch_lines(["*1 passed*"]) |
| 860 |
|
| 861 |
|
| 862 |
class TestNodeKeywords: |
| 863 |
def test_no_under(self, pytester: Pytester) -> None: |
| 864 |
modcol = pytester.getmodulecol( |
| 865 |
""" |
| 866 |
def test_pass(): pass |
| 867 |
def test_fail(): assert 0 |
| 868 |
""" |
| 869 |
) |
| 870 |
values = list(modcol.keywords) |
| 871 |
assert modcol.name in values |
| 872 |
for x in values: |
| 873 |
assert not x.startswith("_") |
| 874 |
assert modcol.name in repr(modcol.keywords) |
| 875 |
|
| 876 |
def test_issue345(self, pytester: Pytester) -> None: |
| 877 |
pytester.makepyfile( |
| 878 |
""" |
| 879 |
def test_should_not_be_selected(): |
| 880 |
assert False, 'I should not have been selected to run' |
| 881 |
|
| 882 |
def test___repr__(): |
| 883 |
pass |
| 884 |
""" |
| 885 |
) |
| 886 |
reprec = pytester.inline_run("-k repr") |
| 887 |
reprec.assertoutcome(passed=1, failed=0) |
| 888 |
|
| 889 |
def test_keyword_matching_is_case_insensitive_by_default( |
| 890 |
self, pytester: Pytester |
| 891 |
) -> None: |
| 892 |
"""Check that selection via -k EXPRESSION is case-insensitive. |
| 893 |
|
| 894 |
Since markers are also added to the node keywords, they too can |
| 895 |
be matched without having to think about case sensitivity. |
| 896 |
|
| 897 |
""" |
| 898 |
pytester.makepyfile( |
| 899 |
""" |
| 900 |
import pytest |
| 901 |
|
| 902 |
def test_sPeCiFiCToPiC_1(): |
| 903 |
assert True |
| 904 |
|
| 905 |
class TestSpecificTopic_2: |
| 906 |
def test(self): |
| 907 |
assert True |
| 908 |
|
| 909 |
@pytest.mark.sPeCiFiCToPic_3 |
| 910 |
def test(): |
| 911 |
assert True |
| 912 |
|
| 913 |
@pytest.mark.sPeCiFiCToPic_4 |
| 914 |
class Test: |
| 915 |
def test(self): |
| 916 |
assert True |
| 917 |
|
| 918 |
def test_failing_5(): |
| 919 |
assert False, "This should not match" |
| 920 |
|
| 921 |
""" |
| 922 |
) |
| 923 |
num_matching_tests = 4 |
| 924 |
for expression in ("specifictopic", "SPECIFICTOPIC", "SpecificTopic"): |
| 925 |
reprec = pytester.inline_run("-k " + expression) |
| 926 |
reprec.assertoutcome(passed=num_matching_tests, failed=0) |
| 927 |
|
| 928 |
def test_duplicates_handled_correctly(self, pytester: Pytester) -> None: |
| 929 |
item = pytester.getitem( |
| 930 |
""" |
| 931 |
import pytest |
| 932 |
pytestmark = pytest.mark.kw |
| 933 |
class TestClass: |
| 934 |
pytestmark = pytest.mark.kw |
| 935 |
def test_method(self): pass |
| 936 |
test_method.kw = 'method' |
| 937 |
""", |
| 938 |
"test_method", |
| 939 |
) |
| 940 |
assert item.parent is not None and item.parent.parent is not None |
| 941 |
item.parent.parent.keywords["kw"] = "class" |
| 942 |
|
| 943 |
assert item.keywords["kw"] == "method" |
| 944 |
assert len(item.keywords) == len(set(item.keywords)) |
| 945 |
|
| 946 |
def test_unpacked_marks_added_to_keywords(self, pytester: Pytester) -> None: |
| 947 |
item = pytester.getitem( |
| 948 |
""" |
| 949 |
import pytest |
| 950 |
pytestmark = pytest.mark.foo |
| 951 |
class TestClass: |
| 952 |
pytestmark = pytest.mark.bar |
| 953 |
def test_method(self): pass |
| 954 |
test_method.pytestmark = pytest.mark.baz |
| 955 |
""", |
| 956 |
"test_method", |
| 957 |
) |
| 958 |
assert isinstance(item, pytest.Function) |
| 959 |
cls = item.getparent(pytest.Class) |
| 960 |
assert cls is not None |
| 961 |
mod = item.getparent(pytest.Module) |
| 962 |
assert mod is not None |
| 963 |
|
| 964 |
assert item.keywords["foo"] == pytest.mark.foo.mark |
| 965 |
assert item.keywords["bar"] == pytest.mark.bar.mark |
| 966 |
assert item.keywords["baz"] == pytest.mark.baz.mark |
| 967 |
|
| 968 |
assert cls.keywords["foo"] == pytest.mark.foo.mark |
| 969 |
assert cls.keywords["bar"] == pytest.mark.bar.mark |
| 970 |
assert "baz" not in cls.keywords |
| 971 |
|
| 972 |
assert mod.keywords["foo"] == pytest.mark.foo.mark |
| 973 |
assert "bar" not in mod.keywords |
| 974 |
assert "baz" not in mod.keywords |
| 975 |
|
| 976 |
|
| 977 |
class TestCollectDirectoryHook: |
| 978 |
def test_custom_directory_example(self, pytester: Pytester) -> None: |
| 979 |
"""Verify the example from the customdirectory.rst doc.""" |
| 980 |
pytester.copy_example("customdirectory") |
| 981 |
|
| 982 |
reprec = pytester.inline_run() |
| 983 |
|
| 984 |
reprec.assertoutcome(passed=2, failed=0) |
| 985 |
calls = reprec.getcalls("pytest_collect_directory") |
| 986 |
assert len(calls) == 2 |
| 987 |
assert calls[0].path == pytester.path |
| 988 |
assert isinstance(calls[0].parent, pytest.Session) |
| 989 |
assert calls[1].path == pytester.path / "tests" |
| 990 |
assert isinstance(calls[1].parent, pytest.Dir) |
| 991 |
|
| 992 |
def test_directory_ignored_if_none(self, pytester: Pytester) -> None: |
| 993 |
"""If the (entire) hook returns None, it's OK, the directory is ignored.""" |
| 994 |
pytester.makeconftest( |
| 995 |
""" |
| 996 |
import pytest |
| 997 |
|
| 998 |
@pytest.hookimpl(wrapper=True) |
| 999 |
def pytest_collect_directory(): |
| 1000 |
yield |
| 1001 |
return None |
| 1002 |
""", |
| 1003 |
) |
| 1004 |
pytester.makepyfile( |
| 1005 |
**{ |
| 1006 |
"tests/test_it.py": """ |
| 1007 |
import pytest |
| 1008 |
|
| 1009 |
def test_it(): pass |
| 1010 |
""", |
| 1011 |
}, |
| 1012 |
) |
| 1013 |
reprec = pytester.inline_run() |
| 1014 |
reprec.assertoutcome(passed=0, failed=0) |
| 1015 |
|
| 1016 |
|
| 1017 |
COLLECTION_ERROR_PY_FILES = dict( |
| 1018 |
test_01_failure=""" |
| 1019 |
def test_1(): |
| 1020 |
assert False |
| 1021 |
""", |
| 1022 |
test_02_import_error=""" |
| 1023 |
import asdfasdfasdf |
| 1024 |
def test_2(): |
| 1025 |
assert True |
| 1026 |
""", |
| 1027 |
test_03_import_error=""" |
| 1028 |
import asdfasdfasdf |
| 1029 |
def test_3(): |
| 1030 |
assert True |
| 1031 |
""", |
| 1032 |
test_04_success=""" |
| 1033 |
def test_4(): |
| 1034 |
assert True |
| 1035 |
""", |
| 1036 |
) |
| 1037 |
|
| 1038 |
|
| 1039 |
def test_exit_on_collection_error(pytester: Pytester) -> None: |
| 1040 |
"""Verify that all collection errors are collected and no tests executed""" |
| 1041 |
pytester.makepyfile(**COLLECTION_ERROR_PY_FILES) |
| 1042 |
|
| 1043 |
res = pytester.runpytest() |
| 1044 |
assert res.ret == 2 |
| 1045 |
|
| 1046 |
res.stdout.fnmatch_lines( |
| 1047 |
[ |
| 1048 |
"collected 2 items / 2 errors", |
| 1049 |
"*ERROR collecting test_02_import_error.py*", |
| 1050 |
"*No module named *asdfa*", |
| 1051 |
"*ERROR collecting test_03_import_error.py*", |
| 1052 |
"*No module named *asdfa*", |
| 1053 |
] |
| 1054 |
) |
| 1055 |
|
| 1056 |
|
| 1057 |
def test_exit_on_collection_with_maxfail_smaller_than_n_errors( |
| 1058 |
pytester: Pytester, |
| 1059 |
) -> None: |
| 1060 |
""" |
| 1061 |
Verify collection is aborted once maxfail errors are encountered ignoring |
| 1062 |
further modules which would cause more collection errors. |
| 1063 |
""" |
| 1064 |
pytester.makepyfile(**COLLECTION_ERROR_PY_FILES) |
| 1065 |
|
| 1066 |
res = pytester.runpytest("--maxfail=1") |
| 1067 |
assert res.ret == 1 |
| 1068 |
res.stdout.fnmatch_lines( |
| 1069 |
[ |
| 1070 |
"collected 1 item / 1 error", |
| 1071 |
"*ERROR collecting test_02_import_error.py*", |
| 1072 |
"*No module named *asdfa*", |
| 1073 |
"*! stopping after 1 failures !*", |
| 1074 |
"*= 1 error in *", |
| 1075 |
] |
| 1076 |
) |
| 1077 |
res.stdout.no_fnmatch_line("*test_03*") |
| 1078 |
|
| 1079 |
|
| 1080 |
def test_exit_on_collection_with_maxfail_bigger_than_n_errors( |
| 1081 |
pytester: Pytester, |
| 1082 |
) -> None: |
| 1083 |
""" |
| 1084 |
Verify the test run aborts due to collection errors even if maxfail count of |
| 1085 |
errors was not reached. |
| 1086 |
""" |
| 1087 |
pytester.makepyfile(**COLLECTION_ERROR_PY_FILES) |
| 1088 |
|
| 1089 |
res = pytester.runpytest("--maxfail=4") |
| 1090 |
assert res.ret == 2 |
| 1091 |
res.stdout.fnmatch_lines( |
| 1092 |
[ |
| 1093 |
"collected 2 items / 2 errors", |
| 1094 |
"*ERROR collecting test_02_import_error.py*", |
| 1095 |
"*No module named *asdfa*", |
| 1096 |
"*ERROR collecting test_03_import_error.py*", |
| 1097 |
"*No module named *asdfa*", |
| 1098 |
"*! Interrupted: 2 errors during collection !*", |
| 1099 |
"*= 2 errors in *", |
| 1100 |
] |
| 1101 |
) |
| 1102 |
|
| 1103 |
|
| 1104 |
def test_continue_on_collection_errors(pytester: Pytester) -> None: |
| 1105 |
""" |
| 1106 |
Verify tests are executed even when collection errors occur when the |
| 1107 |
--continue-on-collection-errors flag is set |
| 1108 |
""" |
| 1109 |
pytester.makepyfile(**COLLECTION_ERROR_PY_FILES) |
| 1110 |
|
| 1111 |
res = pytester.runpytest("--continue-on-collection-errors") |
| 1112 |
assert res.ret == 1 |
| 1113 |
|
| 1114 |
res.stdout.fnmatch_lines( |
| 1115 |
["collected 2 items / 2 errors", "*1 failed, 1 passed, 2 errors*"] |
| 1116 |
) |
| 1117 |
|
| 1118 |
|
| 1119 |
def test_continue_on_collection_errors_maxfail(pytester: Pytester) -> None: |
| 1120 |
""" |
| 1121 |
Verify tests are executed even when collection errors occur and that maxfail |
| 1122 |
is honoured (including the collection error count). |
| 1123 |
4 tests: 2 collection errors + 1 failure + 1 success |
| 1124 |
test_4 is never executed because the test run is with --maxfail=3 which |
| 1125 |
means it is interrupted after the 2 collection errors + 1 failure. |
| 1126 |
""" |
| 1127 |
pytester.makepyfile(**COLLECTION_ERROR_PY_FILES) |
| 1128 |
|
| 1129 |
res = pytester.runpytest("--continue-on-collection-errors", "--maxfail=3") |
| 1130 |
assert res.ret == 1 |
| 1131 |
|
| 1132 |
res.stdout.fnmatch_lines(["collected 2 items / 2 errors", "*1 failed, 2 errors*"]) |
| 1133 |
|
| 1134 |
|
| 1135 |
def test_fixture_scope_sibling_conftests(pytester: Pytester) -> None: |
| 1136 |
"""Regression test case for https://github.com/pytest-dev/pytest/issues/2836""" |
| 1137 |
foo_path = pytester.mkdir("foo") |
| 1138 |
foo_path.joinpath("conftest.py").write_text( |
| 1139 |
textwrap.dedent( |
| 1140 |
"""\ |
| 1141 |
import pytest |
| 1142 |
@pytest.fixture |
| 1143 |
def fix(): |
| 1144 |
return 1 |
| 1145 |
""" |
| 1146 |
), |
| 1147 |
encoding="utf-8", |
| 1148 |
) |
| 1149 |
foo_path.joinpath("test_foo.py").write_text( |
| 1150 |
"def test_foo(fix): assert fix == 1", encoding="utf-8" |
| 1151 |
) |
| 1152 |
|
| 1153 |
# Tests in `food/` should not see the conftest fixture from `foo/` |
| 1154 |
food_path = pytester.mkpydir("food") |
| 1155 |
food_path.joinpath("test_food.py").write_text( |
| 1156 |
"def test_food(fix): assert fix == 1", encoding="utf-8" |
| 1157 |
) |
| 1158 |
|
| 1159 |
res = pytester.runpytest() |
| 1160 |
assert res.ret == 1 |
| 1161 |
|
| 1162 |
res.stdout.fnmatch_lines( |
| 1163 |
[ |
| 1164 |
"*ERROR at setup of test_food*", |
| 1165 |
"E*fixture 'fix' not found", |
| 1166 |
"*1 passed, 1 error*", |
| 1167 |
] |
| 1168 |
) |
| 1169 |
|
| 1170 |
|
| 1171 |
def test_collect_init_tests(pytester: Pytester) -> None: |
| 1172 |
"""Check that we collect files from __init__.py files when they patch the 'python_files' (#3773)""" |
| 1173 |
p = pytester.copy_example("collect/collect_init_tests") |
| 1174 |
result = pytester.runpytest(p, "--collect-only") |
| 1175 |
result.stdout.fnmatch_lines( |
| 1176 |
[ |
| 1177 |
"collected 2 items", |
| 1178 |
"<Dir *>", |
| 1179 |
" <Package tests>", |
| 1180 |
" <Module __init__.py>", |
| 1181 |
" <Function test_init>", |
| 1182 |
" <Module test_foo.py>", |
| 1183 |
" <Function test_foo>", |
| 1184 |
] |
| 1185 |
) |
| 1186 |
result = pytester.runpytest("./tests", "--collect-only") |
| 1187 |
result.stdout.fnmatch_lines( |
| 1188 |
[ |
| 1189 |
"collected 2 items", |
| 1190 |
"<Dir *>", |
| 1191 |
" <Package tests>", |
| 1192 |
" <Module __init__.py>", |
| 1193 |
" <Function test_init>", |
| 1194 |
" <Module test_foo.py>", |
| 1195 |
" <Function test_foo>", |
| 1196 |
] |
| 1197 |
) |
| 1198 |
# Ignores duplicates with "." and pkginit (#4310). |
| 1199 |
result = pytester.runpytest("./tests", ".", "--collect-only") |
| 1200 |
result.stdout.fnmatch_lines( |
| 1201 |
[ |
| 1202 |
"collected 2 items", |
| 1203 |
"<Dir *>", |
| 1204 |
" <Package tests>", |
| 1205 |
" <Module __init__.py>", |
| 1206 |
" <Function test_init>", |
| 1207 |
" <Module test_foo.py>", |
| 1208 |
" <Function test_foo>", |
| 1209 |
] |
| 1210 |
) |
| 1211 |
# Same as before, but different order. |
| 1212 |
result = pytester.runpytest(".", "tests", "--collect-only") |
| 1213 |
result.stdout.fnmatch_lines( |
| 1214 |
[ |
| 1215 |
"collected 2 items", |
| 1216 |
"<Dir *>", |
| 1217 |
" <Package tests>", |
| 1218 |
" <Module __init__.py>", |
| 1219 |
" <Function test_init>", |
| 1220 |
" <Module test_foo.py>", |
| 1221 |
" <Function test_foo>", |
| 1222 |
] |
| 1223 |
) |
| 1224 |
result = pytester.runpytest("./tests/test_foo.py", "--collect-only") |
| 1225 |
result.stdout.fnmatch_lines( |
| 1226 |
[ |
| 1227 |
"<Dir *>", |
| 1228 |
" <Package tests>", |
| 1229 |
" <Module test_foo.py>", |
| 1230 |
" <Function test_foo>", |
| 1231 |
] |
| 1232 |
) |
| 1233 |
result.stdout.no_fnmatch_line("*test_init*") |
| 1234 |
result = pytester.runpytest("./tests/__init__.py", "--collect-only") |
| 1235 |
result.stdout.fnmatch_lines( |
| 1236 |
[ |
| 1237 |
"<Dir *>", |
| 1238 |
" <Package tests>", |
| 1239 |
" <Module __init__.py>", |
| 1240 |
" <Function test_init>", |
| 1241 |
] |
| 1242 |
) |
| 1243 |
result.stdout.no_fnmatch_line("*test_foo*") |
| 1244 |
|
| 1245 |
|
| 1246 |
def test_collect_invalid_signature_message(pytester: Pytester) -> None: |
| 1247 |
"""Check that we issue a proper message when we can't determine the signature of a test |
| 1248 |
function (#4026). |
| 1249 |
""" |
| 1250 |
pytester.makepyfile( |
| 1251 |
""" |
| 1252 |
import pytest |
| 1253 |
|
| 1254 |
class TestCase: |
| 1255 |
@pytest.fixture |
| 1256 |
def fix(): |
| 1257 |
pass |
| 1258 |
""" |
| 1259 |
) |
| 1260 |
result = pytester.runpytest() |
| 1261 |
result.stdout.fnmatch_lines( |
| 1262 |
["Could not determine arguments of *.fix *: invalid method signature"] |
| 1263 |
) |
| 1264 |
|
| 1265 |
|
| 1266 |
def test_collect_handles_raising_on_dunder_class(pytester: Pytester) -> None: |
| 1267 |
"""Handle proxy classes like Django's LazySettings that might raise on |
| 1268 |
``isinstance`` (#4266). |
| 1269 |
""" |
| 1270 |
pytester.makepyfile( |
| 1271 |
""" |
| 1272 |
class ImproperlyConfigured(Exception): |
| 1273 |
pass |
| 1274 |
|
| 1275 |
class RaisesOnGetAttr(object): |
| 1276 |
def raises(self): |
| 1277 |
raise ImproperlyConfigured |
| 1278 |
|
| 1279 |
__class__ = property(raises) |
| 1280 |
|
| 1281 |
raises = RaisesOnGetAttr() |
| 1282 |
|
| 1283 |
|
| 1284 |
def test_1(): |
| 1285 |
pass |
| 1286 |
""" |
| 1287 |
) |
| 1288 |
result = pytester.runpytest() |
| 1289 |
result.assert_outcomes(passed=1) |
| 1290 |
assert result.ret == 0 |
| 1291 |
|
| 1292 |
|
| 1293 |
def test_collect_with_chdir_during_import(pytester: Pytester) -> None: |
| 1294 |
subdir = pytester.mkdir("sub") |
| 1295 |
pytester.path.joinpath("conftest.py").write_text( |
| 1296 |
textwrap.dedent( |
| 1297 |
f""" |
| 1298 |
import os |
| 1299 |
os.chdir({str(subdir)!r}) |
| 1300 |
""" |
| 1301 |
), |
| 1302 |
encoding="utf-8", |
| 1303 |
) |
| 1304 |
pytester.makepyfile( |
| 1305 |
f""" |
| 1306 |
def test_1(): |
| 1307 |
import os |
| 1308 |
assert os.getcwd() == {str(subdir)!r} |
| 1309 |
""" |
| 1310 |
) |
| 1311 |
result = pytester.runpytest() |
| 1312 |
result.stdout.fnmatch_lines(["*1 passed in*"]) |
| 1313 |
assert result.ret == 0 |
| 1314 |
|
| 1315 |
# Handles relative testpaths. |
| 1316 |
pytester.makeini( |
| 1317 |
""" |
| 1318 |
[pytest] |
| 1319 |
testpaths = . |
| 1320 |
""" |
| 1321 |
) |
| 1322 |
result = pytester.runpytest("--collect-only") |
| 1323 |
result.stdout.fnmatch_lines(["collected 1 item"]) |
| 1324 |
|
| 1325 |
|
| 1326 |
def test_collect_pyargs_with_testpaths( |
| 1327 |
pytester: Pytester, monkeypatch: MonkeyPatch |
| 1328 |
) -> None: |
| 1329 |
testmod = pytester.mkdir("testmod") |
| 1330 |
# NOTE: __init__.py is not collected since it does not match python_files. |
| 1331 |
testmod.joinpath("__init__.py").write_text( |
| 1332 |
"def test_func(): pass", encoding="utf-8" |
| 1333 |
) |
| 1334 |
testmod.joinpath("test_file.py").write_text( |
| 1335 |
"def test_func(): pass", encoding="utf-8" |
| 1336 |
) |
| 1337 |
|
| 1338 |
root = pytester.mkdir("root") |
| 1339 |
root.joinpath("pytest.ini").write_text( |
| 1340 |
textwrap.dedent( |
| 1341 |
""" |
| 1342 |
[pytest] |
| 1343 |
addopts = --pyargs |
| 1344 |
testpaths = testmod |
| 1345 |
""" |
| 1346 |
), |
| 1347 |
encoding="utf-8", |
| 1348 |
) |
| 1349 |
monkeypatch.setenv("PYTHONPATH", str(pytester.path), prepend=os.pathsep) |
| 1350 |
with monkeypatch.context() as mp: |
| 1351 |
mp.chdir(root) |
| 1352 |
result = pytester.runpytest_subprocess() |
| 1353 |
result.assert_outcomes(passed=1) |
| 1354 |
|
| 1355 |
|
| 1356 |
def test_initial_conftests_with_testpaths(pytester: Pytester) -> None: |
| 1357 |
"""The testpaths config option should load conftests in those paths as 'initial' (#10987).""" |
| 1358 |
p = pytester.mkdir("some_path") |
| 1359 |
p.joinpath("conftest.py").write_text( |
| 1360 |
textwrap.dedent( |
| 1361 |
""" |
| 1362 |
def pytest_sessionstart(session): |
| 1363 |
raise Exception("pytest_sessionstart hook successfully run") |
| 1364 |
""" |
| 1365 |
), |
| 1366 |
encoding="utf-8", |
| 1367 |
) |
| 1368 |
pytester.makeini( |
| 1369 |
""" |
| 1370 |
[pytest] |
| 1371 |
testpaths = some_path |
| 1372 |
""" |
| 1373 |
) |
| 1374 |
|
| 1375 |
# No command line args - falls back to testpaths. |
| 1376 |
result = pytester.runpytest() |
| 1377 |
assert result.ret == ExitCode.INTERNAL_ERROR |
| 1378 |
result.stdout.fnmatch_lines( |
| 1379 |
"INTERNALERROR* Exception: pytest_sessionstart hook successfully run" |
| 1380 |
) |
| 1381 |
|
| 1382 |
# No fallback. |
| 1383 |
result = pytester.runpytest(".") |
| 1384 |
assert result.ret == ExitCode.NO_TESTS_COLLECTED |
| 1385 |
|
| 1386 |
|
| 1387 |
def test_large_option_breaks_initial_conftests(pytester: Pytester) -> None: |
| 1388 |
"""Long option values do not break initial conftests handling (#10169).""" |
| 1389 |
option_value = "x" * 1024 * 1000 |
| 1390 |
pytester.makeconftest( |
| 1391 |
""" |
| 1392 |
def pytest_addoption(parser): |
| 1393 |
parser.addoption("--xx", default=None) |
| 1394 |
""" |
| 1395 |
) |
| 1396 |
pytester.makepyfile( |
| 1397 |
f""" |
| 1398 |
def test_foo(request): |
| 1399 |
assert request.config.getoption("xx") == {option_value!r} |
| 1400 |
""" |
| 1401 |
) |
| 1402 |
result = pytester.runpytest(f"--xx={option_value}") |
| 1403 |
assert result.ret == 0 |
| 1404 |
|
| 1405 |
|
| 1406 |
def test_collect_symlink_file_arg(pytester: Pytester) -> None: |
| 1407 |
"""Collect a direct symlink works even if it does not match python_files (#4325).""" |
| 1408 |
real = pytester.makepyfile( |
| 1409 |
real=""" |
| 1410 |
def test_nodeid(request): |
| 1411 |
assert request.node.nodeid == "symlink.py::test_nodeid" |
| 1412 |
""" |
| 1413 |
) |
| 1414 |
symlink = pytester.path.joinpath("symlink.py") |
| 1415 |
symlink_or_skip(real, symlink) |
| 1416 |
result = pytester.runpytest("-v", symlink) |
| 1417 |
result.stdout.fnmatch_lines(["symlink.py::test_nodeid PASSED*", "*1 passed in*"]) |
| 1418 |
assert result.ret == 0 |
| 1419 |
|
| 1420 |
|
| 1421 |
def test_collect_symlink_out_of_tree(pytester: Pytester) -> None: |
| 1422 |
"""Test collection of symlink via out-of-tree rootdir.""" |
| 1423 |
sub = pytester.mkdir("sub") |
| 1424 |
real = sub.joinpath("test_real.py") |
| 1425 |
real.write_text( |
| 1426 |
textwrap.dedent( |
| 1427 |
""" |
| 1428 |
def test_nodeid(request): |
| 1429 |
# Should not contain sub/ prefix. |
| 1430 |
assert request.node.nodeid == "test_real.py::test_nodeid" |
| 1431 |
""" |
| 1432 |
), |
| 1433 |
encoding="utf-8", |
| 1434 |
) |
| 1435 |
|
| 1436 |
out_of_tree = pytester.mkdir("out_of_tree") |
| 1437 |
symlink_to_sub = out_of_tree.joinpath("symlink_to_sub") |
| 1438 |
symlink_or_skip(sub, symlink_to_sub) |
| 1439 |
os.chdir(sub) |
| 1440 |
result = pytester.runpytest("-vs", f"--rootdir={sub}", symlink_to_sub) |
| 1441 |
result.stdout.fnmatch_lines( |
| 1442 |
[ |
| 1443 |
# Should not contain "sub/"! |
| 1444 |
"test_real.py::test_nodeid PASSED" |
| 1445 |
] |
| 1446 |
) |
| 1447 |
assert result.ret == 0 |
| 1448 |
|
| 1449 |
|
| 1450 |
def test_collect_symlink_dir(pytester: Pytester) -> None: |
| 1451 |
"""A symlinked directory is collected.""" |
| 1452 |
dir = pytester.mkdir("dir") |
| 1453 |
dir.joinpath("test_it.py").write_text("def test_it(): pass", "utf-8") |
| 1454 |
symlink_or_skip(pytester.path.joinpath("symlink_dir"), dir) |
| 1455 |
result = pytester.runpytest() |
| 1456 |
result.assert_outcomes(passed=2) |
| 1457 |
|
| 1458 |
|
| 1459 |
def test_collectignore_via_conftest(pytester: Pytester) -> None: |
| 1460 |
"""collect_ignore in parent conftest skips importing child (issue #4592).""" |
| 1461 |
tests = pytester.mkpydir("tests") |
| 1462 |
tests.joinpath("conftest.py").write_text( |
| 1463 |
"collect_ignore = ['ignore_me']", encoding="utf-8" |
| 1464 |
) |
| 1465 |
|
| 1466 |
ignore_me = tests.joinpath("ignore_me") |
| 1467 |
ignore_me.mkdir() |
| 1468 |
ignore_me.joinpath("__init__.py").touch() |
| 1469 |
ignore_me.joinpath("conftest.py").write_text( |
| 1470 |
"assert 0, 'should_not_be_called'", encoding="utf-8" |
| 1471 |
) |
| 1472 |
|
| 1473 |
result = pytester.runpytest() |
| 1474 |
assert result.ret == ExitCode.NO_TESTS_COLLECTED |
| 1475 |
|
| 1476 |
|
| 1477 |
def test_collect_pkg_init_and_file_in_args(pytester: Pytester) -> None: |
| 1478 |
subdir = pytester.mkdir("sub") |
| 1479 |
init = subdir.joinpath("__init__.py") |
| 1480 |
init.write_text("def test_init(): pass", encoding="utf-8") |
| 1481 |
p = subdir.joinpath("test_file.py") |
| 1482 |
p.write_text("def test_file(): pass", encoding="utf-8") |
| 1483 |
|
| 1484 |
# Just the package directory, the __init__.py module is filtered out. |
| 1485 |
result = pytester.runpytest("-v", subdir) |
| 1486 |
result.stdout.fnmatch_lines( |
| 1487 |
[ |
| 1488 |
"sub/test_file.py::test_file PASSED*", |
| 1489 |
"*1 passed in*", |
| 1490 |
] |
| 1491 |
) |
| 1492 |
|
| 1493 |
# But it's included if specified directly. |
| 1494 |
result = pytester.runpytest("-v", init, p) |
| 1495 |
result.stdout.fnmatch_lines( |
| 1496 |
[ |
| 1497 |
"sub/__init__.py::test_init PASSED*", |
| 1498 |
"sub/test_file.py::test_file PASSED*", |
| 1499 |
"*2 passed in*", |
| 1500 |
] |
| 1501 |
) |
| 1502 |
|
| 1503 |
# Or if the pattern allows it. |
| 1504 |
result = pytester.runpytest("-v", "-o", "python_files=*.py", subdir) |
| 1505 |
result.stdout.fnmatch_lines( |
| 1506 |
[ |
| 1507 |
"sub/__init__.py::test_init PASSED*", |
| 1508 |
"sub/test_file.py::test_file PASSED*", |
| 1509 |
"*2 passed in*", |
| 1510 |
] |
| 1511 |
) |
| 1512 |
|
| 1513 |
|
| 1514 |
def test_collect_pkg_init_only(pytester: Pytester) -> None: |
| 1515 |
subdir = pytester.mkdir("sub") |
| 1516 |
init = subdir.joinpath("__init__.py") |
| 1517 |
init.write_text("def test_init(): pass", encoding="utf-8") |
| 1518 |
|
| 1519 |
result = pytester.runpytest(subdir) |
| 1520 |
result.stdout.fnmatch_lines(["*no tests ran in*"]) |
| 1521 |
|
| 1522 |
result = pytester.runpytest("-v", init) |
| 1523 |
result.stdout.fnmatch_lines(["sub/__init__.py::test_init PASSED*", "*1 passed in*"]) |
| 1524 |
|
| 1525 |
result = pytester.runpytest("-v", "-o", "python_files=*.py", subdir) |
| 1526 |
result.stdout.fnmatch_lines(["sub/__init__.py::test_init PASSED*", "*1 passed in*"]) |
| 1527 |
|
| 1528 |
|
| 1529 |
@pytest.mark.parametrize("use_pkg", (True, False)) |
| 1530 |
def test_collect_sub_with_symlinks(use_pkg: bool, pytester: Pytester) -> None: |
| 1531 |
"""Collection works with symlinked files and broken symlinks""" |
| 1532 |
sub = pytester.mkdir("sub") |
| 1533 |
if use_pkg: |
| 1534 |
sub.joinpath("__init__.py").touch() |
| 1535 |
sub.joinpath("test_file.py").write_text("def test_file(): pass", encoding="utf-8") |
| 1536 |
|
| 1537 |
# Create a broken symlink. |
| 1538 |
symlink_or_skip("test_doesnotexist.py", sub.joinpath("test_broken.py")) |
| 1539 |
|
| 1540 |
# Symlink that gets collected. |
| 1541 |
symlink_or_skip("test_file.py", sub.joinpath("test_symlink.py")) |
| 1542 |
|
| 1543 |
result = pytester.runpytest("-v", str(sub)) |
| 1544 |
result.stdout.fnmatch_lines( |
| 1545 |
[ |
| 1546 |
"sub/test_file.py::test_file PASSED*", |
| 1547 |
"sub/test_symlink.py::test_file PASSED*", |
| 1548 |
"*2 passed in*", |
| 1549 |
] |
| 1550 |
) |
| 1551 |
|
| 1552 |
|
| 1553 |
def test_collector_respects_tbstyle(pytester: Pytester) -> None: |
| 1554 |
p1 = pytester.makepyfile("assert 0") |
| 1555 |
result = pytester.runpytest(p1, "--tb=native") |
| 1556 |
assert result.ret == ExitCode.INTERRUPTED |
| 1557 |
result.stdout.fnmatch_lines( |
| 1558 |
[ |
| 1559 |
"*_ ERROR collecting test_collector_respects_tbstyle.py _*", |
| 1560 |
"Traceback (most recent call last):", |
| 1561 |
' File "*/test_collector_respects_tbstyle.py", line 1, in <module>', |
| 1562 |
" assert 0", |
| 1563 |
"AssertionError: assert 0", |
| 1564 |
"*! Interrupted: 1 error during collection !*", |
| 1565 |
"*= 1 error in *", |
| 1566 |
] |
| 1567 |
) |
| 1568 |
|
| 1569 |
|
| 1570 |
def test_does_not_eagerly_collect_packages(pytester: Pytester) -> None: |
| 1571 |
pytester.makepyfile("def test(): pass") |
| 1572 |
pydir = pytester.mkpydir("foopkg") |
| 1573 |
pydir.joinpath("__init__.py").write_text("assert False", encoding="utf-8") |
| 1574 |
result = pytester.runpytest() |
| 1575 |
assert result.ret == ExitCode.OK |
| 1576 |
|
| 1577 |
|
| 1578 |
def test_does_not_put_src_on_path(pytester: Pytester) -> None: |
| 1579 |
# `src` is not on sys.path so it should not be importable |
| 1580 |
ensure_file(pytester.path / "src/nope/__init__.py") |
| 1581 |
pytester.makepyfile( |
| 1582 |
"import pytest\n" |
| 1583 |
"def test():\n" |
| 1584 |
" with pytest.raises(ImportError):\n" |
| 1585 |
" import nope\n" |
| 1586 |
) |
| 1587 |
result = pytester.runpytest() |
| 1588 |
assert result.ret == ExitCode.OK |
| 1589 |
|
| 1590 |
|
| 1591 |
def test_fscollector_from_parent(pytester: Pytester, request: FixtureRequest) -> None: |
| 1592 |
"""Ensure File.from_parent can forward custom arguments to the constructor. |
| 1593 |
|
| 1594 |
Context: https://github.com/pytest-dev/pytest-cpp/pull/47 |
| 1595 |
""" |
| 1596 |
|
| 1597 |
class MyCollector(pytest.File): |
| 1598 |
def __init__(self, *k, x, **kw): |
| 1599 |
super().__init__(*k, **kw) |
| 1600 |
self.x = x |
| 1601 |
|
| 1602 |
def collect(self): |
| 1603 |
raise NotImplementedError() |
| 1604 |
|
| 1605 |
collector = MyCollector.from_parent( |
| 1606 |
parent=request.session, path=pytester.path / "foo", x=10 |
| 1607 |
) |
| 1608 |
assert collector.x == 10 |
| 1609 |
|
| 1610 |
|
| 1611 |
def test_class_from_parent(request: FixtureRequest) -> None: |
| 1612 |
"""Ensure Class.from_parent can forward custom arguments to the constructor.""" |
| 1613 |
|
| 1614 |
class MyCollector(pytest.Class): |
| 1615 |
def __init__(self, name, parent, x): |
| 1616 |
super().__init__(name, parent) |
| 1617 |
self.x = x |
| 1618 |
|
| 1619 |
@classmethod |
| 1620 |
def from_parent(cls, parent, *, name, x): # type: ignore[override] |
| 1621 |
return super().from_parent(parent=parent, name=name, x=x) |
| 1622 |
|
| 1623 |
collector = MyCollector.from_parent(parent=request.session, name="foo", x=10) |
| 1624 |
assert collector.x == 10 |
| 1625 |
|
| 1626 |
|
| 1627 |
class TestImportModeImportlib: |
| 1628 |
def test_collect_duplicate_names(self, pytester: Pytester) -> None: |
| 1629 |
"""--import-mode=importlib can import modules with same names that are not in packages.""" |
| 1630 |
pytester.makepyfile( |
| 1631 |
**{ |
| 1632 |
"tests_a/test_foo.py": "def test_foo1(): pass", |
| 1633 |
"tests_b/test_foo.py": "def test_foo2(): pass", |
| 1634 |
} |
| 1635 |
) |
| 1636 |
result = pytester.runpytest("-v", "--import-mode=importlib") |
| 1637 |
result.stdout.fnmatch_lines( |
| 1638 |
[ |
| 1639 |
"tests_a/test_foo.py::test_foo1 *", |
| 1640 |
"tests_b/test_foo.py::test_foo2 *", |
| 1641 |
"* 2 passed in *", |
| 1642 |
] |
| 1643 |
) |
| 1644 |
|
| 1645 |
def test_conftest(self, pytester: Pytester) -> None: |
| 1646 |
"""Directory containing conftest modules are not put in sys.path as a side-effect of |
| 1647 |
importing them.""" |
| 1648 |
tests_dir = pytester.path.joinpath("tests") |
| 1649 |
pytester.makepyfile( |
| 1650 |
**{ |
| 1651 |
"tests/conftest.py": "", |
| 1652 |
"tests/test_foo.py": f""" |
| 1653 |
import sys |
| 1654 |
def test_check(): |
| 1655 |
assert r"{tests_dir}" not in sys.path |
| 1656 |
""", |
| 1657 |
} |
| 1658 |
) |
| 1659 |
result = pytester.runpytest("-v", "--import-mode=importlib") |
| 1660 |
result.stdout.fnmatch_lines(["* 1 passed in *"]) |
| 1661 |
|
| 1662 |
def setup_conftest_and_foo(self, pytester: Pytester) -> None: |
| 1663 |
"""Setup a tests directory to be used to test if modules in that directory can be imported |
| 1664 |
due to side-effects of --import-mode or not.""" |
| 1665 |
pytester.makepyfile( |
| 1666 |
**{ |
| 1667 |
"tests/conftest.py": "", |
| 1668 |
"tests/foo.py": """ |
| 1669 |
def foo(): return 42 |
| 1670 |
""", |
| 1671 |
"tests/test_foo.py": """ |
| 1672 |
def test_check(): |
| 1673 |
from foo import foo |
| 1674 |
assert foo() == 42 |
| 1675 |
""", |
| 1676 |
} |
| 1677 |
) |
| 1678 |
|
| 1679 |
def test_modules_importable_as_side_effect(self, pytester: Pytester) -> None: |
| 1680 |
"""In import-modes `prepend` and `append`, we are able to import modules from directories |
| 1681 |
containing conftest.py files due to the side effect of changing sys.path.""" |
| 1682 |
self.setup_conftest_and_foo(pytester) |
| 1683 |
result = pytester.runpytest("-v", "--import-mode=prepend") |
| 1684 |
result.stdout.fnmatch_lines(["* 1 passed in *"]) |
| 1685 |
|
| 1686 |
def test_modules_not_importable_as_side_effect(self, pytester: Pytester) -> None: |
| 1687 |
"""In import-mode `importlib`, modules in directories containing conftest.py are not |
| 1688 |
importable, as don't change sys.path or sys.modules as side effect of importing |
| 1689 |
the conftest.py file. |
| 1690 |
""" |
| 1691 |
self.setup_conftest_and_foo(pytester) |
| 1692 |
result = pytester.runpytest("-v", "--import-mode=importlib") |
| 1693 |
result.stdout.fnmatch_lines( |
| 1694 |
[ |
| 1695 |
"*ModuleNotFoundError: No module named 'foo'", |
| 1696 |
"tests?test_foo.py:2: ModuleNotFoundError", |
| 1697 |
"* 1 failed in *", |
| 1698 |
] |
| 1699 |
) |
| 1700 |
|
| 1701 |
def test_using_python_path(self, pytester: Pytester) -> None: |
| 1702 |
""" |
| 1703 |
Dummy modules created by insert_missing_modules should not get in |
| 1704 |
the way of modules that could be imported via python path (#9645). |
| 1705 |
""" |
| 1706 |
pytester.makeini( |
| 1707 |
""" |
| 1708 |
[pytest] |
| 1709 |
pythonpath = . |
| 1710 |
addopts = --import-mode importlib |
| 1711 |
""" |
| 1712 |
) |
| 1713 |
pytester.makepyfile( |
| 1714 |
**{ |
| 1715 |
"tests/__init__.py": "", |
| 1716 |
"tests/conftest.py": "", |
| 1717 |
"tests/subpath/__init__.py": "", |
| 1718 |
"tests/subpath/helper.py": "", |
| 1719 |
"tests/subpath/test_something.py": """ |
| 1720 |
import tests.subpath.helper |
| 1721 |
|
| 1722 |
def test_something(): |
| 1723 |
assert True |
| 1724 |
""", |
| 1725 |
} |
| 1726 |
) |
| 1727 |
result = pytester.runpytest() |
| 1728 |
result.stdout.fnmatch_lines("*1 passed in*") |
| 1729 |
|
| 1730 |
|
| 1731 |
def test_does_not_crash_on_error_from_decorated_function(pytester: Pytester) -> None: |
| 1732 |
"""Regression test for an issue around bad exception formatting due to |
| 1733 |
assertion rewriting mangling lineno's (#4984).""" |
| 1734 |
pytester.makepyfile( |
| 1735 |
""" |
| 1736 |
@pytest.fixture |
| 1737 |
def a(): return 4 |
| 1738 |
""" |
| 1739 |
) |
| 1740 |
result = pytester.runpytest() |
| 1741 |
# Not INTERNAL_ERROR |
| 1742 |
assert result.ret == ExitCode.INTERRUPTED |
| 1743 |
|
| 1744 |
|
| 1745 |
def test_does_not_crash_on_recursive_symlink(pytester: Pytester) -> None: |
| 1746 |
"""Regression test for an issue around recursive symlinks (#7951).""" |
| 1747 |
symlink_or_skip("recursive", pytester.path.joinpath("recursive")) |
| 1748 |
pytester.makepyfile( |
| 1749 |
""" |
| 1750 |
def test_foo(): assert True |
| 1751 |
""" |
| 1752 |
) |
| 1753 |
result = pytester.runpytest() |
| 1754 |
|
| 1755 |
assert result.ret == ExitCode.OK |
| 1756 |
assert result.parseoutcomes() == {"passed": 1} |
| 1757 |
|
| 1758 |
|
| 1759 |
@pytest.mark.skipif(not sys.platform.startswith("win"), reason="Windows only") |
| 1760 |
def test_collect_short_file_windows(pytester: Pytester) -> None: |
| 1761 |
"""Reproducer for #11895: short paths not collected on Windows.""" |
| 1762 |
short_path = tempfile.mkdtemp() |
| 1763 |
if "~" not in short_path: # pragma: no cover |
| 1764 |
if running_on_ci(): |
| 1765 |
# On CI, we are expecting that under the current GitHub actions configuration, |
| 1766 |
# tempfile.mkdtemp() is producing short paths, so we want to fail to prevent |
| 1767 |
# this from silently changing without us noticing. |
| 1768 |
pytest.fail( |
| 1769 |
f"tempfile.mkdtemp() failed to produce a short path on CI: {short_path}" |
| 1770 |
) |
| 1771 |
else: |
| 1772 |
# We want to skip failing this test locally in this situation because |
| 1773 |
# depending on the local configuration tempfile.mkdtemp() might not produce a short path: |
| 1774 |
# For example, user might have configured %TEMP% exactly to avoid generating short paths. |
| 1775 |
pytest.skip( |
| 1776 |
f"tempfile.mkdtemp() failed to produce a short path: {short_path}, skipping" |
| 1777 |
) |
| 1778 |
|
| 1779 |
test_file = Path(short_path).joinpath("test_collect_short_file_windows.py") |
| 1780 |
test_file.write_text("def test(): pass", encoding="UTF-8") |
| 1781 |
result = pytester.runpytest(short_path) |
| 1782 |
assert result.parseoutcomes() == {"passed": 1} |
| 1783 |
|
| 1784 |
|
| 1785 |
def test_collect_short_file_windows_multi_level_symlink( |
| 1786 |
pytester: Pytester, |
| 1787 |
request: FixtureRequest, |
| 1788 |
) -> None: |
| 1789 |
"""Regression test for multi-level Windows short-path comparison with |
| 1790 |
symlinks. |
| 1791 |
|
| 1792 |
Previously, when matching collection arguments against collected nodes on |
| 1793 |
Windows, the short path fallback resolved symlinks. With a chain a -> b -> |
| 1794 |
target, comparing 'a' against 'b' would incorrectly succeed because both |
| 1795 |
resolved to 'target', which could cause incorrect matching or duplicate |
| 1796 |
collection. |
| 1797 |
""" |
| 1798 |
# Prepare target directory with a test file. |
| 1799 |
short_path = Path(tempfile.mkdtemp()) |
| 1800 |
request.addfinalizer(lambda: shutil.rmtree(short_path, ignore_errors=True)) |
| 1801 |
target = short_path / "target" |
| 1802 |
target.mkdir() |
| 1803 |
(target / "test_chain.py").write_text("def test_chain(): pass", encoding="UTF-8") |
| 1804 |
|
| 1805 |
# Create multi-level symlink chain: a -> b -> target. |
| 1806 |
b = short_path / "b" |
| 1807 |
a = short_path / "a" |
| 1808 |
symlink_or_skip(target, b, target_is_directory=True) |
| 1809 |
symlink_or_skip(b, a, target_is_directory=True) |
| 1810 |
|
| 1811 |
# Collect via the first symlink; should find exactly one test. |
| 1812 |
result = pytester.runpytest(a) |
| 1813 |
result.assert_outcomes(passed=1) |
| 1814 |
|
| 1815 |
# Collect via the intermediate symlink; also exactly one test. |
| 1816 |
result = pytester.runpytest(b) |
| 1817 |
result.assert_outcomes(passed=1) |
| 1818 |
|
| 1819 |
|
| 1820 |
def test_pyargs_collection_tree(pytester: Pytester, monkeypatch: MonkeyPatch) -> None: |
| 1821 |
"""When using `--pyargs`, the collection tree of a pyargs collection |
| 1822 |
argument should only include parents in the import path, not up to confcutdir. |
| 1823 |
|
| 1824 |
Regression test for #11904. |
| 1825 |
""" |
| 1826 |
site_packages = pytester.path / "venv/lib/site-packages" |
| 1827 |
site_packages.mkdir(parents=True) |
| 1828 |
monkeypatch.syspath_prepend(site_packages) |
| 1829 |
pytester.makepyfile( |
| 1830 |
**{ |
| 1831 |
"venv/lib/site-packages/pkg/__init__.py": "", |
| 1832 |
"venv/lib/site-packages/pkg/sub/__init__.py": "", |
| 1833 |
"venv/lib/site-packages/pkg/sub/test_it.py": "def test(): pass", |
| 1834 |
} |
| 1835 |
) |
| 1836 |
|
| 1837 |
result = pytester.runpytest("--pyargs", "--collect-only", "pkg.sub.test_it") |
| 1838 |
assert result.ret == ExitCode.OK |
| 1839 |
result.stdout.fnmatch_lines( |
| 1840 |
[ |
| 1841 |
"<Package venv/lib/site-packages/pkg>", |
| 1842 |
" <Package sub>", |
| 1843 |
" <Module test_it.py>", |
| 1844 |
" <Function test>", |
| 1845 |
], |
| 1846 |
consecutive=True, |
| 1847 |
) |
| 1848 |
|
| 1849 |
# Now with an unrelated rootdir with unrelated files. |
| 1850 |
monkeypatch.chdir(tempfile.gettempdir()) |
| 1851 |
|
| 1852 |
result = pytester.runpytest("--pyargs", "--collect-only", "pkg.sub.test_it") |
| 1853 |
assert result.ret == ExitCode.OK |
| 1854 |
result.stdout.fnmatch_lines( |
| 1855 |
[ |
| 1856 |
"<Package *pkg>", |
| 1857 |
" <Package sub>", |
| 1858 |
" <Module test_it.py>", |
| 1859 |
" <Function test>", |
| 1860 |
], |
| 1861 |
consecutive=True, |
| 1862 |
) |
| 1863 |
|
| 1864 |
|
| 1865 |
def test_do_not_collect_symlink_siblings( |
| 1866 |
pytester: Pytester, tmp_path: Path, request: pytest.FixtureRequest |
| 1867 |
) -> None: |
| 1868 |
""" |
| 1869 |
Regression test for #12039: Do not collect from directories that are symlinks to other directories in the same path. |
| 1870 |
|
| 1871 |
The check for short paths under Windows via os.path.samefile, introduced in #11936, also finds the symlinked |
| 1872 |
directory created by tmp_path/tmpdir. |
| 1873 |
""" |
| 1874 |
# Use tmp_path because it creates a symlink with the name "current" next to the directory it creates. |
| 1875 |
symlink_path = tmp_path.parent / (tmp_path.name[:-1] + "current") |
| 1876 |
if not symlink_path.is_symlink(): # pragma: no cover |
| 1877 |
pytest.skip("Symlinks not supported in this environment") |
| 1878 |
|
| 1879 |
# Create test file. |
| 1880 |
tmp_path.joinpath("test_foo.py").write_text("def test(): pass", encoding="UTF-8") |
| 1881 |
|
| 1882 |
# Ensure we collect it only once if we pass the tmp_path. |
| 1883 |
result = pytester.runpytest(tmp_path, "-sv") |
| 1884 |
result.assert_outcomes(passed=1) |
| 1885 |
|
| 1886 |
# Ensure we collect it only once if we pass the symlinked directory. |
| 1887 |
result = pytester.runpytest(symlink_path, "-sv") |
| 1888 |
result.assert_outcomes(passed=1) |
| 1889 |
|
| 1890 |
|
| 1891 |
@pytest.mark.parametrize( |
| 1892 |
"exception_class, msg", |
| 1893 |
[ |
| 1894 |
(KeyboardInterrupt, "*!!! KeyboardInterrupt !!!*"), |
| 1895 |
(SystemExit, "INTERNALERROR> SystemExit"), |
| 1896 |
], |
| 1897 |
) |
| 1898 |
def test_respect_system_exceptions( |
| 1899 |
pytester: Pytester, |
| 1900 |
exception_class: type[BaseException], |
| 1901 |
msg: str, |
| 1902 |
): |
| 1903 |
head = "Before exception" |
| 1904 |
tail = "After exception" |
| 1905 |
ensure_file(pytester.path / "test_eggs.py").write_text( |
| 1906 |
f"print('{head}')", encoding="UTF-8" |
| 1907 |
) |
| 1908 |
ensure_file(pytester.path / "test_ham.py").write_text( |
| 1909 |
f"raise {exception_class.__name__}()", encoding="UTF-8" |
| 1910 |
) |
| 1911 |
ensure_file(pytester.path / "test_spam.py").write_text( |
| 1912 |
f"print('{tail}')", encoding="UTF-8" |
| 1913 |
) |
| 1914 |
|
| 1915 |
result = pytester.runpytest_subprocess("-s") |
| 1916 |
result.stdout.fnmatch_lines([f"*{head}*"]) |
| 1917 |
result.stdout.fnmatch_lines([msg]) |
| 1918 |
result.stdout.no_fnmatch_line(f"*{tail}*") |
| 1919 |
|
| 1920 |
|
| 1921 |
def test_yield_disallowed_in_tests(pytester: Pytester): |
| 1922 |
"""Ensure generator test functions with 'yield' fail collection (#12960).""" |
| 1923 |
pytester.makepyfile( |
| 1924 |
""" |
| 1925 |
def test_with_yield(): |
| 1926 |
yield 1 |
| 1927 |
""" |
| 1928 |
) |
| 1929 |
result = pytester.runpytest() |
| 1930 |
assert result.ret == 2 |
| 1931 |
result.stdout.fnmatch_lines( |
| 1932 |
["*'yield' keyword is allowed in fixtures, but not in tests (test_with_yield)*"] |
| 1933 |
) |
| 1934 |
# Assert that no tests were collected |
| 1935 |
result.stdout.fnmatch_lines(["*collected 0 items*"]) |
| 1936 |
|
| 1937 |
|
| 1938 |
def test_annotations_deferred_future(pytester: Pytester): |
| 1939 |
"""Ensure stringified annotations don't raise any errors.""" |
| 1940 |
pytester.makepyfile( |
| 1941 |
""" |
| 1942 |
from __future__ import annotations |
| 1943 |
import pytest |
| 1944 |
|
| 1945 |
@pytest.fixture |
| 1946 |
def func() -> X: ... # X is undefined |
| 1947 |
|
| 1948 |
def test_func(): |
| 1949 |
assert True |
| 1950 |
""" |
| 1951 |
) |
| 1952 |
result = pytester.runpytest() |
| 1953 |
assert result.ret == 0 |
| 1954 |
result.stdout.fnmatch_lines(["*1 passed*"]) |
| 1955 |
|
| 1956 |
|
| 1957 |
@pytest.mark.skipif( |
| 1958 |
sys.version_info < (3, 14), reason="Annotations are only skipped on 3.14+" |
| 1959 |
) |
| 1960 |
def test_annotations_deferred_314(pytester: Pytester): |
| 1961 |
"""Ensure annotation eval is deferred.""" |
| 1962 |
pytester.makepyfile( |
| 1963 |
""" |
| 1964 |
import pytest |
| 1965 |
|
| 1966 |
@pytest.fixture |
| 1967 |
def func() -> X: ... # X is undefined |
| 1968 |
|
| 1969 |
def test_func(): |
| 1970 |
assert True |
| 1971 |
""" |
| 1972 |
) |
| 1973 |
result = pytester.runpytest() |
| 1974 |
assert result.ret == 0 |
| 1975 |
result.stdout.fnmatch_lines(["*1 passed*"]) |
| 1976 |
|
| 1977 |
|
| 1978 |
@pytest.mark.parametrize("import_mode", ["prepend", "importlib", "append"]) |
| 1979 |
def test_namespace_packages(pytester: Pytester, import_mode: str): |
| 1980 |
pytester.makeini( |
| 1981 |
f""" |
| 1982 |
[pytest] |
| 1983 |
consider_namespace_packages = true |
| 1984 |
pythonpath = . |
| 1985 |
python_files = *.py |
| 1986 |
addopts = --import-mode {import_mode} |
| 1987 |
""" |
| 1988 |
) |
| 1989 |
pytester.makepyfile( |
| 1990 |
**{ |
| 1991 |
"pkg/module1.py": "def test_module1(): pass", |
| 1992 |
"pkg/subpkg_namespace/module2.py": "def test_module1(): pass", |
| 1993 |
"pkg/subpkg_regular/__init__.py": "", |
| 1994 |
"pkg/subpkg_regular/module3": "def test_module3(): pass", |
| 1995 |
} |
| 1996 |
) |
| 1997 |
|
| 1998 |
# should collect when called with top-level package correctly |
| 1999 |
result = pytester.runpytest("--collect-only", "--pyargs", "pkg") |
| 2000 |
result.stdout.fnmatch_lines( |
| 2001 |
[ |
| 2002 |
"collected 3 items", |
| 2003 |
"<Dir pkg>", |
| 2004 |
" <Module module1.py>", |
| 2005 |
" <Function test_module1>", |
| 2006 |
" <Dir subpkg_namespace>", |
| 2007 |
" <Module module2.py>", |
| 2008 |
" <Function test_module1>", |
| 2009 |
" <Package subpkg_regular>", |
| 2010 |
" <Module module3.py>", |
| 2011 |
" <Function test_module3>", |
| 2012 |
] |
| 2013 |
) |
| 2014 |
|
| 2015 |
# should also work when called against a more specific subpackage/module |
| 2016 |
result = pytester.runpytest("--collect-only", "--pyargs", "pkg.subpkg_namespace") |
| 2017 |
result.stdout.fnmatch_lines( |
| 2018 |
[ |
| 2019 |
"collected 1 item", |
| 2020 |
"<Dir pkg>", |
| 2021 |
" <Dir subpkg_namespace>", |
| 2022 |
" <Module module2.py>", |
| 2023 |
" <Function test_module1>", |
| 2024 |
] |
| 2025 |
) |
| 2026 |
|
| 2027 |
result = pytester.runpytest("--collect-only", "--pyargs", "pkg.subpkg_regular") |
| 2028 |
result.stdout.fnmatch_lines( |
| 2029 |
[ |
| 2030 |
"collected 1 item", |
| 2031 |
"<Dir pkg>", |
| 2032 |
" <Package subpkg_regular>", |
| 2033 |
" <Module module3.py>", |
| 2034 |
" <Function test_module3>", |
| 2035 |
] |
| 2036 |
) |
| 2037 |
|
| 2038 |
|
| 2039 |
class TestOverlappingCollectionArguments: |
| 2040 |
"""Test that overlapping collection arguments (e.g. `pytest a/b a |
| 2041 |
a/c::TestIt) are handled correctly (#12083).""" |
| 2042 |
|
| 2043 |
@pytest.mark.parametrize("args", [("a", "a/b"), ("a/b", "a")]) |
| 2044 |
def test_parent_child(self, pytester: Pytester, args: tuple[str, ...]) -> None: |
| 2045 |
"""Test that 'pytest a a/b' and `pytest a/b a` collects all tests from 'a'.""" |
| 2046 |
pytester.makepyfile( |
| 2047 |
**{ |
| 2048 |
"a/test_a.py": """ |
| 2049 |
def test_a1(): pass |
| 2050 |
def test_a2(): pass |
| 2051 |
""", |
| 2052 |
"a/b/test_b.py": """ |
| 2053 |
def test_b1(): pass |
| 2054 |
def test_b2(): pass |
| 2055 |
""", |
| 2056 |
} |
| 2057 |
) |
| 2058 |
|
| 2059 |
result = pytester.runpytest("--collect-only", *args) |
| 2060 |
|
| 2061 |
result.stdout.fnmatch_lines( |
| 2062 |
[ |
| 2063 |
"<Dir *>", |
| 2064 |
" <Dir a>", |
| 2065 |
" <Dir b>", |
| 2066 |
" <Module test_b.py>", |
| 2067 |
" <Function test_b1>", |
| 2068 |
" <Function test_b2>", |
| 2069 |
" <Module test_a.py>", |
| 2070 |
" <Function test_a1>", |
| 2071 |
" <Function test_a2>", |
| 2072 |
"", |
| 2073 |
], |
| 2074 |
consecutive=True, |
| 2075 |
) |
| 2076 |
|
| 2077 |
def test_multiple_nested_paths(self, pytester: Pytester) -> None: |
| 2078 |
"""Test that 'pytest a/b a a/b/c' collects all tests from 'a'.""" |
| 2079 |
pytester.makepyfile( |
| 2080 |
**{ |
| 2081 |
"a/test_a.py": """ |
| 2082 |
def test_a(): pass |
| 2083 |
""", |
| 2084 |
"a/b/test_b.py": """ |
| 2085 |
def test_b(): pass |
| 2086 |
""", |
| 2087 |
"a/b/c/test_c.py": """ |
| 2088 |
def test_c(): pass |
| 2089 |
""", |
| 2090 |
} |
| 2091 |
) |
| 2092 |
|
| 2093 |
result = pytester.runpytest("--collect-only", "a/b", "a", "a/b/c") |
| 2094 |
|
| 2095 |
result.stdout.fnmatch_lines( |
| 2096 |
[ |
| 2097 |
"<Dir *>", |
| 2098 |
" <Dir a>", |
| 2099 |
" <Dir b>", |
| 2100 |
" <Dir c>", |
| 2101 |
" <Module test_c.py>", |
| 2102 |
" <Function test_c>", |
| 2103 |
" <Module test_b.py>", |
| 2104 |
" <Function test_b>", |
| 2105 |
" <Module test_a.py>", |
| 2106 |
" <Function test_a>", |
| 2107 |
"", |
| 2108 |
], |
| 2109 |
consecutive=True, |
| 2110 |
) |
| 2111 |
|
| 2112 |
def test_same_path_twice(self, pytester: Pytester) -> None: |
| 2113 |
"""Test that 'pytest a a' doesn't duplicate tests.""" |
| 2114 |
pytester.makepyfile( |
| 2115 |
**{ |
| 2116 |
"a/test_a.py": """ |
| 2117 |
def test_a(): pass |
| 2118 |
""", |
| 2119 |
} |
| 2120 |
) |
| 2121 |
|
| 2122 |
result = pytester.runpytest("--collect-only", "a", "a") |
| 2123 |
|
| 2124 |
result.stdout.fnmatch_lines( |
| 2125 |
[ |
| 2126 |
"<Dir *>", |
| 2127 |
" <Dir a>", |
| 2128 |
" <Module test_a.py>", |
| 2129 |
" <Function test_a>", |
| 2130 |
"", |
| 2131 |
], |
| 2132 |
consecutive=True, |
| 2133 |
) |
| 2134 |
|
| 2135 |
def test_keep_duplicates_flag(self, pytester: Pytester) -> None: |
| 2136 |
"""Test that --keep-duplicates allows duplication.""" |
| 2137 |
pytester.makepyfile( |
| 2138 |
**{ |
| 2139 |
"a/test_a.py": """ |
| 2140 |
def test_a(): pass |
| 2141 |
""", |
| 2142 |
"a/b/test_b.py": """ |
| 2143 |
def test_b(): pass |
| 2144 |
""", |
| 2145 |
} |
| 2146 |
) |
| 2147 |
|
| 2148 |
result = pytester.runpytest("--collect-only", "--keep-duplicates", "a", "a/b") |
| 2149 |
|
| 2150 |
result.stdout.fnmatch_lines( |
| 2151 |
[ |
| 2152 |
"<Dir *>", |
| 2153 |
" <Dir a>", |
| 2154 |
" <Dir b>", |
| 2155 |
" <Module test_b.py>", |
| 2156 |
" <Function test_b>", |
| 2157 |
" <Module test_a.py>", |
| 2158 |
" <Function test_a>", |
| 2159 |
" <Dir b>", |
| 2160 |
" <Module test_b.py>", |
| 2161 |
" <Function test_b>", |
| 2162 |
"", |
| 2163 |
], |
| 2164 |
consecutive=True, |
| 2165 |
) |
| 2166 |
|
| 2167 |
def test_specific_file_then_parent_dir(self, pytester: Pytester) -> None: |
| 2168 |
"""Test that 'pytest a/test_a.py a' collects all tests from 'a'.""" |
| 2169 |
pytester.makepyfile( |
| 2170 |
**{ |
| 2171 |
"a/test_a.py": """ |
| 2172 |
def test_a(): pass |
| 2173 |
""", |
| 2174 |
"a/test_other.py": """ |
| 2175 |
def test_other(): pass |
| 2176 |
""", |
| 2177 |
} |
| 2178 |
) |
| 2179 |
|
| 2180 |
result = pytester.runpytest("--collect-only", "a/test_a.py", "a") |
| 2181 |
|
| 2182 |
result.stdout.fnmatch_lines( |
| 2183 |
[ |
| 2184 |
"<Dir *>", |
| 2185 |
" <Dir a>", |
| 2186 |
" <Module test_a.py>", |
| 2187 |
" <Function test_a>", |
| 2188 |
" <Module test_other.py>", |
| 2189 |
" <Function test_other>", |
| 2190 |
"", |
| 2191 |
], |
| 2192 |
consecutive=True, |
| 2193 |
) |
| 2194 |
|
| 2195 |
def test_package_scope_fixture_with_overlapping_paths( |
| 2196 |
self, pytester: Pytester |
| 2197 |
) -> None: |
| 2198 |
"""Test that package-scoped fixtures work correctly with overlapping paths.""" |
| 2199 |
pytester.makepyfile( |
| 2200 |
**{ |
| 2201 |
"pkg/__init__.py": "", |
| 2202 |
"pkg/test_pkg.py": """ |
| 2203 |
import pytest |
| 2204 |
|
| 2205 |
counter = {"value": 0} |
| 2206 |
|
| 2207 |
@pytest.fixture(scope="package") |
| 2208 |
def pkg_fixture(): |
| 2209 |
counter["value"] += 1 |
| 2210 |
return counter["value"] |
| 2211 |
|
| 2212 |
def test_pkg1(pkg_fixture): |
| 2213 |
assert pkg_fixture == 1 |
| 2214 |
|
| 2215 |
def test_pkg2(pkg_fixture): |
| 2216 |
assert pkg_fixture == 1 |
| 2217 |
""", |
| 2218 |
"pkg/sub/__init__.py": "", |
| 2219 |
"pkg/sub/test_sub.py": """ |
| 2220 |
def test_sub(): pass |
| 2221 |
""", |
| 2222 |
} |
| 2223 |
) |
| 2224 |
|
| 2225 |
# Package fixture should run only once even with overlapping paths. |
| 2226 |
result = pytester.runpytest("pkg", "pkg/sub", "pkg", "-v") |
| 2227 |
result.assert_outcomes(passed=3) |
| 2228 |
|
| 2229 |
def test_execution_order_preserved(self, pytester: Pytester) -> None: |
| 2230 |
"""Test that test execution order follows argument order.""" |
| 2231 |
pytester.makepyfile( |
| 2232 |
**{ |
| 2233 |
"a/test_a.py": """ |
| 2234 |
def test_a(): pass |
| 2235 |
""", |
| 2236 |
"b/test_b.py": """ |
| 2237 |
def test_b(): pass |
| 2238 |
""", |
| 2239 |
} |
| 2240 |
) |
| 2241 |
|
| 2242 |
result = pytester.runpytest("--collect-only", "b", "a", "b/test_b.py::test_b") |
| 2243 |
|
| 2244 |
result.stdout.fnmatch_lines( |
| 2245 |
[ |
| 2246 |
"<Dir *>", |
| 2247 |
" <Dir b>", |
| 2248 |
" <Module test_b.py>", |
| 2249 |
" <Function test_b>", |
| 2250 |
" <Dir a>", |
| 2251 |
" <Module test_a.py>", |
| 2252 |
" <Function test_a>", |
| 2253 |
"", |
| 2254 |
], |
| 2255 |
consecutive=True, |
| 2256 |
) |
| 2257 |
|
| 2258 |
def test_overlapping_node_ids_class_and_method(self, pytester: Pytester) -> None: |
| 2259 |
"""Test that overlapping node IDs are handled correctly.""" |
| 2260 |
pytester.makepyfile( |
| 2261 |
test_nodeids=""" |
| 2262 |
class TestClass: |
| 2263 |
def test_method1(self): pass |
| 2264 |
def test_method2(self): pass |
| 2265 |
def test_method3(self): pass |
| 2266 |
|
| 2267 |
def test_function(): pass |
| 2268 |
""" |
| 2269 |
) |
| 2270 |
|
| 2271 |
# Class then specific method. |
| 2272 |
result = pytester.runpytest( |
| 2273 |
"--collect-only", |
| 2274 |
"test_nodeids.py::TestClass", |
| 2275 |
"test_nodeids.py::TestClass::test_method2", |
| 2276 |
) |
| 2277 |
result.stdout.fnmatch_lines( |
| 2278 |
[ |
| 2279 |
"<Dir *>", |
| 2280 |
" <Module test_nodeids.py>", |
| 2281 |
" <Class TestClass>", |
| 2282 |
" <Function test_method1>", |
| 2283 |
" <Function test_method2>", |
| 2284 |
" <Function test_method3>", |
| 2285 |
"", |
| 2286 |
], |
| 2287 |
consecutive=True, |
| 2288 |
) |
| 2289 |
|
| 2290 |
# Specific method then class. |
| 2291 |
result = pytester.runpytest( |
| 2292 |
"--collect-only", |
| 2293 |
"test_nodeids.py::TestClass::test_method3", |
| 2294 |
"test_nodeids.py::TestClass", |
| 2295 |
) |
| 2296 |
result.stdout.fnmatch_lines( |
| 2297 |
[ |
| 2298 |
"<Dir *>", |
| 2299 |
" <Module test_nodeids.py>", |
| 2300 |
" <Class TestClass>", |
| 2301 |
" <Function test_method1>", |
| 2302 |
" <Function test_method2>", |
| 2303 |
" <Function test_method3>", |
| 2304 |
"", |
| 2305 |
], |
| 2306 |
consecutive=True, |
| 2307 |
) |
| 2308 |
|
| 2309 |
def test_overlapping_node_ids_file_and_class(self, pytester: Pytester) -> None: |
| 2310 |
"""Test that file-level and class-level selections work correctly.""" |
| 2311 |
pytester.makepyfile( |
| 2312 |
test_file=""" |
| 2313 |
class TestClass: |
| 2314 |
def test_method(self): pass |
| 2315 |
|
| 2316 |
class TestOther: |
| 2317 |
def test_other(self): pass |
| 2318 |
|
| 2319 |
def test_function(): pass |
| 2320 |
""" |
| 2321 |
) |
| 2322 |
|
| 2323 |
# File then class. |
| 2324 |
result = pytester.runpytest( |
| 2325 |
"--collect-only", "test_file.py", "test_file.py::TestClass" |
| 2326 |
) |
| 2327 |
result.stdout.fnmatch_lines( |
| 2328 |
[ |
| 2329 |
"<Dir *>", |
| 2330 |
" <Module test_file.py>", |
| 2331 |
" <Class TestClass>", |
| 2332 |
" <Function test_method>", |
| 2333 |
" <Class TestOther>", |
| 2334 |
" <Function test_other>", |
| 2335 |
" <Function test_function>", |
| 2336 |
"", |
| 2337 |
], |
| 2338 |
consecutive=True, |
| 2339 |
) |
| 2340 |
|
| 2341 |
# Class then file. |
| 2342 |
result = pytester.runpytest( |
| 2343 |
"--collect-only", "test_file.py::TestClass", "test_file.py" |
| 2344 |
) |
| 2345 |
result.stdout.fnmatch_lines( |
| 2346 |
[ |
| 2347 |
"<Dir *>", |
| 2348 |
" <Module test_file.py>", |
| 2349 |
" <Class TestClass>", |
| 2350 |
" <Function test_method>", |
| 2351 |
" <Class TestOther>", |
| 2352 |
" <Function test_other>", |
| 2353 |
" <Function test_function>", |
| 2354 |
"", |
| 2355 |
], |
| 2356 |
consecutive=True, |
| 2357 |
) |
| 2358 |
|
| 2359 |
def test_same_node_id_twice(self, pytester: Pytester) -> None: |
| 2360 |
"""Test that the same node ID specified twice is collected only once.""" |
| 2361 |
pytester.makepyfile( |
| 2362 |
test_dup=""" |
| 2363 |
def test_one(): pass |
| 2364 |
def test_two(): pass |
| 2365 |
""" |
| 2366 |
) |
| 2367 |
|
| 2368 |
result = pytester.runpytest( |
| 2369 |
"--collect-only", |
| 2370 |
"test_dup.py::test_one", |
| 2371 |
"test_dup.py::test_one", |
| 2372 |
) |
| 2373 |
result.stdout.fnmatch_lines( |
| 2374 |
[ |
| 2375 |
"<Dir *>", |
| 2376 |
" <Module test_dup.py>", |
| 2377 |
" <Function test_one>", |
| 2378 |
"", |
| 2379 |
], |
| 2380 |
consecutive=True, |
| 2381 |
) |
| 2382 |
|
| 2383 |
def test_overlapping_with_parametrization(self, pytester: Pytester) -> None: |
| 2384 |
"""Test overlapping with parametrized tests.""" |
| 2385 |
pytester.makepyfile( |
| 2386 |
test_param=""" |
| 2387 |
import pytest |
| 2388 |
|
| 2389 |
@pytest.mark.parametrize("n", [1, 2]) |
| 2390 |
def test_param(n): pass |
| 2391 |
|
| 2392 |
class TestClass: |
| 2393 |
@pytest.mark.parametrize("x", ["a", "b"]) |
| 2394 |
def test_method(self, x): pass |
| 2395 |
""" |
| 2396 |
) |
| 2397 |
|
| 2398 |
result = pytester.runpytest( |
| 2399 |
"--collect-only", |
| 2400 |
"test_param.py::test_param[2]", |
| 2401 |
"test_param.py::TestClass::test_method[a]", |
| 2402 |
"test_param.py", |
| 2403 |
) |
| 2404 |
result.stdout.fnmatch_lines( |
| 2405 |
[ |
| 2406 |
"<Dir *>", |
| 2407 |
" <Module test_param.py>", |
| 2408 |
" <Function test_param[1]>", |
| 2409 |
" <Function test_param[2]>", |
| 2410 |
" <Class TestClass>", |
| 2411 |
" <Function test_method[a]>", |
| 2412 |
" <Function test_method[b]>", |
| 2413 |
"", |
| 2414 |
], |
| 2415 |
consecutive=True, |
| 2416 |
) |
| 2417 |
|
| 2418 |
result = pytester.runpytest( |
| 2419 |
"--collect-only", |
| 2420 |
"test_param.py::test_param[2]", |
| 2421 |
"test_param.py::test_param", |
| 2422 |
) |
| 2423 |
result.stdout.fnmatch_lines( |
| 2424 |
[ |
| 2425 |
"<Dir *>", |
| 2426 |
" <Module test_param.py>", |
| 2427 |
" <Function test_param[1]>", |
| 2428 |
" <Function test_param[2]>", |
| 2429 |
"", |
| 2430 |
], |
| 2431 |
consecutive=True, |
| 2432 |
) |
| 2433 |
|
| 2434 |
@pytest.mark.parametrize("order", [(".", "a"), ("a", ".")]) |
| 2435 |
def test_root_and_subdir(self, pytester: Pytester, order: tuple[str, ...]) -> None: |
| 2436 |
"""Test that '. a' and 'a .' both collect all tests.""" |
| 2437 |
pytester.makepyfile( |
| 2438 |
test_root=""" |
| 2439 |
def test_root(): pass |
| 2440 |
""", |
| 2441 |
**{ |
| 2442 |
"a/test_a.py": """ |
| 2443 |
def test_a(): pass |
| 2444 |
""", |
| 2445 |
}, |
| 2446 |
) |
| 2447 |
|
| 2448 |
result = pytester.runpytest("--collect-only", *order) |
| 2449 |
|
| 2450 |
result.stdout.fnmatch_lines( |
| 2451 |
[ |
| 2452 |
"<Dir *>", |
| 2453 |
" <Dir a>", |
| 2454 |
" <Module test_a.py>", |
| 2455 |
" <Function test_a>", |
| 2456 |
" <Module test_root.py>", |
| 2457 |
" <Function test_root>", |
| 2458 |
"", |
| 2459 |
], |
| 2460 |
consecutive=True, |
| 2461 |
) |
| 2462 |
|
| 2463 |
def test_complex_combined_handling(self, pytester: Pytester) -> None: |
| 2464 |
"""Test some scenarios in a complex hierarchy.""" |
| 2465 |
pytester.makepyfile( |
| 2466 |
**{ |
| 2467 |
"top1/__init__.py": "", |
| 2468 |
"top1/test_1.py": ( |
| 2469 |
""" |
| 2470 |
def test_1(): pass |
| 2471 |
|
| 2472 |
class TestIt: |
| 2473 |
def test_2(): pass |
| 2474 |
|
| 2475 |
def test_3(): pass |
| 2476 |
""" |
| 2477 |
), |
| 2478 |
"top1/test_2.py": ( |
| 2479 |
""" |
| 2480 |
def test_1(): pass |
| 2481 |
""" |
| 2482 |
), |
| 2483 |
"top2/__init__.py": "", |
| 2484 |
"top2/test_1.py": ( |
| 2485 |
""" |
| 2486 |
def test_1(): pass |
| 2487 |
""" |
| 2488 |
), |
| 2489 |
}, |
| 2490 |
) |
| 2491 |
|
| 2492 |
result = pytester.runpytest_inprocess("--collect-only", ".") |
| 2493 |
result.stdout.fnmatch_lines( |
| 2494 |
[ |
| 2495 |
"<Dir *>", |
| 2496 |
" <Package top1>", |
| 2497 |
" <Module test_1.py>", |
| 2498 |
" <Function test_1>", |
| 2499 |
" <Class TestIt>", |
| 2500 |
" <Function test_2>", |
| 2501 |
" <Function test_3>", |
| 2502 |
" <Module test_2.py>", |
| 2503 |
" <Function test_1>", |
| 2504 |
" <Package top2>", |
| 2505 |
" <Module test_1.py>", |
| 2506 |
" <Function test_1>", |
| 2507 |
"", |
| 2508 |
], |
| 2509 |
consecutive=True, |
| 2510 |
) |
| 2511 |
|
| 2512 |
result = pytester.runpytest_inprocess("--collect-only", "top2", "top1") |
| 2513 |
result.stdout.fnmatch_lines( |
| 2514 |
[ |
| 2515 |
"<Dir *>", |
| 2516 |
" <Package top2>", |
| 2517 |
" <Module test_1.py>", |
| 2518 |
" <Function test_1>", |
| 2519 |
" <Package top1>", |
| 2520 |
" <Module test_1.py>", |
| 2521 |
" <Function test_1>", |
| 2522 |
" <Class TestIt>", |
| 2523 |
" <Function test_2>", |
| 2524 |
" <Function test_3>", |
| 2525 |
" <Module test_2.py>", |
| 2526 |
" <Function test_1>", |
| 2527 |
"", |
| 2528 |
], |
| 2529 |
consecutive=True, |
| 2530 |
) |
| 2531 |
|
| 2532 |
result = pytester.runpytest_inprocess( |
| 2533 |
"--collect-only", "top1", "top1/test_2.py" |
| 2534 |
) |
| 2535 |
result.stdout.fnmatch_lines( |
| 2536 |
[ |
| 2537 |
"<Dir *>", |
| 2538 |
" <Package top1>", |
| 2539 |
" <Module test_1.py>", |
| 2540 |
" <Function test_1>", |
| 2541 |
" <Class TestIt>", |
| 2542 |
" <Function test_2>", |
| 2543 |
" <Function test_3>", |
| 2544 |
" <Module test_2.py>", |
| 2545 |
" <Function test_1>", |
| 2546 |
# NOTE: Also sensible arguably even without --keep-duplicates. |
| 2547 |
# " <Module test_2.py>", |
| 2548 |
# " <Function test_1>", |
| 2549 |
"", |
| 2550 |
], |
| 2551 |
consecutive=True, |
| 2552 |
) |
| 2553 |
|
| 2554 |
result = pytester.runpytest_inprocess( |
| 2555 |
"--collect-only", "top1/test_2.py", "top1" |
| 2556 |
) |
| 2557 |
result.stdout.fnmatch_lines( |
| 2558 |
[ |
| 2559 |
"<Dir *>", |
| 2560 |
" <Package top1>", |
| 2561 |
# NOTE: Ideally test_2 would come before test_1 here. |
| 2562 |
" <Module test_1.py>", |
| 2563 |
" <Function test_1>", |
| 2564 |
" <Class TestIt>", |
| 2565 |
" <Function test_2>", |
| 2566 |
" <Function test_3>", |
| 2567 |
" <Module test_2.py>", |
| 2568 |
" <Function test_1>", |
| 2569 |
"", |
| 2570 |
], |
| 2571 |
consecutive=True, |
| 2572 |
) |
| 2573 |
|
| 2574 |
result = pytester.runpytest_inprocess( |
| 2575 |
"--collect-only", "--keep-duplicates", "top1/test_2.py", "top1" |
| 2576 |
) |
| 2577 |
result.stdout.fnmatch_lines( |
| 2578 |
[ |
| 2579 |
"<Dir *>", |
| 2580 |
" <Package top1>", |
| 2581 |
" <Module test_2.py>", |
| 2582 |
" <Function test_1>", |
| 2583 |
" <Module test_1.py>", |
| 2584 |
" <Function test_1>", |
| 2585 |
" <Class TestIt>", |
| 2586 |
" <Function test_2>", |
| 2587 |
" <Function test_3>", |
| 2588 |
" <Module test_2.py>", |
| 2589 |
" <Function test_1>", |
| 2590 |
"", |
| 2591 |
], |
| 2592 |
consecutive=True, |
| 2593 |
) |
| 2594 |
|
| 2595 |
result = pytester.runpytest_inprocess( |
| 2596 |
"--collect-only", "top1/test_2.py", "top1/test_2.py" |
| 2597 |
) |
| 2598 |
result.stdout.fnmatch_lines( |
| 2599 |
[ |
| 2600 |
"<Dir *>", |
| 2601 |
" <Package top1>", |
| 2602 |
" <Module test_2.py>", |
| 2603 |
" <Function test_1>", |
| 2604 |
# NOTE: Also sensible arguably even without --keep-duplicates. |
| 2605 |
# " <Module test_2.py>", |
| 2606 |
# " <Function test_1>", |
| 2607 |
"", |
| 2608 |
], |
| 2609 |
consecutive=True, |
| 2610 |
) |
| 2611 |
|
| 2612 |
result = pytester.runpytest_inprocess("--collect-only", "top2/", "top2/") |
| 2613 |
result.stdout.fnmatch_lines( |
| 2614 |
[ |
| 2615 |
"<Dir *>", |
| 2616 |
" <Package top2>", |
| 2617 |
" <Module test_1.py>", |
| 2618 |
" <Function test_1>", |
| 2619 |
# NOTE: Also sensible arguably even without --keep-duplicates. |
| 2620 |
# " <Package top2>", |
| 2621 |
# " <Module test_1.py>", |
| 2622 |
# " <Function test_1>", |
| 2623 |
"", |
| 2624 |
], |
| 2625 |
consecutive=True, |
| 2626 |
) |
| 2627 |
|
| 2628 |
result = pytester.runpytest_inprocess( |
| 2629 |
"--collect-only", "top2/", "top2/", "top2/test_1.py" |
| 2630 |
) |
| 2631 |
result.stdout.fnmatch_lines( |
| 2632 |
[ |
| 2633 |
"<Dir *>", |
| 2634 |
" <Package top2>", |
| 2635 |
" <Module test_1.py>", |
| 2636 |
" <Function test_1>", |
| 2637 |
# NOTE: Also sensible arguably even without --keep-duplicates. |
| 2638 |
# " <Package top2>", |
| 2639 |
# " <Module test_1.py>", |
| 2640 |
# " <Function test_1>", |
| 2641 |
# " <Module test_1.py>", |
| 2642 |
# " <Function test_1>", |
| 2643 |
"", |
| 2644 |
], |
| 2645 |
consecutive=True, |
| 2646 |
) |
| 2647 |
|
| 2648 |
result = pytester.runpytest_inprocess( |
| 2649 |
"--collect-only", "top1/test_1.py", "top1/test_1.py::test_3" |
| 2650 |
) |
| 2651 |
result.stdout.fnmatch_lines( |
| 2652 |
[ |
| 2653 |
"<Dir *>", |
| 2654 |
" <Package top1>", |
| 2655 |
" <Module test_1.py>", |
| 2656 |
" <Function test_1>", |
| 2657 |
" <Class TestIt>", |
| 2658 |
" <Function test_2>", |
| 2659 |
" <Function test_3>", |
| 2660 |
# NOTE: Also sensible arguably even without --keep-duplicates. |
| 2661 |
# " <Function test_3>", |
| 2662 |
"", |
| 2663 |
], |
| 2664 |
consecutive=True, |
| 2665 |
) |
| 2666 |
|
| 2667 |
result = pytester.runpytest_inprocess( |
| 2668 |
"--collect-only", "top1/test_1.py::test_3", "top1/test_1.py" |
| 2669 |
) |
| 2670 |
result.stdout.fnmatch_lines( |
| 2671 |
[ |
| 2672 |
"<Dir *>", |
| 2673 |
" <Package top1>", |
| 2674 |
" <Module test_1.py>", |
| 2675 |
# NOTE: Ideally test_3 would come before the others here. |
| 2676 |
" <Function test_1>", |
| 2677 |
" <Class TestIt>", |
| 2678 |
" <Function test_2>", |
| 2679 |
" <Function test_3>", |
| 2680 |
"", |
| 2681 |
], |
| 2682 |
consecutive=True, |
| 2683 |
) |
| 2684 |
|
| 2685 |
result = pytester.runpytest_inprocess( |
| 2686 |
"--collect-only", |
| 2687 |
"--keep-duplicates", |
| 2688 |
"top1/test_1.py::test_3", |
| 2689 |
"top1/test_1.py", |
| 2690 |
) |
| 2691 |
result.stdout.fnmatch_lines( |
| 2692 |
[ |
| 2693 |
"<Dir *>", |
| 2694 |
" <Package top1>", |
| 2695 |
# NOTE: That <Module test_1.py> is duplicated here is not great. |
| 2696 |
" <Module test_1.py>", |
| 2697 |
" <Function test_3>", |
| 2698 |
" <Module test_1.py>", |
| 2699 |
" <Function test_1>", |
| 2700 |
" <Class TestIt>", |
| 2701 |
" <Function test_2>", |
| 2702 |
" <Function test_3>", |
| 2703 |
"", |
| 2704 |
], |
| 2705 |
consecutive=True, |
| 2706 |
) |
| 2707 |
|
| 2708 |
|
| 2709 |
@pytest.mark.parametrize( |
| 2710 |
["x_y", "expected_duplicates"], |
| 2711 |
[ |
| 2712 |
( |
| 2713 |
[(1, 1), (1, 1)], |
| 2714 |
["1-1"], |
| 2715 |
), |
| 2716 |
( |
| 2717 |
[(1, 1), (1, 2), (1, 1)], |
| 2718 |
["1-1"], |
| 2719 |
), |
| 2720 |
( |
| 2721 |
[(1, 1), (2, 2), (1, 1)], |
| 2722 |
["1-1"], |
| 2723 |
), |
| 2724 |
( |
| 2725 |
[(1, 1), (2, 2), (1, 2), (2, 1), (1, 1), (2, 1)], |
| 2726 |
["1-1", "2-1"], |
| 2727 |
), |
| 2728 |
], |
| 2729 |
) |
| 2730 |
@pytest.mark.parametrize("option_name", ["strict_parametrization_ids", "strict"]) |
| 2731 |
def test_strict_parametrization_ids( |
| 2732 |
pytester: Pytester, |
| 2733 |
x_y: Sequence[tuple[int, int]], |
| 2734 |
expected_duplicates: Sequence[str], |
| 2735 |
option_name: str, |
| 2736 |
) -> None: |
| 2737 |
pytester.makeini( |
| 2738 |
f""" |
| 2739 |
[pytest] |
| 2740 |
{option_name} = true |
| 2741 |
""" |
| 2742 |
) |
| 2743 |
pytester.makepyfile( |
| 2744 |
f""" |
| 2745 |
import pytest |
| 2746 |
|
| 2747 |
@pytest.mark.parametrize(["x", "y"], {x_y}) |
| 2748 |
def test1(x, y): |
| 2749 |
pass |
| 2750 |
""" |
| 2751 |
) |
| 2752 |
|
| 2753 |
result = pytester.runpytest() |
| 2754 |
|
| 2755 |
assert result.ret == ExitCode.INTERRUPTED |
| 2756 |
expected_parametersets = ", ".join(str(list(p)) for p in x_y) |
| 2757 |
expected_ids = ", ".join(f"{x}-{y}" for x, y in x_y) |
| 2758 |
result.stdout.fnmatch_lines( |
| 2759 |
[ |
| 2760 |
"Duplicate parametrization IDs detected*", |
| 2761 |
"", |
| 2762 |
"Test name: *::test1", |
| 2763 |
"Parameters: x, y", |
| 2764 |
f"Parameter sets: {expected_parametersets}", |
| 2765 |
f"IDs: {expected_ids}", |
| 2766 |
f"Duplicates: {', '.join(expected_duplicates)}", |
| 2767 |
"", |
| 2768 |
"You can fix this problem using *", |
| 2769 |
] |
| 2770 |
) |
| 2771 |
|
| 2772 |
|
| 2773 |
def test_strict_parametrization_ids_with_hidden_param(pytester: Pytester) -> None: |
| 2774 |
pytester.makeini( |
| 2775 |
""" |
| 2776 |
[pytest] |
| 2777 |
strict_parametrization_ids = true |
| 2778 |
""" |
| 2779 |
) |
| 2780 |
pytester.makepyfile( |
| 2781 |
""" |
| 2782 |
import pytest |
| 2783 |
|
| 2784 |
@pytest.mark.parametrize(["x"], ["a", pytest.param("a", id=pytest.HIDDEN_PARAM), "a"]) |
| 2785 |
def test1(x): |
| 2786 |
pass |
| 2787 |
""" |
| 2788 |
) |
| 2789 |
|
| 2790 |
result = pytester.runpytest() |
| 2791 |
|
| 2792 |
assert result.ret == ExitCode.INTERRUPTED |
| 2793 |
result.stdout.fnmatch_lines( |
| 2794 |
[ |
| 2795 |
"Duplicate parametrization IDs detected*", |
| 2796 |
"IDs: a, <hidden>, a", |
| 2797 |
"Duplicates: a", |
| 2798 |
] |
| 2799 |
) |