|
| 1 | +from textwrap import dedent |
| 2 | + |
| 3 | +from pytest import Pytester |
| 4 | + |
| 5 | + |
| 6 | +def test_asyncio_mark_provides_session_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="session") |
| 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="session") |
| 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 | + subpackage_name = "subpkg" |
| 48 | + subpkg = pytester.mkpydir(subpackage_name) |
| 49 | + subpkg.joinpath("test_subpkg.py").write_text( |
| 50 | + dedent( |
| 51 | + f"""\ |
| 52 | + import asyncio |
| 53 | + import pytest |
| 54 | +
|
| 55 | + from {package_name} import shared_module |
| 56 | +
|
| 57 | + pytestmark = pytest.mark.asyncio(scope="session") |
| 58 | +
|
| 59 | + async def test_subpackage_runs_in_same_loop(): |
| 60 | + assert asyncio.get_running_loop() is shared_module.loop |
| 61 | + """ |
| 62 | + ) |
| 63 | + ) |
| 64 | + result = pytester.runpytest("--asyncio-mode=strict") |
| 65 | + result.assert_outcomes(passed=4) |
| 66 | + |
| 67 | + |
| 68 | +def test_raise_when_event_loop_fixture_is_requested_in_addition_to_scoped_loop( |
| 69 | + pytester: Pytester, |
| 70 | +): |
| 71 | + pytester.makepyfile( |
| 72 | + __init__="", |
| 73 | + test_raises=dedent( |
| 74 | + """\ |
| 75 | + import asyncio |
| 76 | + import pytest |
| 77 | +
|
| 78 | + @pytest.mark.asyncio(scope="session") |
| 79 | + async def test_remember_loop(event_loop): |
| 80 | + pass |
| 81 | + """ |
| 82 | + ), |
| 83 | + ) |
| 84 | + result = pytester.runpytest("--asyncio-mode=strict") |
| 85 | + result.assert_outcomes(errors=1) |
| 86 | + result.stdout.fnmatch_lines("*MultipleEventLoopsRequestedError: *") |
| 87 | + |
| 88 | + |
| 89 | +def test_asyncio_mark_respects_the_loop_policy( |
| 90 | + pytester: Pytester, |
| 91 | +): |
| 92 | + pytester.makepyfile( |
| 93 | + __init__="", |
| 94 | + conftest=dedent( |
| 95 | + """\ |
| 96 | + import pytest |
| 97 | +
|
| 98 | + from .custom_policy import CustomEventLoopPolicy |
| 99 | +
|
| 100 | + @pytest.fixture(scope="session") |
| 101 | + def event_loop_policy(): |
| 102 | + return CustomEventLoopPolicy() |
| 103 | + """ |
| 104 | + ), |
| 105 | + custom_policy=dedent( |
| 106 | + """\ |
| 107 | + import asyncio |
| 108 | +
|
| 109 | + class CustomEventLoopPolicy(asyncio.DefaultEventLoopPolicy): |
| 110 | + pass |
| 111 | + """ |
| 112 | + ), |
| 113 | + test_uses_custom_policy=dedent( |
| 114 | + """\ |
| 115 | + import asyncio |
| 116 | + import pytest |
| 117 | +
|
| 118 | + from .custom_policy import CustomEventLoopPolicy |
| 119 | +
|
| 120 | + pytestmark = pytest.mark.asyncio(scope="session") |
| 121 | +
|
| 122 | + async def test_uses_custom_event_loop_policy(): |
| 123 | + assert isinstance( |
| 124 | + asyncio.get_event_loop_policy(), |
| 125 | + CustomEventLoopPolicy, |
| 126 | + ) |
| 127 | + """ |
| 128 | + ), |
| 129 | + test_also_uses_custom_policy=dedent( |
| 130 | + """\ |
| 131 | + import asyncio |
| 132 | + import pytest |
| 133 | +
|
| 134 | + from .custom_policy import CustomEventLoopPolicy |
| 135 | +
|
| 136 | + pytestmark = pytest.mark.asyncio(scope="session") |
| 137 | +
|
| 138 | + async def test_also_uses_custom_event_loop_policy(): |
| 139 | + assert isinstance( |
| 140 | + asyncio.get_event_loop_policy(), |
| 141 | + CustomEventLoopPolicy, |
| 142 | + ) |
| 143 | + """ |
| 144 | + ), |
| 145 | + ) |
| 146 | + result = pytester.runpytest("--asyncio-mode=strict") |
| 147 | + result.assert_outcomes(passed=2) |
| 148 | + |
| 149 | + |
| 150 | +def test_asyncio_mark_respects_parametrized_loop_policies( |
| 151 | + pytester: Pytester, |
| 152 | +): |
| 153 | + pytester.makepyfile( |
| 154 | + __init__="", |
| 155 | + test_parametrization=dedent( |
| 156 | + """\ |
| 157 | + import asyncio |
| 158 | +
|
| 159 | + import pytest |
| 160 | +
|
| 161 | + pytestmark = pytest.mark.asyncio(scope="session") |
| 162 | +
|
| 163 | + @pytest.fixture( |
| 164 | + scope="session", |
| 165 | + params=[ |
| 166 | + asyncio.DefaultEventLoopPolicy(), |
| 167 | + asyncio.DefaultEventLoopPolicy(), |
| 168 | + ], |
| 169 | + ) |
| 170 | + def event_loop_policy(request): |
| 171 | + return request.param |
| 172 | +
|
| 173 | + async def test_parametrized_loop(): |
| 174 | + pass |
| 175 | + """ |
| 176 | + ), |
| 177 | + ) |
| 178 | + result = pytester.runpytest_subprocess("--asyncio-mode=strict") |
| 179 | + result.assert_outcomes(passed=2) |
| 180 | + |
| 181 | + |
| 182 | +def test_asyncio_mark_provides_session_scoped_loop_to_fixtures( |
| 183 | + pytester: Pytester, |
| 184 | +): |
| 185 | + package_name = pytester.path.name |
| 186 | + pytester.makepyfile( |
| 187 | + __init__="", |
| 188 | + conftest=dedent( |
| 189 | + f"""\ |
| 190 | + import asyncio |
| 191 | +
|
| 192 | + import pytest_asyncio |
| 193 | +
|
| 194 | + from {package_name} import shared_module |
| 195 | +
|
| 196 | + @pytest_asyncio.fixture(scope="session") |
| 197 | + async def my_fixture(): |
| 198 | + shared_module.loop = asyncio.get_running_loop() |
| 199 | + """ |
| 200 | + ), |
| 201 | + shared_module=dedent( |
| 202 | + """\ |
| 203 | + import asyncio |
| 204 | +
|
| 205 | + loop: asyncio.AbstractEventLoop = None |
| 206 | + """ |
| 207 | + ), |
| 208 | + ) |
| 209 | + subpackage_name = "subpkg" |
| 210 | + subpkg = pytester.mkpydir(subpackage_name) |
| 211 | + subpkg.joinpath("test_subpkg.py").write_text( |
| 212 | + dedent( |
| 213 | + f"""\ |
| 214 | + import asyncio |
| 215 | +
|
| 216 | + import pytest |
| 217 | + import pytest_asyncio |
| 218 | +
|
| 219 | + from {package_name} import shared_module |
| 220 | +
|
| 221 | + pytestmark = pytest.mark.asyncio(scope="session") |
| 222 | +
|
| 223 | + async def test_runs_in_same_loop_as_fixture(my_fixture): |
| 224 | + assert asyncio.get_running_loop() is shared_module.loop |
| 225 | + """ |
| 226 | + ) |
| 227 | + ) |
| 228 | + result = pytester.runpytest_subprocess("--asyncio-mode=strict") |
| 229 | + result.assert_outcomes(passed=1) |
0 commit comments