Skip to content

Commit bedb100

Browse files
committed
test: Add concurrent fixture teardown test for Hypothesis
1 parent 4fbe1e0 commit bedb100

File tree

1 file changed

+43
-1
lines changed

1 file changed

+43
-1
lines changed

tests/hypothesis/test_base.py

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@
55

66
from __future__ import annotations
77

8+
import asyncio
89
from textwrap import dedent
910

1011
import pytest
11-
from hypothesis import given, strategies as st
12+
from hypothesis import HealthCheck, given, settings, strategies as st
1213
from pytest import Pytester
1314

1415

@@ -90,3 +91,44 @@ def test_hypothesis(request, n: int):
9091
)
9192
result = pytester.runpytest("--asyncio-mode=auto")
9293
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

Comments
 (0)