| 1 |
"""Test if pytestmark works when defined on a class.""" |
| 2 |
|
| 3 |
from __future__ import annotations |
| 4 |
|
| 5 |
import asyncio |
| 6 |
import sys |
| 7 |
from textwrap import dedent |
| 8 |
|
| 9 |
import pytest |
| 10 |
|
| 11 |
|
| 12 |
class TestPyTestMark: |
| 13 |
pytestmark = pytest.mark.asyncio |
| 14 |
|
| 15 |
async def test_is_asyncio(self, sample_fixture): |
| 16 |
assert asyncio.get_event_loop() |
| 17 |
counter = 1 |
| 18 |
|
| 19 |
async def inc(): |
| 20 |
nonlocal counter |
| 21 |
counter += 1 |
| 22 |
await asyncio.sleep(0) |
| 23 |
|
| 24 |
await asyncio.ensure_future(inc()) |
| 25 |
assert counter == 2 |
| 26 |
|
| 27 |
|
| 28 |
@pytest.fixture |
| 29 |
def sample_fixture(): |
| 30 |
return None |
| 31 |
|
| 32 |
|
| 33 |
def test_asyncio_mark_provides_class_scoped_loop_when_applied_to_functions( |
| 34 |
pytester: pytest.Pytester, |
| 35 |
): |
| 36 |
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function") |
| 37 |
pytester.makepyfile(dedent("""\ |
| 38 |
import asyncio |
| 39 |
import pytest |
| 40 |
|
| 41 |
class TestClassScopedLoop: |
| 42 |
loop: asyncio.AbstractEventLoop |
| 43 |
|
| 44 |
@pytest.mark.asyncio(loop_scope="class") |
| 45 |
async def test_remember_loop(self): |
| 46 |
TestClassScopedLoop.loop = asyncio.get_running_loop() |
| 47 |
|
| 48 |
@pytest.mark.asyncio(loop_scope="class") |
| 49 |
async def test_this_runs_in_same_loop(self): |
| 50 |
assert asyncio.get_running_loop() is TestClassScopedLoop.loop |
| 51 |
""")) |
| 52 |
result = pytester.runpytest("--asyncio-mode=strict") |
| 53 |
result.assert_outcomes(passed=2) |
| 54 |
|
| 55 |
|
| 56 |
def test_asyncio_mark_provides_class_scoped_loop_when_applied_to_class( |
| 57 |
pytester: pytest.Pytester, |
| 58 |
): |
| 59 |
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function") |
| 60 |
pytester.makepyfile(dedent("""\ |
| 61 |
import asyncio |
| 62 |
import pytest |
| 63 |
|
| 64 |
@pytest.mark.asyncio(loop_scope="class") |
| 65 |
class TestClassScopedLoop: |
| 66 |
loop: asyncio.AbstractEventLoop |
| 67 |
|
| 68 |
async def test_remember_loop(self): |
| 69 |
TestClassScopedLoop.loop = asyncio.get_running_loop() |
| 70 |
|
| 71 |
async def test_this_runs_in_same_loop(self): |
| 72 |
assert asyncio.get_running_loop() is TestClassScopedLoop.loop |
| 73 |
""")) |
| 74 |
result = pytester.runpytest("--asyncio-mode=strict") |
| 75 |
result.assert_outcomes(passed=2) |
| 76 |
|
| 77 |
|
| 78 |
def test_asyncio_mark_is_inherited_to_subclasses(pytester: pytest.Pytester): |
| 79 |
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function") |
| 80 |
pytester.makepyfile(dedent("""\ |
| 81 |
import asyncio |
| 82 |
import pytest |
| 83 |
|
| 84 |
@pytest.mark.asyncio(loop_scope="class") |
| 85 |
class TestSuperClassWithMark: |
| 86 |
pass |
| 87 |
|
| 88 |
class TestWithoutMark(TestSuperClassWithMark): |
| 89 |
loop: asyncio.AbstractEventLoop |
| 90 |
|
| 91 |
async def test_remember_loop(self): |
| 92 |
TestWithoutMark.loop = asyncio.get_running_loop() |
| 93 |
|
| 94 |
async def test_this_runs_in_same_loop(self): |
| 95 |
assert asyncio.get_running_loop() is TestWithoutMark.loop |
| 96 |
""")) |
| 97 |
result = pytester.runpytest("--asyncio-mode=strict") |
| 98 |
result.assert_outcomes(passed=2) |
| 99 |
|
| 100 |
|
| 101 |
def test_asyncio_mark_respects_the_loop_policy( |
| 102 |
pytester: pytest.Pytester, |
| 103 |
): |
| 104 |
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function") |
| 105 |
pytester.makepyfile(dedent("""\ |
| 106 |
import asyncio |
| 107 |
import pytest |
| 108 |
|
| 109 |
class CustomEventLoopPolicy(asyncio.DefaultEventLoopPolicy): |
| 110 |
pass |
| 111 |
|
| 112 |
class TestUsesCustomEventLoop: |
| 113 |
@pytest.fixture(scope="class") |
| 114 |
def event_loop_policy(self): |
| 115 |
return CustomEventLoopPolicy() |
| 116 |
|
| 117 |
@pytest.mark.asyncio |
| 118 |
async def test_uses_custom_event_loop_policy(self): |
| 119 |
assert isinstance( |
| 120 |
asyncio.get_event_loop_policy(), |
| 121 |
CustomEventLoopPolicy, |
| 122 |
) |
| 123 |
|
| 124 |
@pytest.mark.asyncio |
| 125 |
async def test_does_not_use_custom_event_loop_policy(): |
| 126 |
assert not isinstance( |
| 127 |
asyncio.get_event_loop_policy(), |
| 128 |
CustomEventLoopPolicy, |
| 129 |
) |
| 130 |
""")) |
| 131 |
pytest_args = ["--asyncio-mode=strict"] |
| 132 |
if sys.version_info >= (3, 14): |
| 133 |
pytest_args.extend(["-W", "default"]) |
| 134 |
result = pytester.runpytest(*pytest_args) |
| 135 |
if sys.version_info >= (3, 14): |
| 136 |
result.assert_outcomes(passed=2, warnings=4) |
| 137 |
result.stdout.fnmatch_lines("*DefaultEventLoopPolicy*") |
| 138 |
else: |
| 139 |
result.assert_outcomes(passed=2) |
| 140 |
|
| 141 |
|
| 142 |
def test_asyncio_mark_respects_parametrized_loop_policies( |
| 143 |
pytester: pytest.Pytester, |
| 144 |
): |
| 145 |
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function") |
| 146 |
pytester.makepyfile(dedent("""\ |
| 147 |
import asyncio |
| 148 |
|
| 149 |
import pytest |
| 150 |
|
| 151 |
@pytest.fixture( |
| 152 |
scope="class", |
| 153 |
params=[ |
| 154 |
asyncio.DefaultEventLoopPolicy(), |
| 155 |
asyncio.DefaultEventLoopPolicy(), |
| 156 |
] |
| 157 |
) |
| 158 |
def event_loop_policy(request): |
| 159 |
return request.param |
| 160 |
|
| 161 |
@pytest.mark.asyncio(loop_scope="class") |
| 162 |
class TestWithDifferentLoopPolicies: |
| 163 |
async def test_parametrized_loop(self, request): |
| 164 |
pass |
| 165 |
""")) |
| 166 |
pytest_args = ["--asyncio-mode=strict"] |
| 167 |
if sys.version_info >= (3, 14): |
| 168 |
pytest_args.extend(["-W", "default"]) |
| 169 |
result = pytester.runpytest(*pytest_args) |
| 170 |
if sys.version_info >= (3, 14): |
| 171 |
result.assert_outcomes(passed=2, warnings=4) |
| 172 |
result.stdout.fnmatch_lines("*DefaultEventLoopPolicy*") |
| 173 |
else: |
| 174 |
result.assert_outcomes(passed=2) |
| 175 |
|
| 176 |
|
| 177 |
def test_asyncio_mark_provides_class_scoped_loop_to_fixtures( |
| 178 |
pytester: pytest.Pytester, |
| 179 |
): |
| 180 |
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function") |
| 181 |
pytester.makepyfile(dedent("""\ |
| 182 |
import asyncio |
| 183 |
|
| 184 |
import pytest |
| 185 |
import pytest_asyncio |
| 186 |
|
| 187 |
@pytest.mark.asyncio(loop_scope="class") |
| 188 |
class TestClassScopedLoop: |
| 189 |
loop: asyncio.AbstractEventLoop |
| 190 |
|
| 191 |
@pytest_asyncio.fixture |
| 192 |
async def my_fixture(self): |
| 193 |
TestClassScopedLoop.loop = asyncio.get_running_loop() |
| 194 |
|
| 195 |
@pytest.mark.asyncio |
| 196 |
async def test_runs_is_same_loop_as_fixture(self, my_fixture): |
| 197 |
assert asyncio.get_running_loop() is TestClassScopedLoop.loop |
| 198 |
""")) |
| 199 |
result = pytester.runpytest("--asyncio-mode=strict") |
| 200 |
result.assert_outcomes(passed=1) |
| 201 |
|
| 202 |
|
| 203 |
def test_asyncio_mark_allows_combining_class_scoped_fixture_with_function_scoped_test( |
| 204 |
pytester: pytest.Pytester, |
| 205 |
): |
| 206 |
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function") |
| 207 |
pytester.makepyfile( |
| 208 |
dedent("""\ |
| 209 |
import asyncio |
| 210 |
|
| 211 |
import pytest |
| 212 |
import pytest_asyncio |
| 213 |
|
| 214 |
loop: asyncio.AbstractEventLoop |
| 215 |
|
| 216 |
class TestMixedScopes: |
| 217 |
@pytest_asyncio.fixture(loop_scope="class", scope="class") |
| 218 |
async def async_fixture(self): |
| 219 |
global loop |
| 220 |
loop = asyncio.get_running_loop() |
| 221 |
|
| 222 |
@pytest.mark.asyncio(loop_scope="function") |
| 223 |
async def test_runs_in_different_loop_as_fixture(self, async_fixture): |
| 224 |
global loop |
| 225 |
assert asyncio.get_running_loop() is not loop |
| 226 |
|
| 227 |
"""), |
| 228 |
) |
| 229 |
result = pytester.runpytest("--asyncio-mode=strict") |
| 230 |
result.assert_outcomes(passed=1) |
| 231 |
|
| 232 |
|
| 233 |
def test_asyncio_mark_handles_missing_event_loop_triggered_by_fixture( |
| 234 |
pytester: pytest.Pytester, |
| 235 |
): |
| 236 |
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function") |
| 237 |
pytester.makepyfile(dedent("""\ |
| 238 |
import pytest |
| 239 |
import asyncio |
| 240 |
|
| 241 |
class TestClass: |
| 242 |
@pytest.fixture(scope="class") |
| 243 |
def sets_event_loop_to_none(self): |
| 244 |
# asyncio.run() creates a new event loop without closing the |
| 245 |
# existing one. For any test, but the first one, this leads to |
| 246 |
# a ResourceWarning when the discarded loop is destroyed by the |
| 247 |
# garbage collector. We close the current loop to avoid this. |
| 248 |
try: |
| 249 |
asyncio.get_event_loop().close() |
| 250 |
except RuntimeError: |
| 251 |
pass |
| 252 |
return asyncio.run(asyncio.sleep(0)) |
| 253 |
# asyncio.run() sets the current event loop to None when finished |
| 254 |
|
| 255 |
@pytest.mark.asyncio(loop_scope="class") |
| 256 |
# parametrization may impact fixture ordering |
| 257 |
@pytest.mark.parametrize("n", (0, 1)) |
| 258 |
async def test_does_not_fail(self, sets_event_loop_to_none, n): |
| 259 |
pass |
| 260 |
""")) |
| 261 |
result = pytester.runpytest("--asyncio-mode=strict") |
| 262 |
result.assert_outcomes(passed=2) |
| 263 |
|
| 264 |
|
| 265 |
def test_standalone_test_does_not_trigger_warning_about_no_current_event_loop_being_set( |
| 266 |
pytester: pytest.Pytester, |
| 267 |
): |
| 268 |
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function") |
| 269 |
pytester.makepyfile(dedent("""\ |
| 270 |
import pytest |
| 271 |
|
| 272 |
@pytest.mark.asyncio(loop_scope="class") |
| 273 |
class TestClass: |
| 274 |
async def test_anything(self): |
| 275 |
pass |
| 276 |
""")) |
| 277 |
result = pytester.runpytest("--asyncio-mode=strict") |
| 278 |
result.assert_outcomes(warnings=0, passed=1) |