Skip to content

Commit e66d237

Browse files
authored
Merge pull request #4047 from Zac-HD/various-ci-improvements
Test-only improvements
2 parents 8977126 + 3315be6 commit e66d237

File tree

9 files changed

+354
-321
lines changed

9 files changed

+354
-321
lines changed

hypothesis-python/setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def local_file(name):
6060
"pytest": ["pytest>=4.6"],
6161
"dpcontracts": ["dpcontracts>=0.4"],
6262
"redis": ["redis>=3.0.0"],
63-
"crosshair": ["hypothesis-crosshair>=0.0.7", "crosshair-tool>=0.0.59"],
63+
"crosshair": ["hypothesis-crosshair>=0.0.7", "crosshair-tool>=0.0.60"],
6464
# zoneinfo is an odd one: every dependency is conditional, because they're
6565
# only necessary on old versions of Python or Windows systems or emscripten.
6666
"zoneinfo": [

hypothesis-python/tests/array_api/test_arrays.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,8 @@ def test_generate_arrays_from_zero_dimensions(xp, xps):
133133
def test_generate_arrays_from_zero_sided_shapes(xp, xps, data):
134134
"""Generate arrays from shapes with at least one 0-sized dimension."""
135135
shape = data.draw(xps.array_shapes(min_side=0).filter(lambda s: 0 in s))
136-
assert_all_examples(xps.arrays(xp.int8, shape), lambda x: x.shape == shape)
136+
arr = data.draw(xps.arrays(xp.int8, shape))
137+
assert arr.shape == shape
137138

138139

139140
def test_generate_arrays_from_unsigned_ints(xp, xps):

hypothesis-python/tests/common/debug.py

+29-8
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@
88
# v. 2.0. If a copy of the MPL was not distributed with this file, You can
99
# obtain one at https://mozilla.org/MPL/2.0/.
1010

11+
from unittest import SkipTest
12+
1113
from hypothesis import HealthCheck, Phase, Verbosity, given, settings as Settings
14+
from hypothesis.control import _current_build_context
1215
from hypothesis.errors import Found, NoSuchExample, Unsatisfiable
1316
from hypothesis.internal.reflection import get_pretty_function_description
1417

@@ -26,6 +29,11 @@ def minimal(definition, condition=lambda x: True, settings=None, timeout_after=1
2629
runtime = None
2730
result = None
2831

32+
if (
33+
context := _current_build_context.value
34+
) and context.data.provider.avoid_realization:
35+
raise SkipTest("`minimal()` helper not supported under symbolic execution")
36+
2937
def wrapped_condition(x):
3038
nonlocal runtime
3139
if timeout_after is not None:
@@ -71,6 +79,13 @@ def inner(x):
7179

7280

7381
def find_any(definition, condition=lambda _: True, settings=None):
82+
# If nested within an existing @given
83+
if context := _current_build_context.value:
84+
while True:
85+
if condition(s := context.data.draw(definition)):
86+
return s
87+
88+
# If top-level
7489
settings = settings or Settings.default
7590
return minimal(
7691
definition,
@@ -83,8 +98,7 @@ def find_any(definition, condition=lambda _: True, settings=None):
8398

8499
def assert_no_examples(strategy, condition=lambda _: True):
85100
try:
86-
result = find_any(strategy, condition)
87-
raise AssertionError(f"Expected no results but found {result!r}")
101+
assert_all_examples(strategy, lambda val: not condition(val))
88102
except (Unsatisfiable, NoSuchExample):
89103
pass
90104

@@ -95,14 +109,21 @@ def assert_all_examples(strategy, predicate, settings=None):
95109
:param strategy: Hypothesis strategy to check
96110
:param predicate: (callable) Predicate that takes example and returns bool
97111
"""
112+
if context := _current_build_context.value:
113+
for _ in range(20):
114+
s = context.data.draw(strategy)
115+
msg = f"Found {s!r} using strategy {strategy} which does not match"
116+
assert predicate(s), msg
117+
118+
else:
98119

99-
@given(strategy)
100-
@Settings(parent=settings, database=None)
101-
def assert_examples(s):
102-
msg = f"Found {s!r} using strategy {strategy} which does not match"
103-
assert predicate(s), msg
120+
@given(strategy)
121+
@Settings(parent=settings, database=None)
122+
def assert_examples(s):
123+
msg = f"Found {s!r} using strategy {strategy} which does not match"
124+
assert predicate(s), msg
104125

105-
assert_examples()
126+
assert_examples()
106127

107128

108129
def assert_simple_property(strategy, predicate, settings=None):

hypothesis-python/tests/common/setup.py

+13-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import os
1212
from warnings import filterwarnings
1313

14-
from hypothesis import Phase, Verbosity, settings
14+
from hypothesis import HealthCheck, Phase, Verbosity, settings
1515
from hypothesis._settings import not_set
1616
from hypothesis.internal.conjecture.data import AVAILABLE_PROVIDERS
1717
from hypothesis.internal.coverage import IN_COVERAGE_TESTS
@@ -61,7 +61,17 @@ def run():
6161

6262
settings.register_profile("debug", settings(verbosity=Verbosity.debug))
6363

64-
for backend in set(AVAILABLE_PROVIDERS) - {"hypothesis"}:
65-
settings.register_profile(backend, backend=backend) # e.g. "crosshair"
64+
if "crosshair" in AVAILABLE_PROVIDERS:
65+
settings.register_profile(
66+
"crosshair",
67+
backend="crosshair",
68+
max_examples=20,
69+
deadline=None,
70+
suppress_health_check=list(HealthCheck),
71+
report_multiple_bugs=False,
72+
)
73+
74+
for backend in set(AVAILABLE_PROVIDERS) - {"hypothesis", "crosshair"}:
75+
settings.register_profile(backend, backend=backend)
6676

6777
settings.load_profile(os.getenv("HYPOTHESIS_PROFILE", "default"))

hypothesis-python/tests/cover/test_datetimes.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
import pytest
1414

15-
from hypothesis import given, settings
15+
from hypothesis import HealthCheck, given, settings
1616
from hypothesis.strategies import dates, datetimes, timedeltas, times
1717

1818
from tests.common.debug import assert_simple_property, find_any, minimal
@@ -49,6 +49,7 @@ def test_max_value_is_respected():
4949
assert minimal(timedeltas(max_value=dt.timedelta(days=-10))).days == -10
5050

5151

52+
@settings(suppress_health_check=list(HealthCheck))
5253
@given(timedeltas())
5354
def test_single_timedelta(val):
5455
assert_simple_property(timedeltas(val, val), lambda v: v is val)

0 commit comments

Comments
 (0)