-
Notifications
You must be signed in to change notification settings - Fork 159
/
Copy pathtest_asyncio_fixture.py
68 lines (48 loc) · 1.55 KB
/
test_asyncio_fixture.py
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
from __future__ import annotations
import asyncio
from textwrap import dedent
import pytest
from pytest import Pytester
import pytest_asyncio
@pytest_asyncio.fixture
async def fixture_bare():
await asyncio.sleep(0)
return 1
@pytest.mark.asyncio
async def test_bare_fixture(fixture_bare):
await asyncio.sleep(0)
assert fixture_bare == 1
@pytest_asyncio.fixture(name="new_fixture_name")
async def fixture_with_name(request):
await asyncio.sleep(0)
return request.fixturename
@pytest.mark.asyncio
async def test_fixture_with_name(new_fixture_name):
await asyncio.sleep(0)
assert new_fixture_name == "new_fixture_name"
@pytest_asyncio.fixture(params=[2, 4])
async def fixture_with_params(request):
await asyncio.sleep(0)
return request.param
@pytest.mark.asyncio
async def test_fixture_with_params(fixture_with_params):
await asyncio.sleep(0)
assert fixture_with_params % 2 == 0
@pytest.mark.parametrize("mode", ("auto", "strict"))
def test_sync_function_uses_async_fixture(pytester: Pytester, mode):
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
pytester.makepyfile(
dedent(
"""\
import pytest_asyncio
pytest_plugins = 'pytest_asyncio'
@pytest_asyncio.fixture
async def always_true():
return True
def test_sync_function_uses_async_fixture(always_true):
assert always_true is True
"""
)
)
result = pytester.runpytest(f"--asyncio-mode={mode}")
result.assert_outcomes(passed=1)