-
Notifications
You must be signed in to change notification settings - Fork 162
/
Copy pathtest_simple.py
116 lines (86 loc) · 3.24 KB
/
test_simple.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
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
"""Quick'n'dirty unit tests for provided fixtures and markers."""
from __future__ import annotations
import asyncio
from textwrap import dedent
import pytest
from pytest import Pytester
async def async_coro():
await asyncio.sleep(0)
return "ok"
def test_event_loop_fixture(event_loop):
"""Test the injection of the event_loop fixture."""
assert event_loop
ret = event_loop.run_until_complete(async_coro())
assert ret == "ok"
@pytest.mark.asyncio
async def test_asyncio_marker():
"""Test the asyncio pytest marker."""
await asyncio.sleep(0)
def test_asyncio_marker_compatibility_with_xfail(pytester: Pytester):
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
pytester.makepyfile(
dedent(
"""\
import pytest
pytest_plugins = "pytest_asyncio"
@pytest.mark.xfail(reason="need a failure", strict=True)
@pytest.mark.asyncio
async def test_asyncio_marker_fail():
raise AssertionError
"""
)
)
result = pytester.runpytest("--asyncio-mode=strict")
result.assert_outcomes(xfailed=1)
def test_asyncio_auto_mode_compatibility_with_xfail(pytester: Pytester):
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
pytester.makepyfile(
dedent(
"""\
import pytest
pytest_plugins = "pytest_asyncio"
@pytest.mark.xfail(reason="need a failure", strict=True)
async def test_asyncio_marker_fail():
raise AssertionError
"""
)
)
result = pytester.runpytest("--asyncio-mode=auto")
result.assert_outcomes(xfailed=1)
@pytest.mark.asyncio
async def test_asyncio_marker_with_default_param(a_param=None):
"""Test the asyncio pytest marker."""
await asyncio.sleep(0)
class TestMarkerInClassBasedTests:
"""Test that asyncio marked functions work for methods of test classes."""
@pytest.mark.asyncio
async def test_asyncio_marker_with_implicit_loop_fixture(self):
"""
Test the "asyncio" marker works on a method in
a class-based test with implicit loop fixture.
"""
ret = await async_coro()
assert ret == "ok"
class TestEventLoopStartedBeforeFixtures:
@pytest.fixture
async def loop(self):
return asyncio.get_event_loop()
@staticmethod
def foo():
return 1
@pytest.mark.asyncio
async def test_no_event_loop(self, loop):
assert await loop.run_in_executor(None, self.foo) == 1
@pytest.mark.asyncio
async def test_event_loop_after_fixture(self, loop):
assert await loop.run_in_executor(None, self.foo) == 1
@pytest.mark.asyncio
async def test_event_loop_before_fixture(self, loop):
assert await loop.run_in_executor(None, self.foo) == 1
def test_invalid_asyncio_mode(pytester):
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
result = pytester.runpytest("-o", "asyncio_mode=True")
result.stderr.no_fnmatch_line("INTERNALERROR> *")
result.stderr.fnmatch_lines(
"ERROR: 'True' is not a valid asyncio_mode. Valid modes: auto, strict."
)