-
Notifications
You must be signed in to change notification settings - Fork 189
Expand file tree
/
Copy pathtest_fixture_loop_scopes.py
More file actions
161 lines (133 loc) · 5.17 KB
/
Copy pathtest_fixture_loop_scopes.py
File metadata and controls
161 lines (133 loc) · 5.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
from __future__ import annotations
from textwrap import dedent
import pytest
from pytest import Pytester
@pytest.mark.parametrize(
"fixture_scope", ("session", "package", "module", "class", "function")
)
def test_loop_scope_session_is_independent_of_fixture_scope(
pytester: Pytester,
fixture_scope: str,
):
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
pytester.makepyfile(dedent(f"""\
import asyncio
import pytest
import pytest_asyncio
loop: asyncio.AbstractEventLoop = None
@pytest_asyncio.fixture(scope="{fixture_scope}", loop_scope="session")
async def fixture():
global loop
loop = asyncio.get_running_loop()
@pytest.mark.asyncio(loop_scope="session")
async def test_runs_in_same_loop_as_fixture(fixture):
global loop
assert loop == asyncio.get_running_loop()
"""))
result = pytester.runpytest("--asyncio-mode=strict")
result.assert_outcomes(passed=1)
def test_default_fixture_loop_scope_is_function_when_unset(pytester: Pytester):
pytester.makepyfile(dedent("""
import asyncio
import pytest
import pytest_asyncio
fixture_loops = []
@pytest_asyncio.fixture
async def fixture_loop():
loop = asyncio.get_running_loop()
fixture_loops.append(loop)
return loop
@pytest.mark.asyncio
async def test_fixture_uses_function_loop_scope(fixture_loop):
assert asyncio.get_running_loop() is fixture_loop
@pytest.mark.asyncio
async def test_fixture_uses_new_function_loop(fixture_loop):
assert asyncio.get_running_loop() is fixture_loop
assert fixture_loops[0] is not fixture_loops[1]
"""))
result = pytester.runpytest_subprocess("--asyncio-mode=strict", "-W", "error")
result.assert_outcomes(passed=2)
result.stdout.fnmatch_lines(
[
"*asyncio_default_fixture_loop_scope=function*",
]
)
@pytest.mark.parametrize("default_loop_scope", ("function", "module", "session"))
def test_default_loop_scope_config_option_changes_fixture_loop_scope(
pytester: Pytester,
default_loop_scope: str,
):
pytester.makeini(dedent(f"""\
[pytest]
asyncio_default_fixture_loop_scope = {default_loop_scope}
"""))
pytester.makepyfile(dedent(f"""\
import asyncio
import pytest
import pytest_asyncio
@pytest_asyncio.fixture
async def fixture_loop():
return asyncio.get_running_loop()
@pytest.mark.asyncio(loop_scope="{default_loop_scope}")
async def test_runs_in_fixture_loop(fixture_loop):
assert asyncio.get_running_loop() is fixture_loop
"""))
result = pytester.runpytest("--asyncio-mode=strict")
result.assert_outcomes(passed=1)
def test_default_class_loop_scope_config_option_changes_fixture_loop_scope(
pytester: Pytester,
):
pytester.makeini(dedent("""\
[pytest]
asyncio_default_fixture_loop_scope = class
"""))
pytester.makepyfile(dedent("""\
import asyncio
import pytest
import pytest_asyncio
class TestClass:
@pytest_asyncio.fixture
async def fixture_loop(self):
return asyncio.get_running_loop()
@pytest.mark.asyncio(loop_scope="class")
async def test_runs_in_fixture_loop(self, fixture_loop):
assert asyncio.get_running_loop() is fixture_loop
"""))
result = pytester.runpytest("--asyncio-mode=strict")
result.assert_outcomes(passed=1)
def test_default_package_loop_scope_config_option_changes_fixture_loop_scope(
pytester: Pytester,
):
pytester.makeini(dedent("""\
[pytest]
asyncio_default_fixture_loop_scope = package
"""))
pytester.makepyfile(
__init__="",
test_a=dedent("""\
import asyncio
import pytest
import pytest_asyncio
@pytest_asyncio.fixture
async def fixture_loop():
return asyncio.get_running_loop()
@pytest.mark.asyncio(loop_scope="package")
async def test_runs_in_fixture_loop(fixture_loop):
assert asyncio.get_running_loop() is fixture_loop
"""),
)
result = pytester.runpytest("--asyncio-mode=strict")
result.assert_outcomes(passed=1)
def test_invalid_default_fixture_loop_scope_raises_error(pytester: Pytester):
pytester.makeini("""\
[pytest]
asyncio_default_fixture_loop_scope = invalid_scope
""")
result = pytester.runpytest("--assert=plain")
result.stderr.fnmatch_lines(
[
"ERROR: 'invalid_scope' is not a valid "
"asyncio_default_fixture_loop_scope. Valid scopes are: "
"function, class, module, package, session."
]
)