|
5 | 5 |
|
6 | 6 | from __future__ import annotations
|
7 | 7 |
|
| 8 | +import asyncio |
8 | 9 | from textwrap import dedent
|
9 | 10 |
|
10 | 11 | import pytest
|
11 |
| -from hypothesis import given, strategies as st |
| 12 | +from hypothesis import HealthCheck, given, settings, strategies as st |
12 | 13 | from pytest import Pytester
|
13 | 14 |
|
14 | 15 |
|
@@ -90,3 +91,44 @@ def test_hypothesis(request, n: int):
|
90 | 91 | )
|
91 | 92 | result = pytester.runpytest("--asyncio-mode=auto")
|
92 | 93 | result.assert_outcomes(passed=1)
|
| 94 | + |
| 95 | + |
| 96 | +def test_hypothesis_concurrent_fixture_teardown(pytester: Pytester): |
| 97 | + """ |
| 98 | + Tests that Hypothesis-driven tests with concurrent tasks and async fixtures |
| 99 | + with teardown logic work correctly. |
| 100 | + """ |
| 101 | + pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function") |
| 102 | + pytester.makepyfile( |
| 103 | + dedent( |
| 104 | + """\ |
| 105 | + import asyncio |
| 106 | + import pytest |
| 107 | + from hypothesis import given, strategies as st, settings, HealthCheck |
| 108 | +
|
| 109 | + teardown_complete = False |
| 110 | +
|
| 111 | + @pytest.fixture |
| 112 | + async def concurrent_fixture(): |
| 113 | + global teardown_complete |
| 114 | + teardown_complete = False |
| 115 | + yield |
| 116 | + await asyncio.sleep(0.01) |
| 117 | + teardown_complete = True |
| 118 | +
|
| 119 | + async def background_task(): |
| 120 | + await asyncio.sleep(0.01) |
| 121 | +
|
| 122 | + @settings(suppress_health_check=[HealthCheck.function_scoped_fixture]) |
| 123 | + @given(st.integers()) |
| 124 | + @pytest.mark.asyncio |
| 125 | + async def test_concurrent_teardown(concurrent_fixture, n): |
| 126 | + assert isinstance(n, int) |
| 127 | + task = asyncio.create_task(background_task()) |
| 128 | + await task |
| 129 | + assert not teardown_complete |
| 130 | + """ |
| 131 | + ) |
| 132 | + ) |
| 133 | + result = pytester.runpytest("--asyncio-mode=strict") |
| 134 | + result.assert_outcomes(passed=1) |
0 commit comments