Debian Codegraph @

tests/modes/test_auto_mode.py

raw
1 from __future__ import annotations
2
3 from textwrap import dedent
4
5 from pytest import Pytester
6
7
8 def test_auto_mode_cmdline(pytester: Pytester):
9 pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
10 pytester.makepyfile(dedent("""\
11 import asyncio
12 import pytest
13
14 pytest_plugins = 'pytest_asyncio'
15
16 async def test_a():
17 await asyncio.sleep(0)
18 """))
19 result = pytester.runpytest("--asyncio-mode=auto")
20 result.assert_outcomes(passed=1)
21
22
23 def test_auto_mode_cfg(pytester: Pytester):
24 pytester.makeini(dedent("""\
25 [pytest]
26 asyncio_default_fixture_loop_scope = function
27 asyncio_mode = auto
28 """))
29 pytester.makepyfile(dedent("""\
30 import asyncio
31 import pytest
32
33 pytest_plugins = 'pytest_asyncio'
34
35 async def test_a():
36 await asyncio.sleep(0)
37 """))
38 result = pytester.runpytest("--asyncio-mode=auto")
39 result.assert_outcomes(passed=1)
40
41
42 def test_auto_mode_async_fixture(pytester: Pytester):
43 pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
44 pytester.makepyfile(dedent("""\
45 import asyncio
46 import pytest
47
48 pytest_plugins = 'pytest_asyncio'
49
50 @pytest.fixture
51 async def fixture_a():
52 await asyncio.sleep(0)
53 return 1
54
55 async def test_a(fixture_a):
56 await asyncio.sleep(0)
57 assert fixture_a == 1
58 """))
59 result = pytester.runpytest("--asyncio-mode=auto")
60 result.assert_outcomes(passed=1)
61
62
63 def test_auto_mode_method_fixture(pytester: Pytester):
64 pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
65 pytester.makepyfile(dedent("""\
66 import asyncio
67 import pytest
68
69 pytest_plugins = 'pytest_asyncio'
70
71
72 class TestA:
73
74 @pytest.fixture
75 async def fixture_a(self):
76 await asyncio.sleep(0)
77 return 1
78
79 async def test_a(self, fixture_a):
80 await asyncio.sleep(0)
81 assert fixture_a == 1
82 """))
83 result = pytester.runpytest("--asyncio-mode=auto")
84 result.assert_outcomes(passed=1)
85
86
87 def test_auto_mode_static_method(pytester: Pytester):
88 pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
89 pytester.makepyfile(dedent("""\
90 import asyncio
91
92 pytest_plugins = 'pytest_asyncio'
93
94
95 class TestA:
96
97 @staticmethod
98 async def test_a():
99 await asyncio.sleep(0)
100 """))
101 result = pytester.runpytest("--asyncio-mode=auto")
102 result.assert_outcomes(passed=1)
103
104
105 def test_auto_mode_static_method_fixture(pytester: Pytester):
106 pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
107 pytester.makepyfile(dedent("""\
108 import asyncio
109 import pytest
110
111 pytest_plugins = 'pytest_asyncio'
112
113
114 class TestA:
115
116 @staticmethod
117 @pytest.fixture
118 async def fixture_a():
119 await asyncio.sleep(0)
120 return 1
121
122 @staticmethod
123 async def test_a(fixture_a):
124 await asyncio.sleep(0)
125 assert fixture_a == 1
126 """))
127 result = pytester.runpytest("--asyncio-mode=auto")
128 result.assert_outcomes(passed=1)