| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
from textwrap import dedent |
| 4 |
|
| 5 |
import pytest |
| 6 |
from pytest import Pytester, version_tuple as pytest_version |
| 7 |
|
| 8 |
|
| 9 |
def test_strict_mode_cmdline(pytester: Pytester): |
| 10 |
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function") |
| 11 |
pytester.makepyfile(dedent("""\ |
| 12 |
import asyncio |
| 13 |
import pytest |
| 14 |
|
| 15 |
pytest_plugins = 'pytest_asyncio' |
| 16 |
|
| 17 |
@pytest.mark.asyncio |
| 18 |
async def test_a(): |
| 19 |
await asyncio.sleep(0) |
| 20 |
""")) |
| 21 |
result = pytester.runpytest("--asyncio-mode=strict") |
| 22 |
result.assert_outcomes(passed=1) |
| 23 |
|
| 24 |
|
| 25 |
def test_strict_mode_cfg(pytester: Pytester): |
| 26 |
pytester.makeini(dedent("""\ |
| 27 |
[pytest] |
| 28 |
asyncio_default_fixture_loop_scope = function |
| 29 |
asyncio_mode = strict |
| 30 |
""")) |
| 31 |
pytester.makepyfile(dedent("""\ |
| 32 |
import asyncio |
| 33 |
import pytest |
| 34 |
|
| 35 |
pytest_plugins = 'pytest_asyncio' |
| 36 |
|
| 37 |
@pytest.mark.asyncio |
| 38 |
async def test_a(): |
| 39 |
await asyncio.sleep(0) |
| 40 |
""")) |
| 41 |
result = pytester.runpytest() |
| 42 |
result.assert_outcomes(passed=1) |
| 43 |
|
| 44 |
|
| 45 |
def test_strict_mode_method_fixture(pytester: Pytester): |
| 46 |
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function") |
| 47 |
pytester.makepyfile(dedent("""\ |
| 48 |
import asyncio |
| 49 |
import pytest |
| 50 |
import pytest_asyncio |
| 51 |
|
| 52 |
pytest_plugins = 'pytest_asyncio' |
| 53 |
|
| 54 |
class TestA: |
| 55 |
|
| 56 |
@pytest_asyncio.fixture |
| 57 |
async def fixture_a(self): |
| 58 |
await asyncio.sleep(0) |
| 59 |
return 1 |
| 60 |
|
| 61 |
@pytest.mark.asyncio |
| 62 |
async def test_a(self, fixture_a): |
| 63 |
await asyncio.sleep(0) |
| 64 |
assert fixture_a == 1 |
| 65 |
""")) |
| 66 |
result = pytester.runpytest("--asyncio-mode=auto") |
| 67 |
result.assert_outcomes(passed=1) |
| 68 |
|
| 69 |
|
| 70 |
def test_strict_mode_ignores_unmarked_coroutine(pytester: Pytester): |
| 71 |
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function") |
| 72 |
pytester.makepyfile(dedent("""\ |
| 73 |
import pytest |
| 74 |
|
| 75 |
async def test_anything(): |
| 76 |
pass |
| 77 |
""")) |
| 78 |
result = pytester.runpytest("--asyncio-mode=strict", "-W default", "--assert=plain") |
| 79 |
if pytest_version >= (8, 4, 0): |
| 80 |
result.assert_outcomes(failed=1, skipped=0, warnings=0) |
| 81 |
else: |
| 82 |
result.assert_outcomes(skipped=1, warnings=1) |
| 83 |
result.stdout.fnmatch_lines(["*async def functions are not natively supported*"]) |
| 84 |
|
| 85 |
|
| 86 |
@pytest.mark.skipif( |
| 87 |
pytest_version >= (9, 1, 0), |
| 88 |
reason="pytest >=9.1 converts unhandled async fixtures to errors", |
| 89 |
) |
| 90 |
def test_strict_mode_ignores_unmarked_fixture(pytester: Pytester): |
| 91 |
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function") |
| 92 |
pytester.makepyfile(dedent("""\ |
| 93 |
import pytest |
| 94 |
|
| 95 |
# Not using pytest_asyncio.fixture |
| 96 |
@pytest.fixture() |
| 97 |
async def any_fixture(): |
| 98 |
raise RuntimeError() |
| 99 |
|
| 100 |
async def test_anything(any_fixture): |
| 101 |
pass |
| 102 |
""")) |
| 103 |
result = pytester.runpytest("--asyncio-mode=strict", "-W default", "--assert=plain") |
| 104 |
|
| 105 |
if pytest_version >= (8, 4, 0): |
| 106 |
result.assert_outcomes(failed=1, skipped=0, warnings=2) |
| 107 |
else: |
| 108 |
result.assert_outcomes(skipped=1, warnings=2) |
| 109 |
result.stdout.fnmatch_lines( |
| 110 |
[ |
| 111 |
"*async def functions are not natively supported*", |
| 112 |
"*coroutine 'any_fixture' was never awaited*", |
| 113 |
], |
| 114 |
) |
| 115 |
|
| 116 |
|
| 117 |
@pytest.mark.skipif( |
| 118 |
pytest_version >= (9, 1, 0), |
| 119 |
reason="pytest >=9.1 converts unhandled async fixtures to errors", |
| 120 |
) |
| 121 |
def test_strict_mode_marked_test_unmarked_fixture_warning(pytester: Pytester): |
| 122 |
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function") |
| 123 |
pytester.makepyfile(dedent("""\ |
| 124 |
import pytest |
| 125 |
|
| 126 |
# Not using pytest_asyncio.fixture |
| 127 |
@pytest.fixture() |
| 128 |
async def any_fixture(): |
| 129 |
pass |
| 130 |
|
| 131 |
@pytest.mark.asyncio |
| 132 |
async def test_anything(any_fixture): |
| 133 |
# suppress unawaited coroutine warning |
| 134 |
try: |
| 135 |
any_fixture.send(None) |
| 136 |
except StopIteration: |
| 137 |
pass |
| 138 |
""")) |
| 139 |
result = pytester.runpytest("--asyncio-mode=strict", "-W default", "--assert=plain") |
| 140 |
if pytest_version >= (8, 4, 0): |
| 141 |
result.assert_outcomes(passed=1, failed=0, skipped=0, warnings=2) |
| 142 |
else: |
| 143 |
result.assert_outcomes(passed=1, failed=0, skipped=0, warnings=1) |
| 144 |
result.stdout.fnmatch_lines( |
| 145 |
[ |
| 146 |
"*warnings summary*", |
| 147 |
( |
| 148 |
"test_strict_mode_marked_test_unmarked_fixture_warning.py::" |
| 149 |
"test_anything" |
| 150 |
), |
| 151 |
( |
| 152 |
"*/pytest_asyncio/plugin.py:*: PytestDeprecationWarning: " |
| 153 |
"asyncio test 'test_anything' requested async " |
| 154 |
"@pytest.fixture 'any_fixture' in strict mode. " |
| 155 |
"You might want to use @pytest_asyncio.fixture or switch to " |
| 156 |
"auto mode. " |
| 157 |
"This will become an error in future versions of pytest-asyncio." |
| 158 |
), |
| 159 |
], |
| 160 |
) |
| 161 |
|
| 162 |
|
| 163 |
# autouse is not handled in any special way currently |
| 164 |
@pytest.mark.skipif( |
| 165 |
pytest_version >= (9, 1, 0), |
| 166 |
reason="pytest >=9.1 converts unhandled async fixtures to errors", |
| 167 |
) |
| 168 |
def test_strict_mode_marked_test_unmarked_autouse_fixture_warning(pytester: Pytester): |
| 169 |
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function") |
| 170 |
pytester.makepyfile(dedent("""\ |
| 171 |
import pytest |
| 172 |
|
| 173 |
# Not using pytest_asyncio.fixture |
| 174 |
@pytest.fixture(autouse=True) |
| 175 |
async def any_fixture(): |
| 176 |
pass |
| 177 |
|
| 178 |
@pytest.mark.asyncio |
| 179 |
async def test_anything(any_fixture): |
| 180 |
# suppress unawaited coroutine warning |
| 181 |
try: |
| 182 |
any_fixture.send(None) |
| 183 |
except StopIteration: |
| 184 |
pass |
| 185 |
""")) |
| 186 |
result = pytester.runpytest("--asyncio-mode=strict", "-W default", "--assert=plain") |
| 187 |
if pytest_version >= (8, 4, 0): |
| 188 |
result.assert_outcomes(passed=1, warnings=2) |
| 189 |
else: |
| 190 |
result.assert_outcomes(passed=1, warnings=1) |
| 191 |
result.stdout.fnmatch_lines( |
| 192 |
[ |
| 193 |
"*warnings summary*", |
| 194 |
( |
| 195 |
"test_strict_mode_marked_test_unmarked_autouse_fixture_warning.py::" |
| 196 |
"test_anything" |
| 197 |
), |
| 198 |
( |
| 199 |
"*/pytest_asyncio/plugin.py:*: PytestDeprecationWarning: " |
| 200 |
"*asyncio test 'test_anything' requested async " |
| 201 |
"@pytest.fixture 'any_fixture' in strict mode. " |
| 202 |
"You might want to use @pytest_asyncio.fixture or switch to " |
| 203 |
"auto mode. " |
| 204 |
"This will become an error in future versions of pytest-asyncio." |
| 205 |
), |
| 206 |
], |
| 207 |
) |