diff --git a/README.rst b/README.rst index 7c888870..2e6fd79b 100644 --- a/README.rst +++ b/README.rst @@ -262,6 +262,7 @@ Changelog - Fixed a bug that closes the default event loop if the loop doesn't exist `#257 `_ - Added type annotations. `#198 `_ - Show asyncio mode in pytest report headers. `#266 `_ +- Relax ``asyncio_mode`` type definition; it allows to support pytest 6.1+. `#262 `_ 0.17.0 (22-01-13) ~~~~~~~~~~~~~~~~~~~ diff --git a/pytest_asyncio/plugin.py b/pytest_asyncio/plugin.py index 8635b624..7a7c8dd5 100644 --- a/pytest_asyncio/plugin.py +++ b/pytest_asyncio/plugin.py @@ -90,7 +90,6 @@ def pytest_addoption(parser: Parser, pluginmanager: PytestPluginManager) -> None parser.addini( "asyncio_mode", help="default value for --asyncio-mode", - type="string", default="legacy", ) @@ -461,7 +460,7 @@ def pytest_runtest_setup(item: pytest.Item) -> None: @pytest.fixture -def event_loop(request: pytest.FixtureRequest) -> Iterator[asyncio.AbstractEventLoop]: +def event_loop(request: "pytest.FixtureRequest") -> Iterator[asyncio.AbstractEventLoop]: """Create an instance of the default event loop for each test case.""" loop = asyncio.get_event_loop_policy().new_event_loop() yield loop diff --git a/setup.cfg b/setup.cfg index 1b3f97be..f05526bd 100644 --- a/setup.cfg +++ b/setup.cfg @@ -38,7 +38,7 @@ setup_requires = setuptools_scm >= 6.2 install_requires = - pytest >= 5.4.0 + pytest >= 6.1.0 typing-extensions >= 4.0 [options.extras_require] diff --git a/tests/conftest.py b/tests/conftest.py index 03fd33f2..4aa8c89a 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -2,6 +2,8 @@ import pytest +pytest_plugins = "pytester" + @pytest.fixture def dependent_fixture(event_loop): diff --git a/tests/hypothesis/test_base.py b/tests/hypothesis/test_base.py index fbee75bf..e6da3427 100644 --- a/tests/hypothesis/test_base.py +++ b/tests/hypothesis/test_base.py @@ -43,8 +43,8 @@ async def test_can_use_fixture_provided_event_loop(event_loop, n): await semaphore.acquire() -def test_async_auto_marked(pytester): - pytester.makepyfile( +def test_async_auto_marked(testdir): + testdir.makepyfile( dedent( """\ import asyncio @@ -60,13 +60,13 @@ async def test_hypothesis(n: int): """ ) ) - result = pytester.runpytest("--asyncio-mode=auto") + result = testdir.runpytest("--asyncio-mode=auto") result.assert_outcomes(passed=1) -def test_sync_not_auto_marked(pytester): +def test_sync_not_auto_marked(testdir): """Assert that synchronous Hypothesis functions are not marked with asyncio""" - pytester.makepyfile( + testdir.makepyfile( dedent( """\ import asyncio @@ -84,5 +84,5 @@ def test_hypothesis(request, n: int): """ ) ) - result = pytester.runpytest("--asyncio-mode=auto") + result = testdir.runpytest("--asyncio-mode=auto") result.assert_outcomes(passed=1) diff --git a/tests/modes/test_auto_mode.py b/tests/modes/test_auto_mode.py index 980b0b04..157ffded 100644 --- a/tests/modes/test_auto_mode.py +++ b/tests/modes/test_auto_mode.py @@ -1,10 +1,8 @@ from textwrap import dedent -pytest_plugins = "pytester" - -def test_auto_mode_cmdline(pytester): - pytester.makepyfile( +def test_auto_mode_cmdline(testdir): + testdir.makepyfile( dedent( """\ import asyncio @@ -17,12 +15,12 @@ async def test_a(): """ ) ) - result = pytester.runpytest("--asyncio-mode=auto") + result = testdir.runpytest("--asyncio-mode=auto") result.assert_outcomes(passed=1) -def test_auto_mode_cfg(pytester): - pytester.makepyfile( +def test_auto_mode_cfg(testdir): + testdir.makepyfile( dedent( """\ import asyncio @@ -35,13 +33,13 @@ async def test_a(): """ ) ) - pytester.makefile(".ini", pytest="[pytest]\nasyncio_mode = auto\n") - result = pytester.runpytest() + testdir.makefile(".ini", pytest="[pytest]\nasyncio_mode = auto\n") + result = testdir.runpytest() result.assert_outcomes(passed=1) -def test_auto_mode_async_fixture(pytester): - pytester.makepyfile( +def test_auto_mode_async_fixture(testdir): + testdir.makepyfile( dedent( """\ import asyncio @@ -60,12 +58,12 @@ async def test_a(fixture_a): """ ) ) - result = pytester.runpytest("--asyncio-mode=auto") + result = testdir.runpytest("--asyncio-mode=auto") result.assert_outcomes(passed=1) -def test_auto_mode_method_fixture(pytester): - pytester.makepyfile( +def test_auto_mode_method_fixture(testdir): + testdir.makepyfile( dedent( """\ import asyncio @@ -87,5 +85,5 @@ async def test_a(self, fixture_a): """ ) ) - result = pytester.runpytest("--asyncio-mode=auto") + result = testdir.runpytest("--asyncio-mode=auto") result.assert_outcomes(passed=1) diff --git a/tests/modes/test_legacy_mode.py b/tests/modes/test_legacy_mode.py index df9c2cb6..12d4afe1 100644 --- a/tests/modes/test_legacy_mode.py +++ b/tests/modes/test_legacy_mode.py @@ -1,8 +1,5 @@ from textwrap import dedent -pytest_plugins = "pytester" - - LEGACY_MODE = ( "The 'asyncio_mode' default value will change to 'strict' in future, " "please explicitly use 'asyncio_mode=strict' or 'asyncio_mode=auto' " @@ -18,8 +15,8 @@ ).format(name="*") -def test_warning_for_legacy_mode_cmdline(pytester): - pytester.makepyfile( +def test_warning_for_legacy_mode_cmdline(testdir): + testdir.makepyfile( dedent( """\ import asyncio @@ -33,13 +30,13 @@ async def test_a(): """ ) ) - result = pytester.runpytest("--asyncio-mode=legacy") + result = testdir.runpytest("--asyncio-mode=legacy") assert result.parseoutcomes()["warnings"] == 1 result.stdout.fnmatch_lines(["*" + LEGACY_MODE + "*"]) -def test_warning_for_legacy_mode_cfg(pytester): - pytester.makepyfile( +def test_warning_for_legacy_mode_cfg(testdir): + testdir.makepyfile( dedent( """\ import asyncio @@ -53,15 +50,15 @@ async def test_a(): """ ) ) - pytester.makefile(".ini", pytest="[pytest]\nasyncio_mode = legacy\n") - result = pytester.runpytest() + testdir.makefile(".ini", pytest="[pytest]\nasyncio_mode = legacy\n") + result = testdir.runpytest() assert result.parseoutcomes()["warnings"] == 1 result.stdout.fnmatch_lines(["*" + LEGACY_MODE + "*"]) result.stdout.no_fnmatch_line("*" + LEGACY_ASYNCIO_FIXTURE + "*") -def test_warning_for_legacy_fixture(pytester): - pytester.makepyfile( +def test_warning_for_legacy_fixture(testdir): + testdir.makepyfile( dedent( """\ import asyncio @@ -81,13 +78,13 @@ async def test_a(fixture_a): """ ) ) - result = pytester.runpytest("--asyncio-mode=legacy") + result = testdir.runpytest("--asyncio-mode=legacy") assert result.parseoutcomes()["warnings"] == 2 result.stdout.fnmatch_lines(["*" + LEGACY_ASYNCIO_FIXTURE + "*"]) -def test_warning_for_legacy_method_fixture(pytester): - pytester.makepyfile( +def test_warning_for_legacy_method_fixture(testdir): + testdir.makepyfile( dedent( """\ import asyncio @@ -110,6 +107,6 @@ async def test_a(self, fixture_a): """ ) ) - result = pytester.runpytest("--asyncio-mode=legacy") + result = testdir.runpytest("--asyncio-mode=legacy") assert result.parseoutcomes()["warnings"] == 2 result.stdout.fnmatch_lines(["*" + LEGACY_ASYNCIO_FIXTURE + "*"]) diff --git a/tests/modes/test_strict_mode.py b/tests/modes/test_strict_mode.py index 7b574012..3b6487c7 100644 --- a/tests/modes/test_strict_mode.py +++ b/tests/modes/test_strict_mode.py @@ -1,10 +1,8 @@ from textwrap import dedent -pytest_plugins = "pytester" - -def test_strict_mode_cmdline(pytester): - pytester.makepyfile( +def test_strict_mode_cmdline(testdir): + testdir.makepyfile( dedent( """\ import asyncio @@ -18,12 +16,12 @@ async def test_a(): """ ) ) - result = pytester.runpytest("--asyncio-mode=strict") + result = testdir.runpytest("--asyncio-mode=strict") result.assert_outcomes(passed=1) -def test_strict_mode_cfg(pytester): - pytester.makepyfile( +def test_strict_mode_cfg(testdir): + testdir.makepyfile( dedent( """\ import asyncio @@ -37,13 +35,13 @@ async def test_a(): """ ) ) - pytester.makefile(".ini", pytest="[pytest]\nasyncio_mode = strict\n") - result = pytester.runpytest() + testdir.makefile(".ini", pytest="[pytest]\nasyncio_mode = strict\n") + result = testdir.runpytest() result.assert_outcomes(passed=1) -def test_strict_mode_method_fixture(pytester): - pytester.makepyfile( +def test_strict_mode_method_fixture(testdir): + testdir.makepyfile( dedent( """\ import asyncio @@ -66,5 +64,5 @@ async def test_a(self, fixture_a): """ ) ) - result = pytester.runpytest("--asyncio-mode=auto") + result = testdir.runpytest("--asyncio-mode=auto") result.assert_outcomes(passed=1) diff --git a/tests/test_flaky_integration.py b/tests/test_flaky_integration.py index 2e551aad..54c9d2ea 100644 --- a/tests/test_flaky_integration.py +++ b/tests/test_flaky_integration.py @@ -1,14 +1,10 @@ """Tests for the Flaky integration, which retries failed tests. """ - - from textwrap import dedent -pytest_plugins = "pytester" - -def test_auto_mode_cmdline(pytester): - pytester.makepyfile( +def test_auto_mode_cmdline(testdir): + testdir.makepyfile( dedent( """\ import asyncio @@ -29,7 +25,7 @@ async def test_asyncio_flaky_thing_that_fails_then_succeeds(): ) # runpytest_subprocess() is required to don't pollute the output # with flaky restart information - result = pytester.runpytest_subprocess() + result = testdir.runpytest_subprocess("--asyncio-mode=strict") result.assert_outcomes(passed=1) result.stdout.fnmatch_lines( [ diff --git a/tox.ini b/tox.ini index a5477455..574ea6d4 100644 --- a/tox.ini +++ b/tox.ini @@ -1,12 +1,22 @@ [tox] minversion = 3.14.0 -envlist = py37, py38, py39, py310, lint, version-info +envlist = py37, py38, py39, py310, lint, version-info, pytest-min skip_missing_interpreters = true passenv = CI [testenv] extras = testing +deps = + pytest == 6.2.5 # required for Python 3.10, not bad for others +commands = make test +allowlist_externals = + make + +[testenv:pytest-min] +extras = testing +deps = + pytest == 6.1.0 commands = make test allowlist_externals = make @@ -38,7 +48,7 @@ commands = [gh-actions] python = - 3.7: py37 + 3.7: py37, pytest-min 3.8: py38 3.9: py39, lint 3.10: py310