| 1 |
# mypy: allow-untyped-defs |
| 2 |
from __future__ import annotations |
| 3 |
|
| 4 |
import textwrap |
| 5 |
|
| 6 |
from _pytest.pytester import Pytester |
| 7 |
from _pytest.runner import runtestprotocol |
| 8 |
from _pytest.skipping import evaluate_skip_marks |
| 9 |
from _pytest.skipping import evaluate_xfail_marks |
| 10 |
from _pytest.skipping import pytest_runtest_setup |
| 11 |
import pytest |
| 12 |
|
| 13 |
|
| 14 |
class TestEvaluation: |
| 15 |
def test_no_marker(self, pytester: Pytester) -> None: |
| 16 |
item = pytester.getitem("def test_func(): pass") |
| 17 |
skipped = evaluate_skip_marks(item) |
| 18 |
assert not skipped |
| 19 |
|
| 20 |
def test_marked_xfail_no_args(self, pytester: Pytester) -> None: |
| 21 |
item = pytester.getitem( |
| 22 |
""" |
| 23 |
import pytest |
| 24 |
@pytest.mark.xfail |
| 25 |
def test_func(): |
| 26 |
pass |
| 27 |
""" |
| 28 |
) |
| 29 |
xfailed = evaluate_xfail_marks(item) |
| 30 |
assert xfailed |
| 31 |
assert xfailed.reason == "" |
| 32 |
assert xfailed.run |
| 33 |
|
| 34 |
def test_marked_skipif_no_args(self, pytester: Pytester) -> None: |
| 35 |
item = pytester.getitem( |
| 36 |
""" |
| 37 |
import pytest |
| 38 |
@pytest.mark.skipif |
| 39 |
def test_func(): |
| 40 |
pass |
| 41 |
""" |
| 42 |
) |
| 43 |
skipped = evaluate_skip_marks(item) |
| 44 |
assert skipped |
| 45 |
assert skipped.reason == "" |
| 46 |
|
| 47 |
def test_marked_one_arg(self, pytester: Pytester) -> None: |
| 48 |
item = pytester.getitem( |
| 49 |
""" |
| 50 |
import pytest |
| 51 |
@pytest.mark.skipif("hasattr(os, 'sep')") |
| 52 |
def test_func(): |
| 53 |
pass |
| 54 |
""" |
| 55 |
) |
| 56 |
skipped = evaluate_skip_marks(item) |
| 57 |
assert skipped |
| 58 |
assert skipped.reason == "condition: hasattr(os, 'sep')" |
| 59 |
|
| 60 |
def test_marked_one_arg_with_reason(self, pytester: Pytester) -> None: |
| 61 |
item = pytester.getitem( |
| 62 |
""" |
| 63 |
import pytest |
| 64 |
@pytest.mark.skipif("hasattr(os, 'sep')", attr=2, reason="hello world") |
| 65 |
def test_func(): |
| 66 |
pass |
| 67 |
""" |
| 68 |
) |
| 69 |
skipped = evaluate_skip_marks(item) |
| 70 |
assert skipped |
| 71 |
assert skipped.reason == "hello world" |
| 72 |
|
| 73 |
def test_marked_one_arg_twice(self, pytester: Pytester) -> None: |
| 74 |
lines = [ |
| 75 |
"""@pytest.mark.skipif("not hasattr(os, 'murks')")""", |
| 76 |
"""@pytest.mark.skipif(condition="hasattr(os, 'murks')")""", |
| 77 |
] |
| 78 |
for i in range(2): |
| 79 |
item = pytester.getitem( |
| 80 |
f""" |
| 81 |
import pytest |
| 82 |
{lines[i]} |
| 83 |
{lines[(i + 1) % 2]} |
| 84 |
def test_func(): |
| 85 |
pass |
| 86 |
""" |
| 87 |
) |
| 88 |
skipped = evaluate_skip_marks(item) |
| 89 |
assert skipped |
| 90 |
assert skipped.reason == "condition: not hasattr(os, 'murks')" |
| 91 |
|
| 92 |
def test_marked_one_arg_twice2(self, pytester: Pytester) -> None: |
| 93 |
item = pytester.getitem( |
| 94 |
""" |
| 95 |
import pytest |
| 96 |
@pytest.mark.skipif("hasattr(os, 'murks')") |
| 97 |
@pytest.mark.skipif("not hasattr(os, 'murks')") |
| 98 |
def test_func(): |
| 99 |
pass |
| 100 |
""" |
| 101 |
) |
| 102 |
skipped = evaluate_skip_marks(item) |
| 103 |
assert skipped |
| 104 |
assert skipped.reason == "condition: not hasattr(os, 'murks')" |
| 105 |
|
| 106 |
def test_marked_skipif_with_boolean_without_reason( |
| 107 |
self, pytester: Pytester |
| 108 |
) -> None: |
| 109 |
item = pytester.getitem( |
| 110 |
""" |
| 111 |
import pytest |
| 112 |
@pytest.mark.skipif(False) |
| 113 |
def test_func(): |
| 114 |
pass |
| 115 |
""" |
| 116 |
) |
| 117 |
with pytest.raises(pytest.fail.Exception) as excinfo: |
| 118 |
evaluate_skip_marks(item) |
| 119 |
assert excinfo.value.msg is not None |
| 120 |
assert ( |
| 121 |
"""Error evaluating 'skipif': you need to specify reason=STRING when using booleans as conditions.""" |
| 122 |
in excinfo.value.msg |
| 123 |
) |
| 124 |
|
| 125 |
def test_marked_skipif_with_invalid_boolean(self, pytester: Pytester) -> None: |
| 126 |
item = pytester.getitem( |
| 127 |
""" |
| 128 |
import pytest |
| 129 |
|
| 130 |
class InvalidBool: |
| 131 |
def __bool__(self): |
| 132 |
raise TypeError("INVALID") |
| 133 |
|
| 134 |
@pytest.mark.skipif(InvalidBool(), reason="xxx") |
| 135 |
def test_func(): |
| 136 |
pass |
| 137 |
""" |
| 138 |
) |
| 139 |
with pytest.raises(pytest.fail.Exception) as excinfo: |
| 140 |
evaluate_skip_marks(item) |
| 141 |
assert excinfo.value.msg is not None |
| 142 |
assert "Error evaluating 'skipif' condition as a boolean" in excinfo.value.msg |
| 143 |
assert "INVALID" in excinfo.value.msg |
| 144 |
|
| 145 |
def test_skipif_class(self, pytester: Pytester) -> None: |
| 146 |
(item,) = pytester.getitems( |
| 147 |
""" |
| 148 |
import pytest |
| 149 |
class TestClass(object): |
| 150 |
pytestmark = pytest.mark.skipif("config._hackxyz") |
| 151 |
def test_func(self): |
| 152 |
pass |
| 153 |
""" |
| 154 |
) |
| 155 |
item.config._hackxyz = 3 # type: ignore[attr-defined] |
| 156 |
skipped = evaluate_skip_marks(item) |
| 157 |
assert skipped |
| 158 |
assert skipped.reason == "condition: config._hackxyz" |
| 159 |
|
| 160 |
def test_skipif_markeval_namespace(self, pytester: Pytester) -> None: |
| 161 |
pytester.makeconftest( |
| 162 |
""" |
| 163 |
import pytest |
| 164 |
|
| 165 |
def pytest_markeval_namespace(): |
| 166 |
return {"color": "green"} |
| 167 |
""" |
| 168 |
) |
| 169 |
p = pytester.makepyfile( |
| 170 |
""" |
| 171 |
import pytest |
| 172 |
|
| 173 |
@pytest.mark.skipif("color == 'green'") |
| 174 |
def test_1(): |
| 175 |
assert True |
| 176 |
|
| 177 |
@pytest.mark.skipif("color == 'red'") |
| 178 |
def test_2(): |
| 179 |
assert True |
| 180 |
""" |
| 181 |
) |
| 182 |
res = pytester.runpytest(p) |
| 183 |
assert res.ret == 0 |
| 184 |
res.stdout.fnmatch_lines(["*1 skipped*"]) |
| 185 |
res.stdout.fnmatch_lines(["*1 passed*"]) |
| 186 |
|
| 187 |
def test_skipif_markeval_namespace_multiple(self, pytester: Pytester) -> None: |
| 188 |
"""Keys defined by ``pytest_markeval_namespace()`` in nested plugins override top-level ones.""" |
| 189 |
root = pytester.mkdir("root") |
| 190 |
root.joinpath("__init__.py").touch() |
| 191 |
root.joinpath("conftest.py").write_text( |
| 192 |
textwrap.dedent( |
| 193 |
"""\ |
| 194 |
import pytest |
| 195 |
|
| 196 |
def pytest_markeval_namespace(): |
| 197 |
return {"arg": "root"} |
| 198 |
""" |
| 199 |
), |
| 200 |
encoding="utf-8", |
| 201 |
) |
| 202 |
root.joinpath("test_root.py").write_text( |
| 203 |
textwrap.dedent( |
| 204 |
"""\ |
| 205 |
import pytest |
| 206 |
|
| 207 |
@pytest.mark.skipif("arg == 'root'") |
| 208 |
def test_root(): |
| 209 |
assert False |
| 210 |
""" |
| 211 |
), |
| 212 |
encoding="utf-8", |
| 213 |
) |
| 214 |
foo = root.joinpath("foo") |
| 215 |
foo.mkdir() |
| 216 |
foo.joinpath("__init__.py").touch() |
| 217 |
foo.joinpath("conftest.py").write_text( |
| 218 |
textwrap.dedent( |
| 219 |
"""\ |
| 220 |
import pytest |
| 221 |
|
| 222 |
def pytest_markeval_namespace(): |
| 223 |
return {"arg": "foo"} |
| 224 |
""" |
| 225 |
), |
| 226 |
encoding="utf-8", |
| 227 |
) |
| 228 |
foo.joinpath("test_foo.py").write_text( |
| 229 |
textwrap.dedent( |
| 230 |
"""\ |
| 231 |
import pytest |
| 232 |
|
| 233 |
@pytest.mark.skipif("arg == 'foo'") |
| 234 |
def test_foo(): |
| 235 |
assert False |
| 236 |
""" |
| 237 |
), |
| 238 |
encoding="utf-8", |
| 239 |
) |
| 240 |
bar = root.joinpath("bar") |
| 241 |
bar.mkdir() |
| 242 |
bar.joinpath("__init__.py").touch() |
| 243 |
bar.joinpath("conftest.py").write_text( |
| 244 |
textwrap.dedent( |
| 245 |
"""\ |
| 246 |
import pytest |
| 247 |
|
| 248 |
def pytest_markeval_namespace(): |
| 249 |
return {"arg": "bar"} |
| 250 |
""" |
| 251 |
), |
| 252 |
encoding="utf-8", |
| 253 |
) |
| 254 |
bar.joinpath("test_bar.py").write_text( |
| 255 |
textwrap.dedent( |
| 256 |
"""\ |
| 257 |
import pytest |
| 258 |
|
| 259 |
@pytest.mark.skipif("arg == 'bar'") |
| 260 |
def test_bar(): |
| 261 |
assert False |
| 262 |
""" |
| 263 |
), |
| 264 |
encoding="utf-8", |
| 265 |
) |
| 266 |
|
| 267 |
reprec = pytester.inline_run("-vs", "--capture=no") |
| 268 |
reprec.assertoutcome(skipped=3) |
| 269 |
|
| 270 |
def test_skipif_markeval_namespace_ValueError(self, pytester: Pytester) -> None: |
| 271 |
pytester.makeconftest( |
| 272 |
""" |
| 273 |
import pytest |
| 274 |
|
| 275 |
def pytest_markeval_namespace(): |
| 276 |
return True |
| 277 |
""" |
| 278 |
) |
| 279 |
p = pytester.makepyfile( |
| 280 |
""" |
| 281 |
import pytest |
| 282 |
|
| 283 |
@pytest.mark.skipif("color == 'green'") |
| 284 |
def test_1(): |
| 285 |
assert True |
| 286 |
""" |
| 287 |
) |
| 288 |
res = pytester.runpytest(p) |
| 289 |
assert res.ret == 1 |
| 290 |
res.stdout.fnmatch_lines( |
| 291 |
[ |
| 292 |
"*ValueError: pytest_markeval_namespace() needs to return a dict, got True*" |
| 293 |
] |
| 294 |
) |
| 295 |
|
| 296 |
|
| 297 |
class TestXFail: |
| 298 |
@pytest.mark.parametrize("strict", [True, False]) |
| 299 |
def test_xfail_simple(self, pytester: Pytester, strict: bool) -> None: |
| 300 |
item = pytester.getitem( |
| 301 |
f""" |
| 302 |
import pytest |
| 303 |
@pytest.mark.xfail(strict={strict}) |
| 304 |
def test_func(): |
| 305 |
assert 0 |
| 306 |
""" |
| 307 |
) |
| 308 |
reports = runtestprotocol(item, log=False) |
| 309 |
assert len(reports) == 3 |
| 310 |
callreport = reports[1] |
| 311 |
assert callreport.skipped |
| 312 |
assert callreport.wasxfail == "" |
| 313 |
|
| 314 |
def test_xfail_xpassed(self, pytester: Pytester) -> None: |
| 315 |
item = pytester.getitem( |
| 316 |
""" |
| 317 |
import pytest |
| 318 |
@pytest.mark.xfail(reason="this is an xfail") |
| 319 |
def test_func(): |
| 320 |
assert 1 |
| 321 |
""" |
| 322 |
) |
| 323 |
reports = runtestprotocol(item, log=False) |
| 324 |
assert len(reports) == 3 |
| 325 |
callreport = reports[1] |
| 326 |
assert callreport.passed |
| 327 |
assert callreport.wasxfail == "this is an xfail" |
| 328 |
|
| 329 |
def test_xfail_using_platform(self, pytester: Pytester) -> None: |
| 330 |
"""Verify that platform can be used with xfail statements.""" |
| 331 |
item = pytester.getitem( |
| 332 |
""" |
| 333 |
import pytest |
| 334 |
@pytest.mark.xfail("platform.platform() == platform.platform()") |
| 335 |
def test_func(): |
| 336 |
assert 0 |
| 337 |
""" |
| 338 |
) |
| 339 |
reports = runtestprotocol(item, log=False) |
| 340 |
assert len(reports) == 3 |
| 341 |
callreport = reports[1] |
| 342 |
assert callreport.wasxfail |
| 343 |
|
| 344 |
def test_xfail_xpassed_strict(self, pytester: Pytester) -> None: |
| 345 |
item = pytester.getitem( |
| 346 |
""" |
| 347 |
import pytest |
| 348 |
@pytest.mark.xfail(strict=True, reason="nope") |
| 349 |
def test_func(): |
| 350 |
assert 1 |
| 351 |
""" |
| 352 |
) |
| 353 |
reports = runtestprotocol(item, log=False) |
| 354 |
assert len(reports) == 3 |
| 355 |
callreport = reports[1] |
| 356 |
assert callreport.failed |
| 357 |
assert str(callreport.longrepr) == "[XPASS(strict)] nope" |
| 358 |
assert not hasattr(callreport, "wasxfail") |
| 359 |
|
| 360 |
def test_xfail_run_anyway(self, pytester: Pytester) -> None: |
| 361 |
pytester.makepyfile( |
| 362 |
""" |
| 363 |
import pytest |
| 364 |
@pytest.mark.xfail |
| 365 |
def test_func(): |
| 366 |
assert 0 |
| 367 |
def test_func2(): |
| 368 |
pytest.xfail("hello") |
| 369 |
""" |
| 370 |
) |
| 371 |
result = pytester.runpytest("--runxfail") |
| 372 |
result.stdout.fnmatch_lines( |
| 373 |
["*def test_func():*", "*assert 0*", "*1 failed*1 pass*"] |
| 374 |
) |
| 375 |
|
| 376 |
@pytest.mark.parametrize( |
| 377 |
"test_input,expected", |
| 378 |
[ |
| 379 |
( |
| 380 |
["-rs"], |
| 381 |
["SKIPPED [1] test_sample.py:2: unconditional skip", "*1 skipped*"], |
| 382 |
), |
| 383 |
( |
| 384 |
["-rs", "--runxfail"], |
| 385 |
["SKIPPED [1] test_sample.py:2: unconditional skip", "*1 skipped*"], |
| 386 |
), |
| 387 |
], |
| 388 |
) |
| 389 |
def test_xfail_run_with_skip_mark( |
| 390 |
self, pytester: Pytester, test_input, expected |
| 391 |
) -> None: |
| 392 |
pytester.makepyfile( |
| 393 |
test_sample=""" |
| 394 |
import pytest |
| 395 |
@pytest.mark.skip |
| 396 |
def test_skip_location() -> None: |
| 397 |
assert 0 |
| 398 |
""" |
| 399 |
) |
| 400 |
result = pytester.runpytest(*test_input) |
| 401 |
result.stdout.fnmatch_lines(expected) |
| 402 |
|
| 403 |
def test_xfail_evalfalse_but_fails(self, pytester: Pytester) -> None: |
| 404 |
item = pytester.getitem( |
| 405 |
""" |
| 406 |
import pytest |
| 407 |
@pytest.mark.xfail('False') |
| 408 |
def test_func(): |
| 409 |
assert 0 |
| 410 |
""" |
| 411 |
) |
| 412 |
reports = runtestprotocol(item, log=False) |
| 413 |
callreport = reports[1] |
| 414 |
assert callreport.failed |
| 415 |
assert not hasattr(callreport, "wasxfail") |
| 416 |
assert "xfail" in callreport.keywords |
| 417 |
|
| 418 |
def test_xfail_not_report_default(self, pytester: Pytester) -> None: |
| 419 |
p = pytester.makepyfile( |
| 420 |
test_one=""" |
| 421 |
import pytest |
| 422 |
@pytest.mark.xfail |
| 423 |
def test_this(): |
| 424 |
assert 0 |
| 425 |
""" |
| 426 |
) |
| 427 |
pytester.runpytest(p, "-v") |
| 428 |
# result.stdout.fnmatch_lines([ |
| 429 |
# "*HINT*use*-r*" |
| 430 |
# ]) |
| 431 |
|
| 432 |
def test_xfail_not_run_xfail_reporting(self, pytester: Pytester) -> None: |
| 433 |
p = pytester.makepyfile( |
| 434 |
test_one=""" |
| 435 |
import pytest |
| 436 |
@pytest.mark.xfail(run=False, reason="noway") |
| 437 |
def test_this(): |
| 438 |
assert 0 |
| 439 |
@pytest.mark.xfail("True", run=False) |
| 440 |
def test_this_true(): |
| 441 |
assert 0 |
| 442 |
@pytest.mark.xfail("False", run=False, reason="huh") |
| 443 |
def test_this_false(): |
| 444 |
assert 1 |
| 445 |
""" |
| 446 |
) |
| 447 |
result = pytester.runpytest(p, "-rx") |
| 448 |
result.stdout.fnmatch_lines( |
| 449 |
[ |
| 450 |
"*test_one*test_this - *NOTRUN* noway", |
| 451 |
"*test_one*test_this_true - *NOTRUN* condition: True", |
| 452 |
"*1 passed*", |
| 453 |
] |
| 454 |
) |
| 455 |
|
| 456 |
def test_xfail_not_run_no_setup_run(self, pytester: Pytester) -> None: |
| 457 |
p = pytester.makepyfile( |
| 458 |
test_one=""" |
| 459 |
import pytest |
| 460 |
@pytest.mark.xfail(run=False, reason="hello") |
| 461 |
def test_this(): |
| 462 |
assert 0 |
| 463 |
def setup_module(mod): |
| 464 |
raise ValueError(42) |
| 465 |
""" |
| 466 |
) |
| 467 |
result = pytester.runpytest(p, "-rx") |
| 468 |
result.stdout.fnmatch_lines(["*test_one*test_this*NOTRUN*hello", "*1 xfailed*"]) |
| 469 |
|
| 470 |
def test_xfail_xpass(self, pytester: Pytester) -> None: |
| 471 |
p = pytester.makepyfile( |
| 472 |
test_one=""" |
| 473 |
import pytest |
| 474 |
@pytest.mark.xfail |
| 475 |
def test_that(): |
| 476 |
assert 1 |
| 477 |
""" |
| 478 |
) |
| 479 |
result = pytester.runpytest(p, "-rX") |
| 480 |
result.stdout.fnmatch_lines(["*XPASS*test_that*", "*1 xpassed*"]) |
| 481 |
assert result.ret == 0 |
| 482 |
|
| 483 |
def test_xfail_imperative(self, pytester: Pytester) -> None: |
| 484 |
p = pytester.makepyfile( |
| 485 |
""" |
| 486 |
import pytest |
| 487 |
def test_this(): |
| 488 |
pytest.xfail("hello") |
| 489 |
""" |
| 490 |
) |
| 491 |
result = pytester.runpytest(p) |
| 492 |
result.stdout.fnmatch_lines(["*1 xfailed*"]) |
| 493 |
result = pytester.runpytest(p, "-rx") |
| 494 |
result.stdout.fnmatch_lines(["*XFAIL*test_this*hello*"]) |
| 495 |
result = pytester.runpytest(p, "--runxfail") |
| 496 |
result.stdout.fnmatch_lines(["*1 pass*"]) |
| 497 |
|
| 498 |
def test_xfail_imperative_in_setup_function(self, pytester: Pytester) -> None: |
| 499 |
p = pytester.makepyfile( |
| 500 |
""" |
| 501 |
import pytest |
| 502 |
def setup_function(function): |
| 503 |
pytest.xfail("hello") |
| 504 |
|
| 505 |
def test_this(): |
| 506 |
assert 0 |
| 507 |
""" |
| 508 |
) |
| 509 |
result = pytester.runpytest(p) |
| 510 |
result.stdout.fnmatch_lines(["*1 xfailed*"]) |
| 511 |
result = pytester.runpytest(p, "-rx") |
| 512 |
result.stdout.fnmatch_lines(["*XFAIL*test_this*hello*"]) |
| 513 |
result = pytester.runpytest(p, "--runxfail") |
| 514 |
result.stdout.fnmatch_lines( |
| 515 |
""" |
| 516 |
*def test_this* |
| 517 |
*1 fail* |
| 518 |
""" |
| 519 |
) |
| 520 |
|
| 521 |
def xtest_dynamic_xfail_set_during_setup(self, pytester: Pytester) -> None: |
| 522 |
p = pytester.makepyfile( |
| 523 |
""" |
| 524 |
import pytest |
| 525 |
def setup_function(function): |
| 526 |
pytest.mark.xfail(function) |
| 527 |
def test_this(): |
| 528 |
assert 0 |
| 529 |
def test_that(): |
| 530 |
assert 1 |
| 531 |
""" |
| 532 |
) |
| 533 |
result = pytester.runpytest(p, "-rxX") |
| 534 |
result.stdout.fnmatch_lines(["*XFAIL*test_this*", "*XPASS*test_that*"]) |
| 535 |
|
| 536 |
def test_dynamic_xfail_no_run(self, pytester: Pytester) -> None: |
| 537 |
p = pytester.makepyfile( |
| 538 |
""" |
| 539 |
import pytest |
| 540 |
@pytest.fixture |
| 541 |
def arg(request): |
| 542 |
request.applymarker(pytest.mark.xfail(run=False)) |
| 543 |
def test_this(arg): |
| 544 |
assert 0 |
| 545 |
""" |
| 546 |
) |
| 547 |
result = pytester.runpytest(p, "-rxX") |
| 548 |
result.stdout.fnmatch_lines(["*XFAIL*test_this*NOTRUN*"]) |
| 549 |
|
| 550 |
def test_dynamic_xfail_set_during_funcarg_setup(self, pytester: Pytester) -> None: |
| 551 |
p = pytester.makepyfile( |
| 552 |
""" |
| 553 |
import pytest |
| 554 |
@pytest.fixture |
| 555 |
def arg(request): |
| 556 |
request.applymarker(pytest.mark.xfail) |
| 557 |
def test_this2(arg): |
| 558 |
assert 0 |
| 559 |
""" |
| 560 |
) |
| 561 |
result = pytester.runpytest(p) |
| 562 |
result.stdout.fnmatch_lines(["*1 xfailed*"]) |
| 563 |
|
| 564 |
def test_dynamic_xfail_set_during_runtest_failed(self, pytester: Pytester) -> None: |
| 565 |
# Issue #7486. |
| 566 |
p = pytester.makepyfile( |
| 567 |
""" |
| 568 |
import pytest |
| 569 |
def test_this(request): |
| 570 |
request.node.add_marker(pytest.mark.xfail(reason="xfail")) |
| 571 |
assert 0 |
| 572 |
""" |
| 573 |
) |
| 574 |
result = pytester.runpytest(p) |
| 575 |
result.assert_outcomes(xfailed=1) |
| 576 |
|
| 577 |
def test_dynamic_xfail_set_during_runtest_passed_strict( |
| 578 |
self, pytester: Pytester |
| 579 |
) -> None: |
| 580 |
# Issue #7486. |
| 581 |
p = pytester.makepyfile( |
| 582 |
""" |
| 583 |
import pytest |
| 584 |
def test_this(request): |
| 585 |
request.node.add_marker(pytest.mark.xfail(reason="xfail", strict=True)) |
| 586 |
""" |
| 587 |
) |
| 588 |
result = pytester.runpytest(p) |
| 589 |
result.assert_outcomes(failed=1) |
| 590 |
|
| 591 |
@pytest.mark.parametrize( |
| 592 |
"expected, actual, matchline", |
| 593 |
[ |
| 594 |
("TypeError", "TypeError", "*1 xfailed*"), |
| 595 |
("(AttributeError, TypeError)", "TypeError", "*1 xfailed*"), |
| 596 |
("TypeError", "IndexError", "*1 failed*"), |
| 597 |
("(AttributeError, TypeError)", "IndexError", "*1 failed*"), |
| 598 |
], |
| 599 |
) |
| 600 |
def test_xfail_raises( |
| 601 |
self, expected, actual, matchline, pytester: Pytester |
| 602 |
) -> None: |
| 603 |
p = pytester.makepyfile( |
| 604 |
f""" |
| 605 |
import pytest |
| 606 |
@pytest.mark.xfail(raises={expected}) |
| 607 |
def test_raises(): |
| 608 |
raise {actual}() |
| 609 |
""" |
| 610 |
) |
| 611 |
result = pytester.runpytest(p) |
| 612 |
result.stdout.fnmatch_lines([matchline]) |
| 613 |
|
| 614 |
def test_strict_sanity(self, pytester: Pytester) -> None: |
| 615 |
"""Sanity check for xfail(strict=True): a failing test should behave |
| 616 |
exactly like a normal xfail.""" |
| 617 |
p = pytester.makepyfile( |
| 618 |
""" |
| 619 |
import pytest |
| 620 |
@pytest.mark.xfail(reason='unsupported feature', strict=True) |
| 621 |
def test_foo(): |
| 622 |
assert 0 |
| 623 |
""" |
| 624 |
) |
| 625 |
result = pytester.runpytest(p, "-rxX") |
| 626 |
result.stdout.fnmatch_lines(["*XFAIL*unsupported feature*"]) |
| 627 |
assert result.ret == 0 |
| 628 |
|
| 629 |
@pytest.mark.parametrize("strict", [True, False]) |
| 630 |
def test_strict_xfail(self, pytester: Pytester, strict: bool) -> None: |
| 631 |
p = pytester.makepyfile( |
| 632 |
f""" |
| 633 |
import pytest |
| 634 |
|
| 635 |
@pytest.mark.xfail(reason='unsupported feature', strict={strict}) |
| 636 |
def test_foo(): |
| 637 |
with open('foo_executed', 'w', encoding='utf-8'): |
| 638 |
pass # make sure test executes |
| 639 |
""" |
| 640 |
) |
| 641 |
result = pytester.runpytest(p, "-rxX") |
| 642 |
if strict: |
| 643 |
result.stdout.fnmatch_lines( |
| 644 |
["*test_foo*", "*XPASS(strict)*unsupported feature*"] |
| 645 |
) |
| 646 |
else: |
| 647 |
result.stdout.fnmatch_lines( |
| 648 |
[ |
| 649 |
"*test_strict_xfail*", |
| 650 |
"XPASS test_strict_xfail.py::test_foo - unsupported feature", |
| 651 |
] |
| 652 |
) |
| 653 |
assert result.ret == (1 if strict else 0) |
| 654 |
assert pytester.path.joinpath("foo_executed").exists() |
| 655 |
|
| 656 |
@pytest.mark.parametrize("strict", [True, False]) |
| 657 |
def test_strict_xfail_condition(self, pytester: Pytester, strict: bool) -> None: |
| 658 |
p = pytester.makepyfile( |
| 659 |
f""" |
| 660 |
import pytest |
| 661 |
|
| 662 |
@pytest.mark.xfail(False, reason='unsupported feature', strict={strict}) |
| 663 |
def test_foo(): |
| 664 |
pass |
| 665 |
""" |
| 666 |
) |
| 667 |
result = pytester.runpytest(p, "-rxX") |
| 668 |
result.stdout.fnmatch_lines(["*1 passed*"]) |
| 669 |
assert result.ret == 0 |
| 670 |
|
| 671 |
@pytest.mark.parametrize("strict", [True, False]) |
| 672 |
def test_xfail_condition_keyword(self, pytester: Pytester, strict: bool) -> None: |
| 673 |
p = pytester.makepyfile( |
| 674 |
f""" |
| 675 |
import pytest |
| 676 |
|
| 677 |
@pytest.mark.xfail(condition=False, reason='unsupported feature', strict={strict}) |
| 678 |
def test_foo(): |
| 679 |
pass |
| 680 |
""" |
| 681 |
) |
| 682 |
result = pytester.runpytest(p, "-rxX") |
| 683 |
result.stdout.fnmatch_lines(["*1 passed*"]) |
| 684 |
assert result.ret == 0 |
| 685 |
|
| 686 |
@pytest.mark.parametrize("strict_val", ["true", "false"]) |
| 687 |
@pytest.mark.parametrize("option_name", ["strict_xfail", "strict"]) |
| 688 |
def test_strict_xfail_default_from_file( |
| 689 |
self, pytester: Pytester, strict_val: str, option_name: str |
| 690 |
) -> None: |
| 691 |
pytester.makeini( |
| 692 |
f""" |
| 693 |
[pytest] |
| 694 |
{option_name} = {strict_val} |
| 695 |
""" |
| 696 |
) |
| 697 |
p = pytester.makepyfile( |
| 698 |
""" |
| 699 |
import pytest |
| 700 |
@pytest.mark.xfail(reason='unsupported feature') |
| 701 |
def test_foo(): |
| 702 |
pass |
| 703 |
""" |
| 704 |
) |
| 705 |
result = pytester.runpytest(p, "-rxX") |
| 706 |
strict = strict_val == "true" |
| 707 |
result.stdout.fnmatch_lines(["*1 failed*" if strict else "*1 xpassed*"]) |
| 708 |
assert result.ret == (1 if strict else 0) |
| 709 |
|
| 710 |
def test_xfail_markeval_namespace(self, pytester: Pytester) -> None: |
| 711 |
pytester.makeconftest( |
| 712 |
""" |
| 713 |
import pytest |
| 714 |
|
| 715 |
def pytest_markeval_namespace(): |
| 716 |
return {"color": "green"} |
| 717 |
""" |
| 718 |
) |
| 719 |
p = pytester.makepyfile( |
| 720 |
""" |
| 721 |
import pytest |
| 722 |
|
| 723 |
@pytest.mark.xfail("color == 'green'") |
| 724 |
def test_1(): |
| 725 |
assert False |
| 726 |
|
| 727 |
@pytest.mark.xfail("color == 'red'") |
| 728 |
def test_2(): |
| 729 |
assert False |
| 730 |
""" |
| 731 |
) |
| 732 |
res = pytester.runpytest(p) |
| 733 |
assert res.ret == 1 |
| 734 |
res.stdout.fnmatch_lines(["*1 failed*"]) |
| 735 |
res.stdout.fnmatch_lines(["*1 xfailed*"]) |
| 736 |
|
| 737 |
|
| 738 |
class TestXFailwithSetupTeardown: |
| 739 |
def test_failing_setup_issue9(self, pytester: Pytester) -> None: |
| 740 |
pytester.makepyfile( |
| 741 |
""" |
| 742 |
import pytest |
| 743 |
def setup_function(func): |
| 744 |
assert 0 |
| 745 |
|
| 746 |
@pytest.mark.xfail |
| 747 |
def test_func(): |
| 748 |
pass |
| 749 |
""" |
| 750 |
) |
| 751 |
result = pytester.runpytest() |
| 752 |
result.stdout.fnmatch_lines(["*1 xfail*"]) |
| 753 |
|
| 754 |
def test_failing_teardown_issue9(self, pytester: Pytester) -> None: |
| 755 |
pytester.makepyfile( |
| 756 |
""" |
| 757 |
import pytest |
| 758 |
def teardown_function(func): |
| 759 |
assert 0 |
| 760 |
|
| 761 |
@pytest.mark.xfail |
| 762 |
def test_func(): |
| 763 |
pass |
| 764 |
""" |
| 765 |
) |
| 766 |
result = pytester.runpytest() |
| 767 |
result.stdout.fnmatch_lines(["*1 xfail*"]) |
| 768 |
|
| 769 |
|
| 770 |
class TestSkip: |
| 771 |
def test_skip_class(self, pytester: Pytester) -> None: |
| 772 |
pytester.makepyfile( |
| 773 |
""" |
| 774 |
import pytest |
| 775 |
@pytest.mark.skip |
| 776 |
class TestSomething(object): |
| 777 |
def test_foo(self): |
| 778 |
pass |
| 779 |
def test_bar(self): |
| 780 |
pass |
| 781 |
|
| 782 |
def test_baz(): |
| 783 |
pass |
| 784 |
""" |
| 785 |
) |
| 786 |
rec = pytester.inline_run() |
| 787 |
rec.assertoutcome(skipped=2, passed=1) |
| 788 |
|
| 789 |
def test_skips_on_false_string(self, pytester: Pytester) -> None: |
| 790 |
pytester.makepyfile( |
| 791 |
""" |
| 792 |
import pytest |
| 793 |
@pytest.mark.skip('False') |
| 794 |
def test_foo(): |
| 795 |
pass |
| 796 |
""" |
| 797 |
) |
| 798 |
rec = pytester.inline_run() |
| 799 |
rec.assertoutcome(skipped=1) |
| 800 |
|
| 801 |
def test_arg_as_reason(self, pytester: Pytester) -> None: |
| 802 |
pytester.makepyfile( |
| 803 |
""" |
| 804 |
import pytest |
| 805 |
@pytest.mark.skip('testing stuff') |
| 806 |
def test_bar(): |
| 807 |
pass |
| 808 |
""" |
| 809 |
) |
| 810 |
result = pytester.runpytest("-rs") |
| 811 |
result.stdout.fnmatch_lines(["*testing stuff*", "*1 skipped*"]) |
| 812 |
|
| 813 |
def test_skip_no_reason(self, pytester: Pytester) -> None: |
| 814 |
pytester.makepyfile( |
| 815 |
""" |
| 816 |
import pytest |
| 817 |
@pytest.mark.skip |
| 818 |
def test_foo(): |
| 819 |
pass |
| 820 |
""" |
| 821 |
) |
| 822 |
result = pytester.runpytest("-rs") |
| 823 |
result.stdout.fnmatch_lines(["*unconditional skip*", "*1 skipped*"]) |
| 824 |
|
| 825 |
def test_skip_with_reason(self, pytester: Pytester) -> None: |
| 826 |
pytester.makepyfile( |
| 827 |
""" |
| 828 |
import pytest |
| 829 |
@pytest.mark.skip(reason="for lolz") |
| 830 |
def test_bar(): |
| 831 |
pass |
| 832 |
""" |
| 833 |
) |
| 834 |
result = pytester.runpytest("-rs") |
| 835 |
result.stdout.fnmatch_lines(["*for lolz*", "*1 skipped*"]) |
| 836 |
|
| 837 |
def test_only_skips_marked_test(self, pytester: Pytester) -> None: |
| 838 |
pytester.makepyfile( |
| 839 |
""" |
| 840 |
import pytest |
| 841 |
@pytest.mark.skip |
| 842 |
def test_foo(): |
| 843 |
pass |
| 844 |
@pytest.mark.skip(reason="nothing in particular") |
| 845 |
def test_bar(): |
| 846 |
pass |
| 847 |
def test_baz(): |
| 848 |
assert True |
| 849 |
""" |
| 850 |
) |
| 851 |
result = pytester.runpytest("-rs") |
| 852 |
result.stdout.fnmatch_lines(["*nothing in particular*", "*1 passed*2 skipped*"]) |
| 853 |
|
| 854 |
def test_strict_and_skip(self, pytester: Pytester) -> None: |
| 855 |
pytester.makepyfile( |
| 856 |
""" |
| 857 |
import pytest |
| 858 |
@pytest.mark.skip |
| 859 |
def test_hello(): |
| 860 |
pass |
| 861 |
""" |
| 862 |
) |
| 863 |
result = pytester.runpytest("-rs", "--strict-markers") |
| 864 |
result.stdout.fnmatch_lines(["*unconditional skip*", "*1 skipped*"]) |
| 865 |
|
| 866 |
def test_wrong_skip_usage(self, pytester: Pytester) -> None: |
| 867 |
pytester.makepyfile( |
| 868 |
""" |
| 869 |
import pytest |
| 870 |
@pytest.mark.skip(False, reason="I thought this was skipif") |
| 871 |
def test_hello(): |
| 872 |
pass |
| 873 |
""" |
| 874 |
) |
| 875 |
result = pytester.runpytest() |
| 876 |
result.stdout.fnmatch_lines( |
| 877 |
[ |
| 878 |
"*TypeError: *__init__() got multiple values for argument 'reason'" |
| 879 |
" - maybe you meant pytest.mark.skipif?" |
| 880 |
] |
| 881 |
) |
| 882 |
|
| 883 |
|
| 884 |
class TestSkipif: |
| 885 |
def test_skipif_conditional(self, pytester: Pytester) -> None: |
| 886 |
item = pytester.getitem( |
| 887 |
""" |
| 888 |
import pytest |
| 889 |
@pytest.mark.skipif("hasattr(os, 'sep')") |
| 890 |
def test_func(): |
| 891 |
pass |
| 892 |
""" |
| 893 |
) |
| 894 |
x = pytest.raises(pytest.skip.Exception, lambda: pytest_runtest_setup(item)) |
| 895 |
assert x.value.msg == "condition: hasattr(os, 'sep')" |
| 896 |
|
| 897 |
@pytest.mark.parametrize( |
| 898 |
"params", ["\"hasattr(sys, 'platform')\"", 'True, reason="invalid platform"'] |
| 899 |
) |
| 900 |
def test_skipif_reporting(self, pytester: Pytester, params) -> None: |
| 901 |
p = pytester.makepyfile( |
| 902 |
test_foo=f""" |
| 903 |
import pytest |
| 904 |
@pytest.mark.skipif({params}) |
| 905 |
def test_that(): |
| 906 |
assert 0 |
| 907 |
""" |
| 908 |
) |
| 909 |
result = pytester.runpytest(p, "-s", "-rs") |
| 910 |
result.stdout.fnmatch_lines(["*SKIP*1*test_foo.py*platform*", "*1 skipped*"]) |
| 911 |
assert result.ret == 0 |
| 912 |
|
| 913 |
def test_skipif_using_platform(self, pytester: Pytester) -> None: |
| 914 |
item = pytester.getitem( |
| 915 |
""" |
| 916 |
import pytest |
| 917 |
@pytest.mark.skipif("platform.platform() == platform.platform()") |
| 918 |
def test_func(): |
| 919 |
pass |
| 920 |
""" |
| 921 |
) |
| 922 |
with pytest.raises(pytest.skip.Exception): |
| 923 |
pytest_runtest_setup(item) |
| 924 |
|
| 925 |
@pytest.mark.parametrize( |
| 926 |
"marker, msg1, msg2", |
| 927 |
[("skipif", "SKIP", "skipped"), ("xfail", "XPASS", "xpassed")], |
| 928 |
) |
| 929 |
def test_skipif_reporting_multiple( |
| 930 |
self, pytester: Pytester, marker, msg1, msg2 |
| 931 |
) -> None: |
| 932 |
pytester.makepyfile( |
| 933 |
test_foo=f""" |
| 934 |
import pytest |
| 935 |
@pytest.mark.{marker}(False, reason='first_condition') |
| 936 |
@pytest.mark.{marker}(True, reason='second_condition') |
| 937 |
def test_foobar(): |
| 938 |
assert 1 |
| 939 |
""" |
| 940 |
) |
| 941 |
result = pytester.runpytest("-s", "-rsxX") |
| 942 |
result.stdout.fnmatch_lines( |
| 943 |
[f"*{msg1}*test_foo.py*second_condition*", f"*1 {msg2}*"] |
| 944 |
) |
| 945 |
assert result.ret == 0 |
| 946 |
|
| 947 |
|
| 948 |
def test_skip_not_report_default(pytester: Pytester) -> None: |
| 949 |
p = pytester.makepyfile( |
| 950 |
test_one=""" |
| 951 |
import pytest |
| 952 |
def test_this(): |
| 953 |
pytest.skip("hello") |
| 954 |
""" |
| 955 |
) |
| 956 |
result = pytester.runpytest(p, "-v") |
| 957 |
result.stdout.fnmatch_lines( |
| 958 |
[ |
| 959 |
# "*HINT*use*-r*", |
| 960 |
"*1 skipped*" |
| 961 |
] |
| 962 |
) |
| 963 |
|
| 964 |
|
| 965 |
def test_skipif_class(pytester: Pytester) -> None: |
| 966 |
p = pytester.makepyfile( |
| 967 |
""" |
| 968 |
import pytest |
| 969 |
|
| 970 |
class TestClass(object): |
| 971 |
pytestmark = pytest.mark.skipif("True") |
| 972 |
def test_that(self): |
| 973 |
assert 0 |
| 974 |
def test_though(self): |
| 975 |
assert 0 |
| 976 |
""" |
| 977 |
) |
| 978 |
result = pytester.runpytest(p) |
| 979 |
result.stdout.fnmatch_lines(["*2 skipped*"]) |
| 980 |
|
| 981 |
|
| 982 |
def test_skipped_reasons_functional(pytester: Pytester) -> None: |
| 983 |
pytester.makepyfile( |
| 984 |
test_one=""" |
| 985 |
import pytest |
| 986 |
from helpers import doskip |
| 987 |
|
| 988 |
def setup_function(func): # LINE 4 |
| 989 |
doskip("setup function") |
| 990 |
|
| 991 |
def test_func(): |
| 992 |
pass |
| 993 |
|
| 994 |
class TestClass: |
| 995 |
def test_method(self): |
| 996 |
doskip("test method") |
| 997 |
|
| 998 |
@pytest.mark.skip("via_decorator") # LINE 14 |
| 999 |
def test_deco(self): |
| 1000 |
assert 0 |
| 1001 |
""", |
| 1002 |
helpers=""" |
| 1003 |
import pytest, sys |
| 1004 |
def doskip(reason): |
| 1005 |
assert sys._getframe().f_lineno == 3 |
| 1006 |
pytest.skip(reason) # LINE 4 |
| 1007 |
""", |
| 1008 |
) |
| 1009 |
result = pytester.runpytest("-rs") |
| 1010 |
result.stdout.fnmatch_lines_random( |
| 1011 |
[ |
| 1012 |
"SKIPPED [[]1[]] test_one.py:7: setup function", |
| 1013 |
"SKIPPED [[]1[]] helpers.py:4: test method", |
| 1014 |
"SKIPPED [[]1[]] test_one.py:14: via_decorator", |
| 1015 |
] |
| 1016 |
) |
| 1017 |
assert result.ret == 0 |
| 1018 |
|
| 1019 |
|
| 1020 |
def test_skipped_folding(pytester: Pytester) -> None: |
| 1021 |
pytester.makepyfile( |
| 1022 |
test_one=""" |
| 1023 |
import pytest |
| 1024 |
pytestmark = pytest.mark.skip("Folding") |
| 1025 |
def setup_function(func): |
| 1026 |
pass |
| 1027 |
def test_func(): |
| 1028 |
pass |
| 1029 |
class TestClass(object): |
| 1030 |
def test_method(self): |
| 1031 |
pass |
| 1032 |
""" |
| 1033 |
) |
| 1034 |
result = pytester.runpytest("-rs") |
| 1035 |
result.stdout.fnmatch_lines(["*SKIP*2*test_one.py: Folding"]) |
| 1036 |
assert result.ret == 0 |
| 1037 |
|
| 1038 |
|
| 1039 |
def test_reportchars(pytester: Pytester) -> None: |
| 1040 |
pytester.makepyfile( |
| 1041 |
""" |
| 1042 |
import pytest |
| 1043 |
def test_1(): |
| 1044 |
assert 0 |
| 1045 |
@pytest.mark.xfail |
| 1046 |
def test_2(): |
| 1047 |
assert 0 |
| 1048 |
@pytest.mark.xfail |
| 1049 |
def test_3(): |
| 1050 |
pass |
| 1051 |
def test_4(): |
| 1052 |
pytest.skip("four") |
| 1053 |
""" |
| 1054 |
) |
| 1055 |
result = pytester.runpytest("-rfxXs") |
| 1056 |
result.stdout.fnmatch_lines( |
| 1057 |
["FAIL*test_1*", "XFAIL*test_2*", "XPASS*test_3*", "SKIP*four*"] |
| 1058 |
) |
| 1059 |
|
| 1060 |
|
| 1061 |
def test_reportchars_error(pytester: Pytester) -> None: |
| 1062 |
pytester.makepyfile( |
| 1063 |
conftest=""" |
| 1064 |
def pytest_runtest_teardown(): |
| 1065 |
assert 0 |
| 1066 |
""", |
| 1067 |
test_simple=""" |
| 1068 |
def test_foo(): |
| 1069 |
pass |
| 1070 |
""", |
| 1071 |
) |
| 1072 |
result = pytester.runpytest("-rE") |
| 1073 |
result.stdout.fnmatch_lines(["ERROR*test_foo*"]) |
| 1074 |
|
| 1075 |
|
| 1076 |
def test_reportchars_all(pytester: Pytester) -> None: |
| 1077 |
pytester.makepyfile( |
| 1078 |
""" |
| 1079 |
import pytest |
| 1080 |
def test_1(): |
| 1081 |
assert 0 |
| 1082 |
@pytest.mark.xfail |
| 1083 |
def test_2(): |
| 1084 |
assert 0 |
| 1085 |
@pytest.mark.xfail |
| 1086 |
def test_3(): |
| 1087 |
pass |
| 1088 |
def test_4(): |
| 1089 |
pytest.skip("four") |
| 1090 |
@pytest.fixture |
| 1091 |
def fail(): |
| 1092 |
assert 0 |
| 1093 |
def test_5(fail): |
| 1094 |
pass |
| 1095 |
""" |
| 1096 |
) |
| 1097 |
result = pytester.runpytest("-ra") |
| 1098 |
result.stdout.fnmatch_lines( |
| 1099 |
[ |
| 1100 |
"SKIP*four*", |
| 1101 |
"XFAIL*test_2*", |
| 1102 |
"XPASS*test_3*", |
| 1103 |
"ERROR*test_5*", |
| 1104 |
"FAIL*test_1*", |
| 1105 |
] |
| 1106 |
) |
| 1107 |
|
| 1108 |
|
| 1109 |
def test_reportchars_all_error(pytester: Pytester) -> None: |
| 1110 |
pytester.makepyfile( |
| 1111 |
conftest=""" |
| 1112 |
def pytest_runtest_teardown(): |
| 1113 |
assert 0 |
| 1114 |
""", |
| 1115 |
test_simple=""" |
| 1116 |
def test_foo(): |
| 1117 |
pass |
| 1118 |
""", |
| 1119 |
) |
| 1120 |
result = pytester.runpytest("-ra") |
| 1121 |
result.stdout.fnmatch_lines(["ERROR*test_foo*"]) |
| 1122 |
|
| 1123 |
|
| 1124 |
def test_errors_in_xfail_skip_expressions(pytester: Pytester) -> None: |
| 1125 |
pytester.makepyfile( |
| 1126 |
""" |
| 1127 |
import pytest |
| 1128 |
@pytest.mark.skipif("asd") |
| 1129 |
def test_nameerror(): |
| 1130 |
pass |
| 1131 |
@pytest.mark.xfail("syntax error") |
| 1132 |
def test_syntax(): |
| 1133 |
pass |
| 1134 |
|
| 1135 |
def test_func(): |
| 1136 |
pass |
| 1137 |
""" |
| 1138 |
) |
| 1139 |
result = pytester.runpytest() |
| 1140 |
|
| 1141 |
expected = [ |
| 1142 |
"*ERROR*test_nameerror*", |
| 1143 |
"*asd*", |
| 1144 |
"", |
| 1145 |
"During handling of the above exception, another exception occurred:", |
| 1146 |
] |
| 1147 |
|
| 1148 |
expected += [ |
| 1149 |
"*evaluating*skipif*condition*", |
| 1150 |
"*asd*", |
| 1151 |
"*ERROR*test_syntax*", |
| 1152 |
"*evaluating*xfail*condition*", |
| 1153 |
" syntax error", |
| 1154 |
" ^", |
| 1155 |
"SyntaxError: invalid syntax", |
| 1156 |
"*1 pass*2 errors*", |
| 1157 |
] |
| 1158 |
result.stdout.fnmatch_lines(expected) |
| 1159 |
|
| 1160 |
|
| 1161 |
def test_xfail_skipif_with_globals(pytester: Pytester) -> None: |
| 1162 |
pytester.makepyfile( |
| 1163 |
""" |
| 1164 |
import pytest |
| 1165 |
x = 3 |
| 1166 |
@pytest.mark.skipif("x == 3") |
| 1167 |
def test_skip1(): |
| 1168 |
pass |
| 1169 |
@pytest.mark.xfail("x == 3") |
| 1170 |
def test_boolean(): |
| 1171 |
assert 0 |
| 1172 |
""" |
| 1173 |
) |
| 1174 |
result = pytester.runpytest("-rsx") |
| 1175 |
result.stdout.fnmatch_lines(["*SKIP*x == 3*", "*XFAIL*test_boolean*x == 3*"]) |
| 1176 |
|
| 1177 |
|
| 1178 |
def test_default_markers(pytester: Pytester) -> None: |
| 1179 |
result = pytester.runpytest("--markers") |
| 1180 |
result.stdout.fnmatch_lines( |
| 1181 |
[ |
| 1182 |
"*skipif(condition, ..., [*], reason=...)*skip*", |
| 1183 |
"*xfail(condition, ..., [*], reason=..., run=True, raises=None, strict=strict_xfail)*expected failure*", |
| 1184 |
] |
| 1185 |
) |
| 1186 |
|
| 1187 |
|
| 1188 |
def test_xfail_test_setup_exception(pytester: Pytester) -> None: |
| 1189 |
pytester.makeconftest( |
| 1190 |
""" |
| 1191 |
def pytest_runtest_setup(): |
| 1192 |
0 / 0 |
| 1193 |
""" |
| 1194 |
) |
| 1195 |
p = pytester.makepyfile( |
| 1196 |
""" |
| 1197 |
import pytest |
| 1198 |
@pytest.mark.xfail |
| 1199 |
def test_func(): |
| 1200 |
assert 0 |
| 1201 |
""" |
| 1202 |
) |
| 1203 |
result = pytester.runpytest(p) |
| 1204 |
assert result.ret == 0 |
| 1205 |
assert "xfailed" in result.stdout.str() |
| 1206 |
result.stdout.no_fnmatch_line("*xpassed*") |
| 1207 |
|
| 1208 |
|
| 1209 |
def test_imperativeskip_on_xfail_test(pytester: Pytester) -> None: |
| 1210 |
pytester.makepyfile( |
| 1211 |
""" |
| 1212 |
import pytest |
| 1213 |
@pytest.mark.xfail |
| 1214 |
def test_that_fails(): |
| 1215 |
assert 0 |
| 1216 |
|
| 1217 |
@pytest.mark.skipif("True") |
| 1218 |
def test_hello(): |
| 1219 |
pass |
| 1220 |
""" |
| 1221 |
) |
| 1222 |
pytester.makeconftest( |
| 1223 |
""" |
| 1224 |
import pytest |
| 1225 |
def pytest_runtest_setup(item): |
| 1226 |
pytest.skip("abc") |
| 1227 |
""" |
| 1228 |
) |
| 1229 |
result = pytester.runpytest("-rsxX") |
| 1230 |
result.stdout.fnmatch_lines_random( |
| 1231 |
""" |
| 1232 |
*SKIP*abc* |
| 1233 |
*SKIP*condition: True* |
| 1234 |
*2 skipped* |
| 1235 |
""" |
| 1236 |
) |
| 1237 |
|
| 1238 |
|
| 1239 |
class TestBooleanCondition: |
| 1240 |
def test_skipif(self, pytester: Pytester) -> None: |
| 1241 |
pytester.makepyfile( |
| 1242 |
""" |
| 1243 |
import pytest |
| 1244 |
@pytest.mark.skipif(True, reason="True123") |
| 1245 |
def test_func1(): |
| 1246 |
pass |
| 1247 |
@pytest.mark.skipif(False, reason="True123") |
| 1248 |
def test_func2(): |
| 1249 |
pass |
| 1250 |
""" |
| 1251 |
) |
| 1252 |
result = pytester.runpytest() |
| 1253 |
result.stdout.fnmatch_lines( |
| 1254 |
""" |
| 1255 |
*1 passed*1 skipped* |
| 1256 |
""" |
| 1257 |
) |
| 1258 |
|
| 1259 |
def test_skipif_noreason(self, pytester: Pytester) -> None: |
| 1260 |
pytester.makepyfile( |
| 1261 |
""" |
| 1262 |
import pytest |
| 1263 |
@pytest.mark.skipif(True) |
| 1264 |
def test_func(): |
| 1265 |
pass |
| 1266 |
""" |
| 1267 |
) |
| 1268 |
result = pytester.runpytest("-rs") |
| 1269 |
result.stdout.fnmatch_lines( |
| 1270 |
""" |
| 1271 |
*1 error* |
| 1272 |
""" |
| 1273 |
) |
| 1274 |
|
| 1275 |
def test_xfail(self, pytester: Pytester) -> None: |
| 1276 |
pytester.makepyfile( |
| 1277 |
""" |
| 1278 |
import pytest |
| 1279 |
@pytest.mark.xfail(True, reason="True123") |
| 1280 |
def test_func(): |
| 1281 |
assert 0 |
| 1282 |
""" |
| 1283 |
) |
| 1284 |
result = pytester.runpytest("-rxs") |
| 1285 |
result.stdout.fnmatch_lines( |
| 1286 |
""" |
| 1287 |
*XFAIL*True123* |
| 1288 |
*1 xfail* |
| 1289 |
""" |
| 1290 |
) |
| 1291 |
|
| 1292 |
|
| 1293 |
def test_xfail_item(pytester: Pytester) -> None: |
| 1294 |
# Ensure pytest.xfail works with non-Python Item |
| 1295 |
pytester.makeconftest( |
| 1296 |
""" |
| 1297 |
import pytest |
| 1298 |
|
| 1299 |
class MyItem(pytest.Item): |
| 1300 |
nodeid = 'foo' |
| 1301 |
def runtest(self): |
| 1302 |
pytest.xfail("Expected Failure") |
| 1303 |
|
| 1304 |
def pytest_collect_file(file_path, parent): |
| 1305 |
return MyItem.from_parent(name="foo", parent=parent) |
| 1306 |
""" |
| 1307 |
) |
| 1308 |
result = pytester.inline_run() |
| 1309 |
_passed, skipped, failed = result.listoutcomes() |
| 1310 |
assert not failed |
| 1311 |
xfailed = [r for r in skipped if hasattr(r, "wasxfail")] |
| 1312 |
assert xfailed |
| 1313 |
|
| 1314 |
|
| 1315 |
def test_module_level_skip_error(pytester: Pytester) -> None: |
| 1316 |
"""Verify that using pytest.skip at module level causes a collection error.""" |
| 1317 |
pytester.makepyfile( |
| 1318 |
""" |
| 1319 |
import pytest |
| 1320 |
pytest.skip("skip_module_level") |
| 1321 |
|
| 1322 |
def test_func(): |
| 1323 |
assert True |
| 1324 |
""" |
| 1325 |
) |
| 1326 |
result = pytester.runpytest() |
| 1327 |
result.stdout.fnmatch_lines( |
| 1328 |
["*Using pytest.skip outside of a test will skip the entire module*"] |
| 1329 |
) |
| 1330 |
|
| 1331 |
|
| 1332 |
def test_module_level_skip_with_allow_module_level(pytester: Pytester) -> None: |
| 1333 |
"""Verify that using pytest.skip(allow_module_level=True) is allowed.""" |
| 1334 |
pytester.makepyfile( |
| 1335 |
""" |
| 1336 |
import pytest |
| 1337 |
pytest.skip("skip_module_level", allow_module_level=True) |
| 1338 |
|
| 1339 |
def test_func(): |
| 1340 |
assert 0 |
| 1341 |
""" |
| 1342 |
) |
| 1343 |
result = pytester.runpytest("-rxs") |
| 1344 |
result.stdout.fnmatch_lines(["*SKIP*skip_module_level"]) |
| 1345 |
|
| 1346 |
|
| 1347 |
def test_invalid_skip_keyword_parameter(pytester: Pytester) -> None: |
| 1348 |
"""Verify that using pytest.skip() with unknown parameter raises an error.""" |
| 1349 |
pytester.makepyfile( |
| 1350 |
""" |
| 1351 |
import pytest |
| 1352 |
pytest.skip("skip_module_level", unknown=1) |
| 1353 |
|
| 1354 |
def test_func(): |
| 1355 |
assert 0 |
| 1356 |
""" |
| 1357 |
) |
| 1358 |
result = pytester.runpytest() |
| 1359 |
result.stdout.fnmatch_lines(["*TypeError:*['unknown']*"]) |
| 1360 |
|
| 1361 |
|
| 1362 |
def test_mark_xfail_item(pytester: Pytester) -> None: |
| 1363 |
# Ensure pytest.mark.xfail works with non-Python Item |
| 1364 |
pytester.makeconftest( |
| 1365 |
""" |
| 1366 |
import pytest |
| 1367 |
|
| 1368 |
class MyItem(pytest.Item): |
| 1369 |
nodeid = 'foo' |
| 1370 |
def setup(self): |
| 1371 |
marker = pytest.mark.xfail("1 == 2", reason="Expected failure - false") |
| 1372 |
self.add_marker(marker) |
| 1373 |
marker = pytest.mark.xfail(True, reason="Expected failure - true") |
| 1374 |
self.add_marker(marker) |
| 1375 |
def runtest(self): |
| 1376 |
assert False |
| 1377 |
|
| 1378 |
def pytest_collect_file(file_path, parent): |
| 1379 |
return MyItem.from_parent(name="foo", parent=parent) |
| 1380 |
""" |
| 1381 |
) |
| 1382 |
result = pytester.inline_run() |
| 1383 |
_passed, skipped, failed = result.listoutcomes() |
| 1384 |
assert not failed |
| 1385 |
xfailed = [r for r in skipped if hasattr(r, "wasxfail")] |
| 1386 |
assert xfailed |
| 1387 |
|
| 1388 |
|
| 1389 |
def test_summary_list_after_errors(pytester: Pytester) -> None: |
| 1390 |
"""Ensure the list of errors/fails/xfails/skips appears after tracebacks in terminal reporting.""" |
| 1391 |
pytester.makepyfile( |
| 1392 |
""" |
| 1393 |
import pytest |
| 1394 |
def test_fail(): |
| 1395 |
assert 0 |
| 1396 |
""" |
| 1397 |
) |
| 1398 |
result = pytester.runpytest("-ra") |
| 1399 |
result.stdout.fnmatch_lines( |
| 1400 |
[ |
| 1401 |
"=* FAILURES *=", |
| 1402 |
"*= short test summary info =*", |
| 1403 |
"FAILED test_summary_list_after_errors.py::test_fail - assert 0", |
| 1404 |
] |
| 1405 |
) |
| 1406 |
|
| 1407 |
|
| 1408 |
def test_importorskip() -> None: |
| 1409 |
with pytest.raises( |
| 1410 |
pytest.skip.Exception, |
| 1411 |
match=r"^could not import 'doesnotexist': No module named .*", |
| 1412 |
): |
| 1413 |
pytest.importorskip("doesnotexist") |
| 1414 |
|
| 1415 |
|
| 1416 |
def test_relpath_rootdir(pytester: Pytester) -> None: |
| 1417 |
pytester.makepyfile( |
| 1418 |
**{ |
| 1419 |
"tests/test_1.py": """ |
| 1420 |
import pytest |
| 1421 |
@pytest.mark.skip() |
| 1422 |
def test_pass(): |
| 1423 |
pass |
| 1424 |
""", |
| 1425 |
} |
| 1426 |
) |
| 1427 |
result = pytester.runpytest("-rs", "tests/test_1.py", "--rootdir=tests") |
| 1428 |
result.stdout.fnmatch_lines( |
| 1429 |
["SKIPPED [[]1[]] tests/test_1.py:2: unconditional skip"] |
| 1430 |
) |
| 1431 |
|
| 1432 |
|
| 1433 |
def test_skip_from_fixture(pytester: Pytester) -> None: |
| 1434 |
pytester.makepyfile( |
| 1435 |
**{ |
| 1436 |
"tests/test_1.py": """ |
| 1437 |
import pytest |
| 1438 |
def test_pass(arg): |
| 1439 |
pass |
| 1440 |
@pytest.fixture |
| 1441 |
def arg(): |
| 1442 |
condition = True |
| 1443 |
if condition: |
| 1444 |
pytest.skip("Fixture conditional skip") |
| 1445 |
""", |
| 1446 |
} |
| 1447 |
) |
| 1448 |
result = pytester.runpytest("-rs", "tests/test_1.py", "--rootdir=tests") |
| 1449 |
result.stdout.fnmatch_lines( |
| 1450 |
["SKIPPED [[]1[]] tests/test_1.py:2: Fixture conditional skip"] |
| 1451 |
) |
| 1452 |
|
| 1453 |
|
| 1454 |
def test_skip_using_reason_works_ok(pytester: Pytester) -> None: |
| 1455 |
p = pytester.makepyfile( |
| 1456 |
""" |
| 1457 |
import pytest |
| 1458 |
|
| 1459 |
def test_skipping_reason(): |
| 1460 |
pytest.skip(reason="skippedreason") |
| 1461 |
""" |
| 1462 |
) |
| 1463 |
result = pytester.runpytest(p) |
| 1464 |
result.stdout.no_fnmatch_line("*PytestDeprecationWarning*") |
| 1465 |
result.assert_outcomes(skipped=1) |
| 1466 |
|
| 1467 |
|
| 1468 |
def test_fail_using_reason_works_ok(pytester: Pytester) -> None: |
| 1469 |
p = pytester.makepyfile( |
| 1470 |
""" |
| 1471 |
import pytest |
| 1472 |
|
| 1473 |
def test_failing_reason(): |
| 1474 |
pytest.fail(reason="failedreason") |
| 1475 |
""" |
| 1476 |
) |
| 1477 |
result = pytester.runpytest(p) |
| 1478 |
result.stdout.no_fnmatch_line("*PytestDeprecationWarning*") |
| 1479 |
result.assert_outcomes(failed=1) |
| 1480 |
|
| 1481 |
|
| 1482 |
def test_exit_with_reason_works_ok(pytester: Pytester) -> None: |
| 1483 |
p = pytester.makepyfile( |
| 1484 |
""" |
| 1485 |
import pytest |
| 1486 |
|
| 1487 |
def test_exit_reason_only(): |
| 1488 |
pytest.exit(reason="foo") |
| 1489 |
""" |
| 1490 |
) |
| 1491 |
result = pytester.runpytest(p) |
| 1492 |
result.stdout.fnmatch_lines("*_pytest.outcomes.Exit: foo*") |