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