1111import pytest
1212from pytest import Pytester
1313
14- _prelude = dedent (
15- """
14+ _prelude = dedent ("""
1615 import pytest
1716 import pytest_asyncio
1817 from contextlib import contextmanager
@@ -27,16 +26,12 @@ def context_var_manager(value):
2726 yield
2827 finally:
2928 _context_var.reset(token)
30- """
31- )
29+ """ )
3230
3331
3432def test_var_from_sync_generator_propagates_to_async (pytester : Pytester ):
3533 pytester .makeini ("[pytest]\n asyncio_default_fixture_loop_scope = function" )
36- pytester .makepyfile (
37- _prelude
38- + dedent (
39- """
34+ pytester .makepyfile (_prelude + dedent ("""
4035 @pytest.fixture
4136 def var_fixture():
4237 with context_var_manager("value"):
@@ -49,19 +44,14 @@ async def check_var_fixture(var_fixture):
4944 @pytest.mark.asyncio
5045 async def test(check_var_fixture):
5146 assert _context_var.get() == "value"
52- """
53- )
54- )
47+ """ ))
5548 result = pytester .runpytest ("--asyncio-mode=strict" )
5649 result .assert_outcomes (passed = 1 )
5750
5851
5952def test_var_from_async_generator_propagates_to_sync (pytester : Pytester ):
6053 pytester .makeini ("[pytest]\n asyncio_default_fixture_loop_scope = function" )
61- pytester .makepyfile (
62- _prelude
63- + dedent (
64- """
54+ pytester .makepyfile (_prelude + dedent ("""
6555 @pytest_asyncio.fixture
6656 async def var_fixture():
6757 with context_var_manager("value"):
@@ -74,19 +64,14 @@ def check_var_fixture(var_fixture):
7464 @pytest.mark.asyncio
7565 async def test(check_var_fixture):
7666 assert _context_var.get() == "value"
77- """
78- )
79- )
67+ """ ))
8068 result = pytester .runpytest ("--asyncio-mode=strict" )
8169 result .assert_outcomes (passed = 1 )
8270
8371
8472def test_var_from_async_fixture_propagates_to_sync (pytester : Pytester ):
8573 pytester .makeini ("[pytest]\n asyncio_default_fixture_loop_scope = function" )
86- pytester .makepyfile (
87- _prelude
88- + dedent (
89- """
74+ pytester .makepyfile (_prelude + dedent ("""
9075 @pytest_asyncio.fixture
9176 async def var_fixture():
9277 _context_var.set("value")
@@ -98,19 +83,14 @@ def check_var_fixture(var_fixture):
9883
9984 def test(check_var_fixture):
10085 assert _context_var.get() == "value"
101- """
102- )
103- )
86+ """ ))
10487 result = pytester .runpytest ("--asyncio-mode=strict" )
10588 result .assert_outcomes (passed = 1 )
10689
10790
10891def test_var_from_generator_reset_before_previous_fixture_cleanup (pytester : Pytester ):
10992 pytester .makeini ("[pytest]\n asyncio_default_fixture_loop_scope = function" )
110- pytester .makepyfile (
111- _prelude
112- + dedent (
113- """
93+ pytester .makepyfile (_prelude + dedent ("""
11494 @pytest_asyncio.fixture
11595 async def no_var_fixture():
11696 with pytest.raises(LookupError):
@@ -127,19 +107,14 @@ async def var_fixture(no_var_fixture):
127107 @pytest.mark.asyncio
128108 async def test(var_fixture):
129109 assert _context_var.get() == "value"
130- """
131- )
132- )
110+ """ ))
133111 result = pytester .runpytest ("--asyncio-mode=strict" )
134112 result .assert_outcomes (passed = 1 )
135113
136114
137115def test_var_from_fixture_reset_before_previous_fixture_cleanup (pytester : Pytester ):
138116 pytester .makeini ("[pytest]\n asyncio_default_fixture_loop_scope = function" )
139- pytester .makepyfile (
140- _prelude
141- + dedent (
142- """
117+ pytester .makepyfile (_prelude + dedent ("""
143118 @pytest_asyncio.fixture
144119 async def no_var_fixture():
145120 with pytest.raises(LookupError):
@@ -156,19 +131,14 @@ async def var_fixture(no_var_fixture):
156131 @pytest.mark.asyncio
157132 async def test(var_fixture):
158133 assert _context_var.get() == "value"
159- """
160- )
161- )
134+ """ ))
162135 result = pytester .runpytest ("--asyncio-mode=strict" )
163136 result .assert_outcomes (passed = 1 )
164137
165138
166139def test_var_previous_value_restored_after_fixture (pytester : Pytester ):
167140 pytester .makeini ("[pytest]\n asyncio_default_fixture_loop_scope = function" )
168- pytester .makepyfile (
169- _prelude
170- + dedent (
171- """
141+ pytester .makepyfile (_prelude + dedent ("""
172142 @pytest_asyncio.fixture
173143 async def var_fixture_1():
174144 with context_var_manager("value1"):
@@ -184,19 +154,14 @@ async def var_fixture_2(var_fixture_1):
184154 @pytest.mark.asyncio
185155 async def test(var_fixture_2):
186156 assert _context_var.get() == "value2"
187- """
188- )
189- )
157+ """ ))
190158 result = pytester .runpytest ("--asyncio-mode=strict" )
191159 result .assert_outcomes (passed = 1 )
192160
193161
194162def test_var_set_to_existing_value_ok (pytester : Pytester ):
195163 pytester .makeini ("[pytest]\n asyncio_default_fixture_loop_scope = function" )
196- pytester .makepyfile (
197- _prelude
198- + dedent (
199- """
164+ pytester .makepyfile (_prelude + dedent ("""
200165 @pytest_asyncio.fixture
201166 async def var_fixture():
202167 with context_var_manager("value"):
@@ -210,18 +175,14 @@ async def same_var_fixture(var_fixture):
210175 @pytest.mark.asyncio
211176 async def test(same_var_fixture):
212177 assert _context_var.get() == "value"
213- """
214- )
215- )
178+ """ ))
216179 result = pytester .runpytest ("--asyncio-mode=strict" )
217180 result .assert_outcomes (passed = 1 )
218181
219182
220183def test_no_isolation_against_context_changes_in_sync_tests (pytester : Pytester ):
221184 pytester .makeini ("[pytest]\n asyncio_default_fixture_loop_scope = function" )
222- pytester .makepyfile (
223- dedent (
224- """
185+ pytester .makepyfile (dedent ("""
225186 import pytest
226187 import pytest_asyncio
227188 from contextvars import ContextVar
@@ -234,9 +195,7 @@ def test_sync():
234195 @pytest.mark.asyncio
235196 async def test_async():
236197 assert _context_var.get() == "new_value"
237- """
238- )
239- )
198+ """ ))
240199 result = pytester .runpytest ("--asyncio-mode=strict" )
241200 result .assert_outcomes (passed = 2 )
242201
@@ -246,9 +205,7 @@ def test_isolation_against_context_changes_in_async_tests(
246205 pytester : Pytester , loop_scope : Literal ["function" , "module" ]
247206):
248207 pytester .makeini ("[pytest]\n asyncio_default_fixture_loop_scope = function" )
249- pytester .makepyfile (
250- dedent (
251- f"""
208+ pytester .makepyfile (dedent (f"""
252209 import pytest
253210 import pytest_asyncio
254211 from contextvars import ContextVar
@@ -263,8 +220,6 @@ async def test_async_first():
263220 async def test_async_second():
264221 with pytest.raises(LookupError):
265222 _context_var.get()
266- """
267- )
268- )
223+ """ ))
269224 result = pytester .runpytest ("--asyncio-mode=strict" )
270225 result .assert_outcomes (passed = 2 )
0 commit comments