Debian Codegraph @

tests/markers/test_module_scope.py

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