Skip to content

Commit 36f277d

Browse files
authored
Relax asyncio_mode type definition; it allows to support pytest 6.1+ (#264)
1 parent 087e0b6 commit 36f277d

File tree

10 files changed

+62
-61
lines changed

10 files changed

+62
-61
lines changed

README.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,7 @@ Changelog
262262
- Fixed a bug that closes the default event loop if the loop doesn't exist `#257 <https://github.com/pytest-dev/pytest-asyncio/issues/257>`_
263263
- Added type annotations. `#198 <https://github.com/pytest-dev/pytest-asyncio/issues/198>`_
264264
- Show asyncio mode in pytest report headers. `#266 <https://github.com/pytest-dev/pytest-asyncio/issues/266>`_
265+
- Relax ``asyncio_mode`` type definition; it allows to support pytest 6.1+. `#262 <https://github.com/pytest-dev/pytest-asyncio/issues/262>`_
265266

266267
0.17.0 (22-01-13)
267268
~~~~~~~~~~~~~~~~~~~

pytest_asyncio/plugin.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@ def pytest_addoption(parser: Parser, pluginmanager: PytestPluginManager) -> None
9090
parser.addini(
9191
"asyncio_mode",
9292
help="default value for --asyncio-mode",
93-
type="string",
9493
default="legacy",
9594
)
9695

@@ -461,7 +460,7 @@ def pytest_runtest_setup(item: pytest.Item) -> None:
461460

462461

463462
@pytest.fixture
464-
def event_loop(request: pytest.FixtureRequest) -> Iterator[asyncio.AbstractEventLoop]:
463+
def event_loop(request: "pytest.FixtureRequest") -> Iterator[asyncio.AbstractEventLoop]:
465464
"""Create an instance of the default event loop for each test case."""
466465
loop = asyncio.get_event_loop_policy().new_event_loop()
467466
yield loop

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ setup_requires =
3838
setuptools_scm >= 6.2
3939

4040
install_requires =
41-
pytest >= 5.4.0
41+
pytest >= 6.1.0
4242
typing-extensions >= 4.0
4343

4444
[options.extras_require]

tests/conftest.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import pytest
44

5+
pytest_plugins = "pytester"
6+
57

68
@pytest.fixture
79
def dependent_fixture(event_loop):

tests/hypothesis/test_base.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ async def test_can_use_fixture_provided_event_loop(event_loop, n):
4343
await semaphore.acquire()
4444

4545

46-
def test_async_auto_marked(pytester):
47-
pytester.makepyfile(
46+
def test_async_auto_marked(testdir):
47+
testdir.makepyfile(
4848
dedent(
4949
"""\
5050
import asyncio
@@ -60,13 +60,13 @@ async def test_hypothesis(n: int):
6060
"""
6161
)
6262
)
63-
result = pytester.runpytest("--asyncio-mode=auto")
63+
result = testdir.runpytest("--asyncio-mode=auto")
6464
result.assert_outcomes(passed=1)
6565

6666

67-
def test_sync_not_auto_marked(pytester):
67+
def test_sync_not_auto_marked(testdir):
6868
"""Assert that synchronous Hypothesis functions are not marked with asyncio"""
69-
pytester.makepyfile(
69+
testdir.makepyfile(
7070
dedent(
7171
"""\
7272
import asyncio
@@ -84,5 +84,5 @@ def test_hypothesis(request, n: int):
8484
"""
8585
)
8686
)
87-
result = pytester.runpytest("--asyncio-mode=auto")
87+
result = testdir.runpytest("--asyncio-mode=auto")
8888
result.assert_outcomes(passed=1)

tests/modes/test_auto_mode.py

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
from textwrap import dedent
22

3-
pytest_plugins = "pytester"
43

5-
6-
def test_auto_mode_cmdline(pytester):
7-
pytester.makepyfile(
4+
def test_auto_mode_cmdline(testdir):
5+
testdir.makepyfile(
86
dedent(
97
"""\
108
import asyncio
@@ -17,12 +15,12 @@ async def test_a():
1715
"""
1816
)
1917
)
20-
result = pytester.runpytest("--asyncio-mode=auto")
18+
result = testdir.runpytest("--asyncio-mode=auto")
2119
result.assert_outcomes(passed=1)
2220

2321

24-
def test_auto_mode_cfg(pytester):
25-
pytester.makepyfile(
22+
def test_auto_mode_cfg(testdir):
23+
testdir.makepyfile(
2624
dedent(
2725
"""\
2826
import asyncio
@@ -35,13 +33,13 @@ async def test_a():
3533
"""
3634
)
3735
)
38-
pytester.makefile(".ini", pytest="[pytest]\nasyncio_mode = auto\n")
39-
result = pytester.runpytest()
36+
testdir.makefile(".ini", pytest="[pytest]\nasyncio_mode = auto\n")
37+
result = testdir.runpytest()
4038
result.assert_outcomes(passed=1)
4139

4240

43-
def test_auto_mode_async_fixture(pytester):
44-
pytester.makepyfile(
41+
def test_auto_mode_async_fixture(testdir):
42+
testdir.makepyfile(
4543
dedent(
4644
"""\
4745
import asyncio
@@ -60,12 +58,12 @@ async def test_a(fixture_a):
6058
"""
6159
)
6260
)
63-
result = pytester.runpytest("--asyncio-mode=auto")
61+
result = testdir.runpytest("--asyncio-mode=auto")
6462
result.assert_outcomes(passed=1)
6563

6664

67-
def test_auto_mode_method_fixture(pytester):
68-
pytester.makepyfile(
65+
def test_auto_mode_method_fixture(testdir):
66+
testdir.makepyfile(
6967
dedent(
7068
"""\
7169
import asyncio
@@ -87,5 +85,5 @@ async def test_a(self, fixture_a):
8785
"""
8886
)
8987
)
90-
result = pytester.runpytest("--asyncio-mode=auto")
88+
result = testdir.runpytest("--asyncio-mode=auto")
9189
result.assert_outcomes(passed=1)

tests/modes/test_legacy_mode.py

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
from textwrap import dedent
22

3-
pytest_plugins = "pytester"
4-
5-
63
LEGACY_MODE = (
74
"The 'asyncio_mode' default value will change to 'strict' in future, "
85
"please explicitly use 'asyncio_mode=strict' or 'asyncio_mode=auto' "
@@ -18,8 +15,8 @@
1815
).format(name="*")
1916

2017

21-
def test_warning_for_legacy_mode_cmdline(pytester):
22-
pytester.makepyfile(
18+
def test_warning_for_legacy_mode_cmdline(testdir):
19+
testdir.makepyfile(
2320
dedent(
2421
"""\
2522
import asyncio
@@ -33,13 +30,13 @@ async def test_a():
3330
"""
3431
)
3532
)
36-
result = pytester.runpytest("--asyncio-mode=legacy")
33+
result = testdir.runpytest("--asyncio-mode=legacy")
3734
assert result.parseoutcomes()["warnings"] == 1
3835
result.stdout.fnmatch_lines(["*" + LEGACY_MODE + "*"])
3936

4037

41-
def test_warning_for_legacy_mode_cfg(pytester):
42-
pytester.makepyfile(
38+
def test_warning_for_legacy_mode_cfg(testdir):
39+
testdir.makepyfile(
4340
dedent(
4441
"""\
4542
import asyncio
@@ -53,15 +50,15 @@ async def test_a():
5350
"""
5451
)
5552
)
56-
pytester.makefile(".ini", pytest="[pytest]\nasyncio_mode = legacy\n")
57-
result = pytester.runpytest()
53+
testdir.makefile(".ini", pytest="[pytest]\nasyncio_mode = legacy\n")
54+
result = testdir.runpytest()
5855
assert result.parseoutcomes()["warnings"] == 1
5956
result.stdout.fnmatch_lines(["*" + LEGACY_MODE + "*"])
6057
result.stdout.no_fnmatch_line("*" + LEGACY_ASYNCIO_FIXTURE + "*")
6158

6259

63-
def test_warning_for_legacy_fixture(pytester):
64-
pytester.makepyfile(
60+
def test_warning_for_legacy_fixture(testdir):
61+
testdir.makepyfile(
6562
dedent(
6663
"""\
6764
import asyncio
@@ -81,13 +78,13 @@ async def test_a(fixture_a):
8178
"""
8279
)
8380
)
84-
result = pytester.runpytest("--asyncio-mode=legacy")
81+
result = testdir.runpytest("--asyncio-mode=legacy")
8582
assert result.parseoutcomes()["warnings"] == 2
8683
result.stdout.fnmatch_lines(["*" + LEGACY_ASYNCIO_FIXTURE + "*"])
8784

8885

89-
def test_warning_for_legacy_method_fixture(pytester):
90-
pytester.makepyfile(
86+
def test_warning_for_legacy_method_fixture(testdir):
87+
testdir.makepyfile(
9188
dedent(
9289
"""\
9390
import asyncio
@@ -110,6 +107,6 @@ async def test_a(self, fixture_a):
110107
"""
111108
)
112109
)
113-
result = pytester.runpytest("--asyncio-mode=legacy")
110+
result = testdir.runpytest("--asyncio-mode=legacy")
114111
assert result.parseoutcomes()["warnings"] == 2
115112
result.stdout.fnmatch_lines(["*" + LEGACY_ASYNCIO_FIXTURE + "*"])

tests/modes/test_strict_mode.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
from textwrap import dedent
22

3-
pytest_plugins = "pytester"
43

5-
6-
def test_strict_mode_cmdline(pytester):
7-
pytester.makepyfile(
4+
def test_strict_mode_cmdline(testdir):
5+
testdir.makepyfile(
86
dedent(
97
"""\
108
import asyncio
@@ -18,12 +16,12 @@ async def test_a():
1816
"""
1917
)
2018
)
21-
result = pytester.runpytest("--asyncio-mode=strict")
19+
result = testdir.runpytest("--asyncio-mode=strict")
2220
result.assert_outcomes(passed=1)
2321

2422

25-
def test_strict_mode_cfg(pytester):
26-
pytester.makepyfile(
23+
def test_strict_mode_cfg(testdir):
24+
testdir.makepyfile(
2725
dedent(
2826
"""\
2927
import asyncio
@@ -37,13 +35,13 @@ async def test_a():
3735
"""
3836
)
3937
)
40-
pytester.makefile(".ini", pytest="[pytest]\nasyncio_mode = strict\n")
41-
result = pytester.runpytest()
38+
testdir.makefile(".ini", pytest="[pytest]\nasyncio_mode = strict\n")
39+
result = testdir.runpytest()
4240
result.assert_outcomes(passed=1)
4341

4442

45-
def test_strict_mode_method_fixture(pytester):
46-
pytester.makepyfile(
43+
def test_strict_mode_method_fixture(testdir):
44+
testdir.makepyfile(
4745
dedent(
4846
"""\
4947
import asyncio
@@ -66,5 +64,5 @@ async def test_a(self, fixture_a):
6664
"""
6765
)
6866
)
69-
result = pytester.runpytest("--asyncio-mode=auto")
67+
result = testdir.runpytest("--asyncio-mode=auto")
7068
result.assert_outcomes(passed=1)

tests/test_flaky_integration.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11
"""Tests for the Flaky integration, which retries failed tests.
22
"""
3-
4-
53
from textwrap import dedent
64

7-
pytest_plugins = "pytester"
8-
95

10-
def test_auto_mode_cmdline(pytester):
11-
pytester.makepyfile(
6+
def test_auto_mode_cmdline(testdir):
7+
testdir.makepyfile(
128
dedent(
139
"""\
1410
import asyncio
@@ -29,7 +25,7 @@ async def test_asyncio_flaky_thing_that_fails_then_succeeds():
2925
)
3026
# runpytest_subprocess() is required to don't pollute the output
3127
# with flaky restart information
32-
result = pytester.runpytest_subprocess()
28+
result = testdir.runpytest_subprocess("--asyncio-mode=strict")
3329
result.assert_outcomes(passed=1)
3430
result.stdout.fnmatch_lines(
3531
[

tox.ini

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,22 @@
11
[tox]
22
minversion = 3.14.0
3-
envlist = py37, py38, py39, py310, lint, version-info
3+
envlist = py37, py38, py39, py310, lint, version-info, pytest-min
44
skip_missing_interpreters = true
55
passenv =
66
CI
77

88
[testenv]
99
extras = testing
10+
deps =
11+
pytest == 6.2.5 # required for Python 3.10, not bad for others
12+
commands = make test
13+
allowlist_externals =
14+
make
15+
16+
[testenv:pytest-min]
17+
extras = testing
18+
deps =
19+
pytest == 6.1.0
1020
commands = make test
1121
allowlist_externals =
1222
make
@@ -38,7 +48,7 @@ commands =
3848

3949
[gh-actions]
4050
python =
41-
3.7: py37
51+
3.7: py37, pytest-min
4252
3.8: py38
4353
3.9: py39, lint
4454
3.10: py310

0 commit comments

Comments
 (0)