| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import sys |
| 4 |
from textwrap import dedent |
| 5 |
|
| 6 |
from pytest import Pytester |
| 7 |
|
| 8 |
|
| 9 |
def test_asyncio_mark_provides_session_scoped_loop_strict_mode(pytester: Pytester): |
| 10 |
package_name = pytester.path.name |
| 11 |
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function") |
| 12 |
pytester.makepyfile( |
| 13 |
__init__="", |
| 14 |
shared_module=dedent("""\ |
| 15 |
import asyncio |
| 16 |
|
| 17 |
loop: asyncio.AbstractEventLoop = None |
| 18 |
"""), |
| 19 |
test_module_one=dedent(f"""\ |
| 20 |
import asyncio |
| 21 |
import pytest |
| 22 |
|
| 23 |
from {package_name} import shared_module |
| 24 |
|
| 25 |
@pytest.mark.asyncio(loop_scope="session") |
| 26 |
async def test_remember_loop(): |
| 27 |
shared_module.loop = asyncio.get_running_loop() |
| 28 |
"""), |
| 29 |
test_module_two=dedent(f"""\ |
| 30 |
import asyncio |
| 31 |
import pytest |
| 32 |
|
| 33 |
from {package_name} import shared_module |
| 34 |
|
| 35 |
pytestmark = pytest.mark.asyncio(loop_scope="session") |
| 36 |
|
| 37 |
async def test_this_runs_in_same_loop(): |
| 38 |
assert asyncio.get_running_loop() is shared_module.loop |
| 39 |
|
| 40 |
class TestClassA: |
| 41 |
async def test_this_runs_in_same_loop(self): |
| 42 |
assert asyncio.get_running_loop() is shared_module.loop |
| 43 |
"""), |
| 44 |
) |
| 45 |
|
| 46 |
# subpackage_name must alphabetically come after test_module_one.py |
| 47 |
subpackage_name = "z_subpkg" |
| 48 |
subpkg = pytester.mkpydir(subpackage_name) |
| 49 |
subpkg.joinpath("test_subpkg.py").write_text(dedent(f"""\ |
| 50 |
import asyncio |
| 51 |
import pytest |
| 52 |
|
| 53 |
from {package_name} import shared_module |
| 54 |
|
| 55 |
pytestmark = pytest.mark.asyncio(loop_scope="session") |
| 56 |
|
| 57 |
async def test_subpackage_runs_in_same_loop(): |
| 58 |
assert asyncio.get_running_loop() is shared_module.loop |
| 59 |
""")) |
| 60 |
result = pytester.runpytest("--asyncio-mode=strict") |
| 61 |
result.assert_outcomes(passed=4) |
| 62 |
|
| 63 |
|
| 64 |
def test_asyncio_mark_respects_the_loop_policy( |
| 65 |
pytester: Pytester, |
| 66 |
): |
| 67 |
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function") |
| 68 |
pytester.makepyfile( |
| 69 |
__init__="", |
| 70 |
conftest=dedent("""\ |
| 71 |
import pytest |
| 72 |
|
| 73 |
from .custom_policy import CustomEventLoopPolicy |
| 74 |
|
| 75 |
@pytest.fixture(scope="session") |
| 76 |
def event_loop_policy(): |
| 77 |
return CustomEventLoopPolicy() |
| 78 |
"""), |
| 79 |
custom_policy=dedent("""\ |
| 80 |
import asyncio |
| 81 |
|
| 82 |
class CustomEventLoopPolicy(asyncio.DefaultEventLoopPolicy): |
| 83 |
pass |
| 84 |
"""), |
| 85 |
test_uses_custom_policy=dedent("""\ |
| 86 |
import asyncio |
| 87 |
import pytest |
| 88 |
|
| 89 |
from .custom_policy import CustomEventLoopPolicy |
| 90 |
|
| 91 |
pytestmark = pytest.mark.asyncio(loop_scope="session") |
| 92 |
|
| 93 |
async def test_uses_custom_event_loop_policy(): |
| 94 |
assert isinstance( |
| 95 |
asyncio.get_event_loop_policy(), |
| 96 |
CustomEventLoopPolicy, |
| 97 |
) |
| 98 |
"""), |
| 99 |
test_also_uses_custom_policy=dedent("""\ |
| 100 |
import asyncio |
| 101 |
import pytest |
| 102 |
|
| 103 |
from .custom_policy import CustomEventLoopPolicy |
| 104 |
|
| 105 |
pytestmark = pytest.mark.asyncio(loop_scope="session") |
| 106 |
|
| 107 |
async def test_also_uses_custom_event_loop_policy(): |
| 108 |
assert isinstance( |
| 109 |
asyncio.get_event_loop_policy(), |
| 110 |
CustomEventLoopPolicy, |
| 111 |
) |
| 112 |
"""), |
| 113 |
) |
| 114 |
pytest_args = ["--asyncio-mode=strict"] |
| 115 |
if sys.version_info >= (3, 14): |
| 116 |
pytest_args.extend(["-W", "default"]) |
| 117 |
result = pytester.runpytest(*pytest_args) |
| 118 |
if sys.version_info >= (3, 14): |
| 119 |
result.assert_outcomes(passed=2, warnings=4) |
| 120 |
result.stdout.fnmatch_lines("*DefaultEventLoopPolicy*") |
| 121 |
else: |
| 122 |
result.assert_outcomes(passed=2) |
| 123 |
|
| 124 |
|
| 125 |
def test_asyncio_mark_respects_parametrized_loop_policies( |
| 126 |
pytester: Pytester, |
| 127 |
): |
| 128 |
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function") |
| 129 |
pytester.makepyfile( |
| 130 |
__init__="", |
| 131 |
test_parametrization=dedent("""\ |
| 132 |
import asyncio |
| 133 |
|
| 134 |
import pytest |
| 135 |
|
| 136 |
pytestmark = pytest.mark.asyncio(loop_scope="session") |
| 137 |
|
| 138 |
@pytest.fixture( |
| 139 |
scope="session", |
| 140 |
params=[ |
| 141 |
asyncio.DefaultEventLoopPolicy(), |
| 142 |
asyncio.DefaultEventLoopPolicy(), |
| 143 |
], |
| 144 |
) |
| 145 |
def event_loop_policy(request): |
| 146 |
return request.param |
| 147 |
|
| 148 |
async def test_parametrized_loop(): |
| 149 |
pass |
| 150 |
"""), |
| 151 |
) |
| 152 |
pytest_args = ["--asyncio-mode=strict"] |
| 153 |
if sys.version_info >= (3, 14): |
| 154 |
pytest_args.extend(["-W", "default"]) |
| 155 |
result = pytester.runpytest(*pytest_args) |
| 156 |
if sys.version_info >= (3, 14): |
| 157 |
result.assert_outcomes(passed=2, warnings=4) |
| 158 |
result.stdout.fnmatch_lines("*DefaultEventLoopPolicy*") |
| 159 |
else: |
| 160 |
result.assert_outcomes(passed=2) |
| 161 |
|
| 162 |
|
| 163 |
def test_asyncio_mark_provides_session_scoped_loop_to_fixtures( |
| 164 |
pytester: Pytester, |
| 165 |
): |
| 166 |
package_name = pytester.path.name |
| 167 |
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function") |
| 168 |
pytester.makepyfile( |
| 169 |
__init__="", |
| 170 |
conftest=dedent(f"""\ |
| 171 |
import asyncio |
| 172 |
|
| 173 |
import pytest_asyncio |
| 174 |
|
| 175 |
from {package_name} import shared_module |
| 176 |
|
| 177 |
@pytest_asyncio.fixture(loop_scope="session", scope="session") |
| 178 |
async def my_fixture(): |
| 179 |
shared_module.loop = asyncio.get_running_loop() |
| 180 |
"""), |
| 181 |
shared_module=dedent("""\ |
| 182 |
import asyncio |
| 183 |
|
| 184 |
loop: asyncio.AbstractEventLoop = None |
| 185 |
"""), |
| 186 |
) |
| 187 |
subpackage_name = "subpkg" |
| 188 |
subpkg = pytester.mkpydir(subpackage_name) |
| 189 |
subpkg.joinpath("test_subpkg.py").write_text(dedent(f"""\ |
| 190 |
import asyncio |
| 191 |
|
| 192 |
import pytest |
| 193 |
import pytest_asyncio |
| 194 |
|
| 195 |
from {package_name} import shared_module |
| 196 |
|
| 197 |
pytestmark = pytest.mark.asyncio(loop_scope="session") |
| 198 |
|
| 199 |
async def test_runs_in_same_loop_as_fixture(my_fixture): |
| 200 |
assert asyncio.get_running_loop() is shared_module.loop |
| 201 |
""")) |
| 202 |
result = pytester.runpytest("--asyncio-mode=strict") |
| 203 |
result.assert_outcomes(passed=1) |
| 204 |
|
| 205 |
|
| 206 |
def test_asyncio_mark_allows_combining_session_scoped_fixture_with_package_scoped_test( |
| 207 |
pytester: Pytester, |
| 208 |
): |
| 209 |
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function") |
| 210 |
pytester.makepyfile( |
| 211 |
__init__="", |
| 212 |
test_mixed_scopes=dedent("""\ |
| 213 |
import asyncio |
| 214 |
|
| 215 |
import pytest |
| 216 |
import pytest_asyncio |
| 217 |
|
| 218 |
loop: asyncio.AbstractEventLoop |
| 219 |
|
| 220 |
@pytest_asyncio.fixture(loop_scope="session", scope="session") |
| 221 |
async def async_fixture(): |
| 222 |
global loop |
| 223 |
loop = asyncio.get_running_loop() |
| 224 |
|
| 225 |
@pytest.mark.asyncio(loop_scope="package") |
| 226 |
async def test_runs_in_different_loop_as_fixture(async_fixture): |
| 227 |
global loop |
| 228 |
assert asyncio.get_running_loop() is not loop |
| 229 |
"""), |
| 230 |
) |
| 231 |
result = pytester.runpytest("--asyncio-mode=strict") |
| 232 |
result.assert_outcomes(passed=1) |
| 233 |
|
| 234 |
|
| 235 |
def test_asyncio_mark_allows_combining_session_scoped_fixture_with_module_scoped_test( |
| 236 |
pytester: Pytester, |
| 237 |
): |
| 238 |
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function") |
| 239 |
pytester.makepyfile( |
| 240 |
__init__="", |
| 241 |
test_mixed_scopes=dedent("""\ |
| 242 |
import asyncio |
| 243 |
|
| 244 |
import pytest |
| 245 |
import pytest_asyncio |
| 246 |
|
| 247 |
loop: asyncio.AbstractEventLoop |
| 248 |
|
| 249 |
@pytest_asyncio.fixture(loop_scope="session", scope="session") |
| 250 |
async def async_fixture(): |
| 251 |
global loop |
| 252 |
loop = asyncio.get_running_loop() |
| 253 |
|
| 254 |
@pytest.mark.asyncio(loop_scope="module") |
| 255 |
async def test_runs_in_different_loop_as_fixture(async_fixture): |
| 256 |
global loop |
| 257 |
assert asyncio.get_running_loop() is not loop |
| 258 |
"""), |
| 259 |
) |
| 260 |
result = pytester.runpytest("--asyncio-mode=strict") |
| 261 |
result.assert_outcomes(passed=1) |
| 262 |
|
| 263 |
|
| 264 |
def test_asyncio_mark_allows_combining_session_scoped_fixture_with_class_scoped_test( |
| 265 |
pytester: Pytester, |
| 266 |
): |
| 267 |
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function") |
| 268 |
pytester.makepyfile( |
| 269 |
__init__="", |
| 270 |
test_mixed_scopes=dedent("""\ |
| 271 |
import asyncio |
| 272 |
|
| 273 |
import pytest |
| 274 |
import pytest_asyncio |
| 275 |
|
| 276 |
loop: asyncio.AbstractEventLoop |
| 277 |
|
| 278 |
@pytest_asyncio.fixture(loop_scope="session", scope="session") |
| 279 |
async def async_fixture(): |
| 280 |
global loop |
| 281 |
loop = asyncio.get_running_loop() |
| 282 |
|
| 283 |
@pytest.mark.asyncio(loop_scope="class") |
| 284 |
class TestMixedScopes: |
| 285 |
async def test_runs_in_different_loop_as_fixture(self, async_fixture): |
| 286 |
global loop |
| 287 |
assert asyncio.get_running_loop() is not loop |
| 288 |
"""), |
| 289 |
) |
| 290 |
result = pytester.runpytest("--asyncio-mode=strict") |
| 291 |
result.assert_outcomes(passed=1) |
| 292 |
|
| 293 |
|
| 294 |
def test_asyncio_mark_allows_combining_session_scoped_fixture_with_function_scoped_test( |
| 295 |
pytester: Pytester, |
| 296 |
): |
| 297 |
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function") |
| 298 |
pytester.makepyfile( |
| 299 |
__init__="", |
| 300 |
test_mixed_scopes=dedent("""\ |
| 301 |
import asyncio |
| 302 |
|
| 303 |
import pytest |
| 304 |
import pytest_asyncio |
| 305 |
|
| 306 |
loop: asyncio.AbstractEventLoop |
| 307 |
|
| 308 |
@pytest_asyncio.fixture(loop_scope="session", scope="session") |
| 309 |
async def async_fixture(): |
| 310 |
global loop |
| 311 |
loop = asyncio.get_running_loop() |
| 312 |
|
| 313 |
@pytest.mark.asyncio |
| 314 |
async def test_runs_in_different_loop_as_fixture(async_fixture): |
| 315 |
global loop |
| 316 |
assert asyncio.get_running_loop() is not loop |
| 317 |
"""), |
| 318 |
) |
| 319 |
result = pytester.runpytest("--asyncio-mode=strict") |
| 320 |
result.assert_outcomes(passed=1) |
| 321 |
|
| 322 |
|
| 323 |
def test_allows_combining_session_scoped_asyncgen_fixture_with_function_scoped_test( |
| 324 |
pytester: Pytester, |
| 325 |
): |
| 326 |
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function") |
| 327 |
pytester.makepyfile( |
| 328 |
__init__="", |
| 329 |
test_mixed_scopes=dedent("""\ |
| 330 |
import asyncio |
| 331 |
|
| 332 |
import pytest |
| 333 |
import pytest_asyncio |
| 334 |
|
| 335 |
loop: asyncio.AbstractEventLoop |
| 336 |
|
| 337 |
@pytest_asyncio.fixture(loop_scope="session", scope="session") |
| 338 |
async def async_fixture(): |
| 339 |
global loop |
| 340 |
loop = asyncio.get_running_loop() |
| 341 |
yield |
| 342 |
|
| 343 |
@pytest.mark.asyncio |
| 344 |
async def test_runs_in_different_loop_as_fixture(async_fixture): |
| 345 |
global loop |
| 346 |
assert asyncio.get_running_loop() is not loop |
| 347 |
"""), |
| 348 |
) |
| 349 |
result = pytester.runpytest("--asyncio-mode=strict") |
| 350 |
result.assert_outcomes(passed=1) |
| 351 |
|
| 352 |
|
| 353 |
def test_asyncio_mark_handles_missing_event_loop_triggered_by_fixture( |
| 354 |
pytester: Pytester, |
| 355 |
): |
| 356 |
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function") |
| 357 |
pytester.makepyfile(dedent("""\ |
| 358 |
import pytest |
| 359 |
import asyncio |
| 360 |
|
| 361 |
@pytest.fixture(scope="session") |
| 362 |
def sets_event_loop_to_none(): |
| 363 |
# asyncio.run() creates a new event loop without closing the existing |
| 364 |
# one. For any test, but the first one, this leads to a ResourceWarning |
| 365 |
# when the discarded loop is destroyed by the garbage collector. |
| 366 |
# We close the current loop to avoid this |
| 367 |
try: |
| 368 |
asyncio.get_event_loop().close() |
| 369 |
except RuntimeError: |
| 370 |
pass |
| 371 |
return asyncio.run(asyncio.sleep(0)) |
| 372 |
# asyncio.run() sets the current event loop to None when finished |
| 373 |
|
| 374 |
@pytest.mark.asyncio(loop_scope="session") |
| 375 |
# parametrization may impact fixture ordering |
| 376 |
@pytest.mark.parametrize("n", (0, 1)) |
| 377 |
async def test_does_not_fail(sets_event_loop_to_none, n): |
| 378 |
pass |
| 379 |
""")) |
| 380 |
result = pytester.runpytest("--asyncio-mode=strict") |
| 381 |
result.assert_outcomes(passed=2) |
| 382 |
|
| 383 |
|
| 384 |
def test_standalone_test_does_not_trigger_warning_about_no_current_event_loop_being_set( |
| 385 |
pytester: Pytester, |
| 386 |
): |
| 387 |
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function") |
| 388 |
pytester.makepyfile(dedent("""\ |
| 389 |
import pytest |
| 390 |
|
| 391 |
@pytest.mark.asyncio(loop_scope="session") |
| 392 |
async def test_anything(): |
| 393 |
pass |
| 394 |
""")) |
| 395 |
result = pytester.runpytest("--asyncio-mode=strict") |
| 396 |
result.assert_outcomes(warnings=0, passed=1) |