| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
from _pytest.pytester import Pytester |
| 4 |
|
| 5 |
|
| 6 |
def test_show_fixtures_and_test( |
| 7 |
pytester: Pytester, dummy_yaml_custom_test: None |
| 8 |
) -> None: |
| 9 |
"""Verify that fixtures are not executed.""" |
| 10 |
pytester.makepyfile( |
| 11 |
""" |
| 12 |
import pytest |
| 13 |
@pytest.fixture |
| 14 |
def arg(): |
| 15 |
assert False |
| 16 |
def test_arg(arg): |
| 17 |
assert False |
| 18 |
""" |
| 19 |
) |
| 20 |
|
| 21 |
result = pytester.runpytest("--setup-plan") |
| 22 |
assert result.ret == 0 |
| 23 |
|
| 24 |
result.stdout.fnmatch_lines( |
| 25 |
["*SETUP F arg*", "*test_arg (fixtures used: arg)", "*TEARDOWN F arg*"] |
| 26 |
) |
| 27 |
|
| 28 |
|
| 29 |
def test_show_multi_test_fixture_setup_and_teardown_correctly_simple( |
| 30 |
pytester: Pytester, |
| 31 |
) -> None: |
| 32 |
"""Verify that when a fixture lives for longer than a single test, --setup-plan |
| 33 |
correctly displays the SETUP/TEARDOWN indicators the right number of times. |
| 34 |
|
| 35 |
As reported in https://github.com/pytest-dev/pytest/issues/2049 |
| 36 |
--setup-plan was showing SETUP/TEARDOWN on every test, even when the fixture |
| 37 |
should persist through multiple tests. |
| 38 |
|
| 39 |
(Note that this bug never affected actual test execution, which used the |
| 40 |
correct fixture lifetimes. It was purely a display bug for --setup-plan, and |
| 41 |
did not affect the related --setup-show or --setup-only.) |
| 42 |
""" |
| 43 |
pytester.makepyfile( |
| 44 |
""" |
| 45 |
import pytest |
| 46 |
@pytest.fixture(scope = 'class') |
| 47 |
def fix(): |
| 48 |
return object() |
| 49 |
class TestClass: |
| 50 |
def test_one(self, fix): |
| 51 |
assert False |
| 52 |
def test_two(self, fix): |
| 53 |
assert False |
| 54 |
""" |
| 55 |
) |
| 56 |
|
| 57 |
result = pytester.runpytest("--setup-plan") |
| 58 |
assert result.ret == 0 |
| 59 |
|
| 60 |
setup_fragment = "SETUP C fix" |
| 61 |
setup_count = 0 |
| 62 |
|
| 63 |
teardown_fragment = "TEARDOWN C fix" |
| 64 |
teardown_count = 0 |
| 65 |
|
| 66 |
for line in result.stdout.lines: |
| 67 |
if setup_fragment in line: |
| 68 |
setup_count += 1 |
| 69 |
if teardown_fragment in line: |
| 70 |
teardown_count += 1 |
| 71 |
|
| 72 |
# before the fix this tests, there would have been a setup/teardown |
| 73 |
# message for each test, so the counts would each have been 2 |
| 74 |
assert setup_count == 1 |
| 75 |
assert teardown_count == 1 |
| 76 |
|
| 77 |
|
| 78 |
def test_show_multi_test_fixture_setup_and_teardown_same_as_setup_show( |
| 79 |
pytester: Pytester, |
| 80 |
) -> None: |
| 81 |
"""Verify that SETUP/TEARDOWN messages match what comes out of --setup-show.""" |
| 82 |
pytester.makepyfile( |
| 83 |
""" |
| 84 |
import pytest |
| 85 |
@pytest.fixture(scope = 'session') |
| 86 |
def sess(): |
| 87 |
return True |
| 88 |
@pytest.fixture(scope = 'module') |
| 89 |
def mod(): |
| 90 |
return True |
| 91 |
@pytest.fixture(scope = 'class') |
| 92 |
def cls(): |
| 93 |
return True |
| 94 |
@pytest.fixture(scope = 'function') |
| 95 |
def func(): |
| 96 |
return True |
| 97 |
def test_outside(sess, mod, cls, func): |
| 98 |
assert True |
| 99 |
class TestCls: |
| 100 |
def test_one(self, sess, mod, cls, func): |
| 101 |
assert True |
| 102 |
def test_two(self, sess, mod, cls, func): |
| 103 |
assert True |
| 104 |
""" |
| 105 |
) |
| 106 |
|
| 107 |
plan_result = pytester.runpytest("--setup-plan") |
| 108 |
show_result = pytester.runpytest("--setup-show") |
| 109 |
|
| 110 |
# the number and text of these lines should be identical |
| 111 |
plan_lines = [ |
| 112 |
line |
| 113 |
for line in plan_result.stdout.lines |
| 114 |
if "SETUP" in line or "TEARDOWN" in line |
| 115 |
] |
| 116 |
show_lines = [ |
| 117 |
line |
| 118 |
for line in show_result.stdout.lines |
| 119 |
if "SETUP" in line or "TEARDOWN" in line |
| 120 |
] |
| 121 |
|
| 122 |
assert plan_lines == show_lines |