|
1 | 1 |
|
| 2 | +def test_invocation_request(testdir): |
| 3 | + """ |
| 4 | + Simple test case with session and module scopes requesting an |
| 5 | + invocation-scoped fixture. |
| 6 | + """ |
| 7 | + testdir.makeconftest(""" |
| 8 | + import pytest |
| 9 | +
|
| 10 | + @pytest.fixture(scope='invocation') |
| 11 | + def my_name(request): |
| 12 | + if request.scope == 'function': |
| 13 | + return request.function.__name__ |
| 14 | + elif request.scope == 'module': |
| 15 | + return request.module.__name__ |
| 16 | + elif request.scope == 'session': |
| 17 | + return '<session>' |
| 18 | +
|
| 19 | + @pytest.fixture(scope='session') |
| 20 | + def session_name(my_name): |
| 21 | + return my_name |
| 22 | +
|
| 23 | + @pytest.fixture(scope='module') |
| 24 | + def module_name(my_name): |
| 25 | + return my_name |
| 26 | + """) |
| 27 | + testdir.makepyfile(test_module_foo=""" |
| 28 | + def test_foo(my_name, module_name, session_name): |
| 29 | + assert my_name == 'test_foo' |
| 30 | + assert module_name == 'test_module_foo' |
| 31 | + assert session_name == '<session>' |
| 32 | + """) |
| 33 | + testdir.makepyfile(test_module_bar=""" |
| 34 | + def test_bar(my_name, module_name, session_name): |
| 35 | + assert my_name == 'test_bar' |
| 36 | + assert module_name == 'test_module_bar' |
| 37 | + assert session_name == '<session>' |
| 38 | + """) |
| 39 | + result = testdir.runpytest() |
| 40 | + result.stdout.fnmatch_lines(['*2 passed*']) |
| 41 | + |
| 42 | + |
| 43 | +def test_override_invocation_scoped(testdir): |
| 44 | + """Test that it's possible to override invocation-scoped fixtures.""" |
| 45 | + testdir.makeconftest(""" |
| 46 | + import pytest |
| 47 | +
|
| 48 | + @pytest.fixture(scope='invocation') |
| 49 | + def magic_value(request): |
| 50 | + if request.scope == 'function': |
| 51 | + return 1 |
| 52 | + elif request.scope == 'module': |
| 53 | + return 100 |
| 54 | +
|
| 55 | + @pytest.fixture(scope='module') |
| 56 | + def module_magic_value(magic_value): |
| 57 | + return magic_value * 2 |
| 58 | + """) |
| 59 | + testdir.makepyfile(test_module_override=""" |
| 60 | + import pytest |
| 61 | +
|
| 62 | + @pytest.fixture(scope='module') |
| 63 | + def magic_value(): |
| 64 | + return 42 |
| 65 | +
|
| 66 | + def test_override(magic_value, module_magic_value): |
| 67 | + assert magic_value == 42 |
| 68 | + assert module_magic_value == 42 * 2 |
| 69 | + """) |
| 70 | + testdir.makepyfile(test_normal=""" |
| 71 | + def test_normal(magic_value, module_magic_value): |
| 72 | + assert magic_value == 1 |
| 73 | + assert module_magic_value == 200 |
| 74 | + """) |
| 75 | + result = testdir.runpytest() |
| 76 | + result.stdout.fnmatch_lines(['*2 passed*']) |
| 77 | + |
2 | 78 |
|
3 | 79 | class TestAcceptance:
|
4 | 80 | """
|
|
0 commit comments