|
| 1 | +from textwrap import dedent |
| 2 | + |
| 3 | +from pytest import Pytester |
| 4 | + |
| 5 | + |
| 6 | +def test_emit_warning_when_event_loop_is_explicitly_requested_in_coroutine( |
| 7 | + pytester: Pytester, |
| 8 | +): |
| 9 | + pytester.makepyfile( |
| 10 | + dedent( |
| 11 | + """\ |
| 12 | + import pytest |
| 13 | +
|
| 14 | + @pytest.mark.asyncio |
| 15 | + async def test_coroutine_emits_warning(event_loop): |
| 16 | + pass |
| 17 | + """ |
| 18 | + ) |
| 19 | + ) |
| 20 | + result = pytester.runpytest("--asyncio-mode=strict") |
| 21 | + result.assert_outcomes(passed=1, warnings=1) |
| 22 | + result.stdout.fnmatch_lines( |
| 23 | + ['*is asynchronous and explicitly requests the "event_loop" fixture*'] |
| 24 | + ) |
| 25 | + |
| 26 | + |
| 27 | +def test_emit_warning_when_event_loop_is_explicitly_requested_in_coroutine_method( |
| 28 | + pytester: Pytester, |
| 29 | +): |
| 30 | + pytester.makepyfile( |
| 31 | + dedent( |
| 32 | + """\ |
| 33 | + import pytest |
| 34 | +
|
| 35 | + class TestEmitsWarning: |
| 36 | + @pytest.mark.asyncio |
| 37 | + async def test_coroutine_emits_warning(self, event_loop): |
| 38 | + pass |
| 39 | + """ |
| 40 | + ) |
| 41 | + ) |
| 42 | + result = pytester.runpytest("--asyncio-mode=strict") |
| 43 | + result.assert_outcomes(passed=1, warnings=1) |
| 44 | + result.stdout.fnmatch_lines( |
| 45 | + ['*is asynchronous and explicitly requests the "event_loop" fixture*'] |
| 46 | + ) |
| 47 | + |
| 48 | + |
| 49 | +def test_emit_warning_when_event_loop_is_explicitly_requested_in_coroutine_staticmethod( |
| 50 | + pytester: Pytester, |
| 51 | +): |
| 52 | + pytester.makepyfile( |
| 53 | + dedent( |
| 54 | + """\ |
| 55 | + import pytest |
| 56 | +
|
| 57 | + class TestEmitsWarning: |
| 58 | + @staticmethod |
| 59 | + @pytest.mark.asyncio |
| 60 | + async def test_coroutine_emits_warning(event_loop): |
| 61 | + pass |
| 62 | + """ |
| 63 | + ) |
| 64 | + ) |
| 65 | + result = pytester.runpytest("--asyncio-mode=strict") |
| 66 | + result.assert_outcomes(passed=1, warnings=1) |
| 67 | + result.stdout.fnmatch_lines( |
| 68 | + ['*is asynchronous and explicitly requests the "event_loop" fixture*'] |
| 69 | + ) |
| 70 | + |
| 71 | + |
| 72 | +def test_emit_warning_when_event_loop_is_explicitly_requested_in_coroutine_fixture( |
| 73 | + pytester: Pytester, |
| 74 | +): |
| 75 | + pytester.makepyfile( |
| 76 | + dedent( |
| 77 | + """\ |
| 78 | + import pytest |
| 79 | + import pytest_asyncio |
| 80 | +
|
| 81 | + @pytest_asyncio.fixture |
| 82 | + async def emits_warning(event_loop): |
| 83 | + pass |
| 84 | +
|
| 85 | + @pytest.mark.asyncio |
| 86 | + async def test_uses_fixture(emits_warning): |
| 87 | + pass |
| 88 | + """ |
| 89 | + ) |
| 90 | + ) |
| 91 | + result = pytester.runpytest("--asyncio-mode=strict") |
| 92 | + result.assert_outcomes(passed=1, warnings=1) |
| 93 | + result.stdout.fnmatch_lines( |
| 94 | + ['*is asynchronous and explicitly requests the "event_loop" fixture*'] |
| 95 | + ) |
| 96 | + |
| 97 | + |
| 98 | +def test_emit_warning_when_event_loop_is_explicitly_requested_in_async_gen_fixture( |
| 99 | + pytester: Pytester, |
| 100 | +): |
| 101 | + pytester.makepyfile( |
| 102 | + dedent( |
| 103 | + """\ |
| 104 | + import pytest |
| 105 | + import pytest_asyncio |
| 106 | +
|
| 107 | + @pytest_asyncio.fixture |
| 108 | + async def emits_warning(event_loop): |
| 109 | + yield |
| 110 | +
|
| 111 | + @pytest.mark.asyncio |
| 112 | + async def test_uses_fixture(emits_warning): |
| 113 | + pass |
| 114 | + """ |
| 115 | + ) |
| 116 | + ) |
| 117 | + result = pytester.runpytest("--asyncio-mode=strict") |
| 118 | + result.assert_outcomes(passed=1, warnings=1) |
| 119 | + result.stdout.fnmatch_lines( |
| 120 | + ['*is asynchronous and explicitly requests the "event_loop" fixture*'] |
| 121 | + ) |
| 122 | + |
| 123 | + |
| 124 | +def test_does_not_emit_warning_when_event_loop_is_explicitly_requested_in_sync_function( |
| 125 | + pytester: Pytester, |
| 126 | +): |
| 127 | + pytester.makepyfile( |
| 128 | + dedent( |
| 129 | + """\ |
| 130 | + import pytest |
| 131 | +
|
| 132 | + def test_uses_fixture(event_loop): |
| 133 | + pass |
| 134 | + """ |
| 135 | + ) |
| 136 | + ) |
| 137 | + result = pytester.runpytest("--asyncio-mode=strict") |
| 138 | + result.assert_outcomes(passed=1) |
| 139 | + |
| 140 | + |
| 141 | +def test_does_not_emit_warning_when_event_loop_is_explicitly_requested_in_sync_fixture( |
| 142 | + pytester: Pytester, |
| 143 | +): |
| 144 | + pytester.makepyfile( |
| 145 | + dedent( |
| 146 | + """\ |
| 147 | + import pytest |
| 148 | +
|
| 149 | + @pytest.fixture |
| 150 | + def any_fixture(event_loop): |
| 151 | + pass |
| 152 | +
|
| 153 | + def test_uses_fixture(any_fixture): |
| 154 | + pass |
| 155 | + """ |
| 156 | + ) |
| 157 | + ) |
| 158 | + result = pytester.runpytest("--asyncio-mode=strict") |
| 159 | + result.assert_outcomes(passed=1) |
0 commit comments