| 1 |
# mypy: allow-untyped-defs |
| 2 |
"""Terminal reporting of the full testing process.""" |
| 3 |
|
| 4 |
from __future__ import annotations |
| 5 |
|
| 6 |
from io import StringIO |
| 7 |
import os |
| 8 |
from pathlib import Path |
| 9 |
import sys |
| 10 |
import textwrap |
| 11 |
from types import SimpleNamespace |
| 12 |
from typing import cast |
| 13 |
from typing import Literal |
| 14 |
from typing import NamedTuple |
| 15 |
from unittest import mock |
| 16 |
|
| 17 |
import pluggy |
| 18 |
|
| 19 |
from _pytest._io.wcwidth import wcswidth |
| 20 |
import _pytest.config |
| 21 |
from _pytest.config import Config |
| 22 |
from _pytest.config import ExitCode |
| 23 |
from _pytest.monkeypatch import MonkeyPatch |
| 24 |
from _pytest.pytester import Pytester |
| 25 |
from _pytest.reports import BaseReport |
| 26 |
from _pytest.reports import CollectReport |
| 27 |
from _pytest.reports import TestReport |
| 28 |
import _pytest.terminal |
| 29 |
from _pytest.terminal import _folded_skips |
| 30 |
from _pytest.terminal import _format_trimmed |
| 31 |
from _pytest.terminal import _get_line_with_reprcrash_message |
| 32 |
from _pytest.terminal import _get_raw_skip_reason |
| 33 |
from _pytest.terminal import _plugin_nameversions |
| 34 |
from _pytest.terminal import getreportopt |
| 35 |
from _pytest.terminal import TerminalProgressPlugin |
| 36 |
from _pytest.terminal import TerminalReporter |
| 37 |
import pytest |
| 38 |
|
| 39 |
|
| 40 |
class DistInfo(NamedTuple): |
| 41 |
project_name: str |
| 42 |
version: int |
| 43 |
|
| 44 |
|
| 45 |
TRANS_FNMATCH = str.maketrans({"[": "[[]", "]": "[]]"}) |
| 46 |
|
| 47 |
|
| 48 |
class Option: |
| 49 |
def __init__(self, verbosity=0): |
| 50 |
self.verbosity = verbosity |
| 51 |
|
| 52 |
@property |
| 53 |
def args(self): |
| 54 |
values = [] |
| 55 |
values.append(f"--verbosity={self.verbosity}") |
| 56 |
return values |
| 57 |
|
| 58 |
|
| 59 |
@pytest.fixture( |
| 60 |
params=[Option(verbosity=0), Option(verbosity=1), Option(verbosity=-1)], |
| 61 |
ids=["default", "verbose", "quiet"], |
| 62 |
) |
| 63 |
def option(request): |
| 64 |
return request.param |
| 65 |
|
| 66 |
|
| 67 |
@pytest.mark.parametrize( |
| 68 |
"input,expected", |
| 69 |
[ |
| 70 |
([DistInfo(project_name="test", version=1)], ["test-1"]), |
| 71 |
([DistInfo(project_name="pytest-test", version=1)], ["test-1"]), |
| 72 |
( |
| 73 |
[ |
| 74 |
DistInfo(project_name="test", version=1), |
| 75 |
DistInfo(project_name="test", version=1), |
| 76 |
], |
| 77 |
["test-1"], |
| 78 |
), |
| 79 |
], |
| 80 |
ids=["normal", "prefix-strip", "deduplicate"], |
| 81 |
) |
| 82 |
def test_plugin_nameversion(input, expected): |
| 83 |
pluginlist = [(None, x) for x in input] |
| 84 |
result = _plugin_nameversions(pluginlist) |
| 85 |
assert result == expected |
| 86 |
|
| 87 |
|
| 88 |
class TestTerminal: |
| 89 |
def test_pass_skip_fail(self, pytester: Pytester, option) -> None: |
| 90 |
pytester.makepyfile( |
| 91 |
""" |
| 92 |
import pytest |
| 93 |
def test_ok(): |
| 94 |
pass |
| 95 |
def test_skip(): |
| 96 |
pytest.skip("xx") |
| 97 |
def test_func(): |
| 98 |
assert 0 |
| 99 |
""" |
| 100 |
) |
| 101 |
result = pytester.runpytest(*option.args) |
| 102 |
if option.verbosity > 0: |
| 103 |
result.stdout.fnmatch_lines( |
| 104 |
[ |
| 105 |
"*test_pass_skip_fail.py::test_ok PASS*", |
| 106 |
"*test_pass_skip_fail.py::test_skip SKIP*", |
| 107 |
"*test_pass_skip_fail.py::test_func FAIL*", |
| 108 |
] |
| 109 |
) |
| 110 |
elif option.verbosity == 0: |
| 111 |
result.stdout.fnmatch_lines(["*test_pass_skip_fail.py .sF*"]) |
| 112 |
else: |
| 113 |
result.stdout.fnmatch_lines([".sF*"]) |
| 114 |
result.stdout.fnmatch_lines( |
| 115 |
[" def test_func():", "> assert 0", "E assert 0"] |
| 116 |
) |
| 117 |
|
| 118 |
def test_console_output_style_times_with_skipped_and_passed( |
| 119 |
self, pytester: Pytester |
| 120 |
) -> None: |
| 121 |
pytester.makepyfile( |
| 122 |
test_repro=""" |
| 123 |
def test_hello(): |
| 124 |
pass |
| 125 |
""", |
| 126 |
test_repro_skip=""" |
| 127 |
import pytest |
| 128 |
pytest.importorskip("fakepackage_does_not_exist") |
| 129 |
""", |
| 130 |
) |
| 131 |
result = pytester.runpytest( |
| 132 |
"test_repro.py", |
| 133 |
"test_repro_skip.py", |
| 134 |
"-o", |
| 135 |
"console_output_style=times", |
| 136 |
) |
| 137 |
|
| 138 |
result.stdout.fnmatch_lines("* 1 passed, 1 skipped in *") |
| 139 |
|
| 140 |
combined = "\n".join(result.stdout.lines + result.stderr.lines) |
| 141 |
assert "INTERNALERROR" not in combined |
| 142 |
|
| 143 |
def test_internalerror(self, pytester: Pytester, linecomp) -> None: |
| 144 |
modcol = pytester.getmodulecol("def test_one(): pass") |
| 145 |
rep = TerminalReporter(modcol.config, file=linecomp.stringio) |
| 146 |
with pytest.raises(ValueError) as excinfo: |
| 147 |
raise ValueError("hello") |
| 148 |
rep.pytest_internalerror(excinfo.getrepr()) |
| 149 |
linecomp.assert_contains_lines(["INTERNALERROR> *ValueError*hello*"]) |
| 150 |
|
| 151 |
def test_writeline(self, pytester: Pytester, linecomp) -> None: |
| 152 |
modcol = pytester.getmodulecol("def test_one(): pass") |
| 153 |
rep = TerminalReporter(modcol.config, file=linecomp.stringio) |
| 154 |
rep.write_fspath_result(modcol.nodeid, ".") |
| 155 |
rep.write_line("hello world") |
| 156 |
lines = linecomp.stringio.getvalue().split("\n") |
| 157 |
assert not lines[0] |
| 158 |
assert lines[1].endswith(modcol.name + " .") |
| 159 |
assert lines[2] == "hello world" |
| 160 |
|
| 161 |
def test_show_runtest_logstart(self, pytester: Pytester, linecomp) -> None: |
| 162 |
item = pytester.getitem("def test_func(): pass") |
| 163 |
tr = TerminalReporter(item.config, file=linecomp.stringio) |
| 164 |
item.config.pluginmanager.register(tr) |
| 165 |
location = item.reportinfo() |
| 166 |
tr.config.hook.pytest_runtest_logstart( |
| 167 |
nodeid=item.nodeid, location=location, fspath=str(item.path) |
| 168 |
) |
| 169 |
linecomp.assert_contains_lines(["*test_show_runtest_logstart.py*"]) |
| 170 |
|
| 171 |
def test_runtest_location_shown_before_test_starts( |
| 172 |
self, pytester: Pytester |
| 173 |
) -> None: |
| 174 |
pytester.makepyfile( |
| 175 |
""" |
| 176 |
def test_1(): |
| 177 |
import time |
| 178 |
time.sleep(20) |
| 179 |
""" |
| 180 |
) |
| 181 |
child = pytester.spawn_pytest("") |
| 182 |
child.expect(".*test_runtest_location.*py") |
| 183 |
child.sendeof() |
| 184 |
child.kill(15) |
| 185 |
|
| 186 |
def test_report_collect_after_half_a_second( |
| 187 |
self, pytester: Pytester, monkeypatch: MonkeyPatch |
| 188 |
) -> None: |
| 189 |
"""Test for "collecting" being updated after 0.5s""" |
| 190 |
pytester.makepyfile( |
| 191 |
**{ |
| 192 |
"test1.py": """ |
| 193 |
import _pytest.terminal |
| 194 |
|
| 195 |
_pytest.terminal.REPORT_COLLECTING_RESOLUTION = 0 |
| 196 |
|
| 197 |
def test_1(): |
| 198 |
pass |
| 199 |
""", |
| 200 |
"test2.py": "def test_2(): pass", |
| 201 |
} |
| 202 |
) |
| 203 |
# Explicitly test colored output. |
| 204 |
monkeypatch.setenv("PY_COLORS", "1") |
| 205 |
|
| 206 |
child = pytester.spawn_pytest("-v test1.py test2.py") |
| 207 |
child.expect(r"collecting \.\.\.") |
| 208 |
child.expect(r"collecting 1 item") |
| 209 |
child.expect(r"collecting 2 items") |
| 210 |
child.expect(r"collected 2 items") |
| 211 |
rest = child.read().decode("utf8") |
| 212 |
assert "= \x1b[32m\x1b[1m2 passed\x1b[0m\x1b[32m in" in rest |
| 213 |
|
| 214 |
def test_itemreport_subclasses_show_subclassed_file( |
| 215 |
self, pytester: Pytester |
| 216 |
) -> None: |
| 217 |
pytester.makepyfile( |
| 218 |
**{ |
| 219 |
"tests/test_p1": """ |
| 220 |
class BaseTests(object): |
| 221 |
fail = False |
| 222 |
|
| 223 |
def test_p1(self): |
| 224 |
if self.fail: assert 0 |
| 225 |
""", |
| 226 |
"tests/test_p2": """ |
| 227 |
from test_p1 import BaseTests |
| 228 |
|
| 229 |
class TestMore(BaseTests): pass |
| 230 |
""", |
| 231 |
"tests/test_p3.py": """ |
| 232 |
from test_p1 import BaseTests |
| 233 |
|
| 234 |
BaseTests.fail = True |
| 235 |
|
| 236 |
class TestMore(BaseTests): pass |
| 237 |
""", |
| 238 |
} |
| 239 |
) |
| 240 |
result = pytester.runpytest("tests/test_p2.py", "--rootdir=tests") |
| 241 |
result.stdout.fnmatch_lines(["tests/test_p2.py .*", "=* 1 passed in *"]) |
| 242 |
|
| 243 |
result = pytester.runpytest("-vv", "-rA", "tests/test_p2.py", "--rootdir=tests") |
| 244 |
result.stdout.fnmatch_lines( |
| 245 |
[ |
| 246 |
"tests/test_p2.py::TestMore::test_p1 <- test_p1.py PASSED *", |
| 247 |
"*= short test summary info =*", |
| 248 |
"PASSED tests/test_p2.py::TestMore::test_p1", |
| 249 |
] |
| 250 |
) |
| 251 |
result = pytester.runpytest("-vv", "-rA", "tests/test_p3.py", "--rootdir=tests") |
| 252 |
result.stdout.fnmatch_lines( |
| 253 |
[ |
| 254 |
"tests/test_p3.py::TestMore::test_p1 <- test_p1.py FAILED *", |
| 255 |
"*_ TestMore.test_p1 _*", |
| 256 |
" def test_p1(self):", |
| 257 |
"> if self.fail: assert 0", |
| 258 |
"E assert 0", |
| 259 |
"", |
| 260 |
"tests/test_p1.py:5: AssertionError", |
| 261 |
"*= short test summary info =*", |
| 262 |
"FAILED tests/test_p3.py::TestMore::test_p1 - assert 0", |
| 263 |
"*= 1 failed in *", |
| 264 |
] |
| 265 |
) |
| 266 |
|
| 267 |
def test_itemreport_directclasses_not_shown_as_subclasses( |
| 268 |
self, pytester: Pytester |
| 269 |
) -> None: |
| 270 |
a = pytester.mkpydir("a123") |
| 271 |
a.joinpath("test_hello123.py").write_text( |
| 272 |
textwrap.dedent( |
| 273 |
"""\ |
| 274 |
class TestClass(object): |
| 275 |
def test_method(self): |
| 276 |
pass |
| 277 |
""" |
| 278 |
), |
| 279 |
encoding="utf-8", |
| 280 |
) |
| 281 |
result = pytester.runpytest("-vv") |
| 282 |
assert result.ret == 0 |
| 283 |
result.stdout.fnmatch_lines(["*a123/test_hello123.py*PASS*"]) |
| 284 |
result.stdout.no_fnmatch_line("* <- *") |
| 285 |
|
| 286 |
@pytest.mark.parametrize("fulltrace", ("", "--fulltrace")) |
| 287 |
def test_keyboard_interrupt(self, pytester: Pytester, fulltrace) -> None: |
| 288 |
pytester.makepyfile( |
| 289 |
""" |
| 290 |
def test_foobar(): |
| 291 |
assert 0 |
| 292 |
def test_spamegg(): |
| 293 |
import py; pytest.skip('skip me please!') |
| 294 |
def test_interrupt_me(): |
| 295 |
raise KeyboardInterrupt # simulating the user |
| 296 |
""" |
| 297 |
) |
| 298 |
|
| 299 |
result = pytester.runpytest(fulltrace, no_reraise_ctrlc=True) |
| 300 |
result.stdout.fnmatch_lines( |
| 301 |
[ |
| 302 |
" def test_foobar():", |
| 303 |
"> assert 0", |
| 304 |
"E assert 0", |
| 305 |
"*_keyboard_interrupt.py:6: KeyboardInterrupt*", |
| 306 |
] |
| 307 |
) |
| 308 |
if fulltrace: |
| 309 |
result.stdout.fnmatch_lines( |
| 310 |
["*raise KeyboardInterrupt # simulating the user*"] |
| 311 |
) |
| 312 |
else: |
| 313 |
result.stdout.fnmatch_lines( |
| 314 |
["(to show a full traceback on KeyboardInterrupt use --full-trace)"] |
| 315 |
) |
| 316 |
result.stdout.fnmatch_lines(["*KeyboardInterrupt*"]) |
| 317 |
|
| 318 |
def test_keyboard_in_sessionstart(self, pytester: Pytester) -> None: |
| 319 |
pytester.makeconftest( |
| 320 |
""" |
| 321 |
def pytest_sessionstart(): |
| 322 |
raise KeyboardInterrupt |
| 323 |
""" |
| 324 |
) |
| 325 |
pytester.makepyfile( |
| 326 |
""" |
| 327 |
def test_foobar(): |
| 328 |
pass |
| 329 |
""" |
| 330 |
) |
| 331 |
|
| 332 |
result = pytester.runpytest(no_reraise_ctrlc=True) |
| 333 |
assert result.ret == 2 |
| 334 |
result.stdout.fnmatch_lines(["*KeyboardInterrupt*"]) |
| 335 |
|
| 336 |
def test_collect_single_item(self, pytester: Pytester) -> None: |
| 337 |
"""Use singular 'item' when reporting a single test item""" |
| 338 |
pytester.makepyfile( |
| 339 |
""" |
| 340 |
def test_foobar(): |
| 341 |
pass |
| 342 |
""" |
| 343 |
) |
| 344 |
result = pytester.runpytest() |
| 345 |
result.stdout.fnmatch_lines(["collected 1 item"]) |
| 346 |
|
| 347 |
def test_rewrite(self, pytester: Pytester, monkeypatch) -> None: |
| 348 |
config = pytester.parseconfig() |
| 349 |
f = StringIO() |
| 350 |
monkeypatch.setattr(f, "isatty", lambda *args: True) |
| 351 |
tr = TerminalReporter(config, f) |
| 352 |
tr._tw.fullwidth = 10 |
| 353 |
tr.write("hello") |
| 354 |
tr.rewrite("hey", erase=True) |
| 355 |
assert f.getvalue() == "hello" + "\r" + "hey" + (6 * " ") |
| 356 |
|
| 357 |
@pytest.mark.parametrize("category", ["foo", "failed", "error", "passed"]) |
| 358 |
def test_report_teststatus_explicit_markup( |
| 359 |
self, monkeypatch: MonkeyPatch, pytester: Pytester, color_mapping, category: str |
| 360 |
) -> None: |
| 361 |
"""Test that TerminalReporter handles markup explicitly provided by |
| 362 |
a pytest_report_teststatus hook.""" |
| 363 |
monkeypatch.setenv("PY_COLORS", "1") |
| 364 |
pytester.makeconftest( |
| 365 |
f""" |
| 366 |
def pytest_report_teststatus(report): |
| 367 |
return {category!r}, 'F', ('FOO', {{'red': True}}) |
| 368 |
""" |
| 369 |
) |
| 370 |
pytester.makepyfile( |
| 371 |
""" |
| 372 |
def test_foobar(): |
| 373 |
pass |
| 374 |
""" |
| 375 |
) |
| 376 |
|
| 377 |
result = pytester.runpytest("-v") |
| 378 |
assert not result.stderr.lines |
| 379 |
result.stdout.fnmatch_lines( |
| 380 |
color_mapping.format_for_fnmatch(["*{red}FOO{reset}*"]) |
| 381 |
) |
| 382 |
|
| 383 |
def test_verbose_skip_reason(self, pytester: Pytester) -> None: |
| 384 |
pytester.makepyfile( |
| 385 |
""" |
| 386 |
import pytest |
| 387 |
|
| 388 |
@pytest.mark.skip(reason="123") |
| 389 |
def test_1(): |
| 390 |
pass |
| 391 |
|
| 392 |
@pytest.mark.xfail(reason="456") |
| 393 |
def test_2(): |
| 394 |
pass |
| 395 |
|
| 396 |
@pytest.mark.xfail(reason="789") |
| 397 |
def test_3(): |
| 398 |
assert False |
| 399 |
|
| 400 |
@pytest.mark.xfail(reason="") |
| 401 |
def test_4(): |
| 402 |
assert False |
| 403 |
|
| 404 |
@pytest.mark.skip |
| 405 |
def test_5(): |
| 406 |
pass |
| 407 |
|
| 408 |
@pytest.mark.xfail |
| 409 |
def test_6(): |
| 410 |
pass |
| 411 |
|
| 412 |
def test_7(): |
| 413 |
pytest.skip() |
| 414 |
|
| 415 |
def test_8(): |
| 416 |
pytest.skip("888 is great") |
| 417 |
|
| 418 |
def test_9(): |
| 419 |
pytest.xfail() |
| 420 |
|
| 421 |
def test_10(): |
| 422 |
pytest.xfail("It's 🕙 o'clock") |
| 423 |
|
| 424 |
@pytest.mark.skip( |
| 425 |
reason="1 cannot do foobar because baz is missing due to I don't know what" |
| 426 |
) |
| 427 |
def test_long_skip(): |
| 428 |
pass |
| 429 |
|
| 430 |
@pytest.mark.xfail( |
| 431 |
reason="2 cannot do foobar because baz is missing due to I don't know what" |
| 432 |
) |
| 433 |
def test_long_xfail(): |
| 434 |
print(1 / 0) |
| 435 |
""" |
| 436 |
) |
| 437 |
|
| 438 |
common_output = [ |
| 439 |
"test_verbose_skip_reason.py::test_1 SKIPPED (123) *", |
| 440 |
"test_verbose_skip_reason.py::test_2 XPASS (456) *", |
| 441 |
"test_verbose_skip_reason.py::test_3 XFAIL (789) *", |
| 442 |
"test_verbose_skip_reason.py::test_4 XFAIL *", |
| 443 |
"test_verbose_skip_reason.py::test_5 SKIPPED (unconditional skip) *", |
| 444 |
"test_verbose_skip_reason.py::test_6 XPASS *", |
| 445 |
"test_verbose_skip_reason.py::test_7 SKIPPED *", |
| 446 |
"test_verbose_skip_reason.py::test_8 SKIPPED (888 is great) *", |
| 447 |
"test_verbose_skip_reason.py::test_9 XFAIL *", |
| 448 |
"test_verbose_skip_reason.py::test_10 XFAIL (It's 🕙 o'clock) *", |
| 449 |
] |
| 450 |
|
| 451 |
result = pytester.runpytest("-v") |
| 452 |
result.stdout.fnmatch_lines( |
| 453 |
[ |
| 454 |
*common_output, |
| 455 |
"test_verbose_skip_reason.py::test_long_skip SKIPPED (1 cannot *...) *", |
| 456 |
"test_verbose_skip_reason.py::test_long_xfail XFAIL (2 cannot *...) *", |
| 457 |
] |
| 458 |
) |
| 459 |
|
| 460 |
result = pytester.runpytest("-vv") |
| 461 |
result.stdout.fnmatch_lines( |
| 462 |
[ |
| 463 |
*common_output, |
| 464 |
"test_verbose_skip_reason.py::test_long_skip SKIPPED" |
| 465 |
" (1 cannot do foobar", |
| 466 |
"because baz is missing due to I don't know what) *", |
| 467 |
"test_verbose_skip_reason.py::test_long_xfail XFAIL" |
| 468 |
" (2 cannot do foobar", |
| 469 |
"because baz is missing due to I don't know what) *", |
| 470 |
] |
| 471 |
) |
| 472 |
|
| 473 |
@pytest.mark.parametrize("isatty", [True, False]) |
| 474 |
def test_isatty(self, pytester: Pytester, monkeypatch, isatty: bool) -> None: |
| 475 |
config = pytester.parseconfig() |
| 476 |
f = StringIO() |
| 477 |
monkeypatch.setattr(f, "isatty", lambda: isatty) |
| 478 |
tr = TerminalReporter(config, f) |
| 479 |
assert tr.isatty() == isatty |
| 480 |
# It was incorrectly implemented as a boolean so we still support using it as one. |
| 481 |
assert bool(tr.isatty) == isatty |
| 482 |
|
| 483 |
|
| 484 |
class TestCollectonly: |
| 485 |
def test_collectonly_basic(self, pytester: Pytester) -> None: |
| 486 |
pytester.makepyfile( |
| 487 |
""" |
| 488 |
def test_func(): |
| 489 |
pass |
| 490 |
""" |
| 491 |
) |
| 492 |
result = pytester.runpytest("--collect-only") |
| 493 |
result.stdout.fnmatch_lines( |
| 494 |
[ |
| 495 |
"<Dir test_collectonly_basic0>", |
| 496 |
" <Module test_collectonly_basic.py>", |
| 497 |
" <Function test_func>", |
| 498 |
] |
| 499 |
) |
| 500 |
|
| 501 |
def test_collectonly_skipped_module(self, pytester: Pytester) -> None: |
| 502 |
pytester.makepyfile( |
| 503 |
""" |
| 504 |
import pytest |
| 505 |
pytest.skip("hello") |
| 506 |
""" |
| 507 |
) |
| 508 |
result = pytester.runpytest("--collect-only", "-rs") |
| 509 |
result.stdout.fnmatch_lines(["*ERROR collecting*"]) |
| 510 |
|
| 511 |
def test_collectonly_displays_test_description( |
| 512 |
self, pytester: Pytester, dummy_yaml_custom_test |
| 513 |
) -> None: |
| 514 |
"""Used dummy_yaml_custom_test for an Item without ``obj``.""" |
| 515 |
pytester.makepyfile( |
| 516 |
""" |
| 517 |
def test_with_description(): |
| 518 |
''' This test has a description. |
| 519 |
|
| 520 |
more1. |
| 521 |
more2.''' |
| 522 |
""" |
| 523 |
) |
| 524 |
result = pytester.runpytest("--collect-only", "--verbose") |
| 525 |
result.stdout.fnmatch_lines( |
| 526 |
[ |
| 527 |
"<Dir test_collectonly_displays_test_description0>", |
| 528 |
" <YamlFile test1.yaml>", |
| 529 |
" <YamlItem test1.yaml>", |
| 530 |
" <Module test_collectonly_displays_test_description.py>", |
| 531 |
" <Function test_with_description>", |
| 532 |
" This test has a description.", |
| 533 |
" ", |
| 534 |
" more1.", |
| 535 |
" more2.", |
| 536 |
], |
| 537 |
consecutive=True, |
| 538 |
) |
| 539 |
|
| 540 |
def test_collectonly_failed_module(self, pytester: Pytester) -> None: |
| 541 |
pytester.makepyfile("""raise ValueError(0)""") |
| 542 |
result = pytester.runpytest("--collect-only") |
| 543 |
result.stdout.fnmatch_lines(["*raise ValueError*", "*1 error*"]) |
| 544 |
|
| 545 |
def test_collectonly_fatal(self, pytester: Pytester) -> None: |
| 546 |
pytester.makeconftest( |
| 547 |
""" |
| 548 |
def pytest_collectstart(collector): |
| 549 |
assert 0, "urgs" |
| 550 |
""" |
| 551 |
) |
| 552 |
result = pytester.runpytest("--collect-only") |
| 553 |
result.stdout.fnmatch_lines(["*INTERNAL*args*"]) |
| 554 |
assert result.ret == 3 |
| 555 |
|
| 556 |
def test_collectonly_simple(self, pytester: Pytester) -> None: |
| 557 |
p = pytester.makepyfile( |
| 558 |
""" |
| 559 |
def test_func1(): |
| 560 |
pass |
| 561 |
class TestClass(object): |
| 562 |
def test_method(self): |
| 563 |
pass |
| 564 |
""" |
| 565 |
) |
| 566 |
result = pytester.runpytest("--collect-only", p) |
| 567 |
# assert stderr.startswith("inserting into sys.path") |
| 568 |
assert result.ret == 0 |
| 569 |
result.stdout.fnmatch_lines( |
| 570 |
[ |
| 571 |
"*<Module *.py>", |
| 572 |
"* <Function test_func1>", |
| 573 |
"* <Class TestClass>", |
| 574 |
"* <Function test_method>", |
| 575 |
] |
| 576 |
) |
| 577 |
|
| 578 |
def test_collectonly_error(self, pytester: Pytester) -> None: |
| 579 |
p = pytester.makepyfile("import Errlkjqweqwe") |
| 580 |
result = pytester.runpytest("--collect-only", p) |
| 581 |
assert result.ret == 2 |
| 582 |
result.stdout.fnmatch_lines( |
| 583 |
textwrap.dedent( |
| 584 |
"""\ |
| 585 |
*ERROR* |
| 586 |
*ImportError* |
| 587 |
*No module named *Errlk* |
| 588 |
*1 error* |
| 589 |
""" |
| 590 |
).strip() |
| 591 |
) |
| 592 |
|
| 593 |
def test_collectonly_missing_path(self, pytester: Pytester) -> None: |
| 594 |
"""Issue 115: failure in parseargs will cause session not to |
| 595 |
have the items attribute.""" |
| 596 |
result = pytester.runpytest("--collect-only", "uhm_missing_path") |
| 597 |
assert result.ret == 4 |
| 598 |
result.stderr.fnmatch_lines( |
| 599 |
["*ERROR: file or directory not found: uhm_missing_path"] |
| 600 |
) |
| 601 |
|
| 602 |
def test_collectonly_quiet(self, pytester: Pytester) -> None: |
| 603 |
pytester.makepyfile("def test_foo(): pass") |
| 604 |
result = pytester.runpytest("--collect-only", "-q") |
| 605 |
result.stdout.fnmatch_lines(["*test_foo*"]) |
| 606 |
|
| 607 |
def test_collectonly_more_quiet(self, pytester: Pytester) -> None: |
| 608 |
pytester.makepyfile(test_fun="def test_foo(): pass") |
| 609 |
result = pytester.runpytest("--collect-only", "-qq") |
| 610 |
result.stdout.fnmatch_lines(["*test_fun.py: 1*"]) |
| 611 |
|
| 612 |
def test_collect_only_summary_status(self, pytester: Pytester) -> None: |
| 613 |
"""Custom status depending on test selection using -k or -m. #7701.""" |
| 614 |
pytester.makepyfile( |
| 615 |
test_collect_foo=""" |
| 616 |
def test_foo(): pass |
| 617 |
""", |
| 618 |
test_collect_bar=""" |
| 619 |
def test_foobar(): pass |
| 620 |
def test_bar(): pass |
| 621 |
""", |
| 622 |
) |
| 623 |
result = pytester.runpytest("--collect-only") |
| 624 |
result.stdout.fnmatch_lines("*== 3 tests collected in * ==*") |
| 625 |
|
| 626 |
result = pytester.runpytest("--collect-only", "test_collect_foo.py") |
| 627 |
result.stdout.fnmatch_lines("*== 1 test collected in * ==*") |
| 628 |
|
| 629 |
result = pytester.runpytest("--collect-only", "-k", "foo") |
| 630 |
result.stdout.fnmatch_lines("*== 2/3 tests collected (1 deselected) in * ==*") |
| 631 |
|
| 632 |
result = pytester.runpytest("--collect-only", "-k", "test_bar") |
| 633 |
result.stdout.fnmatch_lines("*== 1/3 tests collected (2 deselected) in * ==*") |
| 634 |
|
| 635 |
result = pytester.runpytest("--collect-only", "-k", "invalid") |
| 636 |
result.stdout.fnmatch_lines("*== no tests collected (3 deselected) in * ==*") |
| 637 |
|
| 638 |
pytester.mkdir("no_tests_here") |
| 639 |
result = pytester.runpytest("--collect-only", "no_tests_here") |
| 640 |
result.stdout.fnmatch_lines("*== no tests collected in * ==*") |
| 641 |
|
| 642 |
pytester.makepyfile( |
| 643 |
test_contains_error=""" |
| 644 |
raise RuntimeError |
| 645 |
""", |
| 646 |
) |
| 647 |
result = pytester.runpytest("--collect-only") |
| 648 |
result.stdout.fnmatch_lines("*== 3 tests collected, 1 error in * ==*") |
| 649 |
result = pytester.runpytest("--collect-only", "-k", "foo") |
| 650 |
result.stdout.fnmatch_lines( |
| 651 |
"*== 2/3 tests collected (1 deselected), 1 error in * ==*" |
| 652 |
) |
| 653 |
|
| 654 |
|
| 655 |
class TestFixtureReporting: |
| 656 |
def test_setup_fixture_error(self, pytester: Pytester) -> None: |
| 657 |
pytester.makepyfile( |
| 658 |
""" |
| 659 |
def setup_function(function): |
| 660 |
print("setup func") |
| 661 |
assert 0 |
| 662 |
def test_nada(): |
| 663 |
pass |
| 664 |
""" |
| 665 |
) |
| 666 |
result = pytester.runpytest() |
| 667 |
result.stdout.fnmatch_lines( |
| 668 |
[ |
| 669 |
"*ERROR at setup of test_nada*", |
| 670 |
"*setup_function(function):*", |
| 671 |
"*setup func*", |
| 672 |
"*assert 0*", |
| 673 |
"*1 error*", |
| 674 |
] |
| 675 |
) |
| 676 |
assert result.ret != 0 |
| 677 |
|
| 678 |
def test_teardown_fixture_error(self, pytester: Pytester) -> None: |
| 679 |
pytester.makepyfile( |
| 680 |
""" |
| 681 |
def test_nada(): |
| 682 |
pass |
| 683 |
def teardown_function(function): |
| 684 |
print("teardown func") |
| 685 |
assert 0 |
| 686 |
""" |
| 687 |
) |
| 688 |
result = pytester.runpytest() |
| 689 |
result.stdout.fnmatch_lines( |
| 690 |
[ |
| 691 |
"*ERROR at teardown*", |
| 692 |
"*teardown_function(function):*", |
| 693 |
"*assert 0*", |
| 694 |
"*Captured stdout*", |
| 695 |
"*teardown func*", |
| 696 |
"*1 passed*1 error*", |
| 697 |
] |
| 698 |
) |
| 699 |
|
| 700 |
def test_teardown_fixture_error_and_test_failure(self, pytester: Pytester) -> None: |
| 701 |
pytester.makepyfile( |
| 702 |
""" |
| 703 |
def test_fail(): |
| 704 |
assert 0, "failingfunc" |
| 705 |
|
| 706 |
def teardown_function(function): |
| 707 |
print("teardown func") |
| 708 |
assert False |
| 709 |
""" |
| 710 |
) |
| 711 |
result = pytester.runpytest() |
| 712 |
result.stdout.fnmatch_lines( |
| 713 |
[ |
| 714 |
"*ERROR at teardown of test_fail*", |
| 715 |
"*teardown_function(function):*", |
| 716 |
"*assert False*", |
| 717 |
"*Captured stdout*", |
| 718 |
"*teardown func*", |
| 719 |
"*test_fail*", |
| 720 |
"*def test_fail():", |
| 721 |
"*failingfunc*", |
| 722 |
"*1 failed*1 error*", |
| 723 |
] |
| 724 |
) |
| 725 |
|
| 726 |
def test_setup_teardown_output_and_test_failure(self, pytester: Pytester) -> None: |
| 727 |
"""Test for issue #442.""" |
| 728 |
pytester.makepyfile( |
| 729 |
""" |
| 730 |
def setup_function(function): |
| 731 |
print("setup func") |
| 732 |
|
| 733 |
def test_fail(): |
| 734 |
assert 0, "failingfunc" |
| 735 |
|
| 736 |
def teardown_function(function): |
| 737 |
print("teardown func") |
| 738 |
""" |
| 739 |
) |
| 740 |
result = pytester.runpytest() |
| 741 |
result.stdout.fnmatch_lines( |
| 742 |
[ |
| 743 |
"*test_fail*", |
| 744 |
"*def test_fail():", |
| 745 |
"*failingfunc*", |
| 746 |
"*Captured stdout setup*", |
| 747 |
"*setup func*", |
| 748 |
"*Captured stdout teardown*", |
| 749 |
"*teardown func*", |
| 750 |
"*1 failed*", |
| 751 |
] |
| 752 |
) |
| 753 |
|
| 754 |
|
| 755 |
class TestTerminalFunctional: |
| 756 |
def test_deselected(self, pytester: Pytester) -> None: |
| 757 |
testpath = pytester.makepyfile( |
| 758 |
""" |
| 759 |
def test_one(): |
| 760 |
pass |
| 761 |
def test_two(): |
| 762 |
pass |
| 763 |
def test_three(): |
| 764 |
pass |
| 765 |
""" |
| 766 |
) |
| 767 |
result = pytester.runpytest("-k", "test_t", testpath) |
| 768 |
result.stdout.fnmatch_lines( |
| 769 |
["collected 3 items / 1 deselected / 2 selected", "*test_deselected.py ..*"] |
| 770 |
) |
| 771 |
assert result.ret == 0 |
| 772 |
|
| 773 |
def test_deselected_with_hook_wrapper(self, pytester: Pytester) -> None: |
| 774 |
pytester.makeconftest( |
| 775 |
""" |
| 776 |
import pytest |
| 777 |
|
| 778 |
@pytest.hookimpl(wrapper=True) |
| 779 |
def pytest_collection_modifyitems(config, items): |
| 780 |
yield |
| 781 |
deselected = items.pop() |
| 782 |
config.hook.pytest_deselected(items=[deselected]) |
| 783 |
""" |
| 784 |
) |
| 785 |
testpath = pytester.makepyfile( |
| 786 |
""" |
| 787 |
def test_one(): |
| 788 |
pass |
| 789 |
def test_two(): |
| 790 |
pass |
| 791 |
def test_three(): |
| 792 |
pass |
| 793 |
""" |
| 794 |
) |
| 795 |
result = pytester.runpytest(testpath) |
| 796 |
result.stdout.fnmatch_lines( |
| 797 |
[ |
| 798 |
"collected 3 items / 1 deselected / 2 selected", |
| 799 |
"*= 2 passed, 1 deselected in*", |
| 800 |
] |
| 801 |
) |
| 802 |
assert result.ret == 0 |
| 803 |
|
| 804 |
def test_show_deselected_items_using_markexpr_before_test_execution( |
| 805 |
self, pytester: Pytester |
| 806 |
) -> None: |
| 807 |
pytester.makepyfile( |
| 808 |
test_show_deselected=""" |
| 809 |
import pytest |
| 810 |
|
| 811 |
@pytest.mark.foo |
| 812 |
def test_foobar(): |
| 813 |
pass |
| 814 |
|
| 815 |
@pytest.mark.bar |
| 816 |
def test_bar(): |
| 817 |
pass |
| 818 |
|
| 819 |
def test_pass(): |
| 820 |
pass |
| 821 |
""" |
| 822 |
) |
| 823 |
result = pytester.runpytest("-m", "not foo") |
| 824 |
result.stdout.fnmatch_lines( |
| 825 |
[ |
| 826 |
"collected 3 items / 1 deselected / 2 selected", |
| 827 |
"*test_show_deselected.py ..*", |
| 828 |
"*= 2 passed, 1 deselected in * =*", |
| 829 |
] |
| 830 |
) |
| 831 |
result.stdout.no_fnmatch_line("*= 1 deselected =*") |
| 832 |
assert result.ret == 0 |
| 833 |
|
| 834 |
def test_selected_count_with_error(self, pytester: Pytester) -> None: |
| 835 |
pytester.makepyfile( |
| 836 |
test_selected_count_3=""" |
| 837 |
def test_one(): |
| 838 |
pass |
| 839 |
def test_two(): |
| 840 |
pass |
| 841 |
def test_three(): |
| 842 |
pass |
| 843 |
""", |
| 844 |
test_selected_count_error=""" |
| 845 |
5/0 |
| 846 |
def test_foo(): |
| 847 |
pass |
| 848 |
def test_bar(): |
| 849 |
pass |
| 850 |
""", |
| 851 |
) |
| 852 |
result = pytester.runpytest("-k", "test_t") |
| 853 |
result.stdout.fnmatch_lines( |
| 854 |
[ |
| 855 |
"collected 3 items / 1 error / 1 deselected / 2 selected", |
| 856 |
"* ERROR collecting test_selected_count_error.py *", |
| 857 |
] |
| 858 |
) |
| 859 |
assert result.ret == ExitCode.INTERRUPTED |
| 860 |
|
| 861 |
def test_no_skip_summary_if_failure(self, pytester: Pytester) -> None: |
| 862 |
pytester.makepyfile( |
| 863 |
""" |
| 864 |
import pytest |
| 865 |
def test_ok(): |
| 866 |
pass |
| 867 |
def test_fail(): |
| 868 |
assert 0 |
| 869 |
def test_skip(): |
| 870 |
pytest.skip("dontshow") |
| 871 |
""" |
| 872 |
) |
| 873 |
result = pytester.runpytest() |
| 874 |
assert result.stdout.str().find("skip test summary") == -1 |
| 875 |
assert result.ret == 1 |
| 876 |
|
| 877 |
def test_passes(self, pytester: Pytester) -> None: |
| 878 |
p1 = pytester.makepyfile( |
| 879 |
""" |
| 880 |
def test_passes(): |
| 881 |
pass |
| 882 |
class TestClass(object): |
| 883 |
def test_method(self): |
| 884 |
pass |
| 885 |
""" |
| 886 |
) |
| 887 |
old = p1.parent |
| 888 |
pytester.chdir() |
| 889 |
try: |
| 890 |
result = pytester.runpytest() |
| 891 |
finally: |
| 892 |
os.chdir(old) |
| 893 |
result.stdout.fnmatch_lines(["test_passes.py ..*", "* 2 pass*"]) |
| 894 |
assert result.ret == 0 |
| 895 |
|
| 896 |
def test_header_trailer_info( |
| 897 |
self, monkeypatch: MonkeyPatch, pytester: Pytester, request |
| 898 |
) -> None: |
| 899 |
monkeypatch.delenv("PYTEST_DISABLE_PLUGIN_AUTOLOAD") |
| 900 |
monkeypatch.delenv("PYTEST_PLUGINS", raising=False) |
| 901 |
pytester.makepyfile( |
| 902 |
""" |
| 903 |
def test_passes(): |
| 904 |
pass |
| 905 |
""" |
| 906 |
) |
| 907 |
result = pytester.runpytest() |
| 908 |
verinfo = ".".join(map(str, sys.version_info[:3])) |
| 909 |
result.stdout.fnmatch_lines( |
| 910 |
[ |
| 911 |
"*===== test session starts ====*", |
| 912 |
f"platform {sys.platform} -- Python {verinfo}*pytest-{pytest.__version__}**pluggy-{pluggy.__version__}", |
| 913 |
"*test_header_trailer_info.py .*", |
| 914 |
"=* 1 passed*in *.[0-9][0-9]s *=", |
| 915 |
] |
| 916 |
) |
| 917 |
if request.config.pluginmanager.list_plugin_distinfo(): |
| 918 |
result.stdout.fnmatch_lines(["plugins: *"]) |
| 919 |
|
| 920 |
def test_no_header_trailer_info( |
| 921 |
self, monkeypatch: MonkeyPatch, pytester: Pytester, request |
| 922 |
) -> None: |
| 923 |
monkeypatch.delenv("PYTEST_DISABLE_PLUGIN_AUTOLOAD") |
| 924 |
pytester.makepyfile( |
| 925 |
""" |
| 926 |
def test_passes(): |
| 927 |
pass |
| 928 |
""" |
| 929 |
) |
| 930 |
result = pytester.runpytest("--no-header") |
| 931 |
verinfo = ".".join(map(str, sys.version_info[:3])) |
| 932 |
result.stdout.no_fnmatch_line( |
| 933 |
f"platform {sys.platform} -- Python {verinfo}*pytest-{pytest.__version__}**pluggy-{pluggy.__version__}" |
| 934 |
) |
| 935 |
if request.config.pluginmanager.list_plugin_distinfo(): |
| 936 |
result.stdout.no_fnmatch_line("plugins: *") |
| 937 |
|
| 938 |
def test_header(self, pytester: Pytester) -> None: |
| 939 |
pytester.path.joinpath("tests").mkdir() |
| 940 |
pytester.path.joinpath("gui").mkdir() |
| 941 |
|
| 942 |
# no configuration file |
| 943 |
result = pytester.runpytest() |
| 944 |
result.stdout.fnmatch_lines(["rootdir: *test_header0"]) |
| 945 |
|
| 946 |
# with configfile |
| 947 |
pytester.makeini("""[pytest]""") |
| 948 |
result = pytester.runpytest() |
| 949 |
result.stdout.fnmatch_lines(["rootdir: *test_header0", "configfile: tox.ini"]) |
| 950 |
|
| 951 |
# with testpaths option, and not passing anything in the command-line |
| 952 |
pytester.makeini( |
| 953 |
""" |
| 954 |
[pytest] |
| 955 |
testpaths = tests gui |
| 956 |
""" |
| 957 |
) |
| 958 |
result = pytester.runpytest() |
| 959 |
result.stdout.fnmatch_lines( |
| 960 |
["rootdir: *test_header0", "configfile: tox.ini", "testpaths: tests, gui"] |
| 961 |
) |
| 962 |
|
| 963 |
# with testpaths option, passing directory in command-line: do not show testpaths then |
| 964 |
result = pytester.runpytest("tests") |
| 965 |
result.stdout.fnmatch_lines(["rootdir: *test_header0", "configfile: tox.ini"]) |
| 966 |
|
| 967 |
def test_header_absolute_testpath( |
| 968 |
self, pytester: Pytester, monkeypatch: MonkeyPatch |
| 969 |
) -> None: |
| 970 |
"""Regression test for #7814.""" |
| 971 |
tests = pytester.path.joinpath("tests") |
| 972 |
tests.mkdir() |
| 973 |
pytester.makepyprojecttoml( |
| 974 |
f""" |
| 975 |
[tool.pytest.ini_options] |
| 976 |
testpaths = ['{tests}'] |
| 977 |
""" |
| 978 |
) |
| 979 |
result = pytester.runpytest() |
| 980 |
result.stdout.fnmatch_lines( |
| 981 |
[ |
| 982 |
"rootdir: *absolute_testpath0", |
| 983 |
"configfile: pyproject.toml", |
| 984 |
f"testpaths: {tests}", |
| 985 |
] |
| 986 |
) |
| 987 |
|
| 988 |
def test_no_header(self, pytester: Pytester) -> None: |
| 989 |
pytester.path.joinpath("tests").mkdir() |
| 990 |
pytester.path.joinpath("gui").mkdir() |
| 991 |
|
| 992 |
# with testpaths option, and not passing anything in the command-line |
| 993 |
pytester.makeini( |
| 994 |
""" |
| 995 |
[pytest] |
| 996 |
testpaths = tests gui |
| 997 |
""" |
| 998 |
) |
| 999 |
result = pytester.runpytest("--no-header") |
| 1000 |
result.stdout.no_fnmatch_line( |
| 1001 |
"rootdir: *test_header0, inifile: tox.ini, testpaths: tests, gui" |
| 1002 |
) |
| 1003 |
|
| 1004 |
# with testpaths option, passing directory in command-line: do not show testpaths then |
| 1005 |
result = pytester.runpytest("tests", "--no-header") |
| 1006 |
result.stdout.no_fnmatch_line("rootdir: *test_header0, inifile: tox.ini") |
| 1007 |
|
| 1008 |
def test_no_summary(self, pytester: Pytester) -> None: |
| 1009 |
p1 = pytester.makepyfile( |
| 1010 |
""" |
| 1011 |
def test_no_summary(): |
| 1012 |
assert false |
| 1013 |
""" |
| 1014 |
) |
| 1015 |
result = pytester.runpytest(p1, "--no-summary") |
| 1016 |
result.stdout.no_fnmatch_line("*= FAILURES =*") |
| 1017 |
|
| 1018 |
def test_showlocals(self, pytester: Pytester) -> None: |
| 1019 |
p1 = pytester.makepyfile( |
| 1020 |
""" |
| 1021 |
def test_showlocals(): |
| 1022 |
x = 3 |
| 1023 |
y = "x" * 5000 |
| 1024 |
assert 0 |
| 1025 |
""" |
| 1026 |
) |
| 1027 |
result = pytester.runpytest(p1, "-l") |
| 1028 |
result.stdout.fnmatch_lines( |
| 1029 |
[ |
| 1030 |
# "_ _ * Locals *", |
| 1031 |
"x* = 3", |
| 1032 |
"y* = 'xxxxxx*", |
| 1033 |
] |
| 1034 |
) |
| 1035 |
|
| 1036 |
def test_noshowlocals_addopts_override(self, pytester: Pytester) -> None: |
| 1037 |
pytester.makeini("[pytest]\naddopts=--showlocals") |
| 1038 |
p1 = pytester.makepyfile( |
| 1039 |
""" |
| 1040 |
def test_noshowlocals(): |
| 1041 |
x = 3 |
| 1042 |
y = "x" * 5000 |
| 1043 |
assert 0 |
| 1044 |
""" |
| 1045 |
) |
| 1046 |
|
| 1047 |
# Override global --showlocals for py.test via arg |
| 1048 |
result = pytester.runpytest(p1, "--no-showlocals") |
| 1049 |
result.stdout.no_fnmatch_line("x* = 3") |
| 1050 |
result.stdout.no_fnmatch_line("y* = 'xxxxxx*") |
| 1051 |
|
| 1052 |
def test_showlocals_short(self, pytester: Pytester) -> None: |
| 1053 |
p1 = pytester.makepyfile( |
| 1054 |
""" |
| 1055 |
def test_showlocals_short(): |
| 1056 |
x = 3 |
| 1057 |
y = "xxxx" |
| 1058 |
assert 0 |
| 1059 |
""" |
| 1060 |
) |
| 1061 |
result = pytester.runpytest(p1, "-l", "--tb=short") |
| 1062 |
result.stdout.fnmatch_lines( |
| 1063 |
[ |
| 1064 |
"test_showlocals_short.py:*", |
| 1065 |
" assert 0", |
| 1066 |
"E assert 0", |
| 1067 |
" x = 3", |
| 1068 |
" y = 'xxxx'", |
| 1069 |
] |
| 1070 |
) |
| 1071 |
|
| 1072 |
@pytest.fixture |
| 1073 |
def verbose_testfile(self, pytester: Pytester) -> Path: |
| 1074 |
return pytester.makepyfile( |
| 1075 |
""" |
| 1076 |
import pytest |
| 1077 |
def test_fail(): |
| 1078 |
raise ValueError() |
| 1079 |
def test_pass(): |
| 1080 |
pass |
| 1081 |
class TestClass(object): |
| 1082 |
def test_skip(self): |
| 1083 |
pytest.skip("hello") |
| 1084 |
""" |
| 1085 |
) |
| 1086 |
|
| 1087 |
def test_verbose_reporting(self, verbose_testfile, pytester: Pytester) -> None: |
| 1088 |
result = pytester.runpytest( |
| 1089 |
verbose_testfile, "-v", "-Walways::pytest.PytestWarning" |
| 1090 |
) |
| 1091 |
result.stdout.fnmatch_lines( |
| 1092 |
[ |
| 1093 |
"*test_verbose_reporting.py::test_fail *FAIL*", |
| 1094 |
"*test_verbose_reporting.py::test_pass *PASS*", |
| 1095 |
"*test_verbose_reporting.py::TestClass::test_skip *SKIP*", |
| 1096 |
] |
| 1097 |
) |
| 1098 |
assert result.ret == 1 |
| 1099 |
|
| 1100 |
def test_verbose_reporting_xdist( |
| 1101 |
self, |
| 1102 |
verbose_testfile, |
| 1103 |
monkeypatch: MonkeyPatch, |
| 1104 |
pytester: Pytester, |
| 1105 |
pytestconfig, |
| 1106 |
) -> None: |
| 1107 |
if not pytestconfig.pluginmanager.get_plugin("xdist"): |
| 1108 |
pytest.skip("xdist plugin not installed") |
| 1109 |
|
| 1110 |
monkeypatch.delenv("PYTEST_DISABLE_PLUGIN_AUTOLOAD") |
| 1111 |
result = pytester.runpytest( |
| 1112 |
verbose_testfile, "-v", "-n 1", "-Walways::pytest.PytestWarning" |
| 1113 |
) |
| 1114 |
result.stdout.fnmatch_lines( |
| 1115 |
["*FAIL*test_verbose_reporting_xdist.py::test_fail*"] |
| 1116 |
) |
| 1117 |
assert result.ret == 1 |
| 1118 |
|
| 1119 |
def test_quiet_reporting(self, pytester: Pytester) -> None: |
| 1120 |
p1 = pytester.makepyfile("def test_pass(): pass") |
| 1121 |
result = pytester.runpytest(p1, "-q") |
| 1122 |
s = result.stdout.str() |
| 1123 |
assert "test session starts" not in s |
| 1124 |
assert p1.name not in s |
| 1125 |
assert "===" not in s |
| 1126 |
assert "passed" in s |
| 1127 |
|
| 1128 |
def test_more_quiet_reporting(self, pytester: Pytester) -> None: |
| 1129 |
p1 = pytester.makepyfile("def test_pass(): pass") |
| 1130 |
result = pytester.runpytest(p1, "-qq") |
| 1131 |
s = result.stdout.str() |
| 1132 |
assert "test session starts" not in s |
| 1133 |
assert p1.name not in s |
| 1134 |
assert "===" not in s |
| 1135 |
assert "passed" not in s |
| 1136 |
|
| 1137 |
@pytest.mark.parametrize( |
| 1138 |
"params", [(), ("--collect-only",)], ids=["no-params", "collect-only"] |
| 1139 |
) |
| 1140 |
def test_report_collectionfinish_hook(self, pytester: Pytester, params) -> None: |
| 1141 |
pytester.makeconftest( |
| 1142 |
""" |
| 1143 |
def pytest_report_collectionfinish(config, start_path, items): |
| 1144 |
return [f'hello from hook: {len(items)} items'] |
| 1145 |
""" |
| 1146 |
) |
| 1147 |
pytester.makepyfile( |
| 1148 |
""" |
| 1149 |
import pytest |
| 1150 |
@pytest.mark.parametrize('i', range(3)) |
| 1151 |
def test(i): |
| 1152 |
pass |
| 1153 |
""" |
| 1154 |
) |
| 1155 |
result = pytester.runpytest(*params) |
| 1156 |
result.stdout.fnmatch_lines(["collected 3 items", "hello from hook: 3 items"]) |
| 1157 |
|
| 1158 |
def test_summary_f_alias(self, pytester: Pytester) -> None: |
| 1159 |
"""Test that 'f' and 'F' report chars are aliases and don't show up twice in the summary (#6334)""" |
| 1160 |
pytester.makepyfile( |
| 1161 |
""" |
| 1162 |
def test(): |
| 1163 |
assert False |
| 1164 |
""" |
| 1165 |
) |
| 1166 |
result = pytester.runpytest("-rfF") |
| 1167 |
expected = "FAILED test_summary_f_alias.py::test - assert False" |
| 1168 |
result.stdout.fnmatch_lines([expected]) |
| 1169 |
assert result.stdout.lines.count(expected) == 1 |
| 1170 |
|
| 1171 |
def test_summary_s_alias(self, pytester: Pytester) -> None: |
| 1172 |
"""Test that 's' and 'S' report chars are aliases and don't show up twice in the summary""" |
| 1173 |
pytester.makepyfile( |
| 1174 |
""" |
| 1175 |
import pytest |
| 1176 |
|
| 1177 |
@pytest.mark.skip |
| 1178 |
def test(): |
| 1179 |
pass |
| 1180 |
""" |
| 1181 |
) |
| 1182 |
result = pytester.runpytest("-rsS") |
| 1183 |
expected = "SKIPPED [1] test_summary_s_alias.py:3: unconditional skip" |
| 1184 |
result.stdout.fnmatch_lines([expected]) |
| 1185 |
assert result.stdout.lines.count(expected) == 1 |
| 1186 |
|
| 1187 |
def test_summary_s_folded(self, pytester: Pytester) -> None: |
| 1188 |
"""Test that skipped tests are correctly folded""" |
| 1189 |
pytester.makepyfile( |
| 1190 |
""" |
| 1191 |
import pytest |
| 1192 |
|
| 1193 |
@pytest.mark.parametrize("param", [True, False]) |
| 1194 |
@pytest.mark.skip("Some reason") |
| 1195 |
def test(param): |
| 1196 |
pass |
| 1197 |
""" |
| 1198 |
) |
| 1199 |
result = pytester.runpytest("-rs") |
| 1200 |
expected = "SKIPPED [2] test_summary_s_folded.py:3: Some reason" |
| 1201 |
result.stdout.fnmatch_lines([expected]) |
| 1202 |
assert result.stdout.lines.count(expected) == 1 |
| 1203 |
|
| 1204 |
def test_summary_s_unfolded(self, pytester: Pytester) -> None: |
| 1205 |
"""Test that skipped tests are not folded if --no-fold-skipped is set""" |
| 1206 |
pytester.makepyfile( |
| 1207 |
""" |
| 1208 |
import pytest |
| 1209 |
|
| 1210 |
@pytest.mark.parametrize("param", [True, False]) |
| 1211 |
@pytest.mark.skip("Some reason") |
| 1212 |
def test(param): |
| 1213 |
pass |
| 1214 |
""" |
| 1215 |
) |
| 1216 |
result = pytester.runpytest("-rs", "--no-fold-skipped") |
| 1217 |
expected = [ |
| 1218 |
"SKIPPED test_summary_s_unfolded.py::test[True] - Skipped: Some reason", |
| 1219 |
"SKIPPED test_summary_s_unfolded.py::test[False] - Skipped: Some reason", |
| 1220 |
] |
| 1221 |
result.stdout.fnmatch_lines(expected) |
| 1222 |
assert result.stdout.lines.count(expected[0]) == 1 |
| 1223 |
assert result.stdout.lines.count(expected[1]) == 1 |
| 1224 |
|
| 1225 |
|
| 1226 |
@pytest.mark.parametrize( |
| 1227 |
("use_ci", "expected_message"), |
| 1228 |
( |
| 1229 |
(True, f"- AssertionError: {'this_failed' * 100}"), |
| 1230 |
(False, "- AssertionError: this_failedt..."), |
| 1231 |
), |
| 1232 |
ids=("on CI", "not on CI"), |
| 1233 |
) |
| 1234 |
def test_fail_extra_reporting( |
| 1235 |
pytester: Pytester, monkeypatch, use_ci: bool, expected_message: str |
| 1236 |
) -> None: |
| 1237 |
if use_ci: |
| 1238 |
monkeypatch.setenv("CI", "true") |
| 1239 |
else: |
| 1240 |
monkeypatch.delenv("CI", raising=False) |
| 1241 |
monkeypatch.setenv("COLUMNS", "80") |
| 1242 |
pytester.makepyfile("def test_this(): assert 0, 'this_failed' * 100") |
| 1243 |
result = pytester.runpytest("-rN") |
| 1244 |
result.stdout.no_fnmatch_line("*short test summary*") |
| 1245 |
result = pytester.runpytest() |
| 1246 |
result.stdout.fnmatch_lines( |
| 1247 |
[ |
| 1248 |
"*test summary*", |
| 1249 |
f"FAILED test_fail_extra_reporting.py::test_this {expected_message}", |
| 1250 |
] |
| 1251 |
) |
| 1252 |
|
| 1253 |
|
| 1254 |
def test_fail_reporting_on_pass(pytester: Pytester) -> None: |
| 1255 |
pytester.makepyfile("def test_this(): assert 1") |
| 1256 |
result = pytester.runpytest("-rf") |
| 1257 |
result.stdout.no_fnmatch_line("*short test summary*") |
| 1258 |
|
| 1259 |
|
| 1260 |
def test_pass_extra_reporting(pytester: Pytester) -> None: |
| 1261 |
pytester.makepyfile("def test_this(): assert 1") |
| 1262 |
result = pytester.runpytest() |
| 1263 |
result.stdout.no_fnmatch_line("*short test summary*") |
| 1264 |
result = pytester.runpytest("-rp") |
| 1265 |
result.stdout.fnmatch_lines(["*test summary*", "PASS*test_pass_extra_reporting*"]) |
| 1266 |
|
| 1267 |
|
| 1268 |
def test_pass_reporting_on_fail(pytester: Pytester) -> None: |
| 1269 |
pytester.makepyfile("def test_this(): assert 0") |
| 1270 |
result = pytester.runpytest("-rp") |
| 1271 |
result.stdout.no_fnmatch_line("*short test summary*") |
| 1272 |
|
| 1273 |
|
| 1274 |
def test_pass_output_reporting(pytester: Pytester) -> None: |
| 1275 |
pytester.makepyfile( |
| 1276 |
""" |
| 1277 |
def setup_module(): |
| 1278 |
print("setup_module") |
| 1279 |
|
| 1280 |
def teardown_module(): |
| 1281 |
print("teardown_module") |
| 1282 |
|
| 1283 |
def test_pass_has_output(): |
| 1284 |
print("Four score and seven years ago...") |
| 1285 |
|
| 1286 |
def test_pass_no_output(): |
| 1287 |
pass |
| 1288 |
""" |
| 1289 |
) |
| 1290 |
result = pytester.runpytest() |
| 1291 |
s = result.stdout.str() |
| 1292 |
assert "test_pass_has_output" not in s |
| 1293 |
assert "Four score and seven years ago..." not in s |
| 1294 |
assert "test_pass_no_output" not in s |
| 1295 |
result = pytester.runpytest("-rPp") |
| 1296 |
result.stdout.fnmatch_lines( |
| 1297 |
[ |
| 1298 |
"*= PASSES =*", |
| 1299 |
"*_ test_pass_has_output _*", |
| 1300 |
"*- Captured stdout setup -*", |
| 1301 |
"setup_module", |
| 1302 |
"*- Captured stdout call -*", |
| 1303 |
"Four score and seven years ago...", |
| 1304 |
"*- Captured stdout teardown -*", |
| 1305 |
"teardown_module", |
| 1306 |
"*= short test summary info =*", |
| 1307 |
"PASSED test_pass_output_reporting.py::test_pass_has_output", |
| 1308 |
"PASSED test_pass_output_reporting.py::test_pass_no_output", |
| 1309 |
"*= 2 passed in *", |
| 1310 |
] |
| 1311 |
) |
| 1312 |
|
| 1313 |
|
| 1314 |
def test_color_yes(pytester: Pytester, color_mapping) -> None: |
| 1315 |
p1 = pytester.makepyfile( |
| 1316 |
""" |
| 1317 |
def fail(): |
| 1318 |
assert 0 |
| 1319 |
|
| 1320 |
def test_this(): |
| 1321 |
fail() |
| 1322 |
""" |
| 1323 |
) |
| 1324 |
result = pytester.runpytest("--color=yes", str(p1)) |
| 1325 |
result.stdout.fnmatch_lines( |
| 1326 |
color_mapping.format_for_fnmatch( |
| 1327 |
[ |
| 1328 |
"{bold}=*= test session starts =*={reset}", |
| 1329 |
"collected 1 item", |
| 1330 |
"", |
| 1331 |
"test_color_yes.py {red}F{reset}{red} * [100%]{reset}", |
| 1332 |
"", |
| 1333 |
"=*= FAILURES =*=", |
| 1334 |
"{red}{bold}_*_ test_this _*_{reset}", |
| 1335 |
"", |
| 1336 |
" {reset}{kw}def{hl-reset}{kwspace}{function}test_this{hl-reset}():{endline}", |
| 1337 |
"> fail(){endline}", |
| 1338 |
"", |
| 1339 |
"{bold}{red}test_color_yes.py{reset}:5: ", |
| 1340 |
"_ _ * _ _*", |
| 1341 |
"", |
| 1342 |
" {reset}{kw}def{hl-reset}{kwspace}{function}fail{hl-reset}():{endline}", |
| 1343 |
"> {kw}assert{hl-reset} {number}0{hl-reset}{endline}", |
| 1344 |
"{bold}{red}E assert 0{reset}", |
| 1345 |
"", |
| 1346 |
"{bold}{red}test_color_yes.py{reset}:2: AssertionError", |
| 1347 |
"{red}=*= {red}{bold}1 failed{reset}{red} in *s{reset}{red} =*={reset}", |
| 1348 |
] |
| 1349 |
) |
| 1350 |
) |
| 1351 |
result = pytester.runpytest("--color=yes", "--tb=short", str(p1)) |
| 1352 |
result.stdout.fnmatch_lines( |
| 1353 |
color_mapping.format_for_fnmatch( |
| 1354 |
[ |
| 1355 |
"{bold}=*= test session starts =*={reset}", |
| 1356 |
"collected 1 item", |
| 1357 |
"", |
| 1358 |
"test_color_yes.py {red}F{reset}{red} * [100%]{reset}", |
| 1359 |
"", |
| 1360 |
"=*= FAILURES =*=", |
| 1361 |
"{red}{bold}_*_ test_this _*_{reset}", |
| 1362 |
"{bold}{red}test_color_yes.py{reset}:5: in test_this", |
| 1363 |
" {reset}fail(){endline}", |
| 1364 |
"{bold}{red}test_color_yes.py{reset}:2: in fail", |
| 1365 |
" {reset}{kw}assert{hl-reset} {number}0{hl-reset}{endline}", |
| 1366 |
"{bold}{red}E assert 0{reset}", |
| 1367 |
"{red}=*= {red}{bold}1 failed{reset}{red} in *s{reset}{red} =*={reset}", |
| 1368 |
] |
| 1369 |
) |
| 1370 |
) |
| 1371 |
|
| 1372 |
|
| 1373 |
def test_color_no(pytester: Pytester) -> None: |
| 1374 |
pytester.makepyfile("def test_this(): assert 1") |
| 1375 |
result = pytester.runpytest("--color=no") |
| 1376 |
assert "test session starts" in result.stdout.str() |
| 1377 |
result.stdout.no_fnmatch_line("*\x1b[1m*") |
| 1378 |
|
| 1379 |
|
| 1380 |
@pytest.mark.parametrize("verbose", [True, False]) |
| 1381 |
def test_color_yes_collection_on_non_atty(pytester: Pytester, verbose) -> None: |
| 1382 |
"""#1397: Skip collect progress report when working on non-terminals.""" |
| 1383 |
pytester.makepyfile( |
| 1384 |
""" |
| 1385 |
import pytest |
| 1386 |
@pytest.mark.parametrize('i', range(10)) |
| 1387 |
def test_this(i): |
| 1388 |
assert 1 |
| 1389 |
""" |
| 1390 |
) |
| 1391 |
args = ["--color=yes"] |
| 1392 |
if verbose: |
| 1393 |
args.append("-vv") |
| 1394 |
result = pytester.runpytest(*args) |
| 1395 |
assert "test session starts" in result.stdout.str() |
| 1396 |
assert "\x1b[1m" in result.stdout.str() |
| 1397 |
result.stdout.no_fnmatch_line("*collecting 10 items*") |
| 1398 |
if verbose: |
| 1399 |
assert "collecting ..." in result.stdout.str() |
| 1400 |
assert "collected 10 items" in result.stdout.str() |
| 1401 |
|
| 1402 |
|
| 1403 |
def test_getreportopt() -> None: |
| 1404 |
from _pytest.terminal import _REPORTCHARS_DEFAULT |
| 1405 |
|
| 1406 |
class FakeConfig: |
| 1407 |
class Option: |
| 1408 |
reportchars = _REPORTCHARS_DEFAULT |
| 1409 |
disable_warnings = False |
| 1410 |
|
| 1411 |
option = Option() |
| 1412 |
|
| 1413 |
config = cast(Config, FakeConfig()) |
| 1414 |
|
| 1415 |
assert _REPORTCHARS_DEFAULT == "fE" |
| 1416 |
|
| 1417 |
# Default. |
| 1418 |
assert getreportopt(config) == "wfE" |
| 1419 |
|
| 1420 |
config.option.reportchars = "sf" |
| 1421 |
assert getreportopt(config) == "wsf" |
| 1422 |
|
| 1423 |
config.option.reportchars = "sfxw" |
| 1424 |
assert getreportopt(config) == "sfxw" |
| 1425 |
|
| 1426 |
config.option.reportchars = "a" |
| 1427 |
assert getreportopt(config) == "wsxXEf" |
| 1428 |
|
| 1429 |
config.option.reportchars = "N" |
| 1430 |
assert getreportopt(config) == "w" |
| 1431 |
|
| 1432 |
config.option.reportchars = "NwfE" |
| 1433 |
assert getreportopt(config) == "wfE" |
| 1434 |
|
| 1435 |
config.option.reportchars = "NfENx" |
| 1436 |
assert getreportopt(config) == "wx" |
| 1437 |
|
| 1438 |
# Now with --disable-warnings. |
| 1439 |
config.option.disable_warnings = True |
| 1440 |
config.option.reportchars = "a" |
| 1441 |
assert getreportopt(config) == "sxXEf" |
| 1442 |
|
| 1443 |
config.option.reportchars = "sfx" |
| 1444 |
assert getreportopt(config) == "sfx" |
| 1445 |
|
| 1446 |
config.option.reportchars = "sfxw" |
| 1447 |
assert getreportopt(config) == "sfx" |
| 1448 |
|
| 1449 |
config.option.reportchars = "a" |
| 1450 |
assert getreportopt(config) == "sxXEf" |
| 1451 |
|
| 1452 |
config.option.reportchars = "A" |
| 1453 |
assert getreportopt(config) == "PpsxXEf" |
| 1454 |
|
| 1455 |
config.option.reportchars = "AN" |
| 1456 |
assert getreportopt(config) == "" |
| 1457 |
|
| 1458 |
config.option.reportchars = "NwfE" |
| 1459 |
assert getreportopt(config) == "fE" |
| 1460 |
|
| 1461 |
|
| 1462 |
def test_terminalreporter_reportopt_addopts(pytester: Pytester) -> None: |
| 1463 |
pytester.makeini("[pytest]\naddopts=-rs") |
| 1464 |
pytester.makepyfile( |
| 1465 |
""" |
| 1466 |
import pytest |
| 1467 |
|
| 1468 |
@pytest.fixture |
| 1469 |
def tr(request): |
| 1470 |
tr = request.config.pluginmanager.getplugin("terminalreporter") |
| 1471 |
return tr |
| 1472 |
def test_opt(tr): |
| 1473 |
assert tr.hasopt('skipped') |
| 1474 |
assert not tr.hasopt('qwe') |
| 1475 |
""" |
| 1476 |
) |
| 1477 |
result = pytester.runpytest() |
| 1478 |
result.stdout.fnmatch_lines(["*1 passed*"]) |
| 1479 |
|
| 1480 |
|
| 1481 |
def test_tbstyle_short(pytester: Pytester) -> None: |
| 1482 |
p = pytester.makepyfile( |
| 1483 |
""" |
| 1484 |
import pytest |
| 1485 |
|
| 1486 |
@pytest.fixture |
| 1487 |
def arg(request): |
| 1488 |
return 42 |
| 1489 |
def test_opt(arg): |
| 1490 |
x = 0 |
| 1491 |
assert x |
| 1492 |
""" |
| 1493 |
) |
| 1494 |
result = pytester.runpytest("--tb=short") |
| 1495 |
s = result.stdout.str() |
| 1496 |
assert "arg = 42" not in s |
| 1497 |
assert "x = 0" not in s |
| 1498 |
result.stdout.fnmatch_lines([f"*{p.name}:8*", " assert x", "E assert*"]) |
| 1499 |
result = pytester.runpytest() |
| 1500 |
s = result.stdout.str() |
| 1501 |
assert "x = 0" in s |
| 1502 |
assert "assert x" in s |
| 1503 |
|
| 1504 |
|
| 1505 |
def test_traceconfig(pytester: Pytester) -> None: |
| 1506 |
result = pytester.runpytest("--traceconfig") |
| 1507 |
result.stdout.fnmatch_lines(["*active plugins*"]) |
| 1508 |
assert result.ret == ExitCode.NO_TESTS_COLLECTED |
| 1509 |
|
| 1510 |
|
| 1511 |
class TestGenericReporting: |
| 1512 |
"""Test class which can be subclassed with a different option provider to |
| 1513 |
run e.g. distributed tests.""" |
| 1514 |
|
| 1515 |
def test_collect_fail(self, pytester: Pytester, option) -> None: |
| 1516 |
pytester.makepyfile("import xyz\n") |
| 1517 |
result = pytester.runpytest(*option.args) |
| 1518 |
result.stdout.fnmatch_lines( |
| 1519 |
["ImportError while importing*", "*No module named *xyz*", "*1 error*"] |
| 1520 |
) |
| 1521 |
|
| 1522 |
def test_maxfailures(self, pytester: Pytester, option) -> None: |
| 1523 |
pytester.makepyfile( |
| 1524 |
""" |
| 1525 |
def test_1(): |
| 1526 |
assert 0 |
| 1527 |
def test_2(): |
| 1528 |
assert 0 |
| 1529 |
def test_3(): |
| 1530 |
assert 0 |
| 1531 |
""" |
| 1532 |
) |
| 1533 |
result = pytester.runpytest("--maxfail=2", *option.args) |
| 1534 |
result.stdout.fnmatch_lines( |
| 1535 |
[ |
| 1536 |
"*def test_1():*", |
| 1537 |
"*def test_2():*", |
| 1538 |
"*! stopping after 2 failures !*", |
| 1539 |
"*2 failed*", |
| 1540 |
] |
| 1541 |
) |
| 1542 |
|
| 1543 |
def test_maxfailures_with_interrupted(self, pytester: Pytester) -> None: |
| 1544 |
pytester.makepyfile( |
| 1545 |
""" |
| 1546 |
def test(request): |
| 1547 |
request.session.shouldstop = "session_interrupted" |
| 1548 |
assert 0 |
| 1549 |
""" |
| 1550 |
) |
| 1551 |
result = pytester.runpytest("--maxfail=1", "-ra") |
| 1552 |
result.stdout.fnmatch_lines( |
| 1553 |
[ |
| 1554 |
"*= short test summary info =*", |
| 1555 |
"FAILED *", |
| 1556 |
"*! stopping after 1 failures !*", |
| 1557 |
"*! session_interrupted !*", |
| 1558 |
"*= 1 failed in*", |
| 1559 |
] |
| 1560 |
) |
| 1561 |
|
| 1562 |
def test_tb_option(self, pytester: Pytester, option) -> None: |
| 1563 |
pytester.makepyfile( |
| 1564 |
""" |
| 1565 |
import pytest |
| 1566 |
def g(): |
| 1567 |
raise IndexError |
| 1568 |
def test_func(): |
| 1569 |
print(6*7) |
| 1570 |
g() # --calling-- |
| 1571 |
""" |
| 1572 |
) |
| 1573 |
for tbopt in ["long", "short", "no"]: |
| 1574 |
print(f"testing --tb={tbopt}...") |
| 1575 |
result = pytester.runpytest("-rN", f"--tb={tbopt}") |
| 1576 |
s = result.stdout.str() |
| 1577 |
if tbopt == "long": |
| 1578 |
assert "print(6*7)" in s |
| 1579 |
else: |
| 1580 |
assert "print(6*7)" not in s |
| 1581 |
if tbopt != "no": |
| 1582 |
assert "--calling--" in s |
| 1583 |
assert "IndexError" in s |
| 1584 |
else: |
| 1585 |
assert "FAILURES" not in s |
| 1586 |
assert "--calling--" not in s |
| 1587 |
assert "IndexError" not in s |
| 1588 |
|
| 1589 |
def test_tb_line_show_capture(self, pytester: Pytester, option) -> None: |
| 1590 |
output_to_capture = "help! let me out!" |
| 1591 |
pytester.makepyfile( |
| 1592 |
f""" |
| 1593 |
import pytest |
| 1594 |
def test_fail(): |
| 1595 |
print('{output_to_capture}') |
| 1596 |
assert False |
| 1597 |
""" |
| 1598 |
) |
| 1599 |
result = pytester.runpytest("--tb=line") |
| 1600 |
result.stdout.fnmatch_lines(["*- Captured stdout call -*", output_to_capture]) |
| 1601 |
|
| 1602 |
def test_tb_crashline(self, pytester: Pytester, option) -> None: |
| 1603 |
p = pytester.makepyfile( |
| 1604 |
""" |
| 1605 |
import pytest |
| 1606 |
def g(): |
| 1607 |
raise IndexError |
| 1608 |
def test_func1(): |
| 1609 |
print(6*7) |
| 1610 |
g() # --calling-- |
| 1611 |
def test_func2(): |
| 1612 |
assert 0, "hello" |
| 1613 |
""" |
| 1614 |
) |
| 1615 |
result = pytester.runpytest("--tb=line") |
| 1616 |
bn = p.name |
| 1617 |
result.stdout.fnmatch_lines( |
| 1618 |
[f"*{bn}:3: IndexError*", f"*{bn}:8: AssertionError: hello*"] |
| 1619 |
) |
| 1620 |
s = result.stdout.str() |
| 1621 |
assert "def test_func2" not in s |
| 1622 |
|
| 1623 |
def test_tb_crashline_pytrace_false(self, pytester: Pytester, option) -> None: |
| 1624 |
p = pytester.makepyfile( |
| 1625 |
""" |
| 1626 |
import pytest |
| 1627 |
def test_func1(): |
| 1628 |
pytest.fail('test_func1', pytrace=False) |
| 1629 |
""" |
| 1630 |
) |
| 1631 |
result = pytester.runpytest("--tb=line") |
| 1632 |
result.stdout.str() |
| 1633 |
bn = p.name |
| 1634 |
result.stdout.fnmatch_lines([f"*{bn}:3: Failed: test_func1"]) |
| 1635 |
|
| 1636 |
def test_pytest_report_header(self, pytester: Pytester, option) -> None: |
| 1637 |
pytester.makeconftest( |
| 1638 |
""" |
| 1639 |
def pytest_sessionstart(session): |
| 1640 |
session.config._somevalue = 42 |
| 1641 |
def pytest_report_header(config): |
| 1642 |
return "hello: %s" % config._somevalue |
| 1643 |
""" |
| 1644 |
) |
| 1645 |
pytester.mkdir("a").joinpath("conftest.py").write_text( |
| 1646 |
""" |
| 1647 |
def pytest_report_header(config, start_path): |
| 1648 |
return ["line1", str(start_path)] |
| 1649 |
""", |
| 1650 |
encoding="utf-8", |
| 1651 |
) |
| 1652 |
result = pytester.runpytest("a") |
| 1653 |
result.stdout.fnmatch_lines(["*hello: 42*", "line1", str(pytester.path)]) |
| 1654 |
|
| 1655 |
def test_show_capture(self, pytester: Pytester) -> None: |
| 1656 |
pytester.makepyfile( |
| 1657 |
""" |
| 1658 |
import sys |
| 1659 |
import logging |
| 1660 |
def test_one(): |
| 1661 |
sys.stdout.write('!This is stdout!') |
| 1662 |
sys.stderr.write('!This is stderr!') |
| 1663 |
logging.warning('!This is a warning log msg!') |
| 1664 |
assert False, 'Something failed' |
| 1665 |
""" |
| 1666 |
) |
| 1667 |
|
| 1668 |
result = pytester.runpytest("--tb=short") |
| 1669 |
result.stdout.fnmatch_lines( |
| 1670 |
[ |
| 1671 |
"!This is stdout!", |
| 1672 |
"!This is stderr!", |
| 1673 |
"*WARNING*!This is a warning log msg!", |
| 1674 |
] |
| 1675 |
) |
| 1676 |
|
| 1677 |
result = pytester.runpytest("--show-capture=all", "--tb=short") |
| 1678 |
result.stdout.fnmatch_lines( |
| 1679 |
[ |
| 1680 |
"!This is stdout!", |
| 1681 |
"!This is stderr!", |
| 1682 |
"*WARNING*!This is a warning log msg!", |
| 1683 |
] |
| 1684 |
) |
| 1685 |
|
| 1686 |
stdout = pytester.runpytest("--show-capture=stdout", "--tb=short").stdout.str() |
| 1687 |
assert "!This is stderr!" not in stdout |
| 1688 |
assert "!This is stdout!" in stdout |
| 1689 |
assert "!This is a warning log msg!" not in stdout |
| 1690 |
|
| 1691 |
stdout = pytester.runpytest("--show-capture=stderr", "--tb=short").stdout.str() |
| 1692 |
assert "!This is stdout!" not in stdout |
| 1693 |
assert "!This is stderr!" in stdout |
| 1694 |
assert "!This is a warning log msg!" not in stdout |
| 1695 |
|
| 1696 |
stdout = pytester.runpytest("--show-capture=log", "--tb=short").stdout.str() |
| 1697 |
assert "!This is stdout!" not in stdout |
| 1698 |
assert "!This is stderr!" not in stdout |
| 1699 |
assert "!This is a warning log msg!" in stdout |
| 1700 |
|
| 1701 |
stdout = pytester.runpytest("--show-capture=no", "--tb=short").stdout.str() |
| 1702 |
assert "!This is stdout!" not in stdout |
| 1703 |
assert "!This is stderr!" not in stdout |
| 1704 |
assert "!This is a warning log msg!" not in stdout |
| 1705 |
|
| 1706 |
def test_show_capture_with_teardown_logs(self, pytester: Pytester) -> None: |
| 1707 |
"""Ensure that the capturing of teardown logs honor --show-capture setting""" |
| 1708 |
pytester.makepyfile( |
| 1709 |
""" |
| 1710 |
import logging |
| 1711 |
import sys |
| 1712 |
import pytest |
| 1713 |
|
| 1714 |
@pytest.fixture(scope="function", autouse="True") |
| 1715 |
def hook_each_test(request): |
| 1716 |
yield |
| 1717 |
sys.stdout.write("!stdout!") |
| 1718 |
sys.stderr.write("!stderr!") |
| 1719 |
logging.warning("!log!") |
| 1720 |
|
| 1721 |
def test_func(): |
| 1722 |
assert False |
| 1723 |
""" |
| 1724 |
) |
| 1725 |
|
| 1726 |
result = pytester.runpytest("--show-capture=stdout", "--tb=short").stdout.str() |
| 1727 |
assert "!stdout!" in result |
| 1728 |
assert "!stderr!" not in result |
| 1729 |
assert "!log!" not in result |
| 1730 |
|
| 1731 |
result = pytester.runpytest("--show-capture=stderr", "--tb=short").stdout.str() |
| 1732 |
assert "!stdout!" not in result |
| 1733 |
assert "!stderr!" in result |
| 1734 |
assert "!log!" not in result |
| 1735 |
|
| 1736 |
result = pytester.runpytest("--show-capture=log", "--tb=short").stdout.str() |
| 1737 |
assert "!stdout!" not in result |
| 1738 |
assert "!stderr!" not in result |
| 1739 |
assert "!log!" in result |
| 1740 |
|
| 1741 |
result = pytester.runpytest("--show-capture=no", "--tb=short").stdout.str() |
| 1742 |
assert "!stdout!" not in result |
| 1743 |
assert "!stderr!" not in result |
| 1744 |
assert "!log!" not in result |
| 1745 |
|
| 1746 |
|
| 1747 |
@pytest.mark.xfail("not hasattr(os, 'dup')") |
| 1748 |
def test_fdopen_kept_alive_issue124(pytester: Pytester) -> None: |
| 1749 |
pytester.makepyfile( |
| 1750 |
""" |
| 1751 |
import os, sys |
| 1752 |
k = [] |
| 1753 |
def test_open_file_and_keep_alive(capfd): |
| 1754 |
stdout = os.fdopen(1, 'w', buffering=1, encoding='utf-8') |
| 1755 |
k.append(stdout) |
| 1756 |
|
| 1757 |
def test_close_kept_alive_file(): |
| 1758 |
stdout = k.pop() |
| 1759 |
stdout.close() |
| 1760 |
""" |
| 1761 |
) |
| 1762 |
result = pytester.runpytest() |
| 1763 |
result.stdout.fnmatch_lines(["*2 passed*"]) |
| 1764 |
|
| 1765 |
|
| 1766 |
def test_tbstyle_native_setup_error(pytester: Pytester) -> None: |
| 1767 |
pytester.makepyfile( |
| 1768 |
""" |
| 1769 |
import pytest |
| 1770 |
@pytest.fixture |
| 1771 |
def setup_error_fixture(): |
| 1772 |
raise Exception("error in exception") |
| 1773 |
|
| 1774 |
def test_error_fixture(setup_error_fixture): |
| 1775 |
pass |
| 1776 |
""" |
| 1777 |
) |
| 1778 |
result = pytester.runpytest("--tb=native") |
| 1779 |
result.stdout.fnmatch_lines( |
| 1780 |
['*File *test_tbstyle_native_setup_error.py", line *, in setup_error_fixture*'] |
| 1781 |
) |
| 1782 |
|
| 1783 |
|
| 1784 |
def test_terminal_summary(pytester: Pytester) -> None: |
| 1785 |
pytester.makeconftest( |
| 1786 |
""" |
| 1787 |
def pytest_terminal_summary(terminalreporter, exitstatus): |
| 1788 |
w = terminalreporter |
| 1789 |
w.section("hello") |
| 1790 |
w.line("world") |
| 1791 |
w.line("exitstatus: {0}".format(exitstatus)) |
| 1792 |
""" |
| 1793 |
) |
| 1794 |
result = pytester.runpytest() |
| 1795 |
result.stdout.fnmatch_lines( |
| 1796 |
""" |
| 1797 |
*==== hello ====* |
| 1798 |
world |
| 1799 |
exitstatus: 5 |
| 1800 |
""" |
| 1801 |
) |
| 1802 |
|
| 1803 |
|
| 1804 |
@pytest.mark.filterwarnings("default::UserWarning") |
| 1805 |
def test_terminal_summary_warnings_are_displayed(pytester: Pytester) -> None: |
| 1806 |
"""Test that warnings emitted during pytest_terminal_summary are displayed. |
| 1807 |
(#1305). |
| 1808 |
""" |
| 1809 |
pytester.makeconftest( |
| 1810 |
""" |
| 1811 |
import warnings |
| 1812 |
def pytest_terminal_summary(terminalreporter): |
| 1813 |
warnings.warn(UserWarning('internal warning')) |
| 1814 |
""" |
| 1815 |
) |
| 1816 |
pytester.makepyfile( |
| 1817 |
""" |
| 1818 |
def test_failure(): |
| 1819 |
import warnings |
| 1820 |
warnings.warn("warning_from_" + "test") |
| 1821 |
assert 0 |
| 1822 |
""" |
| 1823 |
) |
| 1824 |
result = pytester.runpytest("-ra") |
| 1825 |
result.stdout.fnmatch_lines( |
| 1826 |
[ |
| 1827 |
"*= warnings summary =*", |
| 1828 |
"*warning_from_test*", |
| 1829 |
"*= short test summary info =*", |
| 1830 |
"*= warnings summary (final) =*", |
| 1831 |
"*conftest.py:3:*internal warning", |
| 1832 |
"*== 1 failed, 2 warnings in *", |
| 1833 |
] |
| 1834 |
) |
| 1835 |
result.stdout.no_fnmatch_line("*None*") |
| 1836 |
stdout = result.stdout.str() |
| 1837 |
assert stdout.count("warning_from_test") == 1 |
| 1838 |
assert stdout.count("=== warnings summary ") == 2 |
| 1839 |
|
| 1840 |
|
| 1841 |
@pytest.mark.filterwarnings("default::UserWarning") |
| 1842 |
def test_terminal_summary_warnings_header_once(pytester: Pytester) -> None: |
| 1843 |
pytester.makepyfile( |
| 1844 |
""" |
| 1845 |
def test_failure(): |
| 1846 |
import warnings |
| 1847 |
warnings.warn("warning_from_" + "test") |
| 1848 |
assert 0 |
| 1849 |
""" |
| 1850 |
) |
| 1851 |
result = pytester.runpytest("-ra") |
| 1852 |
result.stdout.fnmatch_lines( |
| 1853 |
[ |
| 1854 |
"*= warnings summary =*", |
| 1855 |
"*warning_from_test*", |
| 1856 |
"*= short test summary info =*", |
| 1857 |
"*== 1 failed, 1 warning in *", |
| 1858 |
] |
| 1859 |
) |
| 1860 |
result.stdout.no_fnmatch_line("*None*") |
| 1861 |
stdout = result.stdout.str() |
| 1862 |
assert stdout.count("warning_from_test") == 1 |
| 1863 |
assert stdout.count("=== warnings summary ") == 1 |
| 1864 |
|
| 1865 |
|
| 1866 |
@pytest.mark.filterwarnings("default") |
| 1867 |
def test_terminal_no_summary_warnings_header_once(pytester: Pytester) -> None: |
| 1868 |
pytester.makepyfile( |
| 1869 |
""" |
| 1870 |
def test_failure(): |
| 1871 |
import warnings |
| 1872 |
warnings.warn("warning_from_" + "test") |
| 1873 |
assert 0 |
| 1874 |
""" |
| 1875 |
) |
| 1876 |
result = pytester.runpytest("--no-summary") |
| 1877 |
result.stdout.no_fnmatch_line("*= warnings summary =*") |
| 1878 |
result.stdout.no_fnmatch_line("*= short test summary info =*") |
| 1879 |
|
| 1880 |
|
| 1881 |
@pytest.fixture(scope="session") |
| 1882 |
def tr() -> TerminalReporter: |
| 1883 |
config = _pytest.config._prepareconfig([]) |
| 1884 |
return TerminalReporter(config) |
| 1885 |
|
| 1886 |
|
| 1887 |
@pytest.mark.parametrize( |
| 1888 |
"exp_color, exp_line, stats_arg", |
| 1889 |
[ |
| 1890 |
# The method under test only cares about the length of each |
| 1891 |
# dict value, not the actual contents, so tuples of anything |
| 1892 |
# suffice |
| 1893 |
# Important statuses -- the highest priority of these always wins |
| 1894 |
("red", [("1 failed", {"bold": True, "red": True})], {"failed": [1]}), |
| 1895 |
( |
| 1896 |
"red", |
| 1897 |
[ |
| 1898 |
("1 failed", {"bold": True, "red": True}), |
| 1899 |
("1 passed", {"bold": False, "green": True}), |
| 1900 |
], |
| 1901 |
{"failed": [1], "passed": [1]}, |
| 1902 |
), |
| 1903 |
("red", [("1 error", {"bold": True, "red": True})], {"error": [1]}), |
| 1904 |
("red", [("2 errors", {"bold": True, "red": True})], {"error": [1, 2]}), |
| 1905 |
( |
| 1906 |
"red", |
| 1907 |
[ |
| 1908 |
("1 passed", {"bold": False, "green": True}), |
| 1909 |
("1 error", {"bold": True, "red": True}), |
| 1910 |
], |
| 1911 |
{"error": [1], "passed": [1]}, |
| 1912 |
), |
| 1913 |
# (a status that's not known to the code) |
| 1914 |
("yellow", [("1 weird", {"bold": True, "yellow": True})], {"weird": [1]}), |
| 1915 |
( |
| 1916 |
"yellow", |
| 1917 |
[ |
| 1918 |
("1 passed", {"bold": False, "green": True}), |
| 1919 |
("1 weird", {"bold": True, "yellow": True}), |
| 1920 |
], |
| 1921 |
{"weird": [1], "passed": [1]}, |
| 1922 |
), |
| 1923 |
("yellow", [("1 warning", {"bold": True, "yellow": True})], {"warnings": [1]}), |
| 1924 |
( |
| 1925 |
"yellow", |
| 1926 |
[ |
| 1927 |
("1 passed", {"bold": False, "green": True}), |
| 1928 |
("1 warning", {"bold": True, "yellow": True}), |
| 1929 |
], |
| 1930 |
{"warnings": [1], "passed": [1]}, |
| 1931 |
), |
| 1932 |
( |
| 1933 |
"green", |
| 1934 |
[("5 passed", {"bold": True, "green": True})], |
| 1935 |
{"passed": [1, 2, 3, 4, 5]}, |
| 1936 |
), |
| 1937 |
# "Boring" statuses. These have no effect on the color of the summary |
| 1938 |
# line. Thus, if *every* test has a boring status, the summary line stays |
| 1939 |
# at its default color, i.e. yellow, to warn the user that the test run |
| 1940 |
# produced no useful information |
| 1941 |
("yellow", [("1 skipped", {"bold": True, "yellow": True})], {"skipped": [1]}), |
| 1942 |
( |
| 1943 |
"green", |
| 1944 |
[ |
| 1945 |
("1 passed", {"bold": True, "green": True}), |
| 1946 |
("1 skipped", {"bold": False, "yellow": True}), |
| 1947 |
], |
| 1948 |
{"skipped": [1], "passed": [1]}, |
| 1949 |
), |
| 1950 |
( |
| 1951 |
"yellow", |
| 1952 |
[("1 deselected", {"bold": True, "yellow": True})], |
| 1953 |
{"deselected": [1]}, |
| 1954 |
), |
| 1955 |
( |
| 1956 |
"green", |
| 1957 |
[ |
| 1958 |
("1 passed", {"bold": True, "green": True}), |
| 1959 |
("1 deselected", {"bold": False, "yellow": True}), |
| 1960 |
], |
| 1961 |
{"deselected": [1], "passed": [1]}, |
| 1962 |
), |
| 1963 |
("yellow", [("1 xfailed", {"bold": True, "yellow": True})], {"xfailed": [1]}), |
| 1964 |
( |
| 1965 |
"green", |
| 1966 |
[ |
| 1967 |
("1 passed", {"bold": True, "green": True}), |
| 1968 |
("1 xfailed", {"bold": False, "yellow": True}), |
| 1969 |
], |
| 1970 |
{"xfailed": [1], "passed": [1]}, |
| 1971 |
), |
| 1972 |
("yellow", [("1 xpassed", {"bold": True, "yellow": True})], {"xpassed": [1]}), |
| 1973 |
( |
| 1974 |
"yellow", |
| 1975 |
[ |
| 1976 |
("1 passed", {"bold": False, "green": True}), |
| 1977 |
("1 xpassed", {"bold": True, "yellow": True}), |
| 1978 |
], |
| 1979 |
{"xpassed": [1], "passed": [1]}, |
| 1980 |
), |
| 1981 |
# Likewise if no tests were found at all |
| 1982 |
("yellow", [("no tests ran", {"yellow": True})], {}), |
| 1983 |
# Test the empty-key special case |
| 1984 |
("yellow", [("no tests ran", {"yellow": True})], {"": [1]}), |
| 1985 |
( |
| 1986 |
"green", |
| 1987 |
[("1 passed", {"bold": True, "green": True})], |
| 1988 |
{"": [1], "passed": [1]}, |
| 1989 |
), |
| 1990 |
# A couple more complex combinations |
| 1991 |
( |
| 1992 |
"red", |
| 1993 |
[ |
| 1994 |
("1 failed", {"bold": True, "red": True}), |
| 1995 |
("2 passed", {"bold": False, "green": True}), |
| 1996 |
("3 xfailed", {"bold": False, "yellow": True}), |
| 1997 |
], |
| 1998 |
{"passed": [1, 2], "failed": [1], "xfailed": [1, 2, 3]}, |
| 1999 |
), |
| 2000 |
( |
| 2001 |
"green", |
| 2002 |
[ |
| 2003 |
("1 passed", {"bold": True, "green": True}), |
| 2004 |
("2 skipped", {"bold": False, "yellow": True}), |
| 2005 |
("3 deselected", {"bold": False, "yellow": True}), |
| 2006 |
("2 xfailed", {"bold": False, "yellow": True}), |
| 2007 |
], |
| 2008 |
{ |
| 2009 |
"passed": [1], |
| 2010 |
"skipped": [1, 2], |
| 2011 |
"deselected": [1, 2, 3], |
| 2012 |
"xfailed": [1, 2], |
| 2013 |
}, |
| 2014 |
), |
| 2015 |
], |
| 2016 |
) |
| 2017 |
def test_summary_stats( |
| 2018 |
tr: TerminalReporter, |
| 2019 |
exp_line: list[tuple[str, dict[str, bool]]], |
| 2020 |
exp_color: str, |
| 2021 |
stats_arg: dict[str, list[object]], |
| 2022 |
) -> None: |
| 2023 |
tr.stats = stats_arg |
| 2024 |
|
| 2025 |
# Fake "_is_last_item" to be True. |
| 2026 |
class fake_session: |
| 2027 |
testscollected = 0 |
| 2028 |
|
| 2029 |
tr._session = fake_session # type: ignore[assignment] |
| 2030 |
assert tr._is_last_item |
| 2031 |
|
| 2032 |
# Reset cache. |
| 2033 |
tr._main_color = None |
| 2034 |
|
| 2035 |
print(f"Based on stats: {stats_arg}") |
| 2036 |
print(f'Expect summary: "{exp_line}"; with color "{exp_color}"') |
| 2037 |
(line, color) = tr.build_summary_stats_line() |
| 2038 |
print(f'Actually got: "{line}"; with color "{color}"') |
| 2039 |
assert line == exp_line |
| 2040 |
assert color == exp_color |
| 2041 |
|
| 2042 |
|
| 2043 |
def test_skip_counting_towards_summary(tr): |
| 2044 |
class DummyReport(BaseReport): |
| 2045 |
count_towards_summary = True |
| 2046 |
|
| 2047 |
r1 = DummyReport() |
| 2048 |
r2 = DummyReport() |
| 2049 |
tr.stats = {"failed": (r1, r2)} |
| 2050 |
tr._main_color = None |
| 2051 |
res = tr.build_summary_stats_line() |
| 2052 |
assert res == ([("2 failed", {"bold": True, "red": True})], "red") |
| 2053 |
|
| 2054 |
r1.count_towards_summary = False |
| 2055 |
tr.stats = {"failed": (r1, r2)} |
| 2056 |
tr._main_color = None |
| 2057 |
res = tr.build_summary_stats_line() |
| 2058 |
assert res == ([("1 failed", {"bold": True, "red": True})], "red") |
| 2059 |
|
| 2060 |
|
| 2061 |
class TestClassicOutputStyle: |
| 2062 |
"""Ensure classic output style works as expected (#3883)""" |
| 2063 |
|
| 2064 |
@pytest.fixture |
| 2065 |
def test_files(self, pytester: Pytester) -> None: |
| 2066 |
pytester.makepyfile( |
| 2067 |
**{ |
| 2068 |
"test_one.py": "def test_one(): pass", |
| 2069 |
"test_two.py": "def test_two(): assert 0", |
| 2070 |
"sub/test_three.py": """ |
| 2071 |
def test_three_1(): pass |
| 2072 |
def test_three_2(): assert 0 |
| 2073 |
def test_three_3(): pass |
| 2074 |
""", |
| 2075 |
} |
| 2076 |
) |
| 2077 |
|
| 2078 |
def test_normal_verbosity(self, pytester: Pytester, test_files) -> None: |
| 2079 |
result = pytester.runpytest("-o", "console_output_style=classic") |
| 2080 |
result.stdout.fnmatch_lines( |
| 2081 |
[ |
| 2082 |
f"sub{os.sep}test_three.py .F.", |
| 2083 |
"test_one.py .", |
| 2084 |
"test_two.py F", |
| 2085 |
"*2 failed, 3 passed in*", |
| 2086 |
] |
| 2087 |
) |
| 2088 |
|
| 2089 |
def test_verbose(self, pytester: Pytester, test_files) -> None: |
| 2090 |
result = pytester.runpytest("-o", "console_output_style=classic", "-v") |
| 2091 |
result.stdout.fnmatch_lines( |
| 2092 |
[ |
| 2093 |
f"sub{os.sep}test_three.py::test_three_1 PASSED", |
| 2094 |
f"sub{os.sep}test_three.py::test_three_2 FAILED", |
| 2095 |
f"sub{os.sep}test_three.py::test_three_3 PASSED", |
| 2096 |
"test_one.py::test_one PASSED", |
| 2097 |
"test_two.py::test_two FAILED", |
| 2098 |
"*2 failed, 3 passed in*", |
| 2099 |
] |
| 2100 |
) |
| 2101 |
|
| 2102 |
def test_quiet(self, pytester: Pytester, test_files) -> None: |
| 2103 |
result = pytester.runpytest("-o", "console_output_style=classic", "-q") |
| 2104 |
result.stdout.fnmatch_lines([".F..F", "*2 failed, 3 passed in*"]) |
| 2105 |
|
| 2106 |
|
| 2107 |
class TestProgressOutputStyle: |
| 2108 |
@pytest.fixture |
| 2109 |
def many_tests_files(self, pytester: Pytester) -> None: |
| 2110 |
pytester.makepyfile( |
| 2111 |
test_bar=""" |
| 2112 |
import pytest |
| 2113 |
@pytest.mark.parametrize('i', range(10)) |
| 2114 |
def test_bar(i): pass |
| 2115 |
""", |
| 2116 |
test_foo=""" |
| 2117 |
import pytest |
| 2118 |
@pytest.mark.parametrize('i', range(5)) |
| 2119 |
def test_foo(i): pass |
| 2120 |
""", |
| 2121 |
test_foobar=""" |
| 2122 |
import pytest |
| 2123 |
@pytest.mark.parametrize('i', range(5)) |
| 2124 |
def test_foobar(i): pass |
| 2125 |
""", |
| 2126 |
) |
| 2127 |
|
| 2128 |
@pytest.fixture |
| 2129 |
def more_tests_files(self, pytester: Pytester) -> None: |
| 2130 |
pytester.makepyfile( |
| 2131 |
test_bar=""" |
| 2132 |
import pytest |
| 2133 |
@pytest.mark.parametrize('i', range(30)) |
| 2134 |
def test_bar(i): pass |
| 2135 |
""", |
| 2136 |
test_foo=""" |
| 2137 |
import pytest |
| 2138 |
@pytest.mark.parametrize('i', range(5)) |
| 2139 |
def test_foo(i): pass |
| 2140 |
""", |
| 2141 |
) |
| 2142 |
|
| 2143 |
def test_zero_tests_collected(self, pytester: Pytester) -> None: |
| 2144 |
"""Some plugins (testmon for example) might issue pytest_runtest_logreport without any tests being |
| 2145 |
actually collected (#2971).""" |
| 2146 |
pytester.makeconftest( |
| 2147 |
""" |
| 2148 |
def pytest_collection_modifyitems(items, config): |
| 2149 |
from _pytest.runner import CollectReport |
| 2150 |
for node_id in ('nodeid1', 'nodeid2'): |
| 2151 |
rep = CollectReport(node_id, 'passed', None, None) |
| 2152 |
rep.when = 'passed' |
| 2153 |
rep.duration = 0.1 |
| 2154 |
config.hook.pytest_runtest_logreport(report=rep) |
| 2155 |
""" |
| 2156 |
) |
| 2157 |
output = pytester.runpytest() |
| 2158 |
output.stdout.no_fnmatch_line("*ZeroDivisionError*") |
| 2159 |
output.stdout.fnmatch_lines(["=* 2 passed in *="]) |
| 2160 |
|
| 2161 |
def test_normal(self, many_tests_files, pytester: Pytester) -> None: |
| 2162 |
output = pytester.runpytest() |
| 2163 |
output.stdout.re_match_lines( |
| 2164 |
[ |
| 2165 |
r"test_bar.py \.{10} \s+ \[ 50%\]", |
| 2166 |
r"test_foo.py \.{5} \s+ \[ 75%\]", |
| 2167 |
r"test_foobar.py \.{5} \s+ \[100%\]", |
| 2168 |
] |
| 2169 |
) |
| 2170 |
|
| 2171 |
def test_colored_progress( |
| 2172 |
self, pytester: Pytester, monkeypatch, color_mapping |
| 2173 |
) -> None: |
| 2174 |
monkeypatch.setenv("PY_COLORS", "1") |
| 2175 |
pytester.makepyfile( |
| 2176 |
test_axfail=""" |
| 2177 |
import pytest |
| 2178 |
@pytest.mark.xfail |
| 2179 |
def test_axfail(): assert 0 |
| 2180 |
""", |
| 2181 |
test_bar=""" |
| 2182 |
import pytest |
| 2183 |
@pytest.mark.parametrize('i', range(10)) |
| 2184 |
def test_bar(i): pass |
| 2185 |
""", |
| 2186 |
test_foo=""" |
| 2187 |
import pytest |
| 2188 |
import warnings |
| 2189 |
@pytest.mark.parametrize('i', range(5)) |
| 2190 |
def test_foo(i): |
| 2191 |
warnings.warn(DeprecationWarning("collection")) |
| 2192 |
pass |
| 2193 |
""", |
| 2194 |
test_foobar=""" |
| 2195 |
import pytest |
| 2196 |
@pytest.mark.parametrize('i', range(5)) |
| 2197 |
def test_foobar(i): raise ValueError() |
| 2198 |
""", |
| 2199 |
) |
| 2200 |
result = pytester.runpytest() |
| 2201 |
result.stdout.re_match_lines( |
| 2202 |
color_mapping.format_for_rematch( |
| 2203 |
[ |
| 2204 |
r"test_axfail.py {yellow}x{reset}{green} \s+ \[ 4%\]{reset}", |
| 2205 |
r"test_bar.py ({green}\.{reset}){{10}}{green} \s+ \[ 52%\]{reset}", |
| 2206 |
r"test_foo.py ({green}\.{reset}){{5}}{yellow} \s+ \[ 76%\]{reset}", |
| 2207 |
r"test_foobar.py ({red}F{reset}){{5}}{red} \s+ \[100%\]{reset}", |
| 2208 |
] |
| 2209 |
) |
| 2210 |
) |
| 2211 |
|
| 2212 |
# Only xfail should have yellow progress indicator. |
| 2213 |
result = pytester.runpytest("test_axfail.py") |
| 2214 |
result.stdout.re_match_lines( |
| 2215 |
color_mapping.format_for_rematch( |
| 2216 |
[ |
| 2217 |
r"test_axfail.py {yellow}x{reset}{yellow} \s+ \[100%\]{reset}", |
| 2218 |
r"^{yellow}=+ ({yellow}{bold}|{bold}{yellow})1 xfailed{reset}{yellow} in ", |
| 2219 |
] |
| 2220 |
) |
| 2221 |
) |
| 2222 |
|
| 2223 |
def test_count(self, many_tests_files, pytester: Pytester) -> None: |
| 2224 |
pytester.makeini( |
| 2225 |
""" |
| 2226 |
[pytest] |
| 2227 |
console_output_style = count |
| 2228 |
""" |
| 2229 |
) |
| 2230 |
output = pytester.runpytest() |
| 2231 |
output.stdout.re_match_lines( |
| 2232 |
[ |
| 2233 |
r"test_bar.py \.{10} \s+ \[10/20\]", |
| 2234 |
r"test_foo.py \.{5} \s+ \[15/20\]", |
| 2235 |
r"test_foobar.py \.{5} \s+ \[20/20\]", |
| 2236 |
] |
| 2237 |
) |
| 2238 |
|
| 2239 |
def test_times(self, many_tests_files, pytester: Pytester) -> None: |
| 2240 |
pytester.makeini( |
| 2241 |
""" |
| 2242 |
[pytest] |
| 2243 |
console_output_style = times |
| 2244 |
""" |
| 2245 |
) |
| 2246 |
output = pytester.runpytest() |
| 2247 |
output.stdout.re_match_lines( |
| 2248 |
[ |
| 2249 |
r"test_bar.py \.{10} \s+ \d{1,3}[\.[a-z\ ]{1,2}\d{0,3}\w{1,2}$", |
| 2250 |
r"test_foo.py \.{5} \s+ \d{1,3}[\.[a-z\ ]{1,2}\d{0,3}\w{1,2}$", |
| 2251 |
r"test_foobar.py \.{5} \s+ \d{1,3}[\.[a-z\ ]{1,2}\d{0,3}\w{1,2}$", |
| 2252 |
] |
| 2253 |
) |
| 2254 |
|
| 2255 |
def test_times_multiline( |
| 2256 |
self, more_tests_files, monkeypatch, pytester: Pytester |
| 2257 |
) -> None: |
| 2258 |
monkeypatch.setenv("COLUMNS", "40") |
| 2259 |
pytester.makeini( |
| 2260 |
""" |
| 2261 |
[pytest] |
| 2262 |
console_output_style = times |
| 2263 |
""" |
| 2264 |
) |
| 2265 |
output = pytester.runpytest() |
| 2266 |
output.stdout.re_match_lines( |
| 2267 |
[ |
| 2268 |
r"test_bar.py ...................", |
| 2269 |
r"........... \s+ \d{1,4}[\.[a-z\ ]{1,2}\d{0,3}\w{1,2}$", |
| 2270 |
r"test_foo.py \.{5} \s+ \d{1,4}[\.[a-z\ ]{1,2}\d{0,3}\w{1,2}$", |
| 2271 |
], |
| 2272 |
consecutive=True, |
| 2273 |
) |
| 2274 |
|
| 2275 |
def test_times_none_collected(self, pytester: Pytester) -> None: |
| 2276 |
pytester.makeini( |
| 2277 |
""" |
| 2278 |
[pytest] |
| 2279 |
console_output_style = times |
| 2280 |
""" |
| 2281 |
) |
| 2282 |
output = pytester.runpytest() |
| 2283 |
assert output.ret == ExitCode.NO_TESTS_COLLECTED |
| 2284 |
|
| 2285 |
def test_verbose(self, many_tests_files, pytester: Pytester) -> None: |
| 2286 |
output = pytester.runpytest("-v") |
| 2287 |
output.stdout.re_match_lines( |
| 2288 |
[ |
| 2289 |
r"test_bar.py::test_bar\[0\] PASSED \s+ \[ 5%\]", |
| 2290 |
r"test_foo.py::test_foo\[4\] PASSED \s+ \[ 75%\]", |
| 2291 |
r"test_foobar.py::test_foobar\[4\] PASSED \s+ \[100%\]", |
| 2292 |
] |
| 2293 |
) |
| 2294 |
|
| 2295 |
def test_verbose_count(self, many_tests_files, pytester: Pytester) -> None: |
| 2296 |
pytester.makeini( |
| 2297 |
""" |
| 2298 |
[pytest] |
| 2299 |
console_output_style = count |
| 2300 |
""" |
| 2301 |
) |
| 2302 |
output = pytester.runpytest("-v") |
| 2303 |
output.stdout.re_match_lines( |
| 2304 |
[ |
| 2305 |
r"test_bar.py::test_bar\[0\] PASSED \s+ \[ 1/20\]", |
| 2306 |
r"test_foo.py::test_foo\[4\] PASSED \s+ \[15/20\]", |
| 2307 |
r"test_foobar.py::test_foobar\[4\] PASSED \s+ \[20/20\]", |
| 2308 |
] |
| 2309 |
) |
| 2310 |
|
| 2311 |
def test_verbose_times(self, many_tests_files, pytester: Pytester) -> None: |
| 2312 |
pytester.makeini( |
| 2313 |
""" |
| 2314 |
[pytest] |
| 2315 |
console_output_style = times |
| 2316 |
""" |
| 2317 |
) |
| 2318 |
output = pytester.runpytest("-v") |
| 2319 |
output.stdout.re_match_lines( |
| 2320 |
[ |
| 2321 |
r"test_bar.py::test_bar\[0\] PASSED \s+ \d{1,3}[\.[a-z\ ]{1,2}\d{0,3}\w{1,2}$", |
| 2322 |
r"test_foo.py::test_foo\[4\] PASSED \s+ \d{1,3}[\.[a-z\ ]{1,2}\d{0,3}\w{1,2}$", |
| 2323 |
r"test_foobar.py::test_foobar\[4\] PASSED \s+ \d{1,3}[\.[a-z\ ]{1,2}\d{0,3}\w{1,2}$", |
| 2324 |
] |
| 2325 |
) |
| 2326 |
|
| 2327 |
def test_xdist_normal( |
| 2328 |
self, many_tests_files, pytester: Pytester, monkeypatch |
| 2329 |
) -> None: |
| 2330 |
pytest.importorskip("xdist") |
| 2331 |
monkeypatch.delenv("PYTEST_DISABLE_PLUGIN_AUTOLOAD", raising=False) |
| 2332 |
output = pytester.runpytest("-n2") |
| 2333 |
output.stdout.re_match_lines([r"\.{20} \s+ \[100%\]"]) |
| 2334 |
|
| 2335 |
def test_xdist_normal_count( |
| 2336 |
self, many_tests_files, pytester: Pytester, monkeypatch |
| 2337 |
) -> None: |
| 2338 |
pytest.importorskip("xdist") |
| 2339 |
monkeypatch.delenv("PYTEST_DISABLE_PLUGIN_AUTOLOAD", raising=False) |
| 2340 |
pytester.makeini( |
| 2341 |
""" |
| 2342 |
[pytest] |
| 2343 |
console_output_style = count |
| 2344 |
""" |
| 2345 |
) |
| 2346 |
output = pytester.runpytest("-n2") |
| 2347 |
output.stdout.re_match_lines([r"\.{20} \s+ \[20/20\]"]) |
| 2348 |
|
| 2349 |
def test_xdist_verbose( |
| 2350 |
self, many_tests_files, pytester: Pytester, monkeypatch |
| 2351 |
) -> None: |
| 2352 |
pytest.importorskip("xdist") |
| 2353 |
monkeypatch.delenv("PYTEST_DISABLE_PLUGIN_AUTOLOAD", raising=False) |
| 2354 |
output = pytester.runpytest("-n2", "-v") |
| 2355 |
output.stdout.re_match_lines_random( |
| 2356 |
[ |
| 2357 |
r"\[gw\d\] \[\s*\d+%\] PASSED test_bar.py::test_bar\[1\]", |
| 2358 |
r"\[gw\d\] \[\s*\d+%\] PASSED test_foo.py::test_foo\[1\]", |
| 2359 |
r"\[gw\d\] \[\s*\d+%\] PASSED test_foobar.py::test_foobar\[1\]", |
| 2360 |
] |
| 2361 |
) |
| 2362 |
output.stdout.fnmatch_lines_random( |
| 2363 |
[ |
| 2364 |
line.translate(TRANS_FNMATCH) |
| 2365 |
for line in [ |
| 2366 |
"test_bar.py::test_bar[0] ", |
| 2367 |
"test_foo.py::test_foo[0] ", |
| 2368 |
"test_foobar.py::test_foobar[0] ", |
| 2369 |
"[gw?] [ 5%] PASSED test_*[?] ", |
| 2370 |
"[gw?] [ 10%] PASSED test_*[?] ", |
| 2371 |
"[gw?] [ 55%] PASSED test_*[?] ", |
| 2372 |
"[gw?] [ 60%] PASSED test_*[?] ", |
| 2373 |
"[gw?] [ 95%] PASSED test_*[?] ", |
| 2374 |
"[gw?] [100%] PASSED test_*[?] ", |
| 2375 |
] |
| 2376 |
] |
| 2377 |
) |
| 2378 |
|
| 2379 |
def test_xdist_times( |
| 2380 |
self, many_tests_files, pytester: Pytester, monkeypatch |
| 2381 |
) -> None: |
| 2382 |
pytest.importorskip("xdist") |
| 2383 |
monkeypatch.delenv("PYTEST_DISABLE_PLUGIN_AUTOLOAD", raising=False) |
| 2384 |
pytester.makeini( |
| 2385 |
""" |
| 2386 |
[pytest] |
| 2387 |
console_output_style = times |
| 2388 |
""" |
| 2389 |
) |
| 2390 |
output = pytester.runpytest("-n2", "-v") |
| 2391 |
output.stdout.re_match_lines_random( |
| 2392 |
[ |
| 2393 |
r"\[gw\d\] \d{1,3}[\.[a-z\ ]{1,2}\d{0,3}\w{1,2} PASSED test_bar.py::test_bar\[1\]", |
| 2394 |
r"\[gw\d\] \d{1,3}[\.[a-z\ ]{1,2}\d{0,3}\w{1,2} PASSED test_foo.py::test_foo\[1\]", |
| 2395 |
r"\[gw\d\] \d{1,3}[\.[a-z\ ]{1,2}\d{0,3}\w{1,2} PASSED test_foobar.py::test_foobar\[1\]", |
| 2396 |
] |
| 2397 |
) |
| 2398 |
|
| 2399 |
def test_capture_no(self, many_tests_files, pytester: Pytester) -> None: |
| 2400 |
output = pytester.runpytest("-s") |
| 2401 |
output.stdout.re_match_lines( |
| 2402 |
[r"test_bar.py \.{10}", r"test_foo.py \.{5}", r"test_foobar.py \.{5}"] |
| 2403 |
) |
| 2404 |
|
| 2405 |
output = pytester.runpytest("--capture=no") |
| 2406 |
output.stdout.no_fnmatch_line("*%]*") |
| 2407 |
|
| 2408 |
def test_capture_no_progress_enabled( |
| 2409 |
self, many_tests_files, pytester: Pytester |
| 2410 |
) -> None: |
| 2411 |
pytester.makeini( |
| 2412 |
""" |
| 2413 |
[pytest] |
| 2414 |
console_output_style = progress-even-when-capture-no |
| 2415 |
""" |
| 2416 |
) |
| 2417 |
output = pytester.runpytest("-s") |
| 2418 |
output.stdout.re_match_lines( |
| 2419 |
[ |
| 2420 |
r"test_bar.py \.{10} \s+ \[ 50%\]", |
| 2421 |
r"test_foo.py \.{5} \s+ \[ 75%\]", |
| 2422 |
r"test_foobar.py \.{5} \s+ \[100%\]", |
| 2423 |
] |
| 2424 |
) |
| 2425 |
|
| 2426 |
|
| 2427 |
class TestProgressWithTeardown: |
| 2428 |
"""Ensure we show the correct percentages for tests that fail during teardown (#3088)""" |
| 2429 |
|
| 2430 |
@pytest.fixture |
| 2431 |
def contest_with_teardown_fixture(self, pytester: Pytester) -> None: |
| 2432 |
pytester.makeconftest( |
| 2433 |
""" |
| 2434 |
import pytest |
| 2435 |
|
| 2436 |
@pytest.fixture |
| 2437 |
def fail_teardown(): |
| 2438 |
yield |
| 2439 |
assert False |
| 2440 |
""" |
| 2441 |
) |
| 2442 |
|
| 2443 |
@pytest.fixture |
| 2444 |
def many_files(self, pytester: Pytester, contest_with_teardown_fixture) -> None: |
| 2445 |
pytester.makepyfile( |
| 2446 |
test_bar=""" |
| 2447 |
import pytest |
| 2448 |
@pytest.mark.parametrize('i', range(5)) |
| 2449 |
def test_bar(fail_teardown, i): |
| 2450 |
pass |
| 2451 |
""", |
| 2452 |
test_foo=""" |
| 2453 |
import pytest |
| 2454 |
@pytest.mark.parametrize('i', range(15)) |
| 2455 |
def test_foo(fail_teardown, i): |
| 2456 |
pass |
| 2457 |
""", |
| 2458 |
) |
| 2459 |
|
| 2460 |
def test_teardown_simple( |
| 2461 |
self, pytester: Pytester, contest_with_teardown_fixture |
| 2462 |
) -> None: |
| 2463 |
pytester.makepyfile( |
| 2464 |
""" |
| 2465 |
def test_foo(fail_teardown): |
| 2466 |
pass |
| 2467 |
""" |
| 2468 |
) |
| 2469 |
output = pytester.runpytest() |
| 2470 |
output.stdout.re_match_lines([r"test_teardown_simple.py \.E\s+\[100%\]"]) |
| 2471 |
|
| 2472 |
def test_teardown_with_test_also_failing( |
| 2473 |
self, pytester: Pytester, contest_with_teardown_fixture |
| 2474 |
) -> None: |
| 2475 |
pytester.makepyfile( |
| 2476 |
""" |
| 2477 |
def test_foo(fail_teardown): |
| 2478 |
assert 0 |
| 2479 |
""" |
| 2480 |
) |
| 2481 |
output = pytester.runpytest("-rfE") |
| 2482 |
output.stdout.re_match_lines( |
| 2483 |
[ |
| 2484 |
r"test_teardown_with_test_also_failing.py FE\s+\[100%\]", |
| 2485 |
"FAILED test_teardown_with_test_also_failing.py::test_foo - assert 0", |
| 2486 |
"ERROR test_teardown_with_test_also_failing.py::test_foo - assert False", |
| 2487 |
] |
| 2488 |
) |
| 2489 |
|
| 2490 |
def test_teardown_many(self, pytester: Pytester, many_files) -> None: |
| 2491 |
output = pytester.runpytest() |
| 2492 |
output.stdout.re_match_lines( |
| 2493 |
[r"test_bar.py (\.E){5}\s+\[ 25%\]", r"test_foo.py (\.E){15}\s+\[100%\]"] |
| 2494 |
) |
| 2495 |
|
| 2496 |
def test_teardown_many_verbose( |
| 2497 |
self, pytester: Pytester, many_files, color_mapping |
| 2498 |
) -> None: |
| 2499 |
result = pytester.runpytest("-v") |
| 2500 |
result.stdout.fnmatch_lines( |
| 2501 |
color_mapping.format_for_fnmatch( |
| 2502 |
[ |
| 2503 |
"test_bar.py::test_bar[0] PASSED * [ 5%]", |
| 2504 |
"test_bar.py::test_bar[0] ERROR * [ 5%]", |
| 2505 |
"test_bar.py::test_bar[4] PASSED * [ 25%]", |
| 2506 |
"test_foo.py::test_foo[14] PASSED * [100%]", |
| 2507 |
"test_foo.py::test_foo[14] ERROR * [100%]", |
| 2508 |
"=* 20 passed, 20 errors in *", |
| 2509 |
] |
| 2510 |
) |
| 2511 |
) |
| 2512 |
|
| 2513 |
def test_xdist_normal(self, many_files, pytester: Pytester, monkeypatch) -> None: |
| 2514 |
pytest.importorskip("xdist") |
| 2515 |
monkeypatch.delenv("PYTEST_DISABLE_PLUGIN_AUTOLOAD", raising=False) |
| 2516 |
output = pytester.runpytest("-n2") |
| 2517 |
output.stdout.re_match_lines([r"[\.E]{40} \s+ \[100%\]"]) |
| 2518 |
|
| 2519 |
|
| 2520 |
def test_skip_reasons_folding() -> None: |
| 2521 |
path = "xyz" |
| 2522 |
lineno = 3 |
| 2523 |
message = "justso" |
| 2524 |
longrepr = (path, lineno, message) |
| 2525 |
|
| 2526 |
class X: |
| 2527 |
pass |
| 2528 |
|
| 2529 |
ev1 = cast(CollectReport, X()) |
| 2530 |
ev1.when = "execute" |
| 2531 |
ev1.skipped = True # type: ignore[misc] |
| 2532 |
ev1.longrepr = longrepr |
| 2533 |
|
| 2534 |
ev2 = cast(CollectReport, X()) |
| 2535 |
ev2.when = "execute" |
| 2536 |
ev2.longrepr = longrepr |
| 2537 |
ev2.skipped = True # type: ignore[misc] |
| 2538 |
|
| 2539 |
# ev3 might be a collection report |
| 2540 |
ev3 = cast(CollectReport, X()) |
| 2541 |
ev3.when = "collect" |
| 2542 |
ev3.longrepr = longrepr |
| 2543 |
ev3.skipped = True # type: ignore[misc] |
| 2544 |
|
| 2545 |
values = _folded_skips(Path.cwd(), [ev1, ev2, ev3]) |
| 2546 |
assert len(values) == 1 |
| 2547 |
num, fspath, lineno_, reason = values[0] |
| 2548 |
assert num == 3 |
| 2549 |
assert fspath == path |
| 2550 |
assert lineno_ == lineno |
| 2551 |
assert reason == message |
| 2552 |
|
| 2553 |
|
| 2554 |
def test_line_with_reprcrash(monkeypatch: MonkeyPatch) -> None: |
| 2555 |
mocked_verbose_word = "FAILED" |
| 2556 |
|
| 2557 |
mocked_pos = "some::nodeid" |
| 2558 |
|
| 2559 |
def mock_get_pos(*args): |
| 2560 |
return mocked_pos |
| 2561 |
|
| 2562 |
monkeypatch.setattr(_pytest.terminal, "_get_node_id_with_markup", mock_get_pos) |
| 2563 |
|
| 2564 |
class Namespace: |
| 2565 |
def __init__(self, **kwargs): |
| 2566 |
self.__dict__.update(kwargs) |
| 2567 |
|
| 2568 |
class config: |
| 2569 |
def __init__(self): |
| 2570 |
self.option = Namespace(verbose=0) |
| 2571 |
|
| 2572 |
class rep: |
| 2573 |
def _get_verbose_word_with_markup(self, *args): |
| 2574 |
return mocked_verbose_word, {} |
| 2575 |
|
| 2576 |
class longrepr: |
| 2577 |
class reprcrash: |
| 2578 |
pass |
| 2579 |
|
| 2580 |
def check(msg, width, expected): |
| 2581 |
class DummyTerminalWriter: |
| 2582 |
fullwidth = width |
| 2583 |
|
| 2584 |
def markup(self, word: str, **markup: str): |
| 2585 |
return word |
| 2586 |
|
| 2587 |
__tracebackhide__ = True |
| 2588 |
if msg: |
| 2589 |
rep.longrepr.reprcrash.message = msg # type: ignore |
| 2590 |
actual = _get_line_with_reprcrash_message( |
| 2591 |
config(), # type: ignore[arg-type] |
| 2592 |
rep(), # type: ignore[arg-type] |
| 2593 |
DummyTerminalWriter(), # type: ignore[arg-type] |
| 2594 |
{}, |
| 2595 |
) |
| 2596 |
|
| 2597 |
assert actual == expected |
| 2598 |
if actual != f"{mocked_verbose_word} {mocked_pos}": |
| 2599 |
assert len(actual) <= width |
| 2600 |
assert wcswidth(actual) <= width |
| 2601 |
|
| 2602 |
# AttributeError with message |
| 2603 |
check(None, 80, "FAILED some::nodeid") |
| 2604 |
|
| 2605 |
check("msg", 80, "FAILED some::nodeid - msg") |
| 2606 |
check("msg", 3, "FAILED some::nodeid") |
| 2607 |
|
| 2608 |
check("msg", 24, "FAILED some::nodeid") |
| 2609 |
check("msg", 25, "FAILED some::nodeid - msg") |
| 2610 |
|
| 2611 |
check("some longer msg", 24, "FAILED some::nodeid") |
| 2612 |
check("some longer msg", 25, "FAILED some::nodeid - ...") |
| 2613 |
check("some longer msg", 26, "FAILED some::nodeid - s...") |
| 2614 |
|
| 2615 |
check("some\nmessage", 25, "FAILED some::nodeid - ...") |
| 2616 |
check("some\nmessage", 26, "FAILED some::nodeid - some") |
| 2617 |
check("some\nmessage", 80, "FAILED some::nodeid - some") |
| 2618 |
|
| 2619 |
# Test unicode safety. |
| 2620 |
check("🉐🉐🉐🉐🉐\n2nd line", 25, "FAILED some::nodeid - ...") |
| 2621 |
check("🉐🉐🉐🉐🉐\n2nd line", 26, "FAILED some::nodeid - ...") |
| 2622 |
check("🉐🉐🉐🉐🉐\n2nd line", 27, "FAILED some::nodeid - 🉐...") |
| 2623 |
check("🉐🉐🉐🉐🉐\n2nd line", 28, "FAILED some::nodeid - 🉐...") |
| 2624 |
check("🉐🉐🉐🉐🉐\n2nd line", 29, "FAILED some::nodeid - 🉐🉐...") |
| 2625 |
|
| 2626 |
# NOTE: constructed, not sure if this is supported. |
| 2627 |
mocked_pos = "nodeid::🉐::withunicode" |
| 2628 |
check("🉐🉐🉐🉐🉐\n2nd line", 29, "FAILED nodeid::🉐::withunicode") |
| 2629 |
check("🉐🉐🉐🉐🉐\n2nd line", 40, "FAILED nodeid::🉐::withunicode - 🉐🉐...") |
| 2630 |
check("🉐🉐🉐🉐🉐\n2nd line", 41, "FAILED nodeid::🉐::withunicode - 🉐🉐...") |
| 2631 |
check("🉐🉐🉐🉐🉐\n2nd line", 42, "FAILED nodeid::🉐::withunicode - 🉐🉐🉐...") |
| 2632 |
check("🉐🉐🉐🉐🉐\n2nd line", 80, "FAILED nodeid::🉐::withunicode - 🉐🉐🉐🉐🉐") |
| 2633 |
|
| 2634 |
|
| 2635 |
def test_short_summary_with_verbose( |
| 2636 |
monkeypatch: MonkeyPatch, pytester: Pytester |
| 2637 |
) -> None: |
| 2638 |
"""With -vv do not truncate the summary info (#11777).""" |
| 2639 |
# On CI we also do not truncate the summary info, monkeypatch it to ensure we |
| 2640 |
# are testing against the -vv flag on CI. |
| 2641 |
monkeypatch.setattr(_pytest.terminal, "running_on_ci", lambda: False) |
| 2642 |
|
| 2643 |
string_length = 200 |
| 2644 |
pytester.makepyfile( |
| 2645 |
f""" |
| 2646 |
def test(): |
| 2647 |
s1 = "A" * {string_length} |
| 2648 |
s2 = "B" * {string_length} |
| 2649 |
assert s1 == s2 |
| 2650 |
""" |
| 2651 |
) |
| 2652 |
|
| 2653 |
# No -vv, summary info should be truncated. |
| 2654 |
result = pytester.runpytest() |
| 2655 |
result.stdout.fnmatch_lines( |
| 2656 |
[ |
| 2657 |
"*short test summary info*", |
| 2658 |
"* assert 'AAA...", |
| 2659 |
], |
| 2660 |
) |
| 2661 |
|
| 2662 |
# No truncation with -vv. |
| 2663 |
result = pytester.runpytest("-vv") |
| 2664 |
result.stdout.fnmatch_lines( |
| 2665 |
[ |
| 2666 |
"*short test summary info*", |
| 2667 |
f"*{'A' * string_length}*{'B' * string_length}'", |
| 2668 |
] |
| 2669 |
) |
| 2670 |
|
| 2671 |
|
| 2672 |
def test_full_sequence_print_with_vv( |
| 2673 |
monkeypatch: MonkeyPatch, pytester: Pytester |
| 2674 |
) -> None: |
| 2675 |
"""Do not truncate sequences in summaries with -vv (#11777).""" |
| 2676 |
monkeypatch.setattr(_pytest.terminal, "running_on_ci", lambda: False) |
| 2677 |
|
| 2678 |
pytester.makepyfile( |
| 2679 |
""" |
| 2680 |
def test_len_list(): |
| 2681 |
l = list(range(10)) |
| 2682 |
assert len(l) == 9 |
| 2683 |
|
| 2684 |
def test_len_dict(): |
| 2685 |
d = dict(zip(range(10), range(10))) |
| 2686 |
assert len(d) == 9 |
| 2687 |
""" |
| 2688 |
) |
| 2689 |
|
| 2690 |
result = pytester.runpytest("-vv") |
| 2691 |
assert result.ret == 1 |
| 2692 |
result.stdout.fnmatch_lines( |
| 2693 |
[ |
| 2694 |
"*short test summary info*", |
| 2695 |
f"*{list(range(10))}*", |
| 2696 |
f"*{dict(zip(range(10), range(10), strict=True))}*", |
| 2697 |
] |
| 2698 |
) |
| 2699 |
|
| 2700 |
|
| 2701 |
def test_force_short_summary(monkeypatch: MonkeyPatch, pytester: Pytester) -> None: |
| 2702 |
monkeypatch.setattr(_pytest.terminal, "running_on_ci", lambda: False) |
| 2703 |
|
| 2704 |
pytester.makepyfile( |
| 2705 |
""" |
| 2706 |
def test(): |
| 2707 |
assert "a\\n" * 10 == "" |
| 2708 |
""" |
| 2709 |
) |
| 2710 |
|
| 2711 |
result = pytester.runpytest("-vv", "--force-short-summary") |
| 2712 |
assert result.ret == 1 |
| 2713 |
result.stdout.fnmatch_lines( |
| 2714 |
["*short test summary info*", "*AssertionError: assert 'a\\na\\na\\na..."] |
| 2715 |
) |
| 2716 |
|
| 2717 |
|
| 2718 |
@pytest.mark.parametrize( |
| 2719 |
"seconds, expected", |
| 2720 |
[ |
| 2721 |
(10.0, "10.00s"), |
| 2722 |
(10.34, "10.34s"), |
| 2723 |
(59.99, "59.99s"), |
| 2724 |
(60.55, "60.55s (0:01:00)"), |
| 2725 |
(123.55, "123.55s (0:02:03)"), |
| 2726 |
(60 * 60 + 0.5, "3600.50s (1:00:00)"), |
| 2727 |
], |
| 2728 |
) |
| 2729 |
def test_format_session_duration(seconds, expected): |
| 2730 |
from _pytest.terminal import format_session_duration |
| 2731 |
|
| 2732 |
assert format_session_duration(seconds) == expected |
| 2733 |
|
| 2734 |
|
| 2735 |
@pytest.mark.parametrize( |
| 2736 |
"seconds, expected", |
| 2737 |
[ |
| 2738 |
(3600 * 100 - 60, " 99h 59m"), |
| 2739 |
(31 * 60 - 1, " 30m 59s"), |
| 2740 |
(10.1236, " 10.124s"), |
| 2741 |
(9.1236, " 9.124s"), |
| 2742 |
(0.1236, " 123.6ms"), |
| 2743 |
(0.01236, " 12.36ms"), |
| 2744 |
(0.001236, " 1.236ms"), |
| 2745 |
(0.0001236, " 123.6us"), |
| 2746 |
(0.00001236, " 12.36us"), |
| 2747 |
(0.000001236, " 1.236us"), |
| 2748 |
], |
| 2749 |
) |
| 2750 |
def test_format_node_duration(seconds: float, expected: str) -> None: |
| 2751 |
from _pytest.terminal import format_node_duration |
| 2752 |
|
| 2753 |
assert format_node_duration(seconds) == expected |
| 2754 |
|
| 2755 |
|
| 2756 |
def test_collecterror(pytester: Pytester) -> None: |
| 2757 |
p1 = pytester.makepyfile("raise SyntaxError()") |
| 2758 |
result = pytester.runpytest("-ra", str(p1)) |
| 2759 |
result.stdout.fnmatch_lines( |
| 2760 |
[ |
| 2761 |
"collected 0 items / 1 error", |
| 2762 |
"*= ERRORS =*", |
| 2763 |
"*_ ERROR collecting test_collecterror.py _*", |
| 2764 |
"E SyntaxError: *", |
| 2765 |
"*= short test summary info =*", |
| 2766 |
"ERROR test_collecterror.py", |
| 2767 |
"*! Interrupted: 1 error during collection !*", |
| 2768 |
"*= 1 error in *", |
| 2769 |
] |
| 2770 |
) |
| 2771 |
|
| 2772 |
|
| 2773 |
def test_no_summary_collecterror(pytester: Pytester) -> None: |
| 2774 |
p1 = pytester.makepyfile("raise SyntaxError()") |
| 2775 |
result = pytester.runpytest("-ra", "--no-summary", str(p1)) |
| 2776 |
result.stdout.no_fnmatch_line("*= ERRORS =*") |
| 2777 |
|
| 2778 |
|
| 2779 |
def test_via_exec(pytester: Pytester) -> None: |
| 2780 |
p1 = pytester.makepyfile("exec('def test_via_exec(): pass')") |
| 2781 |
result = pytester.runpytest(str(p1), "-vv") |
| 2782 |
result.stdout.fnmatch_lines( |
| 2783 |
["test_via_exec.py::test_via_exec <- <string> PASSED*", "*= 1 passed in *"] |
| 2784 |
) |
| 2785 |
|
| 2786 |
|
| 2787 |
class TestCodeHighlight: |
| 2788 |
def test_code_highlight_simple(self, pytester: Pytester, color_mapping) -> None: |
| 2789 |
pytester.makepyfile( |
| 2790 |
""" |
| 2791 |
def test_foo(): |
| 2792 |
assert 1 == 10 |
| 2793 |
""" |
| 2794 |
) |
| 2795 |
result = pytester.runpytest("--color=yes") |
| 2796 |
result.stdout.fnmatch_lines( |
| 2797 |
color_mapping.format_for_fnmatch( |
| 2798 |
[ |
| 2799 |
" {reset}{kw}def{hl-reset}{kwspace}{function}test_foo{hl-reset}():{endline}", |
| 2800 |
"> {kw}assert{hl-reset} {number}1{hl-reset} == {number}10{hl-reset}{endline}", |
| 2801 |
"{bold}{red}E assert 1 == 10{reset}", |
| 2802 |
] |
| 2803 |
) |
| 2804 |
) |
| 2805 |
|
| 2806 |
def test_code_highlight_continuation( |
| 2807 |
self, pytester: Pytester, color_mapping |
| 2808 |
) -> None: |
| 2809 |
pytester.makepyfile( |
| 2810 |
""" |
| 2811 |
def test_foo(): |
| 2812 |
print(''' |
| 2813 |
'''); assert 0 |
| 2814 |
""" |
| 2815 |
) |
| 2816 |
result = pytester.runpytest("--color=yes") |
| 2817 |
|
| 2818 |
result.stdout.fnmatch_lines( |
| 2819 |
color_mapping.format_for_fnmatch( |
| 2820 |
[ |
| 2821 |
" {reset}{kw}def{hl-reset}{kwspace}{function}test_foo{hl-reset}():{endline}", |
| 2822 |
" {print}print{hl-reset}({str}'''{hl-reset}{str}{hl-reset}", |
| 2823 |
"> {str} {hl-reset}{str}'''{hl-reset}); {kw}assert{hl-reset} {number}0{hl-reset}{endline}", |
| 2824 |
"{bold}{red}E assert 0{reset}", |
| 2825 |
] |
| 2826 |
) |
| 2827 |
) |
| 2828 |
|
| 2829 |
def test_code_highlight_custom_theme( |
| 2830 |
self, pytester: Pytester, color_mapping, monkeypatch: MonkeyPatch |
| 2831 |
) -> None: |
| 2832 |
pytester.makepyfile( |
| 2833 |
""" |
| 2834 |
def test_foo(): |
| 2835 |
assert 1 == 10 |
| 2836 |
""" |
| 2837 |
) |
| 2838 |
monkeypatch.setenv("PYTEST_THEME", "solarized-dark") |
| 2839 |
monkeypatch.setenv("PYTEST_THEME_MODE", "dark") |
| 2840 |
result = pytester.runpytest("--color=yes") |
| 2841 |
result.stdout.fnmatch_lines( |
| 2842 |
color_mapping.format_for_fnmatch( |
| 2843 |
[ |
| 2844 |
" {reset}{kw}def{hl-reset}{kwspace}{function}test_foo{hl-reset}():{endline}", |
| 2845 |
"> {kw}assert{hl-reset} {number}1{hl-reset} == {number}10{hl-reset}{endline}", |
| 2846 |
"{bold}{red}E assert 1 == 10{reset}", |
| 2847 |
] |
| 2848 |
) |
| 2849 |
) |
| 2850 |
|
| 2851 |
def test_code_highlight_invalid_theme( |
| 2852 |
self, pytester: Pytester, color_mapping, monkeypatch: MonkeyPatch |
| 2853 |
) -> None: |
| 2854 |
pytester.makepyfile( |
| 2855 |
""" |
| 2856 |
def test_foo(): |
| 2857 |
assert 1 == 10 |
| 2858 |
""" |
| 2859 |
) |
| 2860 |
monkeypatch.setenv("PYTEST_THEME", "invalid") |
| 2861 |
result = pytester.runpytest_subprocess("--color=yes") |
| 2862 |
result.stderr.fnmatch_lines( |
| 2863 |
"ERROR: PYTEST_THEME environment variable has an invalid value: 'invalid'. " |
| 2864 |
"Hint: See available pygments styles with `pygmentize -L styles`." |
| 2865 |
) |
| 2866 |
|
| 2867 |
def test_code_highlight_invalid_theme_mode( |
| 2868 |
self, pytester: Pytester, color_mapping, monkeypatch: MonkeyPatch |
| 2869 |
) -> None: |
| 2870 |
pytester.makepyfile( |
| 2871 |
""" |
| 2872 |
def test_foo(): |
| 2873 |
assert 1 == 10 |
| 2874 |
""" |
| 2875 |
) |
| 2876 |
monkeypatch.setenv("PYTEST_THEME_MODE", "invalid") |
| 2877 |
result = pytester.runpytest_subprocess("--color=yes") |
| 2878 |
result.stderr.fnmatch_lines( |
| 2879 |
"ERROR: PYTEST_THEME_MODE environment variable has an invalid value: 'invalid'. " |
| 2880 |
"The allowed values are 'dark' (default) and 'light'." |
| 2881 |
) |
| 2882 |
|
| 2883 |
|
| 2884 |
def test_raw_skip_reason_skipped() -> None: |
| 2885 |
report = SimpleNamespace() |
| 2886 |
report.skipped = True |
| 2887 |
report.longrepr = ("xyz", 3, "Skipped: Just so") |
| 2888 |
|
| 2889 |
reason = _get_raw_skip_reason(cast(TestReport, report)) |
| 2890 |
assert reason == "Just so" |
| 2891 |
|
| 2892 |
|
| 2893 |
def test_raw_skip_reason_xfail() -> None: |
| 2894 |
report = SimpleNamespace() |
| 2895 |
report.wasxfail = "reason: To everything there is a season" |
| 2896 |
|
| 2897 |
reason = _get_raw_skip_reason(cast(TestReport, report)) |
| 2898 |
assert reason == "To everything there is a season" |
| 2899 |
|
| 2900 |
|
| 2901 |
def test_format_trimmed() -> None: |
| 2902 |
msg = "unconditional skip" |
| 2903 |
|
| 2904 |
assert _format_trimmed(" ({}) ", msg, len(msg) + 4) == " (unconditional skip) " |
| 2905 |
assert _format_trimmed(" ({}) ", msg, len(msg) + 3) == " (unconditional ...) " |
| 2906 |
|
| 2907 |
|
| 2908 |
def test_warning_when_init_trumps_pyproject_toml( |
| 2909 |
pytester: Pytester, monkeypatch: MonkeyPatch |
| 2910 |
) -> None: |
| 2911 |
"""Regression test for #7814.""" |
| 2912 |
tests = pytester.path.joinpath("tests") |
| 2913 |
tests.mkdir() |
| 2914 |
pytester.makepyprojecttoml( |
| 2915 |
f""" |
| 2916 |
[tool.pytest.ini_options] |
| 2917 |
testpaths = ['{tests}'] |
| 2918 |
""" |
| 2919 |
) |
| 2920 |
pytester.makefile(".ini", pytest="") |
| 2921 |
result = pytester.runpytest() |
| 2922 |
result.stdout.fnmatch_lines( |
| 2923 |
[ |
| 2924 |
"configfile: pytest.ini (WARNING: ignoring pytest config in pyproject.toml!)", |
| 2925 |
] |
| 2926 |
) |
| 2927 |
|
| 2928 |
|
| 2929 |
def test_warning_when_init_trumps_multiple_files( |
| 2930 |
pytester: Pytester, monkeypatch: MonkeyPatch |
| 2931 |
) -> None: |
| 2932 |
"""Regression test for #7814.""" |
| 2933 |
tests = pytester.path.joinpath("tests") |
| 2934 |
tests.mkdir() |
| 2935 |
pytester.makepyprojecttoml( |
| 2936 |
f""" |
| 2937 |
[tool.pytest.ini_options] |
| 2938 |
testpaths = ['{tests}'] |
| 2939 |
""" |
| 2940 |
) |
| 2941 |
pytester.makefile(".ini", pytest="") |
| 2942 |
pytester.makeini( |
| 2943 |
""" |
| 2944 |
# tox.ini |
| 2945 |
[pytest] |
| 2946 |
minversion = 6.0 |
| 2947 |
addopts = -ra -q |
| 2948 |
testpaths = |
| 2949 |
tests |
| 2950 |
integration |
| 2951 |
""" |
| 2952 |
) |
| 2953 |
result = pytester.runpytest() |
| 2954 |
result.stdout.fnmatch_lines( |
| 2955 |
[ |
| 2956 |
"configfile: pytest.ini (WARNING: ignoring pytest config in pyproject.toml, tox.ini!)", |
| 2957 |
] |
| 2958 |
) |
| 2959 |
|
| 2960 |
|
| 2961 |
def test_no_warning_when_init_but_pyproject_toml_has_no_entry( |
| 2962 |
pytester: Pytester, monkeypatch: MonkeyPatch |
| 2963 |
) -> None: |
| 2964 |
"""Regression test for #7814.""" |
| 2965 |
tests = pytester.path.joinpath("tests") |
| 2966 |
tests.mkdir() |
| 2967 |
pytester.makepyprojecttoml( |
| 2968 |
f""" |
| 2969 |
[tool] |
| 2970 |
testpaths = ['{tests}'] |
| 2971 |
""" |
| 2972 |
) |
| 2973 |
pytester.makefile(".ini", pytest="") |
| 2974 |
result = pytester.runpytest() |
| 2975 |
result.stdout.fnmatch_lines( |
| 2976 |
[ |
| 2977 |
"configfile: pytest.ini", |
| 2978 |
] |
| 2979 |
) |
| 2980 |
|
| 2981 |
|
| 2982 |
def test_no_warning_on_terminal_with_a_single_config_file( |
| 2983 |
pytester: Pytester, monkeypatch: MonkeyPatch |
| 2984 |
) -> None: |
| 2985 |
"""Regression test for #7814.""" |
| 2986 |
tests = pytester.path.joinpath("tests") |
| 2987 |
tests.mkdir() |
| 2988 |
pytester.makepyprojecttoml( |
| 2989 |
f""" |
| 2990 |
[tool.pytest.ini_options] |
| 2991 |
testpaths = ['{tests}'] |
| 2992 |
""" |
| 2993 |
) |
| 2994 |
result = pytester.runpytest() |
| 2995 |
result.stdout.fnmatch_lines( |
| 2996 |
[ |
| 2997 |
"configfile: pyproject.toml", |
| 2998 |
] |
| 2999 |
) |
| 3000 |
|
| 3001 |
|
| 3002 |
class TestFineGrainedTestCase: |
| 3003 |
DEFAULT_FILE_CONTENTS = """ |
| 3004 |
import pytest |
| 3005 |
|
| 3006 |
@pytest.mark.parametrize("i", range(4)) |
| 3007 |
def test_ok(i): |
| 3008 |
''' |
| 3009 |
some docstring |
| 3010 |
''' |
| 3011 |
pass |
| 3012 |
|
| 3013 |
def test_fail(): |
| 3014 |
assert False |
| 3015 |
""" |
| 3016 |
LONG_SKIP_FILE_CONTENTS = """ |
| 3017 |
import pytest |
| 3018 |
|
| 3019 |
@pytest.mark.skip( |
| 3020 |
"some long skip reason that will not fit on a single line with other content that goes" |
| 3021 |
" on and on and on and on and on" |
| 3022 |
) |
| 3023 |
def test_skip(): |
| 3024 |
pass |
| 3025 |
""" |
| 3026 |
|
| 3027 |
@pytest.mark.parametrize("verbosity", [1, 2]) |
| 3028 |
def test_execute_positive(self, verbosity, pytester: Pytester) -> None: |
| 3029 |
# expected: one test case per line (with file name), word describing result |
| 3030 |
p = TestFineGrainedTestCase._initialize_files(pytester, verbosity=verbosity) |
| 3031 |
result = pytester.runpytest(p) |
| 3032 |
|
| 3033 |
result.stdout.fnmatch_lines( |
| 3034 |
[ |
| 3035 |
"collected 5 items", |
| 3036 |
"", |
| 3037 |
f"{p.name}::test_ok[0] PASSED [ 20%]", |
| 3038 |
f"{p.name}::test_ok[1] PASSED [ 40%]", |
| 3039 |
f"{p.name}::test_ok[2] PASSED [ 60%]", |
| 3040 |
f"{p.name}::test_ok[3] PASSED [ 80%]", |
| 3041 |
f"{p.name}::test_fail FAILED [100%]", |
| 3042 |
], |
| 3043 |
consecutive=True, |
| 3044 |
) |
| 3045 |
|
| 3046 |
def test_execute_0_global_1(self, pytester: Pytester) -> None: |
| 3047 |
# expected: one file name per line, single character describing result |
| 3048 |
p = TestFineGrainedTestCase._initialize_files(pytester, verbosity=0) |
| 3049 |
result = pytester.runpytest("-v", p) |
| 3050 |
|
| 3051 |
result.stdout.fnmatch_lines( |
| 3052 |
[ |
| 3053 |
"collecting ... collected 5 items", |
| 3054 |
"", |
| 3055 |
f"{p.name} ....F [100%]", |
| 3056 |
], |
| 3057 |
consecutive=True, |
| 3058 |
) |
| 3059 |
|
| 3060 |
@pytest.mark.parametrize("verbosity", [-1, -2]) |
| 3061 |
def test_execute_negative(self, verbosity, pytester: Pytester) -> None: |
| 3062 |
# expected: single character describing result |
| 3063 |
p = TestFineGrainedTestCase._initialize_files(pytester, verbosity=verbosity) |
| 3064 |
result = pytester.runpytest(p) |
| 3065 |
|
| 3066 |
result.stdout.fnmatch_lines( |
| 3067 |
[ |
| 3068 |
"collected 5 items", |
| 3069 |
"....F [100%]", |
| 3070 |
], |
| 3071 |
consecutive=True, |
| 3072 |
) |
| 3073 |
|
| 3074 |
def test_execute_skipped_positive_2(self, pytester: Pytester) -> None: |
| 3075 |
# expected: one test case per line (with file name), word describing result, full reason |
| 3076 |
p = TestFineGrainedTestCase._initialize_files( |
| 3077 |
pytester, |
| 3078 |
verbosity=2, |
| 3079 |
file_contents=TestFineGrainedTestCase.LONG_SKIP_FILE_CONTENTS, |
| 3080 |
) |
| 3081 |
result = pytester.runpytest(p) |
| 3082 |
|
| 3083 |
result.stdout.fnmatch_lines( |
| 3084 |
[ |
| 3085 |
"collected 1 item", |
| 3086 |
"", |
| 3087 |
f"{p.name}::test_skip SKIPPED (some long skip", |
| 3088 |
"reason that will not fit on a single line with other content that goes", |
| 3089 |
"on and on and on and on and on) [100%]", |
| 3090 |
], |
| 3091 |
consecutive=True, |
| 3092 |
) |
| 3093 |
|
| 3094 |
def test_execute_skipped_positive_1(self, pytester: Pytester) -> None: |
| 3095 |
# expected: one test case per line (with file name), word describing result, reason truncated |
| 3096 |
p = TestFineGrainedTestCase._initialize_files( |
| 3097 |
pytester, |
| 3098 |
verbosity=1, |
| 3099 |
file_contents=TestFineGrainedTestCase.LONG_SKIP_FILE_CONTENTS, |
| 3100 |
) |
| 3101 |
result = pytester.runpytest(p) |
| 3102 |
|
| 3103 |
result.stdout.fnmatch_lines( |
| 3104 |
[ |
| 3105 |
"collected 1 item", |
| 3106 |
"", |
| 3107 |
f"{p.name}::test_skip SKIPPED (some long ski...) [100%]", |
| 3108 |
], |
| 3109 |
consecutive=True, |
| 3110 |
) |
| 3111 |
|
| 3112 |
def test_execute_skipped__0_global_1(self, pytester: Pytester) -> None: |
| 3113 |
# expected: one file name per line, single character describing result (no reason) |
| 3114 |
p = TestFineGrainedTestCase._initialize_files( |
| 3115 |
pytester, |
| 3116 |
verbosity=0, |
| 3117 |
file_contents=TestFineGrainedTestCase.LONG_SKIP_FILE_CONTENTS, |
| 3118 |
) |
| 3119 |
result = pytester.runpytest("-v", p) |
| 3120 |
|
| 3121 |
result.stdout.fnmatch_lines( |
| 3122 |
[ |
| 3123 |
"collecting ... collected 1 item", |
| 3124 |
"", |
| 3125 |
f"{p.name} s [100%]", |
| 3126 |
], |
| 3127 |
consecutive=True, |
| 3128 |
) |
| 3129 |
|
| 3130 |
@pytest.mark.parametrize("verbosity", [-1, -2]) |
| 3131 |
def test_execute_skipped_negative(self, verbosity, pytester: Pytester) -> None: |
| 3132 |
# expected: single character describing result (no reason) |
| 3133 |
p = TestFineGrainedTestCase._initialize_files( |
| 3134 |
pytester, |
| 3135 |
verbosity=verbosity, |
| 3136 |
file_contents=TestFineGrainedTestCase.LONG_SKIP_FILE_CONTENTS, |
| 3137 |
) |
| 3138 |
result = pytester.runpytest(p) |
| 3139 |
|
| 3140 |
result.stdout.fnmatch_lines( |
| 3141 |
[ |
| 3142 |
"collected 1 item", |
| 3143 |
"s [100%]", |
| 3144 |
], |
| 3145 |
consecutive=True, |
| 3146 |
) |
| 3147 |
|
| 3148 |
@pytest.mark.parametrize("verbosity", [1, 2]) |
| 3149 |
def test__collect_only_positive(self, verbosity, pytester: Pytester) -> None: |
| 3150 |
p = TestFineGrainedTestCase._initialize_files(pytester, verbosity=verbosity) |
| 3151 |
result = pytester.runpytest("--collect-only", p) |
| 3152 |
|
| 3153 |
result.stdout.fnmatch_lines( |
| 3154 |
[ |
| 3155 |
"collected 5 items", |
| 3156 |
"", |
| 3157 |
f"<Dir {p.parent.name}>", |
| 3158 |
f" <Module {p.name}>", |
| 3159 |
" <Function test_ok[0]>", |
| 3160 |
" some docstring", |
| 3161 |
" <Function test_ok[1]>", |
| 3162 |
" some docstring", |
| 3163 |
" <Function test_ok[2]>", |
| 3164 |
" some docstring", |
| 3165 |
" <Function test_ok[3]>", |
| 3166 |
" some docstring", |
| 3167 |
" <Function test_fail>", |
| 3168 |
], |
| 3169 |
consecutive=True, |
| 3170 |
) |
| 3171 |
|
| 3172 |
def test_collect_only_0_global_1(self, pytester: Pytester) -> None: |
| 3173 |
p = TestFineGrainedTestCase._initialize_files(pytester, verbosity=0) |
| 3174 |
result = pytester.runpytest("-v", "--collect-only", p) |
| 3175 |
|
| 3176 |
result.stdout.fnmatch_lines( |
| 3177 |
[ |
| 3178 |
"collecting ... collected 5 items", |
| 3179 |
"", |
| 3180 |
f"<Dir {p.parent.name}>", |
| 3181 |
f" <Module {p.name}>", |
| 3182 |
" <Function test_ok[0]>", |
| 3183 |
" <Function test_ok[1]>", |
| 3184 |
" <Function test_ok[2]>", |
| 3185 |
" <Function test_ok[3]>", |
| 3186 |
" <Function test_fail>", |
| 3187 |
], |
| 3188 |
consecutive=True, |
| 3189 |
) |
| 3190 |
|
| 3191 |
def test_collect_only_negative_1(self, pytester: Pytester) -> None: |
| 3192 |
p = TestFineGrainedTestCase._initialize_files(pytester, verbosity=-1) |
| 3193 |
result = pytester.runpytest("--collect-only", p) |
| 3194 |
|
| 3195 |
result.stdout.fnmatch_lines( |
| 3196 |
[ |
| 3197 |
"collected 5 items", |
| 3198 |
"", |
| 3199 |
f"{p.name}::test_ok[0]", |
| 3200 |
f"{p.name}::test_ok[1]", |
| 3201 |
f"{p.name}::test_ok[2]", |
| 3202 |
f"{p.name}::test_ok[3]", |
| 3203 |
f"{p.name}::test_fail", |
| 3204 |
], |
| 3205 |
consecutive=True, |
| 3206 |
) |
| 3207 |
|
| 3208 |
def test_collect_only_negative_2(self, pytester: Pytester) -> None: |
| 3209 |
p = TestFineGrainedTestCase._initialize_files(pytester, verbosity=-2) |
| 3210 |
result = pytester.runpytest("--collect-only", p) |
| 3211 |
|
| 3212 |
result.stdout.fnmatch_lines( |
| 3213 |
[ |
| 3214 |
"collected 5 items", |
| 3215 |
"", |
| 3216 |
f"{p.name}: 5", |
| 3217 |
], |
| 3218 |
consecutive=True, |
| 3219 |
) |
| 3220 |
|
| 3221 |
@staticmethod |
| 3222 |
def _initialize_files( |
| 3223 |
pytester: Pytester, verbosity: int, file_contents: str = DEFAULT_FILE_CONTENTS |
| 3224 |
) -> Path: |
| 3225 |
p = pytester.makepyfile(file_contents) |
| 3226 |
pytester.makeini( |
| 3227 |
f""" |
| 3228 |
[pytest] |
| 3229 |
verbosity_test_cases = {verbosity} |
| 3230 |
""" |
| 3231 |
) |
| 3232 |
return p |
| 3233 |
|
| 3234 |
|
| 3235 |
def test_summary_xfail_reason(pytester: Pytester) -> None: |
| 3236 |
pytester.makepyfile( |
| 3237 |
""" |
| 3238 |
import pytest |
| 3239 |
|
| 3240 |
@pytest.mark.xfail |
| 3241 |
def test_xfail(): |
| 3242 |
assert False |
| 3243 |
|
| 3244 |
@pytest.mark.xfail(reason="foo") |
| 3245 |
def test_xfail_reason(): |
| 3246 |
assert False |
| 3247 |
""" |
| 3248 |
) |
| 3249 |
result = pytester.runpytest("-rx") |
| 3250 |
expect1 = "XFAIL test_summary_xfail_reason.py::test_xfail" |
| 3251 |
expect2 = "XFAIL test_summary_xfail_reason.py::test_xfail_reason - foo" |
| 3252 |
result.stdout.fnmatch_lines([expect1, expect2]) |
| 3253 |
assert result.stdout.lines.count(expect1) == 1 |
| 3254 |
assert result.stdout.lines.count(expect2) == 1 |
| 3255 |
|
| 3256 |
|
| 3257 |
@pytest.fixture() |
| 3258 |
def xfail_testfile(pytester: Pytester) -> Path: |
| 3259 |
return pytester.makepyfile( |
| 3260 |
""" |
| 3261 |
import pytest |
| 3262 |
|
| 3263 |
def test_fail(): |
| 3264 |
a, b = 1, 2 |
| 3265 |
assert a == b |
| 3266 |
|
| 3267 |
@pytest.mark.xfail |
| 3268 |
def test_xfail(): |
| 3269 |
c, d = 3, 4 |
| 3270 |
assert c == d |
| 3271 |
""" |
| 3272 |
) |
| 3273 |
|
| 3274 |
|
| 3275 |
def test_xfail_tb_default(xfail_testfile, pytester: Pytester) -> None: |
| 3276 |
result = pytester.runpytest(xfail_testfile) |
| 3277 |
|
| 3278 |
# test_fail, show traceback |
| 3279 |
result.stdout.fnmatch_lines( |
| 3280 |
[ |
| 3281 |
"*= FAILURES =*", |
| 3282 |
"*_ test_fail _*", |
| 3283 |
"*def test_fail():*", |
| 3284 |
"* a, b = 1, 2*", |
| 3285 |
"*> assert a == b*", |
| 3286 |
"*E assert 1 == 2*", |
| 3287 |
] |
| 3288 |
) |
| 3289 |
|
| 3290 |
# test_xfail, don't show traceback |
| 3291 |
result.stdout.no_fnmatch_line("*= XFAILURES =*") |
| 3292 |
|
| 3293 |
|
| 3294 |
def test_xfail_tb_true(xfail_testfile, pytester: Pytester) -> None: |
| 3295 |
result = pytester.runpytest(xfail_testfile, "--xfail-tb") |
| 3296 |
|
| 3297 |
# both test_fail and test_xfail, show traceback |
| 3298 |
result.stdout.fnmatch_lines( |
| 3299 |
[ |
| 3300 |
"*= FAILURES =*", |
| 3301 |
"*_ test_fail _*", |
| 3302 |
"*def test_fail():*", |
| 3303 |
"* a, b = 1, 2*", |
| 3304 |
"*> assert a == b*", |
| 3305 |
"*E assert 1 == 2*", |
| 3306 |
"*= XFAILURES =*", |
| 3307 |
"*_ test_xfail _*", |
| 3308 |
"*def test_xfail():*", |
| 3309 |
"* c, d = 3, 4*", |
| 3310 |
"*> assert c == d*", |
| 3311 |
"*E assert 3 == 4*", |
| 3312 |
"*short test summary info*", |
| 3313 |
] |
| 3314 |
) |
| 3315 |
|
| 3316 |
|
| 3317 |
def test_xfail_tb_line(xfail_testfile, pytester: Pytester) -> None: |
| 3318 |
result = pytester.runpytest(xfail_testfile, "--xfail-tb", "--tb=line") |
| 3319 |
|
| 3320 |
# both test_fail and test_xfail, show line |
| 3321 |
result.stdout.fnmatch_lines( |
| 3322 |
[ |
| 3323 |
"*= FAILURES =*", |
| 3324 |
"*test_xfail_tb_line.py:5: assert 1 == 2", |
| 3325 |
"*= XFAILURES =*", |
| 3326 |
"*test_xfail_tb_line.py:10: assert 3 == 4", |
| 3327 |
"*short test summary info*", |
| 3328 |
] |
| 3329 |
) |
| 3330 |
|
| 3331 |
|
| 3332 |
def test_summary_xpass_reason(pytester: Pytester) -> None: |
| 3333 |
pytester.makepyfile( |
| 3334 |
""" |
| 3335 |
import pytest |
| 3336 |
|
| 3337 |
@pytest.mark.xfail |
| 3338 |
def test_pass(): |
| 3339 |
... |
| 3340 |
|
| 3341 |
@pytest.mark.xfail(reason="foo") |
| 3342 |
def test_reason(): |
| 3343 |
... |
| 3344 |
""" |
| 3345 |
) |
| 3346 |
result = pytester.runpytest("-rX") |
| 3347 |
expect1 = "XPASS test_summary_xpass_reason.py::test_pass" |
| 3348 |
expect2 = "XPASS test_summary_xpass_reason.py::test_reason - foo" |
| 3349 |
result.stdout.fnmatch_lines([expect1, expect2]) |
| 3350 |
assert result.stdout.lines.count(expect1) == 1 |
| 3351 |
assert result.stdout.lines.count(expect2) == 1 |
| 3352 |
|
| 3353 |
|
| 3354 |
def test_xpass_output(pytester: Pytester) -> None: |
| 3355 |
pytester.makepyfile( |
| 3356 |
""" |
| 3357 |
import pytest |
| 3358 |
|
| 3359 |
@pytest.mark.xfail |
| 3360 |
def test_pass(): |
| 3361 |
print('hi there') |
| 3362 |
""" |
| 3363 |
) |
| 3364 |
result = pytester.runpytest("-rX") |
| 3365 |
result.stdout.fnmatch_lines( |
| 3366 |
[ |
| 3367 |
"*= XPASSES =*", |
| 3368 |
"*_ test_pass _*", |
| 3369 |
"*- Captured stdout call -*", |
| 3370 |
"*= short test summary info =*", |
| 3371 |
"XPASS test_xpass_output.py::test_pass*", |
| 3372 |
"*= 1 xpassed in * =*", |
| 3373 |
] |
| 3374 |
) |
| 3375 |
|
| 3376 |
|
| 3377 |
class TestNodeIDHandling: |
| 3378 |
def test_nodeid_handling_windows_paths(self, pytester: Pytester, tmp_path) -> None: |
| 3379 |
"""Test the correct handling of Windows-style paths with backslashes.""" |
| 3380 |
pytester.makeini("[pytest]") # Change `config.rootpath` |
| 3381 |
|
| 3382 |
test_path = pytester.path / "tests" / "test_foo.py" |
| 3383 |
test_path.parent.mkdir() |
| 3384 |
os.chdir(test_path.parent) # Change `config.invocation_params.dir` |
| 3385 |
|
| 3386 |
test_path.write_text( |
| 3387 |
textwrap.dedent( |
| 3388 |
""" |
| 3389 |
import pytest |
| 3390 |
|
| 3391 |
@pytest.mark.parametrize("a", ["x/y", "C:/path", "\\\\", "C:\\\\path", "a::b/"]) |
| 3392 |
def test_x(a): |
| 3393 |
assert False |
| 3394 |
""" |
| 3395 |
), |
| 3396 |
encoding="utf-8", |
| 3397 |
) |
| 3398 |
|
| 3399 |
result = pytester.runpytest("-v") |
| 3400 |
|
| 3401 |
result.stdout.re_match_lines( |
| 3402 |
[ |
| 3403 |
r".*test_foo.py::test_x\[x/y\] .*FAILED.*", |
| 3404 |
r".*test_foo.py::test_x\[C:/path\] .*FAILED.*", |
| 3405 |
r".*test_foo.py::test_x\[\\\\\] .*FAILED.*", |
| 3406 |
r".*test_foo.py::test_x\[C:\\\\path\] .*FAILED.*", |
| 3407 |
r".*test_foo.py::test_x\[a::b/\] .*FAILED.*", |
| 3408 |
] |
| 3409 |
) |
| 3410 |
|
| 3411 |
|
| 3412 |
class TestTerminalProgressPlugin: |
| 3413 |
"""Tests for the TerminalProgressPlugin.""" |
| 3414 |
|
| 3415 |
@pytest.fixture |
| 3416 |
def mock_file(self) -> StringIO: |
| 3417 |
return StringIO() |
| 3418 |
|
| 3419 |
@pytest.fixture |
| 3420 |
def mock_tr(self, mock_file: StringIO) -> pytest.TerminalReporter: |
| 3421 |
tr: pytest.TerminalReporter = mock.create_autospec(pytest.TerminalReporter) |
| 3422 |
|
| 3423 |
def write_raw(content: str, *, flush: bool = False) -> None: |
| 3424 |
mock_file.write(content) |
| 3425 |
|
| 3426 |
tr.write_raw = write_raw # type: ignore[method-assign] |
| 3427 |
tr._progress_nodeids_reported = set() |
| 3428 |
return tr |
| 3429 |
|
| 3430 |
@pytest.mark.skipif(sys.platform != "win32", reason="#13896") |
| 3431 |
def test_plugin_registration_enabled_by_default( |
| 3432 |
self, pytester: pytest.Pytester, monkeypatch: MonkeyPatch |
| 3433 |
) -> None: |
| 3434 |
"""Test that the plugin registration is enabled by default. |
| 3435 |
|
| 3436 |
Currently only on Windows (#13896). |
| 3437 |
""" |
| 3438 |
monkeypatch.setattr(sys.stdout, "isatty", lambda: True) |
| 3439 |
# The plugin module should be registered as a default plugin. |
| 3440 |
config = pytester.parseconfigure() |
| 3441 |
plugin = config.pluginmanager.get_plugin("terminalprogress") |
| 3442 |
assert plugin is not None |
| 3443 |
|
| 3444 |
def test_plugin_registred_on_all_platforms_when_explicitly_requested( |
| 3445 |
self, pytester: pytest.Pytester, monkeypatch: MonkeyPatch |
| 3446 |
) -> None: |
| 3447 |
"""Test that the plugin is registered on any platform if explicitly requested.""" |
| 3448 |
monkeypatch.setattr(sys.stdout, "isatty", lambda: True) |
| 3449 |
# The plugin module should be registered as a default plugin. |
| 3450 |
config = pytester.parseconfigure("-p", "terminalprogress") |
| 3451 |
plugin = config.pluginmanager.get_plugin("terminalprogress") |
| 3452 |
assert plugin is not None |
| 3453 |
|
| 3454 |
def test_disabled_for_non_tty( |
| 3455 |
self, pytester: pytest.Pytester, monkeypatch: MonkeyPatch |
| 3456 |
) -> None: |
| 3457 |
"""Test that plugin is disabled for non-TTY output.""" |
| 3458 |
monkeypatch.setattr(sys.stdout, "isatty", lambda: False) |
| 3459 |
config = pytester.parseconfigure("-p", "terminalprogress") |
| 3460 |
plugin = config.pluginmanager.get_plugin("terminalprogress-plugin") |
| 3461 |
assert plugin is None |
| 3462 |
|
| 3463 |
def test_disabled_for_dumb_terminal( |
| 3464 |
self, pytester: pytest.Pytester, monkeypatch: MonkeyPatch |
| 3465 |
) -> None: |
| 3466 |
"""Test that plugin is disabled when TERM=dumb.""" |
| 3467 |
monkeypatch.setenv("TERM", "dumb") |
| 3468 |
monkeypatch.setattr(sys.stdout, "isatty", lambda: True) |
| 3469 |
config = pytester.parseconfigure("-p", "terminalprogress") |
| 3470 |
plugin = config.pluginmanager.get_plugin("terminalprogress-plugin") |
| 3471 |
assert plugin is None |
| 3472 |
|
| 3473 |
@pytest.mark.parametrize( |
| 3474 |
["state", "progress", "expected"], |
| 3475 |
[ |
| 3476 |
("indeterminate", None, "\x1b]9;4;3;\x1b\\"), |
| 3477 |
("normal", 50, "\x1b]9;4;1;50\x1b\\"), |
| 3478 |
("error", 75, "\x1b]9;4;2;75\x1b\\"), |
| 3479 |
("paused", None, "\x1b]9;4;4;\x1b\\"), |
| 3480 |
("paused", 80, "\x1b]9;4;4;80\x1b\\"), |
| 3481 |
("remove", None, "\x1b]9;4;0;\x1b\\"), |
| 3482 |
], |
| 3483 |
) |
| 3484 |
def test_emit_progress_sequences( |
| 3485 |
self, |
| 3486 |
mock_file: StringIO, |
| 3487 |
mock_tr: pytest.TerminalReporter, |
| 3488 |
state: Literal["remove", "normal", "error", "indeterminate", "paused"], |
| 3489 |
progress: int | None, |
| 3490 |
expected: str, |
| 3491 |
) -> None: |
| 3492 |
"""Test that progress sequences are emitted correctly.""" |
| 3493 |
plugin = TerminalProgressPlugin(mock_tr) |
| 3494 |
plugin._emit_progress(state, progress) |
| 3495 |
assert expected in mock_file.getvalue() |
| 3496 |
|
| 3497 |
def test_session_lifecycle( |
| 3498 |
self, mock_file: StringIO, mock_tr: pytest.TerminalReporter |
| 3499 |
) -> None: |
| 3500 |
"""Test progress updates during session lifecycle.""" |
| 3501 |
plugin = TerminalProgressPlugin(mock_tr) |
| 3502 |
|
| 3503 |
session = mock.create_autospec(pytest.Session) |
| 3504 |
session.testscollected = 3 |
| 3505 |
|
| 3506 |
# Session start - should emit indeterminate progress. |
| 3507 |
plugin.pytest_sessionstart(session) |
| 3508 |
assert "\x1b]9;4;3;\x1b\\" in mock_file.getvalue() |
| 3509 |
mock_file.truncate(0) |
| 3510 |
mock_file.seek(0) |
| 3511 |
|
| 3512 |
# Collection finish - should emit 0% progress. |
| 3513 |
plugin.pytest_collection_finish() |
| 3514 |
assert "\x1b]9;4;1;0\x1b\\" in mock_file.getvalue() |
| 3515 |
mock_file.truncate(0) |
| 3516 |
mock_file.seek(0) |
| 3517 |
|
| 3518 |
# First test - 33% progress. |
| 3519 |
report1 = pytest.TestReport( |
| 3520 |
nodeid="test_1", |
| 3521 |
location=("test.py", 0, "test_1"), |
| 3522 |
when="call", |
| 3523 |
outcome="passed", |
| 3524 |
keywords={}, |
| 3525 |
longrepr=None, |
| 3526 |
) |
| 3527 |
mock_tr.reported_progress = 1 # type: ignore[misc] |
| 3528 |
plugin.pytest_runtest_logreport(report1) |
| 3529 |
assert "\x1b]9;4;1;33\x1b\\" in mock_file.getvalue() |
| 3530 |
mock_file.truncate(0) |
| 3531 |
mock_file.seek(0) |
| 3532 |
|
| 3533 |
# Second test with failure - 66% in error state. |
| 3534 |
report2 = pytest.TestReport( |
| 3535 |
nodeid="test_2", |
| 3536 |
location=("test.py", 1, "test_2"), |
| 3537 |
when="call", |
| 3538 |
outcome="failed", |
| 3539 |
keywords={}, |
| 3540 |
longrepr=None, |
| 3541 |
) |
| 3542 |
mock_tr.reported_progress = 2 # type: ignore[misc] |
| 3543 |
plugin.pytest_runtest_logreport(report2) |
| 3544 |
assert "\x1b]9;4;2;66\x1b\\" in mock_file.getvalue() |
| 3545 |
mock_file.truncate(0) |
| 3546 |
mock_file.seek(0) |
| 3547 |
|
| 3548 |
# Session finish - should remove progress. |
| 3549 |
plugin.pytest_sessionfinish() |
| 3550 |
assert "\x1b]9;4;0;\x1b\\" in mock_file.getvalue() |