Skip to content

Commit 24926fd

Browse files
committed
Clean up some backend-dependant tests
1 parent 962802a commit 24926fd

File tree

5 files changed

+35
-31
lines changed

5 files changed

+35
-31
lines changed

hypothesis-python/tests/cover/test_core.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,18 +80,20 @@ def inner(x):
8080

8181

8282
def test_pytest_skip_skips_shrinking():
83-
values = []
83+
seen_large = False
8484

8585
@settings(derandomize=True, max_examples=100)
8686
@given(s.integers())
8787
def inner(x):
88-
values.append(x)
88+
nonlocal seen_large
8989
if x > 100:
90+
if seen_large:
91+
raise Exception("Should never replay a skipped test!")
92+
seen_large = True
9093
pytest.skip(f"{x=} is too big!")
9194

9295
with pytest.raises(Skipped):
9396
inner()
94-
assert len([x for x in values if x > 100]) == 1
9597

9698

9799
def test_can_find_with_db_eq_none():

hypothesis-python/tests/cover/test_deadline.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def test_slow_with_none_deadline(i):
5353
def test_raises_flaky_if_a_test_becomes_fast_on_rerun():
5454
once = [True]
5555

56-
@settings(deadline=500)
56+
@settings(deadline=500, backend="hypothesis")
5757
@given(st.integers())
5858
def test_flaky_slow(i):
5959
if once[0]:
@@ -82,11 +82,10 @@ def test_keeps_you_well_above_the_deadline():
8282
seen = set()
8383
failed_once = [False]
8484

85-
@settings(deadline=100)
85+
@settings(deadline=100, backend="hypothesis")
8686
@given(st.integers(0, 2000))
8787
def slow(i):
88-
# Make sure our initial failure isn't something that immediately goes
89-
# flaky.
88+
# Make sure our initial failure isn't something that immediately goes flaky.
9089
if not failed_once[0]:
9190
if i * 0.9 <= 100:
9291
return
@@ -107,7 +106,7 @@ def slow(i):
107106
def test_gives_a_deadline_specific_flaky_error_message():
108107
once = [True]
109108

110-
@settings(deadline=100)
109+
@settings(deadline=100, backend="hypothesis")
111110
@given(st.integers())
112111
def slow_once(i):
113112
if once[0]:

hypothesis-python/tests/cover/test_health_checks.py

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
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+
import re
1112
import time
1213

1314
import pytest
14-
from pytest import raises
1515

