| 1 |
# mypy: allow-untyped-defs |
| 2 |
import os |
| 3 |
import sys |
| 4 |
from typing import List |
| 5 |
from typing import Optional |
| 6 |
from unittest import mock |
| 7 |
|
| 8 |
from _pytest.config import ExitCode |
| 9 |
from _pytest.mark import MarkGenerator |
| 10 |
from _pytest.mark.structures import EMPTY_PARAMETERSET_OPTION |
| 11 |
from _pytest.nodes import Collector |
| 12 |
from _pytest.nodes import Node |
| 13 |
from _pytest.pytester import Pytester |
| 14 |
import pytest |
| 15 |
|
| 16 |
|
| 17 |
class TestMark: |
| 18 |
@pytest.mark.parametrize("attr", ["mark", "param"]) |
| 19 |
def test_pytest_exists_in_namespace_all(self, attr: str) -> None: |
| 20 |
module = sys.modules["pytest"] |
| 21 |
assert attr in module.__all__ |
| 22 |
|
| 23 |
def test_pytest_mark_notcallable(self) -> None: |
| 24 |
mark = MarkGenerator(_ispytest=True) |
| 25 |
with pytest.raises(TypeError): |
| 26 |
mark() # type: ignore[operator] |
| 27 |
|
| 28 |
def test_mark_with_param(self): |
| 29 |
def some_function(abc): |
| 30 |
pass |
| 31 |
|
| 32 |
class SomeClass: |
| 33 |
pass |
| 34 |
|
| 35 |
assert pytest.mark.foo(some_function) is some_function |
| 36 |
marked_with_args = pytest.mark.foo.with_args(some_function) |
| 37 |
assert marked_with_args is not some_function |
| 38 |
|
| 39 |
assert pytest.mark.foo(SomeClass) is SomeClass |
| 40 |
assert pytest.mark.foo.with_args(SomeClass) is not SomeClass # type: ignore[comparison-overlap] |
| 41 |
|
| 42 |
def test_pytest_mark_name_starts_with_underscore(self) -> None: |
| 43 |
mark = MarkGenerator(_ispytest=True) |
| 44 |
with pytest.raises(AttributeError): |
| 45 |
_ = mark._some_name |
| 46 |
|
| 47 |
|
| 48 |
def test_marked_class_run_twice(pytester: Pytester) -> None: |
| 49 |
"""Test fails file is run twice that contains marked class. |
| 50 |
See issue#683. |
| 51 |
""" |
| 52 |
py_file = pytester.makepyfile( |
| 53 |
""" |
| 54 |
import pytest |
| 55 |
@pytest.mark.parametrize('abc', [1, 2, 3]) |
| 56 |
class Test1(object): |
| 57 |
def test_1(self, abc): |
| 58 |
assert abc in [1, 2, 3] |
| 59 |
""" |
| 60 |
) |
| 61 |
file_name = os.path.basename(py_file) |
| 62 |
rec = pytester.inline_run(file_name, file_name) |
| 63 |
rec.assertoutcome(passed=6) |
| 64 |
|
| 65 |
|
| 66 |
def test_ini_markers(pytester: Pytester) -> None: |
| 67 |
pytester.makeini( |
| 68 |
""" |
| 69 |
[pytest] |
| 70 |
markers = |
| 71 |
a1: this is a webtest marker |
| 72 |
a2: this is a smoke marker |
| 73 |
""" |
| 74 |
) |
| 75 |
pytester.makepyfile( |
| 76 |
""" |
| 77 |
def test_markers(pytestconfig): |
| 78 |
markers = pytestconfig.getini("markers") |
| 79 |
print(markers) |
| 80 |
assert len(markers) >= 2 |
| 81 |
assert markers[0].startswith("a1:") |
| 82 |
assert markers[1].startswith("a2:") |
| 83 |
""" |
| 84 |
) |
| 85 |
rec = pytester.inline_run() |
| 86 |
rec.assertoutcome(passed=1) |
| 87 |
|
| 88 |
|
| 89 |
def test_markers_option(pytester: Pytester) -> None: |
| 90 |
pytester.makeini( |
| 91 |
""" |
| 92 |
[pytest] |
| 93 |
markers = |
| 94 |
a1: this is a webtest marker |
| 95 |
a1some: another marker |
| 96 |
nodescription |
| 97 |
""" |
| 98 |
) |
| 99 |
result = pytester.runpytest("--markers") |
| 100 |
result.stdout.fnmatch_lines( |
| 101 |
["*a1*this is a webtest*", "*a1some*another marker", "*nodescription*"] |
| 102 |
) |
| 103 |
|
| 104 |
|
| 105 |
def test_ini_markers_whitespace(pytester: Pytester) -> None: |
| 106 |
pytester.makeini( |
| 107 |
""" |
| 108 |
[pytest] |
| 109 |
markers = |
| 110 |
a1 : this is a whitespace marker |
| 111 |
""" |
| 112 |
) |
| 113 |
pytester.makepyfile( |
| 114 |
""" |
| 115 |
import pytest |
| 116 |
|
| 117 |
@pytest.mark.a1 |
| 118 |
def test_markers(): |
| 119 |
assert True |
| 120 |
""" |
| 121 |
) |
| 122 |
rec = pytester.inline_run("--strict-markers", "-m", "a1") |
| 123 |
rec.assertoutcome(passed=1) |
| 124 |
|
| 125 |
|
| 126 |
def test_marker_without_description(pytester: Pytester) -> None: |
| 127 |
pytester.makefile( |
| 128 |
".cfg", |
| 129 |
setup=""" |
| 130 |
[tool:pytest] |
| 131 |
markers=slow |
| 132 |
""", |
| 133 |
) |
| 134 |
pytester.makeconftest( |
| 135 |
""" |
| 136 |
import pytest |
| 137 |
pytest.mark.xfail('FAIL') |
| 138 |
""" |
| 139 |
) |
| 140 |
ftdir = pytester.mkdir("ft1_dummy") |
| 141 |
pytester.path.joinpath("conftest.py").replace(ftdir.joinpath("conftest.py")) |
| 142 |
rec = pytester.runpytest("--strict-markers") |
| 143 |
rec.assert_outcomes() |
| 144 |
|
| 145 |
|
| 146 |
def test_markers_option_with_plugin_in_current_dir(pytester: Pytester) -> None: |
| 147 |
pytester.makeconftest('pytest_plugins = "flip_flop"') |
| 148 |
pytester.makepyfile( |
| 149 |
flip_flop="""\ |
| 150 |
def pytest_configure(config): |
| 151 |
config.addinivalue_line("markers", "flip:flop") |
| 152 |
|
| 153 |
def pytest_generate_tests(metafunc): |
| 154 |
try: |
| 155 |
mark = metafunc.function.flipper |
| 156 |
except AttributeError: |
| 157 |
return |
| 158 |
metafunc.parametrize("x", (10, 20))""" |
| 159 |
) |
| 160 |
pytester.makepyfile( |
| 161 |
"""\ |
| 162 |
import pytest |
| 163 |
@pytest.mark.flipper |
| 164 |
def test_example(x): |
| 165 |
assert x""" |
| 166 |
) |
| 167 |
|
| 168 |
result = pytester.runpytest("--markers") |
| 169 |
result.stdout.fnmatch_lines(["*flip*flop*"]) |
| 170 |
|
| 171 |
|
| 172 |
def test_mark_on_pseudo_function(pytester: Pytester) -> None: |
| 173 |
pytester.makepyfile( |
| 174 |
""" |
| 175 |
import pytest |
| 176 |
|
| 177 |
@pytest.mark.r(lambda x: 0/0) |
| 178 |
def test_hello(): |
| 179 |
pass |
| 180 |
""" |
| 181 |
) |
| 182 |
reprec = pytester.inline_run() |
| 183 |
reprec.assertoutcome(passed=1) |
| 184 |
|
| 185 |
|
| 186 |
@pytest.mark.parametrize("option_name", ["--strict-markers", "--strict"]) |
| 187 |
def test_strict_prohibits_unregistered_markers( |
| 188 |
pytester: Pytester, option_name: str |
| 189 |
) -> None: |
| 190 |
pytester.makepyfile( |
| 191 |
""" |
| 192 |
import pytest |
| 193 |
@pytest.mark.unregisteredmark |
| 194 |
def test_hello(): |
| 195 |
pass |
| 196 |
""" |
| 197 |
) |
| 198 |
result = pytester.runpytest(option_name) |
| 199 |
assert result.ret != 0 |
| 200 |
result.stdout.fnmatch_lines( |
| 201 |
["'unregisteredmark' not found in `markers` configuration option"] |
| 202 |
) |
| 203 |
|
| 204 |
|
| 205 |
@pytest.mark.parametrize( |
| 206 |
("expr", "expected_passed"), |
| 207 |
[ |
| 208 |
("xyz", ["test_one"]), |
| 209 |
("((( xyz)) )", ["test_one"]), |
| 210 |
("not not xyz", ["test_one"]), |
| 211 |
("xyz and xyz2", []), |
| 212 |
("xyz2", ["test_two"]), |
| 213 |
("xyz or xyz2", ["test_one", "test_two"]), |
| 214 |
], |
| 215 |
) |
| 216 |
def test_mark_option( |
| 217 |
expr: str, expected_passed: List[Optional[str]], pytester: Pytester |
| 218 |
) -> None: |
| 219 |
pytester.makepyfile( |
| 220 |
""" |
| 221 |
import pytest |
| 222 |
@pytest.mark.xyz |
| 223 |
def test_one(): |
| 224 |
pass |
| 225 |
@pytest.mark.xyz2 |
| 226 |
def test_two(): |
| 227 |
pass |
| 228 |
""" |
| 229 |
) |
| 230 |
rec = pytester.inline_run("-m", expr) |
| 231 |
passed, skipped, fail = rec.listoutcomes() |
| 232 |
passed_str = [x.nodeid.split("::")[-1] for x in passed] |
| 233 |
assert passed_str == expected_passed |
| 234 |
|
| 235 |
|
| 236 |
@pytest.mark.parametrize( |
| 237 |
("expr", "expected_passed"), |
| 238 |
[("interface", ["test_interface"]), ("not interface", ["test_nointer"])], |
| 239 |
) |
| 240 |
def test_mark_option_custom( |
| 241 |
expr: str, expected_passed: List[str], pytester: Pytester |
| 242 |
) -> None: |
| 243 |
pytester.makeconftest( |
| 244 |
""" |
| 245 |
import pytest |
| 246 |
def pytest_collection_modifyitems(items): |
| 247 |
for item in items: |
| 248 |
if "interface" in item.nodeid: |
| 249 |
item.add_marker(pytest.mark.interface) |
| 250 |
""" |
| 251 |
) |
| 252 |
pytester.makepyfile( |
| 253 |
""" |
| 254 |
def test_interface(): |
| 255 |
pass |
| 256 |
def test_nointer(): |
| 257 |
pass |
| 258 |
""" |
| 259 |
) |
| 260 |
rec = pytester.inline_run("-m", expr) |
| 261 |
passed, skipped, fail = rec.listoutcomes() |
| 262 |
passed_str = [x.nodeid.split("::")[-1] for x in passed] |
| 263 |
assert passed_str == expected_passed |
| 264 |
|
| 265 |
|
| 266 |
@pytest.mark.parametrize( |
| 267 |
("expr", "expected_passed"), |
| 268 |
[ |
| 269 |
("interface", ["test_interface"]), |
| 270 |
("not interface", ["test_nointer", "test_pass", "test_1", "test_2"]), |
| 271 |
("pass", ["test_pass"]), |
| 272 |
("not pass", ["test_interface", "test_nointer", "test_1", "test_2"]), |
| 273 |
("not not not (pass)", ["test_interface", "test_nointer", "test_1", "test_2"]), |
| 274 |
("1 or 2", ["test_1", "test_2"]), |
| 275 |
("not (1 or 2)", ["test_interface", "test_nointer", "test_pass"]), |
| 276 |
], |
| 277 |
) |
| 278 |
def test_keyword_option_custom( |
| 279 |
expr: str, expected_passed: List[str], pytester: Pytester |
| 280 |
) -> None: |
| 281 |
pytester.makepyfile( |
| 282 |
""" |
| 283 |
def test_interface(): |
| 284 |
pass |
| 285 |
def test_nointer(): |
| 286 |
pass |
| 287 |
def test_pass(): |
| 288 |
pass |
| 289 |
def test_1(): |
| 290 |
pass |
| 291 |
def test_2(): |
| 292 |
pass |
| 293 |
""" |
| 294 |
) |
| 295 |
rec = pytester.inline_run("-k", expr) |
| 296 |
passed, skipped, fail = rec.listoutcomes() |
| 297 |
passed_str = [x.nodeid.split("::")[-1] for x in passed] |
| 298 |
assert passed_str == expected_passed |
| 299 |
|
| 300 |
|
| 301 |
def test_keyword_option_considers_mark(pytester: Pytester) -> None: |
| 302 |
pytester.copy_example("marks/marks_considered_keywords") |
| 303 |
rec = pytester.inline_run("-k", "foo") |
| 304 |
passed = rec.listoutcomes()[0] |
| 305 |
assert len(passed) == 1 |
| 306 |
|
| 307 |
|
| 308 |
@pytest.mark.parametrize( |
| 309 |
("expr", "expected_passed"), |
| 310 |
[ |
| 311 |
("None", ["test_func[None]"]), |
| 312 |
("[1.3]", ["test_func[1.3]"]), |
| 313 |
("2-3", ["test_func[2-3]"]), |
| 314 |
], |
| 315 |
) |
| 316 |
def test_keyword_option_parametrize( |
| 317 |
expr: str, expected_passed: List[str], pytester: Pytester |
| 318 |
) -> None: |
| 319 |
pytester.makepyfile( |
| 320 |
""" |
| 321 |
import pytest |
| 322 |
@pytest.mark.parametrize("arg", [None, 1.3, "2-3"]) |
| 323 |
def test_func(arg): |
| 324 |
pass |
| 325 |
""" |
| 326 |
) |
| 327 |
rec = pytester.inline_run("-k", expr) |
| 328 |
passed, skipped, fail = rec.listoutcomes() |
| 329 |
passed_str = [x.nodeid.split("::")[-1] for x in passed] |
| 330 |
assert passed_str == expected_passed |
| 331 |
|
| 332 |
|
| 333 |
def test_parametrize_with_module(pytester: Pytester) -> None: |
| 334 |
pytester.makepyfile( |
| 335 |
""" |
| 336 |
import pytest |
| 337 |
@pytest.mark.parametrize("arg", [pytest,]) |
| 338 |
def test_func(arg): |
| 339 |
pass |
| 340 |
""" |
| 341 |
) |
| 342 |
rec = pytester.inline_run() |
| 343 |
passed, skipped, fail = rec.listoutcomes() |
| 344 |
expected_id = "test_func[" + pytest.__name__ + "]" |
| 345 |
assert passed[0].nodeid.split("::")[-1] == expected_id |
| 346 |
|
| 347 |
|
| 348 |
@pytest.mark.parametrize( |
| 349 |
("expr", "expected_error"), |
| 350 |
[ |
| 351 |
( |
| 352 |
"foo or", |
| 353 |
"at column 7: expected not OR left parenthesis OR identifier; got end of input", |
| 354 |
), |
| 355 |
( |
| 356 |
"foo or or", |
| 357 |
"at column 8: expected not OR left parenthesis OR identifier; got or", |
| 358 |
), |
| 359 |
( |
| 360 |
"(foo", |
| 361 |
"at column 5: expected right parenthesis; got end of input", |
| 362 |
), |
| 363 |
( |
| 364 |
"foo bar", |
| 365 |
"at column 5: expected end of input; got identifier", |
| 366 |
), |
| 367 |
( |
| 368 |
"or or", |
| 369 |
"at column 1: expected not OR left parenthesis OR identifier; got or", |
| 370 |
), |
| 371 |
( |
| 372 |
"not or", |
| 373 |
"at column 5: expected not OR left parenthesis OR identifier; got or", |
| 374 |
), |
| 375 |
], |
| 376 |
) |
| 377 |
def test_keyword_option_wrong_arguments( |
| 378 |
expr: str, expected_error: str, pytester: Pytester, capsys |
| 379 |
) -> None: |
| 380 |
pytester.makepyfile( |
| 381 |
""" |
| 382 |
def test_func(arg): |
| 383 |
pass |
| 384 |
""" |
| 385 |
) |
| 386 |
pytester.inline_run("-k", expr) |
| 387 |
err = capsys.readouterr().err |
| 388 |
assert expected_error in err |
| 389 |
|
| 390 |
|
| 391 |
def test_parametrized_collected_from_command_line(pytester: Pytester) -> None: |
| 392 |
"""Parametrized test not collected if test named specified in command |
| 393 |
line issue#649.""" |
| 394 |
py_file = pytester.makepyfile( |
| 395 |
""" |
| 396 |
import pytest |
| 397 |
@pytest.mark.parametrize("arg", [None, 1.3, "2-3"]) |
| 398 |
def test_func(arg): |
| 399 |
pass |
| 400 |
""" |
| 401 |
) |
| 402 |
file_name = os.path.basename(py_file) |
| 403 |
rec = pytester.inline_run(file_name + "::" + "test_func") |
| 404 |
rec.assertoutcome(passed=3) |
| 405 |
|
| 406 |
|
| 407 |
def test_parametrized_collect_with_wrong_args(pytester: Pytester) -> None: |
| 408 |
"""Test collect parametrized func with wrong number of args.""" |
| 409 |
py_file = pytester.makepyfile( |
| 410 |
""" |
| 411 |
import pytest |
| 412 |
|
| 413 |
@pytest.mark.parametrize('foo, bar', [(1, 2, 3)]) |
| 414 |
def test_func(foo, bar): |
| 415 |
pass |
| 416 |
""" |
| 417 |
) |
| 418 |
|
| 419 |
result = pytester.runpytest(py_file) |
| 420 |
result.stdout.fnmatch_lines( |
| 421 |
[ |
| 422 |
'test_parametrized_collect_with_wrong_args.py::test_func: in "parametrize" the number of names (2):', |
| 423 |
" ['foo', 'bar']", |
| 424 |
"must be equal to the number of values (3):", |
| 425 |
" (1, 2, 3)", |
| 426 |
] |
| 427 |
) |
| 428 |
|
| 429 |
|
| 430 |
def test_parametrized_with_kwargs(pytester: Pytester) -> None: |
| 431 |
"""Test collect parametrized func with wrong number of args.""" |
| 432 |
py_file = pytester.makepyfile( |
| 433 |
""" |
| 434 |
import pytest |
| 435 |
|
| 436 |
@pytest.fixture(params=[1,2]) |
| 437 |
def a(request): |
| 438 |
return request.param |
| 439 |
|
| 440 |
@pytest.mark.parametrize(argnames='b', argvalues=[1, 2]) |
| 441 |
def test_func(a, b): |
| 442 |
pass |
| 443 |
""" |
| 444 |
) |
| 445 |
|
| 446 |
result = pytester.runpytest(py_file) |
| 447 |
assert result.ret == 0 |
| 448 |
|
| 449 |
|
| 450 |
def test_parametrize_iterator(pytester: Pytester) -> None: |
| 451 |
"""`parametrize` should work with generators (#5354).""" |
| 452 |
py_file = pytester.makepyfile( |
| 453 |
"""\ |
| 454 |
import pytest |
| 455 |
|
| 456 |
def gen(): |
| 457 |
yield 1 |
| 458 |
yield 2 |
| 459 |
yield 3 |
| 460 |
|
| 461 |
@pytest.mark.parametrize('a', gen()) |
| 462 |
def test(a): |
| 463 |
assert a >= 1 |
| 464 |
""" |
| 465 |
) |
| 466 |
result = pytester.runpytest(py_file) |
| 467 |
assert result.ret == 0 |
| 468 |
# should not skip any tests |
| 469 |
result.stdout.fnmatch_lines(["*3 passed*"]) |
| 470 |
|
| 471 |
|
| 472 |
class TestFunctional: |
| 473 |
def test_merging_markers_deep(self, pytester: Pytester) -> None: |
| 474 |
# issue 199 - propagate markers into nested classes |
| 475 |
p = pytester.makepyfile( |
| 476 |
""" |
| 477 |
import pytest |
| 478 |
class TestA(object): |
| 479 |
pytestmark = pytest.mark.a |
| 480 |
def test_b(self): |
| 481 |
assert True |
| 482 |
class TestC(object): |
| 483 |
# this one didn't get marked |
| 484 |
def test_d(self): |
| 485 |
assert True |
| 486 |
""" |
| 487 |
) |
| 488 |
items, rec = pytester.inline_genitems(p) |
| 489 |
for item in items: |
| 490 |
print(item, item.keywords) |
| 491 |
assert [x for x in item.iter_markers() if x.name == "a"] |
| 492 |
|
| 493 |
def test_mark_decorator_subclass_does_not_propagate_to_base( |
| 494 |
self, pytester: Pytester |
| 495 |
) -> None: |
| 496 |
p = pytester.makepyfile( |
| 497 |
""" |
| 498 |
import pytest |
| 499 |
|
| 500 |
@pytest.mark.a |
| 501 |
class Base(object): pass |
| 502 |
|
| 503 |
@pytest.mark.b |
| 504 |
class Test1(Base): |
| 505 |
def test_foo(self): pass |
| 506 |
|
| 507 |
class Test2(Base): |
| 508 |
def test_bar(self): pass |
| 509 |
""" |
| 510 |
) |
| 511 |
items, rec = pytester.inline_genitems(p) |
| 512 |
self.assert_markers(items, test_foo=("a", "b"), test_bar=("a",)) |
| 513 |
|
| 514 |
def test_mark_should_not_pass_to_siebling_class(self, pytester: Pytester) -> None: |
| 515 |
"""#568""" |
| 516 |
p = pytester.makepyfile( |
| 517 |
""" |
| 518 |
import pytest |
| 519 |
|
| 520 |
class TestBase(object): |
| 521 |
def test_foo(self): |
| 522 |
pass |
| 523 |
|
| 524 |
@pytest.mark.b |
| 525 |
class TestSub(TestBase): |
| 526 |
pass |
| 527 |
|
| 528 |
|
| 529 |
class TestOtherSub(TestBase): |
| 530 |
pass |
| 531 |
|
| 532 |
""" |
| 533 |
) |
| 534 |
items, rec = pytester.inline_genitems(p) |
| 535 |
base_item, sub_item, sub_item_other = items |
| 536 |
print(items, [x.nodeid for x in items]) |
| 537 |
# new api segregates |
| 538 |
assert not list(base_item.iter_markers(name="b")) |
| 539 |
assert not list(sub_item_other.iter_markers(name="b")) |
| 540 |
assert list(sub_item.iter_markers(name="b")) |
| 541 |
|
| 542 |
def test_mark_decorator_baseclasses_merged(self, pytester: Pytester) -> None: |
| 543 |
p = pytester.makepyfile( |
| 544 |
""" |
| 545 |
import pytest |
| 546 |
|
| 547 |
@pytest.mark.a |
| 548 |
class Base(object): pass |
| 549 |
|
| 550 |
@pytest.mark.b |
| 551 |
class Base2(Base): pass |
| 552 |
|
| 553 |
@pytest.mark.c |
| 554 |
class Test1(Base2): |
| 555 |
def test_foo(self): pass |
| 556 |
|
| 557 |
class Test2(Base2): |
| 558 |
@pytest.mark.d |
| 559 |
def test_bar(self): pass |
| 560 |
""" |
| 561 |
) |
| 562 |
items, rec = pytester.inline_genitems(p) |
| 563 |
self.assert_markers(items, test_foo=("a", "b", "c"), test_bar=("a", "b", "d")) |
| 564 |
|
| 565 |
def test_mark_closest(self, pytester: Pytester) -> None: |
| 566 |
p = pytester.makepyfile( |
| 567 |
""" |
| 568 |
import pytest |
| 569 |
|
| 570 |
@pytest.mark.c(location="class") |
| 571 |
class Test: |
| 572 |
@pytest.mark.c(location="function") |
| 573 |
def test_has_own(self): |
| 574 |
pass |
| 575 |
|
| 576 |
def test_has_inherited(self): |
| 577 |
pass |
| 578 |
|
| 579 |
""" |
| 580 |
) |
| 581 |
items, rec = pytester.inline_genitems(p) |
| 582 |
has_own, has_inherited = items |
| 583 |
has_own_marker = has_own.get_closest_marker("c") |
| 584 |
has_inherited_marker = has_inherited.get_closest_marker("c") |
| 585 |
assert has_own_marker is not None |
| 586 |
assert has_inherited_marker is not None |
| 587 |
assert has_own_marker.kwargs == {"location": "function"} |
| 588 |
assert has_inherited_marker.kwargs == {"location": "class"} |
| 589 |
assert has_own.get_closest_marker("missing") is None |
| 590 |
|
| 591 |
def test_mark_with_wrong_marker(self, pytester: Pytester) -> None: |
| 592 |
reprec = pytester.inline_runsource( |
| 593 |
""" |
| 594 |
import pytest |
| 595 |
class pytestmark(object): |
| 596 |
pass |
| 597 |
def test_func(): |
| 598 |
pass |
| 599 |
""" |
| 600 |
) |
| 601 |
values = reprec.getfailedcollections() |
| 602 |
assert len(values) == 1 |
| 603 |
assert "TypeError" in str(values[0].longrepr) |
| 604 |
|
| 605 |
def test_mark_dynamically_in_funcarg(self, pytester: Pytester) -> None: |
| 606 |
pytester.makeconftest( |
| 607 |
""" |
| 608 |
import pytest |
| 609 |
@pytest.fixture |
| 610 |
def arg(request): |
| 611 |
request.applymarker(pytest.mark.hello) |
| 612 |
def pytest_terminal_summary(terminalreporter): |
| 613 |
values = terminalreporter.stats['passed'] |
| 614 |
terminalreporter._tw.line("keyword: %s" % values[0].keywords) |
| 615 |
""" |
| 616 |
) |
| 617 |
pytester.makepyfile( |
| 618 |
""" |
| 619 |
def test_func(arg): |
| 620 |
pass |
| 621 |
""" |
| 622 |
) |
| 623 |
result = pytester.runpytest() |
| 624 |
result.stdout.fnmatch_lines(["keyword: *hello*"]) |
| 625 |
|
| 626 |
def test_no_marker_match_on_unmarked_names(self, pytester: Pytester) -> None: |
| 627 |
p = pytester.makepyfile( |
| 628 |
""" |
| 629 |
import pytest |
| 630 |
@pytest.mark.shouldmatch |
| 631 |
def test_marked(): |
| 632 |
assert 1 |
| 633 |
|
| 634 |
def test_unmarked(): |
| 635 |
assert 1 |
| 636 |
""" |
| 637 |
) |
| 638 |
reprec = pytester.inline_run("-m", "test_unmarked", p) |
| 639 |
passed, skipped, failed = reprec.listoutcomes() |
| 640 |
assert len(passed) + len(skipped) + len(failed) == 0 |
| 641 |
dlist = reprec.getcalls("pytest_deselected") |
| 642 |
deselected_tests = dlist[0].items |
| 643 |
assert len(deselected_tests) == 2 |
| 644 |
|
| 645 |
def test_keywords_at_node_level(self, pytester: Pytester) -> None: |
| 646 |
pytester.makepyfile( |
| 647 |
""" |
| 648 |
import pytest |
| 649 |
@pytest.fixture(scope="session", autouse=True) |
| 650 |
def some(request): |
| 651 |
request.keywords["hello"] = 42 |
| 652 |
assert "world" not in request.keywords |
| 653 |
|
| 654 |
@pytest.fixture(scope="function", autouse=True) |
| 655 |
def funcsetup(request): |
| 656 |
assert "world" in request.keywords |
| 657 |
assert "hello" in request.keywords |
| 658 |
|
| 659 |
@pytest.mark.world |
| 660 |
def test_function(): |
| 661 |
pass |
| 662 |
""" |
| 663 |
) |
| 664 |
reprec = pytester.inline_run() |
| 665 |
reprec.assertoutcome(passed=1) |
| 666 |
|
| 667 |
def test_keyword_added_for_session(self, pytester: Pytester) -> None: |
| 668 |
pytester.makeconftest( |
| 669 |
""" |
| 670 |
import pytest |
| 671 |
def pytest_collection_modifyitems(session): |
| 672 |
session.add_marker("mark1") |
| 673 |
session.add_marker(pytest.mark.mark2) |
| 674 |
session.add_marker(pytest.mark.mark3) |
| 675 |
pytest.raises(ValueError, lambda: |
| 676 |
session.add_marker(10)) |
| 677 |
""" |
| 678 |
) |
| 679 |
pytester.makepyfile( |
| 680 |
""" |
| 681 |
def test_some(request): |
| 682 |
assert "mark1" in request.keywords |
| 683 |
assert "mark2" in request.keywords |
| 684 |
assert "mark3" in request.keywords |
| 685 |
assert 10 not in request.keywords |
| 686 |
marker = request.node.get_closest_marker("mark1") |
| 687 |
assert marker.name == "mark1" |
| 688 |
assert marker.args == () |
| 689 |
assert marker.kwargs == {} |
| 690 |
""" |
| 691 |
) |
| 692 |
reprec = pytester.inline_run("-m", "mark1") |
| 693 |
reprec.assertoutcome(passed=1) |
| 694 |
|
| 695 |
def assert_markers(self, items, **expected) -> None: |
| 696 |
"""Assert that given items have expected marker names applied to them. |
| 697 |
expected should be a dict of (item name -> seq of expected marker names). |
| 698 |
|
| 699 |
Note: this could be moved to ``pytester`` if proven to be useful |
| 700 |
to other modules. |
| 701 |
""" |
| 702 |
items = {x.name: x for x in items} |
| 703 |
for name, expected_markers in expected.items(): |
| 704 |
markers = {m.name for m in items[name].iter_markers()} |
| 705 |
assert markers == set(expected_markers) |
| 706 |
|
| 707 |
@pytest.mark.filterwarnings("ignore") |
| 708 |
def test_mark_from_parameters(self, pytester: Pytester) -> None: |
| 709 |
"""#1540""" |
| 710 |
pytester.makepyfile( |
| 711 |
""" |
| 712 |
import pytest |
| 713 |
|
| 714 |
pytestmark = pytest.mark.skipif(True, reason='skip all') |
| 715 |
|
| 716 |
# skipifs inside fixture params |
| 717 |
params = [pytest.mark.skipif(False, reason='dont skip')('parameter')] |
| 718 |
|
| 719 |
|
| 720 |
@pytest.fixture(params=params) |
| 721 |
def parameter(request): |
| 722 |
return request.param |
| 723 |
|
| 724 |
|
| 725 |
def test_1(parameter): |
| 726 |
assert True |
| 727 |
""" |
| 728 |
) |
| 729 |
reprec = pytester.inline_run() |
| 730 |
reprec.assertoutcome(skipped=1) |
| 731 |
|
| 732 |
def test_reevaluate_dynamic_expr(self, pytester: Pytester) -> None: |
| 733 |
"""#7360""" |
| 734 |
py_file1 = pytester.makepyfile( |
| 735 |
test_reevaluate_dynamic_expr1=""" |
| 736 |
import pytest |
| 737 |
|
| 738 |
skip = True |
| 739 |
|
| 740 |
@pytest.mark.skipif("skip") |
| 741 |
def test_should_skip(): |
| 742 |
assert True |
| 743 |
""" |
| 744 |
) |
| 745 |
py_file2 = pytester.makepyfile( |
| 746 |
test_reevaluate_dynamic_expr2=""" |
| 747 |
import pytest |
| 748 |
|
| 749 |
skip = False |
| 750 |
|
| 751 |
@pytest.mark.skipif("skip") |
| 752 |
def test_should_not_skip(): |
| 753 |
assert True |
| 754 |
""" |
| 755 |
) |
| 756 |
|
| 757 |
file_name1 = os.path.basename(py_file1) |
| 758 |
file_name2 = os.path.basename(py_file2) |
| 759 |
reprec = pytester.inline_run(file_name1, file_name2) |
| 760 |
reprec.assertoutcome(passed=1, skipped=1) |
| 761 |
|
| 762 |
|
| 763 |
class TestKeywordSelection: |
| 764 |
def test_select_simple(self, pytester: Pytester) -> None: |
| 765 |
file_test = pytester.makepyfile( |
| 766 |
""" |
| 767 |
def test_one(): |
| 768 |
assert 0 |
| 769 |
class TestClass(object): |
| 770 |
def test_method_one(self): |
| 771 |
assert 42 == 43 |
| 772 |
""" |
| 773 |
) |
| 774 |
|
| 775 |
def check(keyword, name): |
| 776 |
reprec = pytester.inline_run("-s", "-k", keyword, file_test) |
| 777 |
passed, skipped, failed = reprec.listoutcomes() |
| 778 |
assert len(failed) == 1 |
| 779 |
assert failed[0].nodeid.split("::")[-1] == name |
| 780 |
assert len(reprec.getcalls("pytest_deselected")) == 1 |
| 781 |
|
| 782 |
for keyword in ["test_one", "est_on"]: |
| 783 |
check(keyword, "test_one") |
| 784 |
check("TestClass and test", "test_method_one") |
| 785 |
|
| 786 |
@pytest.mark.parametrize( |
| 787 |
"keyword", |
| 788 |
[ |
| 789 |
"xxx", |
| 790 |
"xxx and test_2", |
| 791 |
"TestClass", |
| 792 |
"xxx and not test_1", |
| 793 |
"TestClass and test_2", |
| 794 |
"xxx and TestClass and test_2", |
| 795 |
], |
| 796 |
) |
| 797 |
def test_select_extra_keywords(self, pytester: Pytester, keyword) -> None: |
| 798 |
p = pytester.makepyfile( |
| 799 |
test_select=""" |
| 800 |
def test_1(): |
| 801 |
pass |
| 802 |
class TestClass(object): |
| 803 |
def test_2(self): |
| 804 |
pass |
| 805 |
""" |
| 806 |
) |
| 807 |
pytester.makepyfile( |
| 808 |
conftest=""" |
| 809 |
import pytest |
| 810 |
@pytest.hookimpl(wrapper=True) |
| 811 |
def pytest_pycollect_makeitem(name): |
| 812 |
item = yield |
| 813 |
if name == "TestClass": |
| 814 |
item.extra_keyword_matches.add("xxx") |
| 815 |
return item |
| 816 |
""" |
| 817 |
) |
| 818 |
reprec = pytester.inline_run(p.parent, "-s", "-k", keyword) |
| 819 |
print("keyword", repr(keyword)) |
| 820 |
passed, skipped, failed = reprec.listoutcomes() |
| 821 |
assert len(passed) == 1 |
| 822 |
assert passed[0].nodeid.endswith("test_2") |
| 823 |
dlist = reprec.getcalls("pytest_deselected") |
| 824 |
assert len(dlist) == 1 |
| 825 |
assert dlist[0].items[0].name == "test_1" |
| 826 |
|
| 827 |
def test_keyword_extra(self, pytester: Pytester) -> None: |
| 828 |
p = pytester.makepyfile( |
| 829 |
""" |
| 830 |
def test_one(): |
| 831 |
assert 0 |
| 832 |
test_one.mykeyword = True |
| 833 |
""" |
| 834 |
) |
| 835 |
reprec = pytester.inline_run("-k", "mykeyword", p) |
| 836 |
passed, skipped, failed = reprec.countoutcomes() |
| 837 |
assert failed == 1 |
| 838 |
|
| 839 |
@pytest.mark.xfail |
| 840 |
def test_keyword_extra_dash(self, pytester: Pytester) -> None: |
| 841 |
p = pytester.makepyfile( |
| 842 |
""" |
| 843 |
def test_one(): |
| 844 |
assert 0 |
| 845 |
test_one.mykeyword = True |
| 846 |
""" |
| 847 |
) |
| 848 |
# with argparse the argument to an option cannot |
| 849 |
# start with '-' |
| 850 |
reprec = pytester.inline_run("-k", "-mykeyword", p) |
| 851 |
passed, skipped, failed = reprec.countoutcomes() |
| 852 |
assert passed + skipped + failed == 0 |
| 853 |
|
| 854 |
@pytest.mark.parametrize( |
| 855 |
"keyword", |
| 856 |
["__", "+", ".."], |
| 857 |
) |
| 858 |
def test_no_magic_values(self, pytester: Pytester, keyword: str) -> None: |
| 859 |
"""Make sure the tests do not match on magic values, |
| 860 |
no double underscored values, like '__dict__' and '+'. |
| 861 |
""" |
| 862 |
p = pytester.makepyfile( |
| 863 |
""" |
| 864 |
def test_one(): assert 1 |
| 865 |
""" |
| 866 |
) |
| 867 |
|
| 868 |
reprec = pytester.inline_run("-k", keyword, p) |
| 869 |
passed, skipped, failed = reprec.countoutcomes() |
| 870 |
dlist = reprec.getcalls("pytest_deselected") |
| 871 |
assert passed + skipped + failed == 0 |
| 872 |
deselected_tests = dlist[0].items |
| 873 |
assert len(deselected_tests) == 1 |
| 874 |
|
| 875 |
def test_no_match_directories_outside_the_suite( |
| 876 |
self, |
| 877 |
pytester: Pytester, |
| 878 |
monkeypatch: pytest.MonkeyPatch, |
| 879 |
) -> None: |
| 880 |
"""`-k` should not match against directories containing the test suite (#7040).""" |
| 881 |
pytester.makefile( |
| 882 |
**{ |
| 883 |
"suite/pytest": """[pytest]""", |
| 884 |
}, |
| 885 |
ext=".ini", |
| 886 |
) |
| 887 |
pytester.makepyfile( |
| 888 |
**{ |
| 889 |
"suite/ddd/tests/__init__.py": "", |
| 890 |
"suite/ddd/tests/test_foo.py": """ |
| 891 |
def test_aaa(): pass |
| 892 |
def test_ddd(): pass |
| 893 |
""", |
| 894 |
} |
| 895 |
) |
| 896 |
monkeypatch.chdir(pytester.path / "suite") |
| 897 |
|
| 898 |
def get_collected_names(*args: str) -> List[str]: |
| 899 |
_, rec = pytester.inline_genitems(*args) |
| 900 |
calls = rec.getcalls("pytest_collection_finish") |
| 901 |
assert len(calls) == 1 |
| 902 |
return [x.name for x in calls[0].session.items] |
| 903 |
|
| 904 |
# sanity check: collect both tests in normal runs |
| 905 |
assert get_collected_names() == ["test_aaa", "test_ddd"] |
| 906 |
|
| 907 |
# do not collect anything based on names outside the collection tree |
| 908 |
assert get_collected_names("-k", pytester._name) == [] |
| 909 |
|
| 910 |
|
| 911 |
class TestMarkDecorator: |
| 912 |
@pytest.mark.parametrize( |
| 913 |
"lhs, rhs, expected", |
| 914 |
[ |
| 915 |
(pytest.mark.foo(), pytest.mark.foo(), True), |
| 916 |
(pytest.mark.foo(), pytest.mark.bar(), False), |
| 917 |
(pytest.mark.foo(), "bar", False), |
| 918 |
("foo", pytest.mark.bar(), False), |
| 919 |
], |
| 920 |
) |
| 921 |
def test__eq__(self, lhs, rhs, expected) -> None: |
| 922 |
assert (lhs == rhs) == expected |
| 923 |
|
| 924 |
def test_aliases(self) -> None: |
| 925 |
md = pytest.mark.foo(1, "2", three=3) |
| 926 |
assert md.name == "foo" |
| 927 |
assert md.args == (1, "2") |
| 928 |
assert md.kwargs == {"three": 3} |
| 929 |
|
| 930 |
|
| 931 |
@pytest.mark.parametrize("mark", [None, "", "skip", "xfail"]) |
| 932 |
def test_parameterset_for_parametrize_marks( |
| 933 |
pytester: Pytester, mark: Optional[str] |
| 934 |
) -> None: |
| 935 |
if mark is not None: |
| 936 |
pytester.makeini( |
| 937 |
f""" |
| 938 |
[pytest] |
| 939 |
{EMPTY_PARAMETERSET_OPTION}={mark} |
| 940 |
""" |
| 941 |
) |
| 942 |
|
| 943 |
config = pytester.parseconfig() |
| 944 |
from _pytest.mark import get_empty_parameterset_mark |
| 945 |
from _pytest.mark import pytest_configure |
| 946 |
|
| 947 |
pytest_configure(config) |
| 948 |
result_mark = get_empty_parameterset_mark(config, ["a"], all) |
| 949 |
if mark in (None, ""): |
| 950 |
# normalize to the requested name |
| 951 |
mark = "skip" |
| 952 |
assert result_mark.name == mark |
| 953 |
assert result_mark.kwargs["reason"].startswith("got empty parameter set ") |
| 954 |
if mark == "xfail": |
| 955 |
assert result_mark.kwargs.get("run") is False |
| 956 |
|
| 957 |
|
| 958 |
def test_parameterset_for_fail_at_collect(pytester: Pytester) -> None: |
| 959 |
pytester.makeini( |
| 960 |
f""" |
| 961 |
[pytest] |
| 962 |
{EMPTY_PARAMETERSET_OPTION}=fail_at_collect |
| 963 |
""" |
| 964 |
) |
| 965 |
|
| 966 |
config = pytester.parseconfig() |
| 967 |
from _pytest.mark import get_empty_parameterset_mark |
| 968 |
from _pytest.mark import pytest_configure |
| 969 |
|
| 970 |
pytest_configure(config) |
| 971 |
|
| 972 |
with pytest.raises( |
| 973 |
Collector.CollectError, |
| 974 |
match=r"Empty parameter set in 'pytest_configure' at line \d\d+", |
| 975 |
): |
| 976 |
get_empty_parameterset_mark(config, ["a"], pytest_configure) |
| 977 |
|
| 978 |
p1 = pytester.makepyfile( |
| 979 |
""" |
| 980 |
import pytest |
| 981 |
|
| 982 |
@pytest.mark.parametrize("empty", []) |
| 983 |
def test(): |
| 984 |
pass |
| 985 |
""" |
| 986 |
) |
| 987 |
result = pytester.runpytest(str(p1)) |
| 988 |
result.stdout.fnmatch_lines( |
| 989 |
[ |
| 990 |
"collected 0 items / 1 error", |
| 991 |
"* ERROR collecting test_parameterset_for_fail_at_collect.py *", |
| 992 |
"Empty parameter set in 'test' at line 3", |
| 993 |
"*= 1 error in *", |
| 994 |
] |
| 995 |
) |
| 996 |
assert result.ret == ExitCode.INTERRUPTED |
| 997 |
|
| 998 |
|
| 999 |
def test_parameterset_for_parametrize_bad_markname(pytester: Pytester) -> None: |
| 1000 |
with pytest.raises(pytest.UsageError): |
| 1001 |
test_parameterset_for_parametrize_marks(pytester, "bad") |
| 1002 |
|
| 1003 |
|
| 1004 |
def test_mark_expressions_no_smear(pytester: Pytester) -> None: |
| 1005 |
pytester.makepyfile( |
| 1006 |
""" |
| 1007 |
import pytest |
| 1008 |
|
| 1009 |
class BaseTests(object): |
| 1010 |
def test_something(self): |
| 1011 |
pass |
| 1012 |
|
| 1013 |
@pytest.mark.FOO |
| 1014 |
class TestFooClass(BaseTests): |
| 1015 |
pass |
| 1016 |
|
| 1017 |
@pytest.mark.BAR |
| 1018 |
class TestBarClass(BaseTests): |
| 1019 |
pass |
| 1020 |
""" |
| 1021 |
) |
| 1022 |
|
| 1023 |
reprec = pytester.inline_run("-m", "FOO") |
| 1024 |
passed, skipped, failed = reprec.countoutcomes() |
| 1025 |
dlist = reprec.getcalls("pytest_deselected") |
| 1026 |
assert passed == 1 |
| 1027 |
assert skipped == failed == 0 |
| 1028 |
deselected_tests = dlist[0].items |
| 1029 |
assert len(deselected_tests) == 1 |
| 1030 |
|
| 1031 |
# todo: fixed |
| 1032 |
# keywords smear - expected behaviour |
| 1033 |
# reprec_keywords = pytester.inline_run("-k", "FOO") |
| 1034 |
# passed_k, skipped_k, failed_k = reprec_keywords.countoutcomes() |
| 1035 |
# assert passed_k == 2 |
| 1036 |
# assert skipped_k == failed_k == 0 |
| 1037 |
|
| 1038 |
|
| 1039 |
def test_addmarker_order(pytester) -> None: |
| 1040 |
session = mock.Mock() |
| 1041 |
session.own_markers = [] |
| 1042 |
session.parent = None |
| 1043 |
session.nodeid = "" |
| 1044 |
session.path = pytester.path |
| 1045 |
node = Node.from_parent(session, name="Test") |
| 1046 |
node.add_marker("foo") |
| 1047 |
node.add_marker("bar") |
| 1048 |
node.add_marker("baz", append=False) |
| 1049 |
extracted = [x.name for x in node.iter_markers()] |
| 1050 |
assert extracted == ["baz", "foo", "bar"] |
| 1051 |
|
| 1052 |
|
| 1053 |
@pytest.mark.filterwarnings("ignore") |
| 1054 |
def test_markers_from_parametrize(pytester: Pytester) -> None: |
| 1055 |
"""#3605""" |
| 1056 |
pytester.makepyfile( |
| 1057 |
""" |
| 1058 |
import pytest |
| 1059 |
|
| 1060 |
first_custom_mark = pytest.mark.custom_marker |
| 1061 |
custom_mark = pytest.mark.custom_mark |
| 1062 |
@pytest.fixture(autouse=True) |
| 1063 |
def trigger(request): |
| 1064 |
custom_mark = list(request.node.iter_markers('custom_mark')) |
| 1065 |
print("Custom mark %s" % custom_mark) |
| 1066 |
|
| 1067 |
@custom_mark("custom mark non parametrized") |
| 1068 |
def test_custom_mark_non_parametrized(): |
| 1069 |
print("Hey from test") |
| 1070 |
|
| 1071 |
@pytest.mark.parametrize( |
| 1072 |
"obj_type", |
| 1073 |
[ |
| 1074 |
first_custom_mark("first custom mark")("template"), |
| 1075 |
pytest.param( # Think this should be recommended way? |
| 1076 |
"disk", |
| 1077 |
marks=custom_mark('custom mark1') |
| 1078 |
), |
| 1079 |
custom_mark("custom mark2")("vm"), # Tried also this |
| 1080 |
] |
| 1081 |
) |
| 1082 |
def test_custom_mark_parametrized(obj_type): |
| 1083 |
print("obj_type is:", obj_type) |
| 1084 |
""" |
| 1085 |
) |
| 1086 |
|
| 1087 |
result = pytester.runpytest() |
| 1088 |
result.assert_outcomes(passed=4) |
| 1089 |
|
| 1090 |
|
| 1091 |
def test_pytest_param_id_requires_string() -> None: |
| 1092 |
with pytest.raises(TypeError) as excinfo: |
| 1093 |
pytest.param(id=True) # type: ignore[arg-type] |
| 1094 |
(msg,) = excinfo.value.args |
| 1095 |
assert msg == "Expected id to be a string, got <class 'bool'>: True" |
| 1096 |
|
| 1097 |
|
| 1098 |
@pytest.mark.parametrize("s", (None, "hello world")) |
| 1099 |
def test_pytest_param_id_allows_none_or_string(s) -> None: |
| 1100 |
assert pytest.param(id=s) |
| 1101 |
|
| 1102 |
|
| 1103 |
@pytest.mark.parametrize("expr", ("NOT internal_err", "NOT (internal_err)", "bogus=")) |
| 1104 |
def test_marker_expr_eval_failure_handling(pytester: Pytester, expr) -> None: |
| 1105 |
foo = pytester.makepyfile( |
| 1106 |
""" |
| 1107 |
import pytest |
| 1108 |
|
| 1109 |
@pytest.mark.internal_err |
| 1110 |
def test_foo(): |
| 1111 |
pass |
| 1112 |
""" |
| 1113 |
) |
| 1114 |
expected = f"ERROR: Wrong expression passed to '-m': {expr}: *" |
| 1115 |
result = pytester.runpytest(foo, "-m", expr) |
| 1116 |
result.stderr.fnmatch_lines([expected]) |
| 1117 |
assert result.ret == ExitCode.USAGE_ERROR |
| 1118 |
|
| 1119 |
|
| 1120 |
def test_mark_mro() -> None: |
| 1121 |
xfail = pytest.mark.xfail |
| 1122 |
|
| 1123 |
@xfail("a") |
| 1124 |
class A: |
| 1125 |
pass |
| 1126 |
|
| 1127 |
@xfail("b") |
| 1128 |
class B: |
| 1129 |
pass |
| 1130 |
|
| 1131 |
@xfail("c") |
| 1132 |
class C(A, B): |
| 1133 |
pass |
| 1134 |
|
| 1135 |
from _pytest.mark.structures import get_unpacked_marks |
| 1136 |
|
| 1137 |
all_marks = get_unpacked_marks(C) |
| 1138 |
|
| 1139 |
assert all_marks == [xfail("b").mark, xfail("a").mark, xfail("c").mark] |
| 1140 |
|
| 1141 |
assert get_unpacked_marks(C, consider_mro=False) == [xfail("c").mark] |
| 1142 |
|
| 1143 |
|
| 1144 |
# @pytest.mark.issue("https://github.com/pytest-dev/pytest/issues/10447") |
| 1145 |
def test_mark_fixture_order_mro(pytester: Pytester): |
| 1146 |
"""This ensures we walk marks of the mro starting with the base classes |
| 1147 |
the action at a distance fixtures are taken as minimal example from a real project |
| 1148 |
|
| 1149 |
""" |
| 1150 |
foo = pytester.makepyfile( |
| 1151 |
""" |
| 1152 |
import pytest |
| 1153 |
|
| 1154 |
@pytest.fixture |
| 1155 |
def add_attr1(request): |
| 1156 |
request.instance.attr1 = object() |
| 1157 |
|
| 1158 |
|
| 1159 |
@pytest.fixture |
| 1160 |
def add_attr2(request): |
| 1161 |
request.instance.attr2 = request.instance.attr1 |
| 1162 |
|
| 1163 |
|
| 1164 |
@pytest.mark.usefixtures('add_attr1') |
| 1165 |
class Parent: |
| 1166 |
pass |
| 1167 |
|
| 1168 |
|
| 1169 |
@pytest.mark.usefixtures('add_attr2') |
| 1170 |
class TestThings(Parent): |
| 1171 |
def test_attrs(self): |
| 1172 |
assert self.attr1 == self.attr2 |
| 1173 |
""" |
| 1174 |
) |
| 1175 |
result = pytester.runpytest(foo) |
| 1176 |
result.assert_outcomes(passed=1) |