@@ -89,16 +89,46 @@ def assert_no_examples(strategy, condition=lambda _: True):
89
89
pass
90
90
91
91
92
- def assert_all_examples (strategy , predicate ):
92
+ def assert_all_examples (strategy , predicate , settings = None ):
93
93
"""Asserts that all examples of the given strategy match the predicate.
94
94
95
95
:param strategy: Hypothesis strategy to check
96
96
:param predicate: (callable) Predicate that takes example and returns bool
97
97
"""
98
98
99
99
@given (strategy )
100
+ @Settings (parent = settings )
100
101
def assert_examples (s ):
101
102
msg = f"Found { s !r} using strategy { strategy } which does not match"
102
103
assert predicate (s ), msg
103
104
104
105
assert_examples ()
106
+
107
+
108
+ def assert_simple_property (strategy , predicate , settings = None ):
109
+ """Like assert_all_examples, intended as a self-documenting shortcut for simple constant
110
+ properties (`is`, `isinstance`, `==`, ...) that can be adequately verified in just a few
111
+ examples.
112
+
113
+ For more thorough checking, use assert_all_examples.
114
+ """
115
+
116
+ assert_all_examples (strategy , predicate , Settings (parent = settings , max_examples = 15 ))
117
+
118
+
119
+ def check_can_generate_examples (strategy , settings = None ):
120
+ """Tries to generate a small number of examples from the strategy, to verify that it can
121
+ do so without raising.
122
+
123
+ Nothing is returned, it only checks that no error is raised.
124
+ """
125
+
126
+ assert_simple_property (
127
+ strategy ,
128
+ lambda _ : True ,
129
+ settings = Settings (
130
+ parent = settings ,
131
+ phases = (Phase .generate ,),
132
+ suppress_health_check = list (HealthCheck ),
133
+ ),
134
+ )
0 commit comments