1616
from hypothesis import HealthCheck, given, settings, strategies as st
17-
from hypothesis.control import assume
17+
from hypothesis.control import assume, current_build_context
1818
from hypothesis.errors import FailedHealthCheck, InvalidArgument
1919
from hypothesis.internal.compat import int_from_bytes
2020
from hypothesis.stateful import (
@@ -28,26 +28,28 @@
2828

2929
from tests.common.utils import no_shrink
3030

31-
HEALTH_CHECK_SETTINGS = settings(max_examples=11, database=None)
31+
HEALTH_CHECK_SETTINGS = settings(
32+
max_examples=11, database=None, suppress_health_check=()
33+
)
3234

3335

3436
def test_slow_generation_fails_a_health_check():
35-
@HEALTH_CHECK_SETTINGS
37+
@settings(HEALTH_CHECK_SETTINGS, deadline=200)
3638
@given(st.integers().map(lambda x: time.sleep(0.2)))
3739
def test(x):
3840
pass
3941

40-
with raises(FailedHealthCheck):
42+
with pytest.raises(FailedHealthCheck):
4143
test()
4244

4345

4446
def test_slow_generation_inline_fails_a_health_check():
45-
@HEALTH_CHECK_SETTINGS
47+
@settings(HEALTH_CHECK_SETTINGS, deadline=200)
4648
@given(st.data())
4749
def test(data):
4850
data.draw(st.integers().map(lambda x: time.sleep(0.2)))
4951

50-
with raises(FailedHealthCheck):
52+
with pytest.raises(FailedHealthCheck):
5153
test()
5254

5355

@@ -75,7 +77,7 @@ def unhealthy_filter(x):
7577
def test1(x):
7678
raise ValueError
7779

78-
with raises(FailedHealthCheck):
80+
with pytest.raises(FailedHealthCheck):
7981
test1()
8082

8183
forbidden = set()
@@ -85,7 +87,7 @@ def test1(x):
8587
def test2(x):
8688
raise ValueError
8789

88-
with raises(ValueError):
90+
with pytest.raises(ValueError):
8991
test2()
9092

9193

@@ -95,9 +97,8 @@ def test_filtering_everything_fails_a_health_check():
9597
def test(x):
9698
pass
9799

98-
with raises(FailedHealthCheck) as e:
100+
with pytest.raises(FailedHealthCheck, match="filter"):
99101
test()
100-
assert "filter" in e.value.args[0]
101102

102103

103104
class fails_regularly(SearchStrategy):
@@ -109,21 +110,21 @@ def do_draw(self, data):
109110

110111
def test_filtering_most_things_fails_a_health_check():
111112
@given(fails_regularly())
112-
@settings(database=None, phases=no_shrink)
113+
@settings(database=None, phases=no_shrink, suppress_health_check=())
113114
def test(x):
114-
pass
115+
if current_build_context().data.provider.avoid_realization:
116+
pytest.skip("symbolic backends can filter efficiently!")
115117

116-
with raises(FailedHealthCheck) as e:
118+
with pytest.raises(FailedHealthCheck, match="filter"):
117119
test()
118-
assert "filter" in e.value.args[0]
119120

120121

121122
def test_returning_non_none_is_forbidden():
122123
@given(st.integers())
123124
def a(x):
124125
return 1
125126

126-
with raises(FailedHealthCheck):
127+
with pytest.raises(FailedHealthCheck):
127128
a()
128129

129130

@@ -153,19 +154,18 @@ def test(self, _):
153154

154155
def test_differing_executors_fails_health_check():
155156
sample_test_runner().test()
156-
with pytest.raises(FailedHealthCheck) as exc:
157+
msg = re.escape(str(HealthCheck.differing_executors))
158+
with pytest.raises(FailedHealthCheck, match=msg):
157159
sample_test_runner().test()
158160

159-
assert str(HealthCheck.differing_executors) in str(exc.value)
160-
161161

162162
def test_it_is_an_error_to_suppress_non_iterables():
163-
with raises(InvalidArgument):
163+
with pytest.raises(InvalidArgument):
164164
settings(suppress_health_check=1)
165165

166166

167167
def test_it_is_an_error_to_suppress_non_healthchecks():
168-
with raises(InvalidArgument):
168+
with pytest.raises(InvalidArgument):
169169
settings(suppress_health_check=[1])
170170

171171

hypothesis-python/tests/cover/test_targeting.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import pytest
1414

1515
from hypothesis import example, given, strategies as st, target
16+
from hypothesis.control import current_build_context
1617
from hypothesis.errors import InvalidArgument
1718

1819

@@ -92,6 +93,8 @@ def test_cannot_target_outside_test():
9293

9394
@given(st.none())
9495
def test_cannot_target_same_label_twice(_):
96+
if current_build_context().data.provider.avoid_realization:
97+
pytest.skip("target() is a noop to avoid realizing arguments")
9598
target(0.0, label="label")
9699
with pytest.raises(InvalidArgument):
97100
target(1.0, label="label")

hypothesis-python/tests/nocover/test_randomization.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
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 pytest import raises
11+
import pytest
1212

1313
from hypothesis import Verbosity, core, find, given, settings, strategies as st
1414

@@ -35,7 +35,7 @@ def test_blah(x, rnd):
3535
def test_nest(y):
3636
assert y < x
3737

38-
with raises(AssertionError):
38+
with pytest.raises(AssertionError):
3939
test_nest()
4040

4141
test_blah()

0 commit comments

Comments
 (0)