|
| 1 | +from textwrap import dedent |
| 2 | + |
| 3 | +import pytest |
| 4 | +from pytest import Pytester |
| 5 | + |
| 6 | + |
| 7 | +def test_returns_false_for_sync_item(pytester: Pytester): |
| 8 | + pytester.makepyfile( |
| 9 | + dedent( |
| 10 | + """\ |
| 11 | + import pytest |
| 12 | + import pytest_asyncio |
| 13 | +
|
| 14 | + def test_sync(): |
| 15 | + pass |
| 16 | +
|
| 17 | + def pytest_collection_modifyitems(items): |
| 18 | + async_tests = [ |
| 19 | + item |
| 20 | + for item in items |
| 21 | + if pytest_asyncio.is_async_test(item) |
| 22 | + ] |
| 23 | + assert len(async_tests) == 0 |
| 24 | + """ |
| 25 | + ) |
| 26 | + ) |
| 27 | + result = pytester.runpytest("--asyncio-mode=strict") |
| 28 | + result.assert_outcomes(passed=1) |
| 29 | + |
| 30 | + |
| 31 | +def test_returns_true_for_marked_coroutine_item_in_strict_mode(pytester: Pytester): |
| 32 | + pytester.makepyfile( |
| 33 | + dedent( |
| 34 | + """\ |
| 35 | + import pytest |
| 36 | + import pytest_asyncio |
| 37 | +
|
| 38 | + @pytest.mark.asyncio |
| 39 | + async def test_coro(): |
| 40 | + pass |
| 41 | +
|
| 42 | + def pytest_collection_modifyitems(items): |
| 43 | + async_tests = [ |
| 44 | + item |
| 45 | + for item in items |
| 46 | + if pytest_asyncio.is_async_test(item) |
| 47 | + ] |
| 48 | + assert len(async_tests) == 1 |
| 49 | + """ |
| 50 | + ) |
| 51 | + ) |
| 52 | + result = pytester.runpytest("--asyncio-mode=strict") |
| 53 | + result.assert_outcomes(passed=1) |
| 54 | + |
| 55 | + |
| 56 | +def test_returns_false_for_unmarked_coroutine_item_in_strict_mode(pytester: Pytester): |
| 57 | + pytester.makepyfile( |
| 58 | + dedent( |
| 59 | + """\ |
| 60 | + import pytest |
| 61 | + import pytest_asyncio |
| 62 | +
|
| 63 | + async def test_coro(): |
| 64 | + pass |
| 65 | +
|
| 66 | + def pytest_collection_modifyitems(items): |
| 67 | + async_tests = [ |
| 68 | + item |
| 69 | + for item in items |
| 70 | + if pytest_asyncio.is_async_test(item) |
| 71 | + ] |
| 72 | + assert len(async_tests) == 0 |
| 73 | + """ |
| 74 | + ) |
| 75 | + ) |
| 76 | + result = pytester.runpytest("--asyncio-mode=strict") |
| 77 | + if pytest.version_tuple < (7, 2): |
| 78 | + # Probably related to https://github.com/pytest-dev/pytest/pull/10012 |
| 79 | + result.assert_outcomes(failed=1) |
| 80 | + else: |
| 81 | + result.assert_outcomes(skipped=1) |
| 82 | + |
| 83 | + |
| 84 | +def test_returns_true_for_unmarked_coroutine_item_in_auto_mode(pytester: Pytester): |
| 85 | + pytester.makepyfile( |
| 86 | + dedent( |
| 87 | + """\ |
| 88 | + import pytest |
| 89 | + import pytest_asyncio |
| 90 | +
|
| 91 | + async def test_coro(): |
| 92 | + pass |
| 93 | +
|
| 94 | + def pytest_collection_modifyitems(items): |
| 95 | + async_tests = [ |
| 96 | + item |
| 97 | + for item in items |
| 98 | + if pytest_asyncio.is_async_test(item) |
| 99 | + ] |
| 100 | + assert len(async_tests) == 1 |
| 101 | + """ |
| 102 | + ) |
| 103 | + ) |
| 104 | + result = pytester.runpytest("--asyncio-mode=auto") |
| 105 | + result.assert_outcomes(passed=1) |
0 commit comments