File: test_is_async_test.py

package info (click to toggle)
python-pytest-asyncio 0.25.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 656 kB
  • sloc: python: 3,108; makefile: 26; sh: 1
file content (106 lines) | stat: -rw-r--r-- 3,047 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
from __future__ import annotations

from textwrap import dedent

from pytest import Pytester


def test_returns_false_for_sync_item(pytester: Pytester):
    pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
    pytester.makepyfile(
        dedent(
            """\
            import pytest
            import pytest_asyncio

            def test_sync():
                pass

            def pytest_collection_modifyitems(items):
                async_tests = [
                    item
                    for item in items
                    if pytest_asyncio.is_async_test(item)
                ]
                assert len(async_tests) == 0
            """
        )
    )
    result = pytester.runpytest("--asyncio-mode=strict")
    result.assert_outcomes(passed=1)


def test_returns_true_for_marked_coroutine_item_in_strict_mode(pytester: Pytester):
    pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
    pytester.makepyfile(
        dedent(
            """\
            import pytest
            import pytest_asyncio

            @pytest.mark.asyncio
            async def test_coro():
                pass

            def pytest_collection_modifyitems(items):
                async_tests = [
                    item
                    for item in items
                    if pytest_asyncio.is_async_test(item)
                ]
                assert len(async_tests) == 1
            """
        )
    )
    result = pytester.runpytest("--asyncio-mode=strict")
    result.assert_outcomes(passed=1)


def test_returns_false_for_unmarked_coroutine_item_in_strict_mode(pytester: Pytester):
    pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
    pytester.makepyfile(
        dedent(
            """\
            import pytest
            import pytest_asyncio

            async def test_coro():
                pass

            def pytest_collection_modifyitems(items):
                async_tests = [
                    item
                    for item in items
                    if pytest_asyncio.is_async_test(item)
                ]
                assert len(async_tests) == 0
            """
        )
    )
    result = pytester.runpytest("--asyncio-mode=strict")
    result.assert_outcomes(failed=1)


def test_returns_true_for_unmarked_coroutine_item_in_auto_mode(pytester: Pytester):
    pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
    pytester.makepyfile(
        dedent(
            """\
            import pytest
            import pytest_asyncio

            async def test_coro():
                pass

            def pytest_collection_modifyitems(items):
                async_tests = [
                    item
                    for item in items
                    if pytest_asyncio.is_async_test(item)
                ]
                assert len(async_tests) == 1
            """
        )
    )
    result = pytester.runpytest("--asyncio-mode=auto")
    result.assert_outcomes(passed=1)