Debian Codegraph @

tests/hypothesis/test_base.py

raw
1 """
2 Tests for the Hypothesis integration, which wraps async functions in a
3 sync shim for Hypothesis.
4 """
5
6 from __future__ import annotations
7
8 from textwrap import dedent
9
10 import pytest
11 from hypothesis import given, strategies as st
12 from pytest import Pytester
13
14
15 def test_hypothesis_given_decorator_before_asyncio_mark(pytester: Pytester):
16 pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
17 pytester.makepyfile(dedent("""\
18 import pytest
19 from hypothesis import given, strategies as st
20
21 @given(st.integers())
22 @pytest.mark.asyncio
23 async def test_mark_inner(n):
24 assert isinstance(n, int)
25 """))
26 result = pytester.runpytest("--asyncio-mode=strict", "-W default")
27 result.assert_outcomes(passed=1)
28
29
30 @pytest.mark.asyncio
31 @given(st.integers())
32 async def test_mark_outer(n):
33 assert isinstance(n, int)
34
35
36 @pytest.mark.parametrize("y", [1, 2])
37 @given(x=st.none())
38 @pytest.mark.asyncio
39 async def test_mark_and_parametrize(x, y):
40 assert x is None
41 assert y in (1, 2)
42
43
44 def test_async_auto_marked(pytester: Pytester):
45 pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
46 pytester.makepyfile(dedent("""\
47 import asyncio
48 import pytest
49 from hypothesis import given
50 import hypothesis.strategies as st
51
52 pytest_plugins = 'pytest_asyncio'
53
54 @given(n=st.integers())
55 async def test_hypothesis(n: int):
56 assert isinstance(n, int)
57 """))
58 result = pytester.runpytest("--asyncio-mode=auto")
59 result.assert_outcomes(passed=1)
60
61
62 def test_sync_not_auto_marked(pytester: Pytester):
63 """Assert that synchronous Hypothesis functions are not marked with asyncio"""
64 pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
65 pytester.makepyfile(dedent("""\
66 import asyncio
67 import pytest
68 from hypothesis import given
69 import hypothesis.strategies as st
70
71 pytest_plugins = 'pytest_asyncio'
72
73 @given(n=st.integers())
74 def test_hypothesis(request, n: int):
75 markers = [marker.name for marker in request.node.own_markers]
76 assert "asyncio" not in markers
77 assert isinstance(n, int)
78 """))
79 result = pytester.runpytest("--asyncio-mode=auto")
80 result.assert_outcomes(passed=1)