| 1 |
# mypy: allow-untyped-defs |
| 2 |
from __future__ import annotations |
| 3 |
|
| 4 |
from collections.abc import Sequence |
| 5 |
import dataclasses |
| 6 |
import importlib.metadata |
| 7 |
import os |
| 8 |
from pathlib import Path |
| 9 |
import subprocess |
| 10 |
import sys |
| 11 |
import types |
| 12 |
|
| 13 |
import setuptools |
| 14 |
|
| 15 |
from _pytest.config import ExitCode |
| 16 |
from _pytest.pathlib import symlink_or_skip |
| 17 |
from _pytest.pytester import Pytester |
| 18 |
import pytest |
| 19 |
|
| 20 |
|
| 21 |
def prepend_pythonpath(*dirs) -> str: |
| 22 |
cur = os.getenv("PYTHONPATH") |
| 23 |
if cur: |
| 24 |
dirs += (cur,) |
| 25 |
return os.pathsep.join(str(p) for p in dirs) |
| 26 |
|
| 27 |
|
| 28 |
class TestGeneralUsage: |
| 29 |
def test_config_error(self, pytester: Pytester) -> None: |
| 30 |
pytester.copy_example("conftest_usageerror/conftest.py") |
| 31 |
result = pytester.runpytest(pytester.path) |
| 32 |
assert result.ret == ExitCode.USAGE_ERROR |
| 33 |
result.stderr.fnmatch_lines(["*ERROR: hello"]) |
| 34 |
result.stdout.fnmatch_lines(["*pytest_unconfigure_called"]) |
| 35 |
|
| 36 |
def test_root_conftest_syntax_error(self, pytester: Pytester) -> None: |
| 37 |
pytester.makepyfile(conftest="raise SyntaxError\n") |
| 38 |
result = pytester.runpytest() |
| 39 |
result.stderr.fnmatch_lines(["*raise SyntaxError*"]) |
| 40 |
assert result.ret != 0 |
| 41 |
|
| 42 |
def test_early_hook_error_issue38_1(self, pytester: Pytester) -> None: |
| 43 |
pytester.makeconftest( |
| 44 |
""" |
| 45 |
def pytest_sessionstart(): |
| 46 |
0 / 0 |
| 47 |
""" |
| 48 |
) |
| 49 |
result = pytester.runpytest(pytester.path) |
| 50 |
assert result.ret != 0 |
| 51 |
# tracestyle is native by default for hook failures |
| 52 |
result.stdout.fnmatch_lines( |
| 53 |
["*INTERNALERROR*File*conftest.py*line 2*", "*0 / 0*"] |
| 54 |
) |
| 55 |
result = pytester.runpytest(pytester.path, "--fulltrace") |
| 56 |
assert result.ret != 0 |
| 57 |
# tracestyle is native by default for hook failures |
| 58 |
result.stdout.fnmatch_lines( |
| 59 |
["*INTERNALERROR*def pytest_sessionstart():*", "*INTERNALERROR*0 / 0*"] |
| 60 |
) |
| 61 |
|
| 62 |
def test_early_hook_configure_error_issue38(self, pytester: Pytester) -> None: |
| 63 |
pytester.makeconftest( |
| 64 |
""" |
| 65 |
def pytest_configure(): |
| 66 |
0 / 0 |
| 67 |
""" |
| 68 |
) |
| 69 |
result = pytester.runpytest(pytester.path) |
| 70 |
assert result.ret != 0 |
| 71 |
# here we get it on stderr |
| 72 |
result.stderr.fnmatch_lines( |
| 73 |
["*INTERNALERROR*File*conftest.py*line 2*", "*0 / 0*"] |
| 74 |
) |
| 75 |
|
| 76 |
def test_file_not_found(self, pytester: Pytester) -> None: |
| 77 |
result = pytester.runpytest("asd") |
| 78 |
assert result.ret != 0 |
| 79 |
result.stderr.fnmatch_lines(["ERROR: file or directory not found: asd"]) |
| 80 |
|
| 81 |
def test_file_not_found_unconfigure_issue143(self, pytester: Pytester) -> None: |
| 82 |
pytester.makeconftest( |
| 83 |
""" |
| 84 |
def pytest_configure(): |
| 85 |
print("---configure") |
| 86 |
def pytest_unconfigure(): |
| 87 |
print("---unconfigure") |
| 88 |
""" |
| 89 |
) |
| 90 |
result = pytester.runpytest("-s", "asd") |
| 91 |
assert result.ret == ExitCode.USAGE_ERROR |
| 92 |
result.stderr.fnmatch_lines(["ERROR: file or directory not found: asd"]) |
| 93 |
result.stdout.fnmatch_lines(["*---configure", "*---unconfigure"]) |
| 94 |
|
| 95 |
def test_config_preparse_plugin_option(self, pytester: Pytester) -> None: |
| 96 |
pytester.makepyfile( |
| 97 |
pytest_xyz=""" |
| 98 |
def pytest_addoption(parser): |
| 99 |
parser.addoption("--xyz", dest="xyz", action="store") |
| 100 |
""" |
| 101 |
) |
| 102 |
pytester.makepyfile( |
| 103 |
test_one=""" |
| 104 |
def test_option(pytestconfig): |
| 105 |
assert pytestconfig.option.xyz == "123" |
| 106 |
""" |
| 107 |
) |
| 108 |
result = pytester.runpytest("-p", "pytest_xyz", "--xyz=123", syspathinsert=True) |
| 109 |
assert result.ret == 0 |
| 110 |
result.stdout.fnmatch_lines(["*1 passed*"]) |
| 111 |
|
| 112 |
@pytest.mark.parametrize("load_cov_early", [True, False]) |
| 113 |
def test_early_load_setuptools_name( |
| 114 |
self, pytester: Pytester, monkeypatch, load_cov_early |
| 115 |
) -> None: |
| 116 |
monkeypatch.delenv("PYTEST_DISABLE_PLUGIN_AUTOLOAD") |
| 117 |
|
| 118 |
pytester.makepyfile(mytestplugin1_module="") |
| 119 |
pytester.makepyfile(mytestplugin2_module="") |
| 120 |
pytester.makepyfile(mycov_module="") |
| 121 |
pytester.syspathinsert() |
| 122 |
|
| 123 |
loaded = [] |
| 124 |
|
| 125 |
@dataclasses.dataclass |
| 126 |
class DummyEntryPoint: |
| 127 |
name: str |
| 128 |
module: str |
| 129 |
group: str = "pytest11" |
| 130 |
|
| 131 |
def load(self): |
| 132 |
mod = importlib.import_module(self.module) |
| 133 |
loaded.append(self.name) |
| 134 |
return mod |
| 135 |
|
| 136 |
entry_points = [ |
| 137 |
DummyEntryPoint("myplugin1", "mytestplugin1_module"), |
| 138 |
DummyEntryPoint("myplugin2", "mytestplugin2_module"), |
| 139 |
DummyEntryPoint("mycov", "mycov_module"), |
| 140 |
] |
| 141 |
|
| 142 |
@dataclasses.dataclass |
| 143 |
class DummyDist: |
| 144 |
entry_points: object |
| 145 |
files: object = () |
| 146 |
|
| 147 |
def my_dists(): |
| 148 |
return (DummyDist(entry_points),) |
| 149 |
|
| 150 |
monkeypatch.setattr(importlib.metadata, "distributions", my_dists) |
| 151 |
params = ("-p", "mycov") if load_cov_early else () |
| 152 |
pytester.runpytest_inprocess(*params) |
| 153 |
if load_cov_early: |
| 154 |
assert loaded == ["mycov", "myplugin1", "myplugin2"] |
| 155 |
else: |
| 156 |
assert loaded == ["myplugin1", "myplugin2", "mycov"] |
| 157 |
|
| 158 |
@pytest.mark.parametrize("import_mode", ["prepend", "append", "importlib"]) |
| 159 |
def test_assertion_rewrite(self, pytester: Pytester, import_mode) -> None: |
| 160 |
p = pytester.makepyfile( |
| 161 |
""" |
| 162 |
def test_this(): |
| 163 |
x = 0 |
| 164 |
assert x |
| 165 |
""" |
| 166 |
) |
| 167 |
result = pytester.runpytest(p, f"--import-mode={import_mode}") |
| 168 |
result.stdout.fnmatch_lines(["> assert x", "E assert 0"]) |
| 169 |
assert result.ret == 1 |
| 170 |
|
| 171 |
def test_nested_import_error(self, pytester: Pytester) -> None: |
| 172 |
p = pytester.makepyfile( |
| 173 |
""" |
| 174 |
import import_fails |
| 175 |
def test_this(): |
| 176 |
assert import_fails.a == 1 |
| 177 |
""" |
| 178 |
) |
| 179 |
pytester.makepyfile(import_fails="import does_not_work") |
| 180 |
result = pytester.runpytest(p) |
| 181 |
result.stdout.fnmatch_lines( |
| 182 |
[ |
| 183 |
"ImportError while importing test module*", |
| 184 |
"*No module named *does_not_work*", |
| 185 |
] |
| 186 |
) |
| 187 |
assert result.ret == 2 |
| 188 |
|
| 189 |
def test_not_collectable_arguments(self, pytester: Pytester) -> None: |
| 190 |
p1 = pytester.makepyfile("") |
| 191 |
p2 = pytester.makefile(".pyc", "123") |
| 192 |
result = pytester.runpytest(p1, p2) |
| 193 |
assert result.ret == ExitCode.USAGE_ERROR |
| 194 |
result.stderr.fnmatch_lines( |
| 195 |
[ |
| 196 |
f"ERROR: not found: {p2}", |
| 197 |
"(no match in any of *)", |
| 198 |
"", |
| 199 |
] |
| 200 |
) |
| 201 |
|
| 202 |
@pytest.mark.filterwarnings("default") |
| 203 |
def test_better_reporting_on_conftest_load_failure( |
| 204 |
self, pytester: Pytester |
| 205 |
) -> None: |
| 206 |
"""Show a user-friendly traceback on conftest import failures (#486, #3332)""" |
| 207 |
pytester.makepyfile("") |
| 208 |
conftest = pytester.makeconftest( |
| 209 |
""" |
| 210 |
def foo(): |
| 211 |
import qwerty |
| 212 |
foo() |
| 213 |
""" |
| 214 |
) |
| 215 |
result = pytester.runpytest("--help") |
| 216 |
result.stdout.fnmatch_lines( |
| 217 |
""" |
| 218 |
*--version* |
| 219 |
*warning*conftest.py* |
| 220 |
""" |
| 221 |
) |
| 222 |
result = pytester.runpytest() |
| 223 |
assert result.stdout.lines == [] |
| 224 |
assert result.stderr.lines == [ |
| 225 |
f"ImportError while loading conftest '{conftest}'.", |
| 226 |
"conftest.py:3: in <module>", |
| 227 |
" foo()", |
| 228 |
"conftest.py:2: in foo", |
| 229 |
" import qwerty", |
| 230 |
"E ModuleNotFoundError: No module named 'qwerty'", |
| 231 |
] |
| 232 |
|
| 233 |
def test_early_skip(self, pytester: Pytester) -> None: |
| 234 |
pytester.mkdir("xyz") |
| 235 |
pytester.makeconftest( |
| 236 |
""" |
| 237 |
import pytest |
| 238 |
def pytest_collect_file(): |
| 239 |
pytest.skip("early") |
| 240 |
""" |
| 241 |
) |
| 242 |
result = pytester.runpytest() |
| 243 |
assert result.ret == ExitCode.NO_TESTS_COLLECTED |
| 244 |
result.stdout.fnmatch_lines(["*1 skip*"]) |
| 245 |
|
| 246 |
def test_issue88_initial_file_multinodes(self, pytester: Pytester) -> None: |
| 247 |
pytester.copy_example("issue88_initial_file_multinodes") |
| 248 |
p = pytester.makepyfile("def test_hello(): pass") |
| 249 |
result = pytester.runpytest(p, "--collect-only") |
| 250 |
result.stdout.fnmatch_lines(["*MyFile*test_issue88*", "*Module*test_issue88*"]) |
| 251 |
|
| 252 |
def test_issue93_initialnode_importing_capturing(self, pytester: Pytester) -> None: |
| 253 |
pytester.makeconftest( |
| 254 |
""" |
| 255 |
import sys |
| 256 |
print("should not be seen") |
| 257 |
sys.stderr.write("stder42\\n") |
| 258 |
""" |
| 259 |
) |
| 260 |
result = pytester.runpytest() |
| 261 |
assert result.ret == ExitCode.NO_TESTS_COLLECTED |
| 262 |
result.stdout.no_fnmatch_line("*should not be seen*") |
| 263 |
assert "stderr42" not in result.stderr.str() |
| 264 |
|
| 265 |
def test_conftest_printing_shows_if_error(self, pytester: Pytester) -> None: |
| 266 |
pytester.makeconftest( |
| 267 |
""" |
| 268 |
print("should be seen") |
| 269 |
assert 0 |
| 270 |
""" |
| 271 |
) |
| 272 |
result = pytester.runpytest() |
| 273 |
assert result.ret != 0 |
| 274 |
assert "should be seen" in result.stdout.str() |
| 275 |
|
| 276 |
def test_issue109_sibling_conftests_not_loaded(self, pytester: Pytester) -> None: |
| 277 |
sub1 = pytester.mkdir("sub1") |
| 278 |
sub2 = pytester.mkdir("sub2") |
| 279 |
sub1.joinpath("conftest.py").write_text("assert 0", encoding="utf-8") |
| 280 |
result = pytester.runpytest(sub2) |
| 281 |
assert result.ret == ExitCode.NO_TESTS_COLLECTED |
| 282 |
sub2.joinpath("__init__.py").touch() |
| 283 |
p = sub2.joinpath("test_hello.py") |
| 284 |
p.touch() |
| 285 |
result = pytester.runpytest(p) |
| 286 |
assert result.ret == ExitCode.NO_TESTS_COLLECTED |
| 287 |
result = pytester.runpytest(sub1) |
| 288 |
assert result.ret == ExitCode.USAGE_ERROR |
| 289 |
|
| 290 |
def test_directory_skipped(self, pytester: Pytester) -> None: |
| 291 |
pytester.makeconftest( |
| 292 |
""" |
| 293 |
import pytest |
| 294 |
def pytest_ignore_collect(): |
| 295 |
pytest.skip("intentional") |
| 296 |
""" |
| 297 |
) |
| 298 |
pytester.makepyfile("def test_hello(): pass") |
| 299 |
result = pytester.runpytest() |
| 300 |
assert result.ret == ExitCode.NO_TESTS_COLLECTED |
| 301 |
result.stdout.fnmatch_lines(["*1 skipped*"]) |
| 302 |
|
| 303 |
def test_multiple_items_per_collector_byid(self, pytester: Pytester) -> None: |
| 304 |
c = pytester.makeconftest( |
| 305 |
""" |
| 306 |
import pytest |
| 307 |
class MyItem(pytest.Item): |
| 308 |
def runtest(self): |
| 309 |
pass |
| 310 |
class MyCollector(pytest.File): |
| 311 |
def collect(self): |
| 312 |
return [MyItem.from_parent(name="xyz", parent=self)] |
| 313 |
def pytest_collect_file(file_path, parent): |
| 314 |
if file_path.name.startswith("conftest"): |
| 315 |
return MyCollector.from_parent(path=file_path, parent=parent) |
| 316 |
""" |
| 317 |
) |
| 318 |
result = pytester.runpytest(c.name + "::" + "xyz") |
| 319 |
assert result.ret == 0 |
| 320 |
result.stdout.fnmatch_lines(["*1 pass*"]) |
| 321 |
|
| 322 |
def test_skip_on_generated_funcarg_id(self, pytester: Pytester) -> None: |
| 323 |
pytester.makeconftest( |
| 324 |
""" |
| 325 |
import pytest |
| 326 |
def pytest_generate_tests(metafunc): |
| 327 |
metafunc.parametrize('x', [3], ids=['hello-123']) |
| 328 |
def pytest_runtest_setup(item): |
| 329 |
print(item.keywords) |
| 330 |
if 'hello-123' in item.keywords: |
| 331 |
pytest.skip("hello") |
| 332 |
assert 0 |
| 333 |
""" |
| 334 |
) |
| 335 |
p = pytester.makepyfile("""def test_func(x): pass""") |
| 336 |
res = pytester.runpytest(p) |
| 337 |
assert res.ret == 0 |
| 338 |
res.stdout.fnmatch_lines(["*1 skipped*"]) |
| 339 |
|
| 340 |
def test_direct_addressing_selects(self, pytester: Pytester) -> None: |
| 341 |
p = pytester.makepyfile( |
| 342 |
""" |
| 343 |
def pytest_generate_tests(metafunc): |
| 344 |
metafunc.parametrize('i', [1, 2], ids=["1", "2"]) |
| 345 |
def test_func(i): |
| 346 |
pass |
| 347 |
""" |
| 348 |
) |
| 349 |
res = pytester.runpytest(p.name + "::" + "test_func[1]") |
| 350 |
assert res.ret == 0 |
| 351 |
res.stdout.fnmatch_lines(["*1 passed*"]) |
| 352 |
|
| 353 |
def test_direct_addressing_selects_duplicates(self, pytester: Pytester) -> None: |
| 354 |
p = pytester.makepyfile( |
| 355 |
""" |
| 356 |
import pytest |
| 357 |
|
| 358 |
@pytest.mark.parametrize("a", [1, 2, 10, 11, 2, 1, 12, 11]) |
| 359 |
def test_func(a): |
| 360 |
pass |
| 361 |
""" |
| 362 |
) |
| 363 |
result = pytester.runpytest(p) |
| 364 |
result.assert_outcomes(failed=0, passed=8) |
| 365 |
|
| 366 |
def test_direct_addressing_selects_duplicates_1(self, pytester: Pytester) -> None: |
| 367 |
p = pytester.makepyfile( |
| 368 |
""" |
| 369 |
import pytest |
| 370 |
|
| 371 |
@pytest.mark.parametrize("a", [1, 2, 10, 11, 2, 1, 12, 1_1,2_1]) |
| 372 |
def test_func(a): |
| 373 |
pass |
| 374 |
""" |
| 375 |
) |
| 376 |
result = pytester.runpytest(p) |
| 377 |
result.assert_outcomes(failed=0, passed=9) |
| 378 |
|
| 379 |
def test_direct_addressing_selects_duplicates_2(self, pytester: Pytester) -> None: |
| 380 |
p = pytester.makepyfile( |
| 381 |
""" |
| 382 |
import pytest |
| 383 |
|
| 384 |
@pytest.mark.parametrize("a", ["a","b","c","a","a1"]) |
| 385 |
def test_func(a): |
| 386 |
pass |
| 387 |
""" |
| 388 |
) |
| 389 |
result = pytester.runpytest(p) |
| 390 |
result.assert_outcomes(failed=0, passed=5) |
| 391 |
|
| 392 |
def test_direct_addressing_notfound(self, pytester: Pytester) -> None: |
| 393 |
p = pytester.makepyfile( |
| 394 |
""" |
| 395 |
def test_func(): |
| 396 |
pass |
| 397 |
""" |
| 398 |
) |
| 399 |
res = pytester.runpytest(p.name + "::" + "test_notfound") |
| 400 |
assert res.ret |
| 401 |
res.stderr.fnmatch_lines(["*ERROR*not found*"]) |
| 402 |
|
| 403 |
def test_docstring_on_hookspec(self) -> None: |
| 404 |
from _pytest import hookspec |
| 405 |
|
| 406 |
for name, value in vars(hookspec).items(): |
| 407 |
if name.startswith("pytest_"): |
| 408 |
assert value.__doc__, f"no docstring for {name}" |
| 409 |
|
| 410 |
def test_initialization_error_issue49(self, pytester: Pytester) -> None: |
| 411 |
pytester.makeconftest( |
| 412 |
""" |
| 413 |
def pytest_configure(): |
| 414 |
x |
| 415 |
""" |
| 416 |
) |
| 417 |
result = pytester.runpytest() |
| 418 |
assert result.ret == 3 # internal error |
| 419 |
result.stderr.fnmatch_lines(["INTERNAL*pytest_configure*", "INTERNAL*x*"]) |
| 420 |
assert "sessionstarttime" not in result.stderr.str() |
| 421 |
|
| 422 |
@pytest.mark.parametrize("lookfor", ["test_fun.py::test_a"]) |
| 423 |
def test_issue134_report_error_when_collecting_member( |
| 424 |
self, pytester: Pytester, lookfor |
| 425 |
) -> None: |
| 426 |
pytester.makepyfile( |
| 427 |
test_fun=""" |
| 428 |
def test_a(): |
| 429 |
pass |
| 430 |
def""" |
| 431 |
) |
| 432 |
result = pytester.runpytest(lookfor) |
| 433 |
result.stdout.fnmatch_lines(["*SyntaxError*"]) |
| 434 |
if "::" in lookfor: |
| 435 |
result.stderr.fnmatch_lines(["*ERROR*"]) |
| 436 |
assert result.ret == 4 # usage error only if item not found |
| 437 |
|
| 438 |
def test_report_all_failed_collections_initargs(self, pytester: Pytester) -> None: |
| 439 |
pytester.makeconftest( |
| 440 |
""" |
| 441 |
from _pytest.config import ExitCode |
| 442 |
|
| 443 |
def pytest_sessionfinish(exitstatus): |
| 444 |
assert exitstatus == ExitCode.USAGE_ERROR |
| 445 |
print("pytest_sessionfinish_called") |
| 446 |
""" |
| 447 |
) |
| 448 |
pytester.makepyfile(test_a="def", test_b="def") |
| 449 |
result = pytester.runpytest("test_a.py::a", "test_b.py::b") |
| 450 |
result.stderr.fnmatch_lines(["*ERROR*test_a.py::a*", "*ERROR*test_b.py::b*"]) |
| 451 |
result.stdout.fnmatch_lines(["pytest_sessionfinish_called"]) |
| 452 |
assert result.ret == ExitCode.USAGE_ERROR |
| 453 |
|
| 454 |
def test_namespace_import_doesnt_confuse_import_hook( |
| 455 |
self, pytester: Pytester |
| 456 |
) -> None: |
| 457 |
"""Ref #383. |
| 458 |
|
| 459 |
Python 3.3's namespace package messed with our import hooks. |
| 460 |
Importing a module that didn't exist, even if the ImportError was |
| 461 |
gracefully handled, would make our test crash. |
| 462 |
""" |
| 463 |
pytester.mkdir("not_a_package") |
| 464 |
p = pytester.makepyfile( |
| 465 |
""" |
| 466 |
try: |
| 467 |
from not_a_package import doesnt_exist |
| 468 |
except ImportError: |
| 469 |
# We handle the import error gracefully here |
| 470 |
pass |
| 471 |
|
| 472 |
def test_whatever(): |
| 473 |
pass |
| 474 |
""" |
| 475 |
) |
| 476 |
res = pytester.runpytest(p.name) |
| 477 |
assert res.ret == 0 |
| 478 |
|
| 479 |
def test_unknown_option(self, pytester: Pytester) -> None: |
| 480 |
result = pytester.runpytest("--qwlkej") |
| 481 |
result.stderr.fnmatch_lines( |
| 482 |
""" |
| 483 |
*unrecognized* |
| 484 |
""" |
| 485 |
) |
| 486 |
|
| 487 |
def test_getsourcelines_error_issue553( |
| 488 |
self, pytester: Pytester, monkeypatch |
| 489 |
) -> None: |
| 490 |
monkeypatch.setattr("inspect.getsourcelines", None) |
| 491 |
p = pytester.makepyfile( |
| 492 |
""" |
| 493 |
def raise_error(obj): |
| 494 |
raise OSError('source code not available') |
| 495 |
|
| 496 |
import inspect |
| 497 |
inspect.getsourcelines = raise_error |
| 498 |
|
| 499 |
def test_foo(invalid_fixture): |
| 500 |
pass |
| 501 |
""" |
| 502 |
) |
| 503 |
res = pytester.runpytest(p) |
| 504 |
res.stdout.fnmatch_lines( |
| 505 |
["*source code not available*", "E*fixture 'invalid_fixture' not found"] |
| 506 |
) |
| 507 |
|
| 508 |
def test_plugins_given_as_strings( |
| 509 |
self, pytester: Pytester, monkeypatch, _sys_snapshot |
| 510 |
) -> None: |
| 511 |
"""Test that str values passed to main() as `plugins` arg are |
| 512 |
interpreted as module names to be imported and registered (#855).""" |
| 513 |
with pytest.raises(ImportError) as excinfo: |
| 514 |
pytest.main([str(pytester.path)], plugins=["invalid.module"]) |
| 515 |
assert "invalid" in str(excinfo.value) |
| 516 |
|
| 517 |
p = pytester.path.joinpath("test_test_plugins_given_as_strings.py") |
| 518 |
p.write_text("def test_foo(): pass", encoding="utf-8") |
| 519 |
mod = types.ModuleType("myplugin") |
| 520 |
monkeypatch.setitem(sys.modules, "myplugin", mod) |
| 521 |
assert pytest.main(args=[str(pytester.path)], plugins=["myplugin"]) == 0 |
| 522 |
|
| 523 |
def test_parametrized_with_bytes_regex(self, pytester: Pytester) -> None: |
| 524 |
p = pytester.makepyfile( |
| 525 |
""" |
| 526 |
import re |
| 527 |
import pytest |
| 528 |
@pytest.mark.parametrize('r', [re.compile(b'foo')]) |
| 529 |
def test_stuff(r): |
| 530 |
pass |
| 531 |
""" |
| 532 |
) |
| 533 |
res = pytester.runpytest(p) |
| 534 |
res.stdout.fnmatch_lines(["*1 passed*"]) |
| 535 |
|
| 536 |
def test_parametrized_with_null_bytes(self, pytester: Pytester) -> None: |
| 537 |
"""Test parametrization with values that contain null bytes and unicode characters (#2644, #2957)""" |
| 538 |
p = pytester.makepyfile( |
| 539 |
"""\ |
| 540 |
import pytest |
| 541 |
|
| 542 |
@pytest.mark.parametrize("data", [b"\\x00", "\\x00", 'ação']) |
| 543 |
def test_foo(data): |
| 544 |
assert data |
| 545 |
""" |
| 546 |
) |
| 547 |
res = pytester.runpytest(p) |
| 548 |
res.assert_outcomes(passed=3) |
| 549 |
|
| 550 |
# Warning ignore because of: |
| 551 |
# https://github.com/python/cpython/issues/85308 |
| 552 |
# Can be removed once Python<3.12 support is dropped. |
| 553 |
@pytest.mark.filterwarnings("ignore:'encoding' argument not specified") |
| 554 |
def test_command_line_args_from_file( |
| 555 |
self, pytester: Pytester, tmp_path: Path |
| 556 |
) -> None: |
| 557 |
pytester.makepyfile( |
| 558 |
test_file=""" |
| 559 |
import pytest |
| 560 |
|
| 561 |
class TestClass: |
| 562 |
@pytest.mark.parametrize("a", ["x","y"]) |
| 563 |
def test_func(self, a): |
| 564 |
pass |
| 565 |
""" |
| 566 |
) |
| 567 |
tests = [ |
| 568 |
"test_file.py::TestClass::test_func[x]", |
| 569 |
"test_file.py::TestClass::test_func[y]", |
| 570 |
"-q", |
| 571 |
] |
| 572 |
args_file = pytester.maketxtfile(tests="\n".join(tests)) |
| 573 |
result = pytester.runpytest(f"@{args_file}") |
| 574 |
result.assert_outcomes(failed=0, passed=2) |
| 575 |
|
| 576 |
|
| 577 |
class TestInvocationVariants: |
| 578 |
def test_earlyinit(self, pytester: Pytester) -> None: |
| 579 |
p = pytester.makepyfile( |
| 580 |
""" |
| 581 |
import pytest |
| 582 |
assert hasattr(pytest, 'mark') |
| 583 |
""" |
| 584 |
) |
| 585 |
result = pytester.runpython(p) |
| 586 |
assert result.ret == 0 |
| 587 |
|
| 588 |
def test_pydoc(self, pytester: Pytester) -> None: |
| 589 |
result = pytester.runpython_c("import pytest;help(pytest)") |
| 590 |
assert result.ret == 0 |
| 591 |
s = result.stdout.str() |
| 592 |
assert "MarkGenerator" in s |
| 593 |
|
| 594 |
def test_import_star_pytest(self, pytester: Pytester) -> None: |
| 595 |
p = pytester.makepyfile( |
| 596 |
""" |
| 597 |
from pytest import * |
| 598 |
#Item |
| 599 |
#File |
| 600 |
main |
| 601 |
skip |
| 602 |
xfail |
| 603 |
""" |
| 604 |
) |
| 605 |
result = pytester.runpython(p) |
| 606 |
assert result.ret == 0 |
| 607 |
|
| 608 |
def test_double_pytestcmdline(self, pytester: Pytester) -> None: |
| 609 |
p = pytester.makepyfile( |
| 610 |
run=""" |
| 611 |
import pytest |
| 612 |
pytest.main() |
| 613 |
pytest.main() |
| 614 |
""" |
| 615 |
) |
| 616 |
pytester.makepyfile( |
| 617 |
""" |
| 618 |
def test_hello(): |
| 619 |
pass |
| 620 |
""" |
| 621 |
) |
| 622 |
result = pytester.runpython(p) |
| 623 |
result.stdout.fnmatch_lines(["*1 passed*", "*1 passed*"]) |
| 624 |
|
| 625 |
def test_python_minus_m_invocation_ok(self, pytester: Pytester) -> None: |
| 626 |
p1 = pytester.makepyfile("def test_hello(): pass") |
| 627 |
res = pytester.run(sys.executable, "-m", "pytest", str(p1)) |
| 628 |
assert res.ret == 0 |
| 629 |
|
| 630 |
def test_python_minus_m_invocation_fail(self, pytester: Pytester) -> None: |
| 631 |
p1 = pytester.makepyfile("def test_fail(): 0/0") |
| 632 |
res = pytester.run(sys.executable, "-m", "pytest", str(p1)) |
| 633 |
assert res.ret == 1 |
| 634 |
|
| 635 |
def test_python_pytest_package(self, pytester: Pytester) -> None: |
| 636 |
p1 = pytester.makepyfile("def test_pass(): pass") |
| 637 |
res = pytester.run(sys.executable, "-m", "pytest", str(p1)) |
| 638 |
assert res.ret == 0 |
| 639 |
res.stdout.fnmatch_lines(["*1 passed*"]) |
| 640 |
|
| 641 |
def test_invoke_with_invalid_type(self) -> None: |
| 642 |
with pytest.raises( |
| 643 |
TypeError, match="expected to be a list of strings, got: '-h'" |
| 644 |
): |
| 645 |
pytest.main("-h") # type: ignore[arg-type] |
| 646 |
|
| 647 |
def test_invoke_with_path(self, pytester: Pytester) -> None: |
| 648 |
retcode = pytest.main([str(pytester.path)]) |
| 649 |
assert retcode == ExitCode.NO_TESTS_COLLECTED |
| 650 |
|
| 651 |
def test_invoke_plugin_api(self, capsys) -> None: |
| 652 |
class MyPlugin: |
| 653 |
def pytest_addoption(self, parser): |
| 654 |
parser.addoption("--myopt") |
| 655 |
|
| 656 |
pytest.main(["-h"], plugins=[MyPlugin()]) |
| 657 |
out, _err = capsys.readouterr() |
| 658 |
assert "--myopt" in out |
| 659 |
|
| 660 |
def test_pyargs_importerror(self, pytester: Pytester, monkeypatch) -> None: |
| 661 |
monkeypatch.delenv("PYTHONDONTWRITEBYTECODE", False) |
| 662 |
path = pytester.mkpydir("tpkg") |
| 663 |
path.joinpath("test_hello.py").write_text("raise ImportError", encoding="utf-8") |
| 664 |
|
| 665 |
result = pytester.runpytest("--pyargs", "tpkg.test_hello", syspathinsert=True) |
| 666 |
assert result.ret != 0 |
| 667 |
|
| 668 |
result.stdout.fnmatch_lines(["collected*0*items*/*1*error"]) |
| 669 |
|
| 670 |
def test_pyargs_only_imported_once(self, pytester: Pytester) -> None: |
| 671 |
pkg = pytester.mkpydir("foo") |
| 672 |
pkg.joinpath("test_foo.py").write_text( |
| 673 |
"print('hello from test_foo')\ndef test(): pass", encoding="utf-8" |
| 674 |
) |
| 675 |
pkg.joinpath("conftest.py").write_text( |
| 676 |
"def pytest_configure(config): print('configuring')", encoding="utf-8" |
| 677 |
) |
| 678 |
|
| 679 |
result = pytester.runpytest( |
| 680 |
"--pyargs", "foo.test_foo", "-s", syspathinsert=True |
| 681 |
) |
| 682 |
# should only import once |
| 683 |
assert result.outlines.count("hello from test_foo") == 1 |
| 684 |
# should only configure once |
| 685 |
assert result.outlines.count("configuring") == 1 |
| 686 |
|
| 687 |
def test_pyargs_filename_looks_like_module(self, pytester: Pytester) -> None: |
| 688 |
pytester.path.joinpath("conftest.py").touch() |
| 689 |
pytester.path.joinpath("t.py").write_text("def test(): pass", encoding="utf-8") |
| 690 |
result = pytester.runpytest("--pyargs", "t.py") |
| 691 |
assert result.ret == ExitCode.OK |
| 692 |
|
| 693 |
def test_cmdline_python_package(self, pytester: Pytester, monkeypatch) -> None: |
| 694 |
import warnings |
| 695 |
|
| 696 |
monkeypatch.delenv("PYTHONDONTWRITEBYTECODE", False) |
| 697 |
path = pytester.mkpydir("tpkg") |
| 698 |
path.joinpath("test_hello.py").write_text( |
| 699 |
"def test_hello(): pass", encoding="utf-8" |
| 700 |
) |
| 701 |
path.joinpath("test_world.py").write_text( |
| 702 |
"def test_world(): pass", encoding="utf-8" |
| 703 |
) |
| 704 |
result = pytester.runpytest("--pyargs", "tpkg") |
| 705 |
assert result.ret == 0 |
| 706 |
result.stdout.fnmatch_lines(["*2 passed*"]) |
| 707 |
result = pytester.runpytest("--pyargs", "tpkg.test_hello", syspathinsert=True) |
| 708 |
assert result.ret == 0 |
| 709 |
result.stdout.fnmatch_lines(["*1 passed*"]) |
| 710 |
|
| 711 |
empty_package = pytester.mkpydir("empty_package") |
| 712 |
monkeypatch.setenv("PYTHONPATH", str(empty_package), prepend=os.pathsep) |
| 713 |
# the path which is not a package raises a warning on pypy; |
| 714 |
# no idea why only pypy and not normal python warn about it here |
| 715 |
with warnings.catch_warnings(): |
| 716 |
warnings.simplefilter("ignore", ImportWarning) |
| 717 |
result = pytester.runpytest("--pyargs", ".") |
| 718 |
assert result.ret == 0 |
| 719 |
result.stdout.fnmatch_lines(["*2 passed*"]) |
| 720 |
|
| 721 |
monkeypatch.setenv("PYTHONPATH", str(pytester), prepend=os.pathsep) |
| 722 |
result = pytester.runpytest("--pyargs", "tpkg.test_missing", syspathinsert=True) |
| 723 |
assert result.ret != 0 |
| 724 |
result.stderr.fnmatch_lines(["*not*found*test_missing*"]) |
| 725 |
|
| 726 |
@pytest.mark.skipif( |
| 727 |
int(setuptools.__version__.split(".")[0]) >= 80, |
| 728 |
reason="modern setuptools removing pkg_resources", |
| 729 |
) |
| 730 |
def test_cmdline_python_legacy_namespace_package( |
| 731 |
self, pytester: Pytester, monkeypatch |
| 732 |
) -> None: |
| 733 |
"""Test --pyargs option with legacy namespace packages (#1567). |
| 734 |
|
| 735 |
Ref: https://packaging.python.org/guides/packaging-namespace-packages/ |
| 736 |
""" |
| 737 |
monkeypatch.delenv("PYTHONDONTWRITEBYTECODE", raising=False) |
| 738 |
|
| 739 |
search_path = [] |
| 740 |
for dirname in "hello", "world": |
| 741 |
d = pytester.mkdir(dirname) |
| 742 |
search_path.append(d) |
| 743 |
ns = d.joinpath("ns_pkg") |
| 744 |
ns.mkdir() |
| 745 |
ns.joinpath("__init__.py").write_text( |
| 746 |
"__import__('pkg_resources').declare_namespace(__name__)", |
| 747 |
encoding="utf-8", |
| 748 |
) |
| 749 |
lib = ns.joinpath(dirname) |
| 750 |
lib.mkdir() |
| 751 |
lib.joinpath("__init__.py").touch() |
| 752 |
lib.joinpath(f"test_{dirname}.py").write_text( |
| 753 |
f"def test_{dirname}(): pass\ndef test_other():pass", |
| 754 |
encoding="utf-8", |
| 755 |
) |
| 756 |
|
| 757 |
# The structure of the test directory is now: |
| 758 |
# . |
| 759 |
# ├── hello |
| 760 |
# │ └── ns_pkg |
| 761 |
# │ ├── __init__.py |
| 762 |
# │ └── hello |
| 763 |
# │ ├── __init__.py |
| 764 |
# │ └── test_hello.py |
| 765 |
# └── world |
| 766 |
# └── ns_pkg |
| 767 |
# ├── __init__.py |
| 768 |
# └── world |
| 769 |
# ├── __init__.py |
| 770 |
# └── test_world.py |
| 771 |
|
| 772 |
# NOTE: the different/reversed ordering is intentional here. |
| 773 |
monkeypatch.setenv("PYTHONPATH", prepend_pythonpath(*search_path)) |
| 774 |
for p in search_path: |
| 775 |
monkeypatch.syspath_prepend(p) |
| 776 |
|
| 777 |
# mixed module and filenames: |
| 778 |
monkeypatch.chdir("world") |
| 779 |
|
| 780 |
# pgk_resources.declare_namespace has been deprecated in favor of implicit namespace packages. |
| 781 |
# pgk_resources has been deprecated entirely. |
| 782 |
# While we could change the test to use implicit namespace packages, seems better |
| 783 |
# to still ensure the old declaration via declare_namespace still works. |
| 784 |
ignore_w = ( |
| 785 |
r"-Wignore:Deprecated call to `pkg_resources.declare_namespace", |
| 786 |
r"-Wignore:pkg_resources is deprecated", |
| 787 |
) |
| 788 |
result = pytester.runpytest( |
| 789 |
"--pyargs", "-v", "ns_pkg.hello", "ns_pkg/world", *ignore_w |
| 790 |
) |
| 791 |
assert result.ret == 0 |
| 792 |
result.stdout.fnmatch_lines( |
| 793 |
[ |
| 794 |
"test_hello.py::test_hello*PASSED*", |
| 795 |
"test_hello.py::test_other*PASSED*", |
| 796 |
"ns_pkg/world/test_world.py::test_world*PASSED*", |
| 797 |
"ns_pkg/world/test_world.py::test_other*PASSED*", |
| 798 |
"*4 passed in*", |
| 799 |
] |
| 800 |
) |
| 801 |
|
| 802 |
# specify tests within a module |
| 803 |
pytester.chdir() |
| 804 |
result = pytester.runpytest( |
| 805 |
"--pyargs", "-v", "ns_pkg.world.test_world::test_other" |
| 806 |
) |
| 807 |
assert result.ret == 0 |
| 808 |
result.stdout.fnmatch_lines( |
| 809 |
["*test_world.py::test_other*PASSED*", "*1 passed*"] |
| 810 |
) |
| 811 |
|
| 812 |
def test_invoke_test_and_doctestmodules(self, pytester: Pytester) -> None: |
| 813 |
p = pytester.makepyfile( |
| 814 |
""" |
| 815 |
def test(): |
| 816 |
pass |
| 817 |
""" |
| 818 |
) |
| 819 |
result = pytester.runpytest(str(p) + "::test", "--doctest-modules") |
| 820 |
result.stdout.fnmatch_lines(["*1 passed*"]) |
| 821 |
|
| 822 |
def test_cmdline_python_package_symlink( |
| 823 |
self, pytester: Pytester, monkeypatch |
| 824 |
) -> None: |
| 825 |
""" |
| 826 |
--pyargs with packages with path containing symlink can have conftest.py in |
| 827 |
their package (#2985) |
| 828 |
""" |
| 829 |
monkeypatch.delenv("PYTHONDONTWRITEBYTECODE", raising=False) |
| 830 |
|
| 831 |
dirname = "lib" |
| 832 |
d = pytester.mkdir(dirname) |
| 833 |
foo = d.joinpath("foo") |
| 834 |
foo.mkdir() |
| 835 |
foo.joinpath("__init__.py").touch() |
| 836 |
lib = foo.joinpath("bar") |
| 837 |
lib.mkdir() |
| 838 |
lib.joinpath("__init__.py").touch() |
| 839 |
lib.joinpath("test_bar.py").write_text( |
| 840 |
"def test_bar(): pass\ndef test_other(a_fixture):pass", encoding="utf-8" |
| 841 |
) |
| 842 |
lib.joinpath("conftest.py").write_text( |
| 843 |
"import pytest\[email protected]\ndef a_fixture():pass", encoding="utf-8" |
| 844 |
) |
| 845 |
|
| 846 |
d_local = pytester.mkdir("symlink_root") |
| 847 |
symlink_location = d_local / "lib" |
| 848 |
symlink_or_skip(d, symlink_location, target_is_directory=True) |
| 849 |
|
| 850 |
# The structure of the test directory is now: |
| 851 |
# . |
| 852 |
# ├── symlink_root |
| 853 |
# │ └── lib -> ../lib |
| 854 |
# └── lib |
| 855 |
# └── foo |
| 856 |
# ├── __init__.py |
| 857 |
# └── bar |
| 858 |
# ├── __init__.py |
| 859 |
# ├── conftest.py |
| 860 |
# └── test_bar.py |
| 861 |
|
| 862 |
# NOTE: the different/reversed ordering is intentional here. |
| 863 |
search_path = ["lib", os.path.join("symlink_root", "lib")] |
| 864 |
monkeypatch.setenv("PYTHONPATH", prepend_pythonpath(*search_path)) |
| 865 |
for p in search_path: |
| 866 |
monkeypatch.syspath_prepend(p) |
| 867 |
|
| 868 |
# module picked up in symlink-ed directory: |
| 869 |
# It picks up symlink_root/lib/foo/bar (symlink) via sys.path. |
| 870 |
result = pytester.runpytest("--pyargs", "-v", "foo.bar") |
| 871 |
pytester.chdir() |
| 872 |
assert result.ret == 0 |
| 873 |
result.stdout.fnmatch_lines( |
| 874 |
[ |
| 875 |
"symlink_root/lib/foo/bar/test_bar.py::test_bar PASSED*", |
| 876 |
"symlink_root/lib/foo/bar/test_bar.py::test_other PASSED*", |
| 877 |
"*2 passed*", |
| 878 |
] |
| 879 |
) |
| 880 |
|
| 881 |
def test_cmdline_python_package_not_exists(self, pytester: Pytester) -> None: |
| 882 |
result = pytester.runpytest("--pyargs", "tpkgwhatv") |
| 883 |
assert result.ret |
| 884 |
result.stderr.fnmatch_lines(["ERROR*module*or*package*not*found*"]) |
| 885 |
|
| 886 |
@pytest.mark.xfail(reason="decide: feature or bug") |
| 887 |
def test_noclass_discovery_if_not_testcase(self, pytester: Pytester) -> None: |
| 888 |
testpath = pytester.makepyfile( |
| 889 |
""" |
| 890 |
import unittest |
| 891 |
class TestHello(object): |
| 892 |
def test_hello(self): |
| 893 |
assert self.attr |
| 894 |
|
| 895 |
class RealTest(unittest.TestCase, TestHello): |
| 896 |
attr = 42 |
| 897 |
""" |
| 898 |
) |
| 899 |
reprec = pytester.inline_run(testpath) |
| 900 |
reprec.assertoutcome(passed=1) |
| 901 |
|
| 902 |
def test_doctest_id(self, pytester: Pytester) -> None: |
| 903 |
pytester.makefile( |
| 904 |
".txt", |
| 905 |
""" |
| 906 |
>>> x=3 |
| 907 |
>>> x |
| 908 |
4 |
| 909 |
""", |
| 910 |
) |
| 911 |
testid = "test_doctest_id.txt::test_doctest_id.txt" |
| 912 |
expected_lines = [ |
| 913 |
"*= FAILURES =*", |
| 914 |
"*_ ?doctest? test_doctest_id.txt _*", |
| 915 |
"FAILED test_doctest_id.txt::test_doctest_id.txt", |
| 916 |
"*= 1 failed in*", |
| 917 |
] |
| 918 |
result = pytester.runpytest(testid, "-rf", "--tb=short") |
| 919 |
result.stdout.fnmatch_lines(expected_lines) |
| 920 |
|
| 921 |
# Ensure that re-running it will still handle it as |
| 922 |
# doctest.DocTestFailure, which was not the case before when |
| 923 |
# re-importing doctest, but not creating a new RUNNER_CLASS. |
| 924 |
result = pytester.runpytest(testid, "-rf", "--tb=short") |
| 925 |
result.stdout.fnmatch_lines(expected_lines) |
| 926 |
|
| 927 |
def test_core_backward_compatibility(self) -> None: |
| 928 |
"""Test backward compatibility for get_plugin_manager function. See #787.""" |
| 929 |
import _pytest.config |
| 930 |
|
| 931 |
assert ( |
| 932 |
type(_pytest.config.get_plugin_manager()) |
| 933 |
is _pytest.config.PytestPluginManager |
| 934 |
) |
| 935 |
|
| 936 |
def test_has_plugin(self, request) -> None: |
| 937 |
"""Test hasplugin function of the plugin manager (#932).""" |
| 938 |
assert request.config.pluginmanager.hasplugin("python") |
| 939 |
|
| 940 |
|
| 941 |
class TestDurations: |
| 942 |
source = """ |
| 943 |
from _pytest import timing |
| 944 |
def test_something(): |
| 945 |
pass |
| 946 |
def test_2(): |
| 947 |
timing.sleep(0.010) |
| 948 |
def test_1(): |
| 949 |
timing.sleep(0.002) |
| 950 |
def test_3(): |
| 951 |
timing.sleep(0.020) |
| 952 |
""" |
| 953 |
|
| 954 |
def test_calls(self, pytester: Pytester, mock_timing) -> None: |
| 955 |
pytester.makepyfile(self.source) |
| 956 |
result = pytester.runpytest_inprocess("--durations=10") |
| 957 |
assert result.ret == 0 |
| 958 |
|
| 959 |
result.stdout.fnmatch_lines_random( |
| 960 |
["*durations*", "*call*test_3*", "*call*test_2*"] |
| 961 |
) |
| 962 |
|
| 963 |
result.stdout.fnmatch_lines( |
| 964 |
["(8 durations < 0.005s hidden. Use -vv to show these durations.)"] |
| 965 |
) |
| 966 |
|
| 967 |
def test_calls_show_2(self, pytester: Pytester, mock_timing) -> None: |
| 968 |
pytester.makepyfile(self.source) |
| 969 |
result = pytester.runpytest_inprocess("--durations=2") |
| 970 |
assert result.ret == 0 |
| 971 |
|
| 972 |
lines = result.stdout.get_lines_after("*slowest*durations*") |
| 973 |
assert "4 passed" in lines[2] |
| 974 |
|
| 975 |
def test_calls_showall(self, pytester: Pytester, mock_timing) -> None: |
| 976 |
pytester.makepyfile(self.source) |
| 977 |
result = pytester.runpytest_inprocess("--durations=0") |
| 978 |
assert result.ret == 0 |
| 979 |
TestDurations.check_tests_in_output(result.stdout.lines, 2, 3) |
| 980 |
|
| 981 |
def test_calls_showall_verbose(self, pytester: Pytester, mock_timing) -> None: |
| 982 |
pytester.makepyfile(self.source) |
| 983 |
result = pytester.runpytest_inprocess("--durations=0", "-vv") |
| 984 |
assert result.ret == 0 |
| 985 |
TestDurations.check_tests_in_output(result.stdout.lines, 1, 2, 3) |
| 986 |
|
| 987 |
def test_calls_showall_durationsmin(self, pytester: Pytester, mock_timing) -> None: |
| 988 |
pytester.makepyfile(self.source) |
| 989 |
result = pytester.runpytest_inprocess("--durations=0", "--durations-min=0.015") |
| 990 |
assert result.ret == 0 |
| 991 |
TestDurations.check_tests_in_output(result.stdout.lines, 3) |
| 992 |
|
| 993 |
def test_calls_showall_durationsmin_verbose( |
| 994 |
self, pytester: Pytester, mock_timing |
| 995 |
) -> None: |
| 996 |
pytester.makepyfile(self.source) |
| 997 |
result = pytester.runpytest_inprocess( |
| 998 |
"--durations=0", "--durations-min=0.015", "-vv" |
| 999 |
) |
| 1000 |
assert result.ret == 0 |
| 1001 |
TestDurations.check_tests_in_output(result.stdout.lines, 3) |
| 1002 |
|
| 1003 |
@staticmethod |
| 1004 |
def check_tests_in_output( |
| 1005 |
lines: Sequence[str], *expected_test_numbers: int, number_of_tests: int = 3 |
| 1006 |
) -> None: |
| 1007 |
found_test_numbers = { |
| 1008 |
test_number |
| 1009 |
for test_number in range(1, number_of_tests + 1) |
| 1010 |
if any( |
| 1011 |
line.endswith(f"test_{test_number}") and " call " in line |
| 1012 |
for line in lines |
| 1013 |
) |
| 1014 |
} |
| 1015 |
assert found_test_numbers == set(expected_test_numbers) |
| 1016 |
|
| 1017 |
def test_with_deselected(self, pytester: Pytester, mock_timing) -> None: |
| 1018 |
pytester.makepyfile(self.source) |
| 1019 |
result = pytester.runpytest_inprocess("--durations=2", "-k test_3") |
| 1020 |
assert result.ret == 0 |
| 1021 |
|
| 1022 |
result.stdout.fnmatch_lines(["*durations*", "*call*test_3*"]) |
| 1023 |
|
| 1024 |
def test_with_failing_collection(self, pytester: Pytester, mock_timing) -> None: |
| 1025 |
pytester.makepyfile(self.source) |
| 1026 |
pytester.makepyfile(test_collecterror="""xyz""") |
| 1027 |
result = pytester.runpytest_inprocess("--durations=2", "-k test_1") |
| 1028 |
assert result.ret == 2 |
| 1029 |
|
| 1030 |
result.stdout.fnmatch_lines(["*Interrupted: 1 error during collection*"]) |
| 1031 |
# Collection errors abort test execution, therefore no duration is |
| 1032 |
# output |
| 1033 |
result.stdout.no_fnmatch_line("*duration*") |
| 1034 |
|
| 1035 |
def test_with_not(self, pytester: Pytester, mock_timing) -> None: |
| 1036 |
pytester.makepyfile(self.source) |
| 1037 |
result = pytester.runpytest_inprocess("-k not 1") |
| 1038 |
assert result.ret == 0 |
| 1039 |
|
| 1040 |
|
| 1041 |
class TestDurationsWithFixture: |
| 1042 |
source = """ |
| 1043 |
import pytest |
| 1044 |
from _pytest import timing |
| 1045 |
|
| 1046 |
@pytest.fixture |
| 1047 |
def setup_fixt(): |
| 1048 |
timing.sleep(2) |
| 1049 |
|
| 1050 |
def test_1(setup_fixt): |
| 1051 |
timing.sleep(5) |
| 1052 |
""" |
| 1053 |
|
| 1054 |
def test_setup_function(self, pytester: Pytester, mock_timing) -> None: |
| 1055 |
pytester.makepyfile(self.source) |
| 1056 |
result = pytester.runpytest_inprocess("--durations=10") |
| 1057 |
assert result.ret == 0 |
| 1058 |
|
| 1059 |
result.stdout.fnmatch_lines_random( |
| 1060 |
""" |
| 1061 |
*durations* |
| 1062 |
5.00s call *test_1* |
| 1063 |
2.00s setup *test_1* |
| 1064 |
""" |
| 1065 |
) |
| 1066 |
|
| 1067 |
|
| 1068 |
def test_zipimport_hook(pytester: Pytester) -> None: |
| 1069 |
"""Test package loader is being used correctly (see #1837).""" |
| 1070 |
zipapp = pytest.importorskip("zipapp") |
| 1071 |
pytester.path.joinpath("app").mkdir() |
| 1072 |
pytester.makepyfile( |
| 1073 |
**{ |
| 1074 |
"app/foo.py": """ |
| 1075 |
import pytest |
| 1076 |
def main(): |
| 1077 |
pytest.main(['--pyargs', 'foo']) |
| 1078 |
""" |
| 1079 |
} |
| 1080 |
) |
| 1081 |
target = pytester.path.joinpath("foo.zip") |
| 1082 |
zipapp.create_archive( |
| 1083 |
str(pytester.path.joinpath("app")), str(target), main="foo:main" |
| 1084 |
) |
| 1085 |
result = pytester.runpython(target) |
| 1086 |
assert result.ret == 0 |
| 1087 |
result.stderr.fnmatch_lines(["*not found*foo*"]) |
| 1088 |
result.stdout.no_fnmatch_line("*INTERNALERROR>*") |
| 1089 |
|
| 1090 |
|
| 1091 |
def test_import_plugin_unicode_name(pytester: Pytester) -> None: |
| 1092 |
pytester.makepyfile(myplugin="") |
| 1093 |
pytester.makepyfile("def test(): pass") |
| 1094 |
pytester.makeconftest("pytest_plugins = ['myplugin']") |
| 1095 |
r = pytester.runpytest() |
| 1096 |
assert r.ret == 0 |
| 1097 |
|
| 1098 |
|
| 1099 |
def test_pytest_plugins_as_module(pytester: Pytester) -> None: |
| 1100 |
"""Do not raise an error if pytest_plugins attribute is a module (#3899)""" |
| 1101 |
pytester.makepyfile( |
| 1102 |
**{ |
| 1103 |
"__init__.py": "", |
| 1104 |
"pytest_plugins.py": "", |
| 1105 |
"conftest.py": "from . import pytest_plugins", |
| 1106 |
"test_foo.py": "def test(): pass", |
| 1107 |
} |
| 1108 |
) |
| 1109 |
result = pytester.runpytest() |
| 1110 |
result.stdout.fnmatch_lines(["* 1 passed in *"]) |
| 1111 |
|
| 1112 |
|
| 1113 |
def test_deferred_hook_checking(pytester: Pytester) -> None: |
| 1114 |
"""Check hooks as late as possible (#1821).""" |
| 1115 |
pytester.syspathinsert() |
| 1116 |
pytester.makepyfile( |
| 1117 |
**{ |
| 1118 |
"plugin.py": """ |
| 1119 |
class Hooks(object): |
| 1120 |
def pytest_my_hook(self, config): |
| 1121 |
pass |
| 1122 |
|
| 1123 |
def pytest_configure(config): |
| 1124 |
config.pluginmanager.add_hookspecs(Hooks) |
| 1125 |
""", |
| 1126 |
"conftest.py": """ |
| 1127 |
pytest_plugins = ['plugin'] |
| 1128 |
def pytest_my_hook(config): |
| 1129 |
return 40 |
| 1130 |
""", |
| 1131 |
"test_foo.py": """ |
| 1132 |
def test(request): |
| 1133 |
assert request.config.hook.pytest_my_hook(config=request.config) == [40] |
| 1134 |
""", |
| 1135 |
} |
| 1136 |
) |
| 1137 |
result = pytester.runpytest() |
| 1138 |
result.stdout.fnmatch_lines(["* 1 passed *"]) |
| 1139 |
|
| 1140 |
|
| 1141 |
def test_fixture_values_leak(pytester: Pytester) -> None: |
| 1142 |
"""Ensure that fixture objects are properly destroyed by the garbage collector at the end of their expected |
| 1143 |
life-times (#2981). |
| 1144 |
""" |
| 1145 |
pytester.makepyfile( |
| 1146 |
""" |
| 1147 |
import dataclasses |
| 1148 |
import gc |
| 1149 |
import pytest |
| 1150 |
import weakref |
| 1151 |
|
| 1152 |
@dataclasses.dataclass |
| 1153 |
class SomeObj: |
| 1154 |
name: str |
| 1155 |
|
| 1156 |
fix_of_test1_ref = None |
| 1157 |
session_ref = None |
| 1158 |
|
| 1159 |
@pytest.fixture(scope='session') |
| 1160 |
def session_fix(): |
| 1161 |
global session_ref |
| 1162 |
obj = SomeObj(name='session-fixture') |
| 1163 |
session_ref = weakref.ref(obj) |
| 1164 |
return obj |
| 1165 |
|
| 1166 |
@pytest.fixture |
| 1167 |
def fix(session_fix): |
| 1168 |
global fix_of_test1_ref |
| 1169 |
obj = SomeObj(name='local-fixture') |
| 1170 |
fix_of_test1_ref = weakref.ref(obj) |
| 1171 |
return obj |
| 1172 |
|
| 1173 |
def test1(fix): |
| 1174 |
assert fix_of_test1_ref() is fix |
| 1175 |
|
| 1176 |
def test2(): |
| 1177 |
gc.collect() |
| 1178 |
# fixture "fix" created during test1 must have been destroyed by now |
| 1179 |
assert fix_of_test1_ref() is None |
| 1180 |
""" |
| 1181 |
) |
| 1182 |
# Running on subprocess does not activate the HookRecorder |
| 1183 |
# which holds itself a reference to objects in case of the |
| 1184 |
# pytest_assert_reprcompare hook |
| 1185 |
result = pytester.runpytest_subprocess() |
| 1186 |
result.stdout.fnmatch_lines(["* 2 passed *"]) |
| 1187 |
|
| 1188 |
|
| 1189 |
def test_fixture_order_respects_scope(pytester: Pytester) -> None: |
| 1190 |
"""Ensure that fixtures are created according to scope order (#2405).""" |
| 1191 |
pytester.makepyfile( |
| 1192 |
""" |
| 1193 |
import pytest |
| 1194 |
|
| 1195 |
data = {} |
| 1196 |
|
| 1197 |
@pytest.fixture(scope='module') |
| 1198 |
def clean_data(): |
| 1199 |
data.clear() |
| 1200 |
|
| 1201 |
@pytest.fixture(autouse=True) |
| 1202 |
def add_data(): |
| 1203 |
data.update(value=True) |
| 1204 |
|
| 1205 |
@pytest.mark.usefixtures('clean_data') |
| 1206 |
def test_value(): |
| 1207 |
assert data.get('value') |
| 1208 |
""" |
| 1209 |
) |
| 1210 |
result = pytester.runpytest() |
| 1211 |
assert result.ret == 0 |
| 1212 |
|
| 1213 |
|
| 1214 |
def test_frame_leak_on_failing_test(pytester: Pytester) -> None: |
| 1215 |
"""Pytest would leak garbage referencing the frames of tests that failed |
| 1216 |
that could never be reclaimed (#2798). |
| 1217 |
|
| 1218 |
Unfortunately it was not possible to remove the actual circles because most of them |
| 1219 |
are made of traceback objects which cannot be weakly referenced. Those objects at least |
| 1220 |
can be eventually claimed by the garbage collector. |
| 1221 |
""" |
| 1222 |
pytester.makepyfile( |
| 1223 |
""" |
| 1224 |
import gc |
| 1225 |
import weakref |
| 1226 |
|
| 1227 |
class Obj: |
| 1228 |
pass |
| 1229 |
|
| 1230 |
ref = None |
| 1231 |
|
| 1232 |
def test1(): |
| 1233 |
obj = Obj() |
| 1234 |
global ref |
| 1235 |
ref = weakref.ref(obj) |
| 1236 |
assert 0 |
| 1237 |
|
| 1238 |
def test2(): |
| 1239 |
gc.collect() |
| 1240 |
assert ref() is None |
| 1241 |
""" |
| 1242 |
) |
| 1243 |
result = pytester.runpytest_subprocess() |
| 1244 |
result.stdout.fnmatch_lines(["*1 failed, 1 passed in*"]) |
| 1245 |
|
| 1246 |
|
| 1247 |
def test_fixture_mock_integration(pytester: Pytester) -> None: |
| 1248 |
"""Test that decorators applied to fixture are left working (#3774)""" |
| 1249 |
p = pytester.copy_example("acceptance/fixture_mock_integration.py") |
| 1250 |
result = pytester.runpytest(p) |
| 1251 |
result.stdout.fnmatch_lines(["*1 passed*"]) |
| 1252 |
|
| 1253 |
|
| 1254 |
def test_usage_error_code(pytester: Pytester) -> None: |
| 1255 |
result = pytester.runpytest("-unknown-option-") |
| 1256 |
assert result.ret == ExitCode.USAGE_ERROR |
| 1257 |
|
| 1258 |
|
| 1259 |
def test_error_on_async_function(pytester: Pytester) -> None: |
| 1260 |
# In the below we .close() the coroutine only to avoid |
| 1261 |
# "RuntimeWarning: coroutine 'test_2' was never awaited" |
| 1262 |
# which messes with other tests. |
| 1263 |
pytester.makepyfile( |
| 1264 |
test_async=""" |
| 1265 |
async def test_1(): |
| 1266 |
pass |
| 1267 |
async def test_2(): |
| 1268 |
pass |
| 1269 |
def test_3(): |
| 1270 |
coro = test_2() |
| 1271 |
coro.close() |
| 1272 |
return coro |
| 1273 |
""" |
| 1274 |
) |
| 1275 |
result = pytester.runpytest() |
| 1276 |
result.stdout.fnmatch_lines( |
| 1277 |
[ |
| 1278 |
"*async def functions are not natively supported*", |
| 1279 |
"*test_async.py::test_1*", |
| 1280 |
"*test_async.py::test_2*", |
| 1281 |
"*test_async.py::test_3*", |
| 1282 |
] |
| 1283 |
) |
| 1284 |
result.assert_outcomes(failed=3) |
| 1285 |
|
| 1286 |
|
| 1287 |
def test_error_on_async_gen_function(pytester: Pytester) -> None: |
| 1288 |
pytester.makepyfile( |
| 1289 |
test_async=""" |
| 1290 |
async def test_1(): |
| 1291 |
yield |
| 1292 |
async def test_2(): |
| 1293 |
yield |
| 1294 |
def test_3(): |
| 1295 |
return test_2() |
| 1296 |
""" |
| 1297 |
) |
| 1298 |
result = pytester.runpytest() |
| 1299 |
result.stdout.fnmatch_lines( |
| 1300 |
[ |
| 1301 |
"*async def functions are not natively supported*", |
| 1302 |
"*test_async.py::test_1*", |
| 1303 |
"*test_async.py::test_2*", |
| 1304 |
"*test_async.py::test_3*", |
| 1305 |
] |
| 1306 |
) |
| 1307 |
result.assert_outcomes(failed=3) |
| 1308 |
|
| 1309 |
|
| 1310 |
def test_error_on_sync_test_async_fixture(pytester: Pytester) -> None: |
| 1311 |
pytester.makepyfile( |
| 1312 |
test_sync=""" |
| 1313 |
import pytest |
| 1314 |
|
| 1315 |
@pytest.fixture |
| 1316 |
async def async_fixture(): |
| 1317 |
... |
| 1318 |
|
| 1319 |
def test_foo(async_fixture): |
| 1320 |
# suppress unawaited coroutine warning |
| 1321 |
try: |
| 1322 |
async_fixture.send(None) |
| 1323 |
except StopIteration: |
| 1324 |
pass |
| 1325 |
""" |
| 1326 |
) |
| 1327 |
result = pytester.runpytest() |
| 1328 |
result.assert_outcomes(errors=1) |
| 1329 |
result.stdout.fnmatch_lines( |
| 1330 |
[ |
| 1331 |
"'test_foo' requested an async fixture 'async_fixture', with no plugin or hook that handled it. " |
| 1332 |
"This is an error, as pytest does not natively support it." |
| 1333 |
] |
| 1334 |
) |
| 1335 |
|
| 1336 |
|
| 1337 |
def test_error_on_sync_test_async_fixture_gen(pytester: Pytester) -> None: |
| 1338 |
pytester.makepyfile( |
| 1339 |
test_sync=""" |
| 1340 |
import pytest |
| 1341 |
|
| 1342 |
@pytest.fixture |
| 1343 |
async def async_fixture(): |
| 1344 |
yield |
| 1345 |
|
| 1346 |
def test_foo(async_fixture): |
| 1347 |
# async gens don't emit unawaited-coroutine |
| 1348 |
... |
| 1349 |
""" |
| 1350 |
) |
| 1351 |
result = pytester.runpytest() |
| 1352 |
result.assert_outcomes(errors=1) |
| 1353 |
result.stdout.fnmatch_lines( |
| 1354 |
[ |
| 1355 |
"'test_foo' requested an async fixture 'async_fixture', with no plugin or hook that handled it. " |
| 1356 |
"This is an error, as pytest does not natively support it." |
| 1357 |
] |
| 1358 |
) |
| 1359 |
|
| 1360 |
|
| 1361 |
def test_error_on_sync_test_async_autouse_fixture(pytester: Pytester) -> None: |
| 1362 |
pytester.makepyfile( |
| 1363 |
test_sync=""" |
| 1364 |
import pytest |
| 1365 |
|
| 1366 |
@pytest.fixture(autouse=True) |
| 1367 |
async def async_fixture(): |
| 1368 |
... |
| 1369 |
|
| 1370 |
# We explicitly request the fixture to be able to |
| 1371 |
# suppress the RuntimeWarning for unawaited coroutine. |
| 1372 |
def test_foo(async_fixture): |
| 1373 |
try: |
| 1374 |
async_fixture.send(None) |
| 1375 |
except StopIteration: |
| 1376 |
pass |
| 1377 |
""" |
| 1378 |
) |
| 1379 |
result = pytester.runpytest() |
| 1380 |
result.assert_outcomes(errors=1) |
| 1381 |
result.stdout.fnmatch_lines( |
| 1382 |
[ |
| 1383 |
"'test_foo' requested an async fixture 'async_fixture' with autouse=True, " |
| 1384 |
"with no plugin or hook that handled it. " |
| 1385 |
"This is an error, as pytest does not natively support it." |
| 1386 |
] |
| 1387 |
) |
| 1388 |
|
| 1389 |
|
| 1390 |
def test_pdb_can_be_rewritten(pytester: Pytester) -> None: |
| 1391 |
pytester.makepyfile( |
| 1392 |
**{ |
| 1393 |
"conftest.py": """ |
| 1394 |
import pytest |
| 1395 |
pytest.register_assert_rewrite("pdb") |
| 1396 |
""", |
| 1397 |
"__init__.py": "", |
| 1398 |
"pdb.py": """ |
| 1399 |
def check(): |
| 1400 |
assert 1 == 2 |
| 1401 |
""", |
| 1402 |
"test_pdb.py": """ |
| 1403 |
def test(): |
| 1404 |
import pdb |
| 1405 |
assert pdb.check() |
| 1406 |
""", |
| 1407 |
} |
| 1408 |
) |
| 1409 |
# Disable debugging plugin itself to avoid: |
| 1410 |
# > INTERNALERROR> AttributeError: module 'pdb' has no attribute 'set_trace' |
| 1411 |
result = pytester.runpytest_subprocess("-p", "no:debugging", "-vv") |
| 1412 |
result.stdout.fnmatch_lines( |
| 1413 |
[ |
| 1414 |
" def check():", |
| 1415 |
"> assert 1 == 2", |
| 1416 |
"E assert 1 == 2", |
| 1417 |
"", |
| 1418 |
"pdb.py:2: AssertionError", |
| 1419 |
"*= 1 failed in *", |
| 1420 |
] |
| 1421 |
) |
| 1422 |
assert result.ret == 1 |
| 1423 |
|
| 1424 |
|
| 1425 |
def test_tee_stdio_captures_and_live_prints(pytester: Pytester) -> None: |
| 1426 |
testpath = pytester.makepyfile( |
| 1427 |
""" |
| 1428 |
import sys |
| 1429 |
def test_simple(): |
| 1430 |
print ("@this is stdout@") |
| 1431 |
print ("@this is stderr@", file=sys.stderr) |
| 1432 |
""" |
| 1433 |
) |
| 1434 |
result = pytester.runpytest_subprocess( |
| 1435 |
testpath, |
| 1436 |
"--capture=tee-sys", |
| 1437 |
"--junitxml=output.xml", |
| 1438 |
"-o", |
| 1439 |
"junit_logging=all", |
| 1440 |
) |
| 1441 |
|
| 1442 |
# ensure stdout/stderr were 'live printed' |
| 1443 |
result.stdout.fnmatch_lines(["*@this is stdout@*"]) |
| 1444 |
result.stderr.fnmatch_lines(["*@this is stderr@*"]) |
| 1445 |
|
| 1446 |
# now ensure the output is in the junitxml |
| 1447 |
fullXml = pytester.path.joinpath("output.xml").read_text(encoding="utf-8") |
| 1448 |
assert "@this is stdout@\n" in fullXml |
| 1449 |
assert "@this is stderr@\n" in fullXml |
| 1450 |
|
| 1451 |
|
| 1452 |
@pytest.mark.skipif( |
| 1453 |
sys.platform == "win32", |
| 1454 |
reason="Windows raises `OSError: [Errno 22] Invalid argument` instead", |
| 1455 |
) |
| 1456 |
def test_no_brokenpipeerror_message(pytester: Pytester) -> None: |
| 1457 |
"""Ensure that the broken pipe error message is suppressed. |
| 1458 |
|
| 1459 |
In some Python versions, it reaches sys.unraisablehook, in others |
| 1460 |
a BrokenPipeError exception is propagated, but either way it prints |
| 1461 |
to stderr on shutdown, so checking nothing is printed is enough. |
| 1462 |
""" |
| 1463 |
popen = pytester.popen((*pytester._getpytestargs(), "--help")) |
| 1464 |
popen.stdout.close() |
| 1465 |
ret = popen.wait() |
| 1466 |
assert popen.stderr.read() == b"" |
| 1467 |
assert ret == 1 |
| 1468 |
|
| 1469 |
# Cleanup. |
| 1470 |
popen.stderr.close() |
| 1471 |
|
| 1472 |
|
| 1473 |
@pytest.mark.filterwarnings("default") |
| 1474 |
def test_function_return_non_none_warning(pytester: Pytester) -> None: |
| 1475 |
pytester.makepyfile( |
| 1476 |
""" |
| 1477 |
def test_stuff(): |
| 1478 |
return "something" |
| 1479 |
""" |
| 1480 |
) |
| 1481 |
res = pytester.runpytest() |
| 1482 |
res.stdout.fnmatch_lines(["*Did you mean to use `assert` instead of `return`?*"]) |
| 1483 |
|
| 1484 |
|
| 1485 |
def test_doctest_and_normal_imports_with_importlib(pytester: Pytester) -> None: |
| 1486 |
""" |
| 1487 |
Regression test for #10811: previously import_path with ImportMode.importlib would |
| 1488 |
not return a module if already in sys.modules, resulting in modules being imported |
| 1489 |
multiple times, which causes problems with modules that have import side effects. |
| 1490 |
""" |
| 1491 |
# Uses the exact reproducer form #10811, given it is very minimal |
| 1492 |
# and illustrates the problem well. |
| 1493 |
pytester.makepyfile( |
| 1494 |
**{ |
| 1495 |
"pmxbot/commands.py": "from . import logging", |
| 1496 |
"pmxbot/logging.py": "", |
| 1497 |
"tests/__init__.py": "", |
| 1498 |
"tests/test_commands.py": """ |
| 1499 |
import importlib |
| 1500 |
from pmxbot import logging |
| 1501 |
|
| 1502 |
class TestCommands: |
| 1503 |
def test_boo(self): |
| 1504 |
assert importlib.import_module('pmxbot.logging') is logging |
| 1505 |
""", |
| 1506 |
} |
| 1507 |
) |
| 1508 |
pytester.makeini( |
| 1509 |
""" |
| 1510 |
[pytest] |
| 1511 |
addopts= |
| 1512 |
--doctest-modules |
| 1513 |
--import-mode importlib |
| 1514 |
""" |
| 1515 |
) |
| 1516 |
result = pytester.runpytest_subprocess() |
| 1517 |
result.stdout.fnmatch_lines("*1 passed*") |
| 1518 |
|
| 1519 |
|
| 1520 |
@pytest.mark.skip(reason="Test is not isolated") |
| 1521 |
def test_issue_9765(pytester: Pytester) -> None: |
| 1522 |
"""Reproducer for issue #9765 on Windows |
| 1523 |
|
| 1524 |
https://github.com/pytest-dev/pytest/issues/9765 |
| 1525 |
""" |
| 1526 |
pytester.makepyprojecttoml( |
| 1527 |
""" |
| 1528 |
[tool.pytest.ini_options] |
| 1529 |
addopts = "-p my_package.plugin.my_plugin" |
| 1530 |
""" |
| 1531 |
) |
| 1532 |
pytester.makepyfile( |
| 1533 |
**{ |
| 1534 |
"setup.py": ( |
| 1535 |
""" |
| 1536 |
from setuptools import setup |
| 1537 |
|
| 1538 |
if __name__ == '__main__': |
| 1539 |
setup(name='my_package', packages=['my_package', 'my_package.plugin']) |
| 1540 |
""" |
| 1541 |
), |
| 1542 |
"my_package/__init__.py": "", |
| 1543 |
"my_package/conftest.py": "", |
| 1544 |
"my_package/test_foo.py": "def test(): pass", |
| 1545 |
"my_package/plugin/__init__.py": "", |
| 1546 |
"my_package/plugin/my_plugin.py": ( |
| 1547 |
""" |
| 1548 |
import pytest |
| 1549 |
|
| 1550 |
def pytest_configure(config): |
| 1551 |
|
| 1552 |
class SimplePlugin: |
| 1553 |
@pytest.fixture(params=[1, 2, 3]) |
| 1554 |
def my_fixture(self, request): |
| 1555 |
yield request.param |
| 1556 |
|
| 1557 |
config.pluginmanager.register(SimplePlugin()) |
| 1558 |
""" |
| 1559 |
), |
| 1560 |
} |
| 1561 |
) |
| 1562 |
|
| 1563 |
subprocess.run( |
| 1564 |
[sys.executable, "-Im", "pip", "install", "-e", "."], |
| 1565 |
check=True, |
| 1566 |
) |
| 1567 |
try: |
| 1568 |
# We are using subprocess.run rather than pytester.run on purpose. |
| 1569 |
# pytester.run is adding the current directory to PYTHONPATH which avoids |
| 1570 |
# the bug. We also use pytest rather than python -m pytest for the same |
| 1571 |
# PYTHONPATH reason. |
| 1572 |
subprocess.run( |
| 1573 |
["pytest", "my_package"], |
| 1574 |
capture_output=True, |
| 1575 |
check=True, |
| 1576 |
encoding="utf-8", |
| 1577 |
text=True, |
| 1578 |
) |
| 1579 |
except subprocess.CalledProcessError as exc: |
| 1580 |
raise AssertionError( |
| 1581 |
f"pytest command failed:\n{exc.stdout=!s}\n{exc.stderr=!s}" |
| 1582 |
) from exc |
| 1583 |
|
| 1584 |
|
| 1585 |
def test_no_terminal_plugin(pytester: Pytester) -> None: |
| 1586 |
"""Smoke test to ensure pytest can execute without the terminal plugin (#9422).""" |
| 1587 |
pytester.makepyfile("def test(): assert 1 == 2") |
| 1588 |
result = pytester.runpytest("-pno:terminal", "-s") |
| 1589 |
assert result.ret == ExitCode.TESTS_FAILED |
| 1590 |
|
| 1591 |
|
| 1592 |
def test_stop_iteration_from_collect(pytester: Pytester) -> None: |
| 1593 |
pytester.makepyfile(test_it="raise StopIteration('hello')") |
| 1594 |
result = pytester.runpytest() |
| 1595 |
assert result.ret == ExitCode.INTERRUPTED |
| 1596 |
result.assert_outcomes(failed=0, passed=0, errors=1) |
| 1597 |
result.stdout.fnmatch_lines( |
| 1598 |
[ |
| 1599 |
"=* short test summary info =*", |
| 1600 |
"ERROR test_it.py - StopIteration: hello", |
| 1601 |
"!* Interrupted: 1 error during collection !*", |
| 1602 |
"=* 1 error in * =*", |
| 1603 |
] |
| 1604 |
) |
| 1605 |
|
| 1606 |
|
| 1607 |
def test_stop_iteration_runtest_protocol(pytester: Pytester) -> None: |
| 1608 |
pytester.makepyfile( |
| 1609 |
test_it=""" |
| 1610 |
import pytest |
| 1611 |
@pytest.fixture |
| 1612 |
def fail_setup(): |
| 1613 |
raise StopIteration(1) |
| 1614 |
def test_fail_setup(fail_setup): |
| 1615 |
pass |
| 1616 |
def test_fail_teardown(request): |
| 1617 |
def stop_iteration(): |
| 1618 |
raise StopIteration(2) |
| 1619 |
request.addfinalizer(stop_iteration) |
| 1620 |
def test_fail_call(): |
| 1621 |
raise StopIteration(3) |
| 1622 |
""" |
| 1623 |
) |
| 1624 |
result = pytester.runpytest() |
| 1625 |
assert result.ret == ExitCode.TESTS_FAILED |
| 1626 |
result.assert_outcomes(failed=1, passed=1, errors=2) |
| 1627 |
result.stdout.fnmatch_lines( |
| 1628 |
[ |
| 1629 |
"=* short test summary info =*", |
| 1630 |
"FAILED test_it.py::test_fail_call - StopIteration: 3", |
| 1631 |
"ERROR test_it.py::test_fail_setup - StopIteration: 1", |
| 1632 |
"ERROR test_it.py::test_fail_teardown - StopIteration: 2", |
| 1633 |
"=* 1 failed, 1 passed, 2 errors in * =*", |
| 1634 |
] |
| 1635 |
) |