|
| 1 | +import pytest |
| 2 | +from pytest_trio import trio_fixture |
| 3 | + |
| 4 | +from .helpers import enable_trio_mode |
| 5 | + |
| 6 | + |
| 7 | +def test_trio_fixture_with_non_trio_test(testdir): |
| 8 | + testdir.makepyfile( |
| 9 | + """ |
| 10 | + import trio |
| 11 | + from pytest_trio import trio_fixture |
| 12 | + import pytest |
| 13 | +
|
| 14 | + @trio_fixture |
| 15 | + def trio_time(): |
| 16 | + return trio.current_time() |
| 17 | +
|
| 18 | + @pytest.fixture |
| 19 | + def indirect_trio_time(trio_time): |
| 20 | + return trio_time + 1 |
| 21 | +
|
| 22 | + @pytest.mark.trio |
| 23 | + async def test_async(mock_clock, trio_time, indirect_trio_time): |
| 24 | + assert trio_time == 0 |
| 25 | + assert indirect_trio_time == 1 |
| 26 | +
|
| 27 | + def test_sync(trio_time): |
| 28 | + pass |
| 29 | +
|
| 30 | + def test_sync_indirect(indirect_trio_time): |
| 31 | + pass |
| 32 | + """ |
| 33 | + ) |
| 34 | + |
| 35 | + result = testdir.runpytest() |
| 36 | + |
| 37 | + result.assert_outcomes(passed=1, error=2) |
| 38 | + result.stdout.fnmatch_lines( |
| 39 | + ["*: Trio fixtures can only be used by Trio tests*"] |
| 40 | + ) |
| 41 | + |
| 42 | + |
| 43 | +def test_trio_fixture_with_wrong_scope_without_trio_mode(testdir): |
| 44 | + # There's a trick here: when you have a non-function-scope fixture, it's |
| 45 | + # not instantiated for any particular function (obviously). So... when our |
| 46 | + # pytest_fixture_setup hook tries to check for marks, it can't normally |
| 47 | + # see @pytest.mark.trio. So... it's actually almost impossible to have an |
| 48 | + # async fixture get treated as a Trio fixture *and* have it be |
| 49 | + # non-function-scope. But, class-scoped fixtures can see marks on the |
| 50 | + # class, so this is one way (the only way?) it can happen: |
| 51 | + testdir.makepyfile( |
| 52 | + """ |
| 53 | + import pytest |
| 54 | +
|
| 55 | + @pytest.fixture(scope="class") |
| 56 | + async def async_class_fixture(): |
| 57 | + pass |
| 58 | +
|
| 59 | + @pytest.mark.trio |
| 60 | + class TestFoo: |
| 61 | + async def test_foo(self, async_class_fixture): |
| 62 | + pass |
| 63 | + """ |
| 64 | + ) |
| 65 | + |
| 66 | + result = testdir.runpytest() |
| 67 | + |
| 68 | + result.assert_outcomes(error=1) |
| 69 | + result.stdout.fnmatch_lines(["*: Trio fixtures must be function-scope*"]) |
| 70 | + |
| 71 | + |
| 72 | +@enable_trio_mode |
| 73 | +def test_trio_fixture_with_wrong_scope_in_trio_mode(testdir, enable_trio_mode): |
| 74 | + enable_trio_mode(testdir) |
| 75 | + |
| 76 | + testdir.makepyfile( |
| 77 | + """ |
| 78 | + import pytest |
| 79 | +
|
| 80 | + @pytest.fixture(scope="session") |
| 81 | + async def async_session_fixture(): |
| 82 | + pass |
| 83 | +
|
| 84 | +
|
| 85 | + async def test_whatever(async_session_fixture): |
| 86 | + pass |
| 87 | + """ |
| 88 | + ) |
| 89 | + |
| 90 | + result = testdir.runpytest() |
| 91 | + |
| 92 | + result.assert_outcomes(error=1) |
| 93 | + result.stdout.fnmatch_lines(["*: Trio fixtures must be function-scope*"]) |
| 94 | + |
| 95 | + |
| 96 | +@enable_trio_mode |
| 97 | +def test_async_fixture_with_sync_test_in_trio_mode(testdir, enable_trio_mode): |
| 98 | + enable_trio_mode(testdir) |
| 99 | + |
| 100 | + testdir.makepyfile( |
| 101 | + """ |
| 102 | + import pytest |
| 103 | +
|
| 104 | + @pytest.fixture |
| 105 | + async def async_fixture(): |
| 106 | + pass |
| 107 | +
|
| 108 | +
|
| 109 | + def test_whatever(async_fixture): |
| 110 | + pass |
| 111 | + """ |
| 112 | + ) |
| 113 | + |
| 114 | + result = testdir.runpytest() |
| 115 | + |
| 116 | + result.assert_outcomes(error=1) |
| 117 | + result.stdout.fnmatch_lines( |
| 118 | + ["*: Trio fixtures can only be used by Trio tests*"] |
| 119 | + ) |
0 commit comments