|
| 1 | + |
| 2 | + |
| 3 | +class TestAcceptance: |
| 4 | + """ |
| 5 | + Complete acceptance test for a invocation-scoped fixture. |
| 6 | + """ |
| 7 | + |
| 8 | + def test_acceptance(self, testdir): |
| 9 | + """ |
| 10 | + Tests a "stack" fixture which provides a separate list to each scope which uses it. |
| 11 | +
|
| 12 | + Some notes: |
| 13 | +
|
| 14 | + - For each scope, define 2 fixtures of the same scope which use the "stack" fixture, |
| 15 | + to ensure they get the same "stack" instance for its scope. |
| 16 | + - Creates multiple test files, which tests on each modifying and checking fixtures to |
| 17 | + ensure things are working properly. |
| 18 | + """ |
| 19 | + testdir.makeconftest(""" |
| 20 | + import pytest |
| 21 | +
|
| 22 | + @pytest.fixture(scope='invocation') |
| 23 | + def stack(): |
| 24 | + return [] |
| 25 | +
|
| 26 | + @pytest.fixture(scope='session') |
| 27 | + def session1_fix(stack): |
| 28 | + stack.append('session1_fix') |
| 29 | + return stack |
| 30 | +
|
| 31 | + @pytest.fixture(scope='session') |
| 32 | + def session2_fix(stack): |
| 33 | + stack.append('session2_fix') |
| 34 | + return stack |
| 35 | +
|
| 36 | + @pytest.fixture(scope='module') |
| 37 | + def module1_fix(stack): |
| 38 | + stack.append('module1_fix') |
| 39 | + return stack |
| 40 | +
|
| 41 | + @pytest.fixture(scope='module') |
| 42 | + def module2_fix(stack): |
| 43 | + stack.append('module2_fix') |
| 44 | + return stack |
| 45 | +
|
| 46 | + @pytest.fixture(scope='class') |
| 47 | + def class1_fix(stack): |
| 48 | + stack.append('class1_fix') |
| 49 | + return stack |
| 50 | +
|
| 51 | + @pytest.fixture(scope='class') |
| 52 | + def class2_fix(stack): |
| 53 | + stack.append('class2_fix') |
| 54 | + return stack |
| 55 | + """) |
| 56 | + testdir.makepyfile(test_0=""" |
| 57 | + import pytest |
| 58 | +
|
| 59 | + @pytest.fixture |
| 60 | + def func_stack(stack): |
| 61 | + return stack |
| 62 | +
|
| 63 | + def test_scoped_instances(session1_fix, session2_fix, module1_fix, module2_fix, |
| 64 | + class1_fix, class2_fix, stack, func_stack): |
| 65 | + assert session1_fix is session2_fix |
| 66 | + assert module1_fix is module2_fix |
| 67 | + assert class1_fix is class2_fix |
| 68 | + assert stack is func_stack |
| 69 | +
|
| 70 | + assert session1_fix is not module2_fix |
| 71 | + assert module2_fix is not class1_fix |
| 72 | + assert class1_fix is not stack |
| 73 | + """) |
| 74 | + testdir.makepyfile(test_1=""" |
| 75 | + def test_func_1(request, session1_fix, session2_fix, module1_fix, module2_fix, stack): |
| 76 | + assert stack == [] |
| 77 | +
|
| 78 | + assert session1_fix == ['session1_fix', 'session2_fix'] |
| 79 | + session1_fix.append('test_1::test_func_1') |
| 80 | +
|
| 81 | + assert module1_fix == ['module1_fix', 'module2_fix'] |
| 82 | + module1_fix.append('test_1::test_func_1') |
| 83 | +
|
| 84 | +
|
| 85 | + class Test: |
| 86 | +
|
| 87 | + def test_1(self, request, session1_fix, module1_fix, class1_fix, class2_fix, stack): |
| 88 | + assert stack == [] |
| 89 | +
|
| 90 | + assert session1_fix == ['session1_fix', 'session2_fix', 'test_1::test_func_1'] |
| 91 | + session1_fix.append('test_1::Test::test_1') |
| 92 | +
|
| 93 | + assert module1_fix == ['module1_fix', 'module2_fix', 'test_1::test_func_1'] |
| 94 | + module1_fix.append('test_1::test_func_1') |
| 95 | +
|
| 96 | + assert class1_fix == ['class1_fix', 'class2_fix'] |
| 97 | + class1_fix.append('test_1::Test::test_1') |
| 98 | +
|
| 99 | + def test_2(self, request, class1_fix, class2_fix): |
| 100 | + assert class1_fix == ['class1_fix', 'class2_fix', 'test_1::Test::test_1'] |
| 101 | + class1_fix.append('Test.test_2') |
| 102 | +
|
| 103 | +
|
| 104 | + def test_func_2(request, session1_fix, session2_fix, module1_fix, class1_fix, class2_fix, stack): |
| 105 | + assert stack == [] |
| 106 | + assert session1_fix == ['session1_fix', 'session2_fix', 'test_1::test_func_1', |
| 107 | + 'test_1::Test::test_1'] |
| 108 | + session1_fix.append('test_1::test_func_2') |
| 109 | +
|
| 110 | + assert module1_fix == ['module1_fix', 'module2_fix', 'test_1::test_func_1', 'test_1::test_func_1'] |
| 111 | +
|
| 112 | + assert class1_fix == ['class1_fix', 'class2_fix'] |
| 113 | + """) |
| 114 | + testdir.makepyfile(test_2=""" |
| 115 | + import pytest |
| 116 | +
|
| 117 | + @pytest.fixture(scope='session') |
| 118 | + def another_session_stack(stack): |
| 119 | + stack.append('other_session_stack') |
| 120 | + return stack |
| 121 | +
|
| 122 | + def test_func_2(request, another_session_stack, module1_fix, stack): |
| 123 | + assert stack == [] |
| 124 | + assert another_session_stack == [ |
| 125 | + 'session1_fix', |
| 126 | + 'session2_fix', |
| 127 | + 'test_1::test_func_1', |
| 128 | + 'test_1::Test::test_1', |
| 129 | + 'test_1::test_func_2', |
| 130 | + 'other_session_stack', |
| 131 | + ] |
| 132 | + assert module1_fix == ['module1_fix'] |
| 133 | + """) |
| 134 | + result = testdir.runpytest() |
| 135 | + assert result.ret == 0 |
| 136 | + result.stdout.fnmatch_lines('* 6 passed in *') |
| 137 | + |
| 138 | + |
0 commit comments