Asserts that all examples of the given strategy match the predicate. :param strategy: Hypothesis strategy to check :param predicate: (callable) Predicate that takes example and returns bool
(strategy, predicate, settings=None)
| 109 | |
| 110 | |
| 111 | def assert_all_examples(strategy, predicate, settings=None): |
| 112 | """Asserts that all examples of the given strategy match the predicate. |
| 113 | |
| 114 | :param strategy: Hypothesis strategy to check |
| 115 | :param predicate: (callable) Predicate that takes example and returns bool |
| 116 | """ |
| 117 | if context := _current_build_context.value: |
| 118 | with local_settings(Settings(parent=settings)): |
| 119 | for _ in range(20): |
| 120 | s = context.data.draw(strategy) |
| 121 | msg = f"Found {s!r} using strategy {strategy} which does not match" |
| 122 | assert predicate(s), msg |
| 123 | |
| 124 | else: |
| 125 | |
| 126 | @given(strategy) |
| 127 | @Settings(parent=settings, database=None) |
| 128 | def assert_examples(s): |
| 129 | msg = f"Found {s!r} using strategy {strategy} which does not match" |
| 130 | assert predicate(s), msg |
| 131 | |
| 132 | assert_examples() |
| 133 | |
| 134 | |
| 135 | def assert_simple_property(strategy, predicate, settings=None): |