File: test_parametrized_loop.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 (48 lines) | stat: -rw-r--r-- 1,214 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
from __future__ import annotations

from textwrap import dedent

from pytest import Pytester


def test_event_loop_parametrization(pytester: Pytester):
    pytester.makepyfile(
        dedent(
            """\
            import asyncio

            import pytest
            import pytest_asyncio

            TESTS_COUNT = 0


            def teardown_module():
                # parametrized 2 * 2 times: 2 for 'event_loop' and 2 for 'fix'
                assert TESTS_COUNT == 4


            @pytest.fixture(scope="module", params=[1, 2])
            def event_loop(request):
                request.param
                loop = asyncio.new_event_loop()
                yield loop
                loop.close()


            @pytest_asyncio.fixture(params=["a", "b"])
            async def fix(request):
                await asyncio.sleep(0)
                return request.param


            @pytest.mark.asyncio
            async def test_parametrized_loop(fix):
                await asyncio.sleep(0)
                global TESTS_COUNT
                TESTS_COUNT += 1
            """
        )
    )
    result = pytester.runpytest_subprocess("--asyncio-mode=strict")
    result.assert_outcomes(passed=4)