File: test_task_cleanup.py

package info (click to toggle)
python-pytest-asyncio 1.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 616 kB
  • sloc: python: 2,421; makefile: 24; sh: 1
file content (30 lines) | stat: -rw-r--r-- 831 bytes parent folder | download | duplicates (2)
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
from __future__ import annotations

from textwrap import dedent

from pytest import Pytester


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

        @pytest.mark.asyncio
        async def test_create_task():
            async def coroutine():
                try:
                    while True:
                        await asyncio.sleep(0)
                finally:
                    raise RuntimeError("The task should be cancelled at this point.")

            asyncio.create_task(coroutine())
        """
        )
    )
    result = pytester.runpytest("--asyncio-mode=strict")
    result.assert_outcomes(passed=1)