|
| 1 | +from textwrap import dedent |
| 2 | + |
| 3 | +from pytest import Pytester |
| 4 | + |
| 5 | + |
| 6 | +def test_asyncio_mark_provides_package_scoped_loop_strict_mode(pytester: Pytester): |
| 7 | + package_name = pytester.path.name |
| 8 | + pytester.makepyfile( |
| 9 | + __init__="", |
| 10 | + shared_module=dedent( |
| 11 | + """\ |
| 12 | + import asyncio |
| 13 | +
|
| 14 | + loop: asyncio.AbstractEventLoop = None |
| 15 | + """ |
| 16 | + ), |
| 17 | + test_module_one=dedent( |
| 18 | + f"""\ |
| 19 | + import asyncio |
| 20 | + import pytest |
| 21 | +
|
| 22 | + from {package_name} import shared_module |
| 23 | +
|
| 24 | + @pytest.mark.asyncio(scope="package") |
| 25 | + async def test_remember_loop(): |
| 26 | + shared_module.loop = asyncio.get_running_loop() |
| 27 | + """ |
| 28 | + ), |
| 29 | + test_module_two=dedent( |
| 30 | + f"""\ |
| 31 | + import asyncio |
| 32 | + import pytest |
| 33 | +
|
| 34 | + from {package_name} import shared_module |
| 35 | +
|
| 36 | + pytestmark = pytest.mark.asyncio(scope="package") |
| 37 | +
|
| 38 | + async def test_this_runs_in_same_loop(): |
| 39 | + assert asyncio.get_running_loop() is shared_module.loop |
| 40 | +
|
| 41 | + class TestClassA: |
| 42 | + async def test_this_runs_in_same_loop(self): |
| 43 | + assert asyncio.get_running_loop() is shared_module.loop |
| 44 | + """ |
| 45 | + ), |
| 46 | + ) |
| 47 | + result = pytester.runpytest("--asyncio-mode=strict") |
| 48 | + result.assert_outcomes(passed=3) |
| 49 | + |
| 50 | + |
| 51 | +def test_raise_when_event_loop_fixture_is_requested_in_addition_to_scoped_loop( |
| 52 | + pytester: Pytester, |
| 53 | +): |
| 54 | + pytester.makepyfile( |
| 55 | + __init__="", |
| 56 | + test_raises=dedent( |
| 57 | + """\ |
| 58 | + import asyncio |
| 59 | + import pytest |
| 60 | +
|
| 61 | + @pytest.mark.asyncio(scope="package") |
| 62 | + async def test_remember_loop(event_loop): |
| 63 | + pass |
| 64 | + """ |
| 65 | + ), |
| 66 | + ) |
| 67 | + result = pytester.runpytest("--asyncio-mode=strict") |
| 68 | + result.assert_outcomes(errors=1) |
| 69 | + result.stdout.fnmatch_lines("*MultipleEventLoopsRequestedError: *") |
| 70 | + |
| 71 | + |
| 72 | +def test_asyncio_mark_respects_the_loop_policy( |
| 73 | + pytester: Pytester, |
| 74 | +): |
| 75 | + pytester.makepyfile( |
| 76 | + __init__="", |
| 77 | + conftest=dedent( |
| 78 | + """\ |
| 79 | + import pytest |
| 80 | +
|
| 81 | + from .custom_policy import CustomEventLoopPolicy |
| 82 | +
|
| 83 | + @pytest.fixture(scope="package") |
| 84 | + def event_loop_policy(): |
| 85 | + return CustomEventLoopPolicy() |
| 86 | + """ |
| 87 | + ), |
| 88 | + custom_policy=dedent( |
| 89 | + """\ |
| 90 | + import asyncio |
| 91 | +
|
| 92 | + class CustomEventLoopPolicy(asyncio.DefaultEventLoopPolicy): |
| 93 | + pass |
| 94 | + """ |
| 95 | + ), |
| 96 | + test_uses_custom_policy=dedent( |
| 97 | + """\ |
| 98 | + import asyncio |
| 99 | + import pytest |
| 100 | +
|
| 101 | + from .custom_policy import CustomEventLoopPolicy |
| 102 | +
|
| 103 | + pytestmark = pytest.mark.asyncio(scope="package") |
| 104 | +
|
| 105 | + async def test_uses_custom_event_loop_policy(): |
| 106 | + assert isinstance( |
| 107 | + asyncio.get_event_loop_policy(), |
| 108 | + CustomEventLoopPolicy, |
| 109 | + ) |
| 110 | + """ |
| 111 | + ), |
| 112 | + test_also_uses_custom_policy=dedent( |
| 113 | + """\ |
| 114 | + import asyncio |
| 115 | + import pytest |
| 116 | +
|
| 117 | + from .custom_policy import CustomEventLoopPolicy |
| 118 | +
|
| 119 | + pytestmark = pytest.mark.asyncio(scope="package") |
| 120 | +
|
| 121 | + async def test_also_uses_custom_event_loop_policy(): |
| 122 | + assert isinstance( |
| 123 | + asyncio.get_event_loop_policy(), |
| 124 | + CustomEventLoopPolicy, |
| 125 | + ) |
| 126 | + """ |
| 127 | + ), |
| 128 | + ) |
| 129 | + result = pytester.runpytest("--asyncio-mode=strict") |
| 130 | + result.assert_outcomes(passed=2) |
| 131 | + |
| 132 | + |
| 133 | +def test_asyncio_mark_respects_parametrized_loop_policies( |
| 134 | + pytester: Pytester, |
| 135 | +): |
| 136 | + pytester.makepyfile( |
| 137 | + dedent( |
| 138 | + """\ |
| 139 | + import asyncio |
| 140 | +
|
| 141 | + import pytest |
| 142 | +
|
| 143 | + pytestmark = pytest.mark.asyncio(scope="module") |
| 144 | +
|
| 145 | + @pytest.fixture( |
| 146 | + scope="module", |
| 147 | + params=[ |
| 148 | + asyncio.DefaultEventLoopPolicy(), |
| 149 | + asyncio.DefaultEventLoopPolicy(), |
| 150 | + ], |
| 151 | + ) |
| 152 | + def event_loop_policy(request): |
| 153 | + return request.param |
| 154 | +
|
| 155 | + async def test_parametrized_loop(): |
| 156 | + pass |
| 157 | + """ |
| 158 | + ) |
| 159 | + ) |
| 160 | + result = pytester.runpytest_subprocess("--asyncio-mode=strict") |
| 161 | + result.assert_outcomes(passed=2) |
| 162 | + |
| 163 | + |
| 164 | +def test_asyncio_mark_provides_module_scoped_loop_to_fixtures( |
| 165 | + pytester: Pytester, |
| 166 | +): |
| 167 | + pytester.makepyfile( |
| 168 | + dedent( |
| 169 | + """\ |
| 170 | + import asyncio |
| 171 | +
|
| 172 | + import pytest |
| 173 | + import pytest_asyncio |
| 174 | +
|
| 175 | + pytestmark = pytest.mark.asyncio(scope="module") |
| 176 | +
|
| 177 | + loop: asyncio.AbstractEventLoop |
| 178 | +
|
| 179 | + @pytest_asyncio.fixture(scope="module") |
| 180 | + async def my_fixture(): |
| 181 | + global loop |
| 182 | + loop = asyncio.get_running_loop() |
| 183 | +
|
| 184 | + async def test_runs_is_same_loop_as_fixture(my_fixture): |
| 185 | + global loop |
| 186 | + assert asyncio.get_running_loop() is loop |
| 187 | + """ |
| 188 | + ) |
| 189 | + ) |
| 190 | + result = pytester.runpytest_subprocess("--asyncio-mode=strict") |
| 191 | + result.assert_outcomes(passed=1) |
0 commit comments