def test_prints_debug_on_no_simplification(): with Settings(verbosity=Verbosity.debug): with capture_out() as o: find(just(u"hi"), bool) v = o.getvalue() print(v) assert u"No simplifiers" in v
def test_seeds_off_random(): s = settings(max_shrinks=0, database=None) r = random.getstate() x = find(st.integers(), lambda x: True, settings=s) random.setstate(r) y = find(st.integers(), lambda x: True, settings=s) assert x == y
def test_keys_and_default_are_not_shared(): find( st.tuples( st.shared(st.integers(), key=1), st.shared(st.integers())), lambda x: x[0] != x[1] )
def test_exclude_characters_of_specific_groups(): st = characters(blacklist_categories=('Lu', 'Nd')) find(st, lambda c: unicodedata.category(c) != 'Lu') find(st, lambda c: unicodedata.category(c) != 'Nd') assert_no_examples(st, lambda c: unicodedata.category(c) in ('Lu', 'Nd'))
def test_different_keys_are_not_shared(): find( st.tuples( st.shared(st.integers(), key=1), st.shared(st.integers(), key=2)), lambda x: x[0] != x[1] )
def test_find_something_rare(): st = characters(whitelist_categories=['Zs'], min_codepoint=12288) find(st, lambda c: unicodedata.category(c) == 'Zs') with pytest.raises(NoSuchExample): find(st, lambda c: unicodedata.category(c) != 'Zs')
def test_does_not_simplify_into_surrogates(): f = find(text(average_size=25.0), lambda x: x >= u'\udfff') assert f == u'\ue000' f = find( text(average_size=25.0), lambda x: len([t for t in x if t >= u'\udfff']) >= 10) assert f == u'\ue000' * 10
def test_raises_when_no_example(): settings = Settings( max_examples=20, min_satisfying_examples=0, ) with pytest.raises(NoSuchExample): find(integers(), lambda x: False, settings=settings)
def test_saves_incremental_steps_in_database(): key = b"a database key" database = InMemoryExampleDatabase() find( st.binary(min_size=10), lambda x: has_a_non_zero_byte(x), settings=settings(database=database), database_key=key ) assert len(all_values(database)) > 1
def test_blacklisted_characters(): bad_chars = u"te02тест49st" st = characters(min_codepoint=ord("0"), max_codepoint=ord("9"), blacklist_characters=bad_chars) assert "1" == find(st, lambda c: True) with pytest.raises(NoSuchExample): find(st, lambda c: c in bad_chars)
def test_times_out(): with pytest.raises(Timeout) as e: find( integers(), lambda x: time.sleep(0.05) or False, settings=Settings(timeout=0.01)) e.value.args[0]
def test_can_hit_timeout_in_find(): def f(x): time.sleep(100) return x >= 100 with pytest.raises(NoSuchExample): with validate_deprecation(): find(integers(), f, settings=timeout_settings)
def test_saves_incremental_steps_in_database(): key = b"a database key" database = SQLiteExampleDatabase(':memory:') find( st.binary(min_size=10), lambda x: any(x), settings=settings(database=database), database_key=key ) assert len(set(database.fetch(key))) > 1
def test_characters_of_specific_groups(): st = characters(whitelist_categories=('Lu', 'Nd')) find(st, lambda c: unicodedata.category(c) == 'Lu') find(st, lambda c: unicodedata.category(c) == 'Nd') assert_no_examples( st, lambda c: unicodedata.category(c) not in ('Lu', 'Nd'))
def test_intervals_shrink_to_center(inter, rnd): lower, center, upper = inter s = interval(lower, upper, center) with settings(database=None, max_shrinks=2000): assert find(s, lambda x: True) == center if lower < center: assert find(s, lambda x: x < center) == center - 1 if center < upper: assert find(s, lambda x: x > center) == center + 1
def test_blacklisted_characters(): bad_chars = u'te02тест49st' st = characters(min_codepoint=ord('0'), max_codepoint=ord('9'), blacklist_characters=bad_chars) assert '1' == find(st, lambda c: True) with pytest.raises(NoSuchExample): find(st, lambda c: c in bad_chars)
def stuff(): try: find( st.binary(min_size=100), check, settings=settings(database=database, max_examples=10), database_key=key ) except NoSuchExample: pass
def stuff(): try: find( st.binary(min_size=50), lambda x: do_we_care and any(x), settings=settings(database=database, max_examples=10), database_key=key ) except NoSuchExample: pass
def test_includes_progress_in_verbose_mode(): with capture_verbosity(Verbosity.verbose) as o: with settings(verbosity=Verbosity.verbose): find(lists(integers()), lambda x: sum(x) >= 1000000) out = o.getvalue() assert out assert u'Shrunk example' in out assert u'Found satisfying example' in out
def test_can_generate_both_zeros_when_in_interval(l, r): interval = st.floats(l, r) find( interval, lambda x: assume(x == 0) and math.copysign(1, x) == 1, settings=settings(max_iterations=20000)) find( interval, lambda x: assume(x == 0) and math.copysign(1, x) == -1, settings=settings(max_iterations=20000))
def foo(): seen = [] def not_first(x): if not seen: seen.append(x) return False return x not in seen find(integers(), not_first, settings=settings(verbosity=Verbosity.verbose))
def test_find_does_not_pollute_state(): with deterministic_PRNG(): find(st.random_module(), lambda r: True) state_a = random.getstate() find(st.random_module(), lambda r: True) state_b = random.getstate() assert state_a != state_b
def stuff(): try: find( st.binary(min_size=100), lambda x: assume(not finicky) and has_a_non_zero_byte(x), settings=settings(database=database, max_shrinks=10), database_key=key ) except Unsatisfiable: pass
def stuff(): try: find( st.binary(min_size=100), lambda x: assume(not finicky) and any(x), settings=settings(database=database, timeout=5), database_key=key ) except Unsatisfiable: pass
def test_can_time_out_in_simplify(): def slow_always_true(x): time.sleep(0.1) return True start = time.time() find(s.lists(s.booleans()), slow_always_true, settings=settings(timeout=0.1, database=None)) finish = time.time() run_time = finish - start assert run_time <= 0.3
def run(): rnd = random.Random(seed) ex = [] def is_first(x): if ex: return x == ex[0] else: ex.append(x) return True find(strategy, lambda x: True, random=rnd)
def test_objects_with_required_and_optional_fields(self): st = json.objects(required_fields={'always': json.booleans()}, optional_fields={'maybe': json.booleans()}) self.assertEqual(hypothesis.find( st, lambda v: v['always'] and 'maybe' not in v), {'always': True}) self.assertEqual(hypothesis.find( st, lambda v: v['always'] and 'maybe' in v), {'always': True, 'maybe': False})
def test_max_shrinks(): seen = set() def tracktrue(s): seen.add(s) return True find( st.binary(min_size=100, max_size=100), tracktrue, settings=settings(max_shrinks=1) ) assert len(seen) == 2
def test_only_raises_if_actually_considered_all(r): examples = set() settings = Settings(min_satisfying_examples=0, max_examples=100) def consider_and_append(x): examples.add(x) return False s = sampled_from(range(100)) with pytest.raises(NoSuchExample) as e: find(s, consider_and_append, settings=settings) assume(len(examples) < 100) assert not isinstance(e.value, DefinitelyNoSuchExample)
def test_stops_after_max_examples_if_satisfying(): tracker = [] def track(x): tracker.append(x) return False max_examples = 100 with pytest.raises(NoSuchExample): find(s.integers(0, 10000), track, settings=settings(max_examples=max_examples)) assert len(tracker) == max_examples
def test_minimal_float_is_zero(): assert find(floats(), lambda x: True) == 0.0
def test_minimize_very_large_float(): t = sys.float_info.max / 2 assert t <= find(floats(), lambda x: x >= t) < float(u'inf')
def test_can_find_integrish(): find(floats(), lambda x: (is_integral(x * (2**32)) and not is_integral(x * 16)))
def test_minimizes_lists_of_negative_ints_up_to_boundary(): result = find(lists(integers()), lambda x: len([t for t in x if t <= -1]) >= 10) assert result == [-1] * 10
def test_find_non_boundary_float(): x = find(floats(min_value=1, max_value=9), lambda x: x > 2) assert 2 < x < 3
def test_minimize_dict(): tab = find(fixed_dictionaries({ u'a': booleans(), u'b': booleans() }), lambda x: x[u'a'] or x[u'b']) assert not (tab[u'a'] and tab[u'b'])
def test_minimizes_int_down_to_boundary(boundary): assert find(integers(), lambda x: x >= boundary) == boundary
def test_force_builds_to_infer_strategies_for_default_args(): # By default, leaves args with defaults and minimises to 2+4=6 assert find(st.builds(annotated_func), lambda ex: True) == 6 # Inferring integers() for args makes it minimise to zero assert find(st.builds(annotated_func, b=infer, d=infer), lambda ex: True) == 0
def test_settings_are_default_in_find(): find(s.booleans(), lambda x: settings.default is some_normal_settings, settings=some_normal_settings)
def test_Optional_minimises_to_None(): assert find(from_type(typing.Optional[int]), lambda ex: True) is None
def test_cannot_find_non_strategies(): with pytest.raises(InvalidArgument): find(bool, bool)
def example(self, random=None): # type: (Random) -> Ex """Provide an example of the sort of value that this strategy generates. This is biased to be slightly simpler than is typical for values from this strategy, for clarity purposes. This method shouldn't be taken too seriously. It's here for interactive exploration of the API, not for any sort of real testing. This method is part of the public API. """ context = _current_build_context.value if context is not None: if context.data is not None and context.data.depth > 0: note_deprecation( 'Using example() inside a strategy definition is a bad ' 'idea. It will become an error in a future version of ' "Hypothesis, but it's unlikely that it's doing what you " 'intend even now. Instead consider using ' 'hypothesis.strategies.builds() or ' '@hypothesis.strategies.composite to define your strategy.' ' See ' 'https://hypothesis.readthedocs.io/en/latest/data.html' '#hypothesis.strategies.builds or ' 'https://hypothesis.readthedocs.io/en/latest/data.html' '#composite-strategies for more details.') else: note_deprecation( 'Using example() inside a test function is a bad ' 'idea. It will become an error in a future version of ' "Hypothesis, but it's unlikely that it's doing what you " 'intend even now. Instead consider using ' 'hypothesis.strategies.data() to draw ' 'more examples during testing. See ' 'https://hypothesis.readthedocs.io/en/latest/data.html' '#drawing-interactively-in-tests for more details.') from hypothesis import find, settings, Verbosity # Conjecture will always try the zero example first. This would result # in us producing the same example each time, which is boring, so we # deliberately skip the first example it feeds us. first = [] # type: list def condition(x): if first: return True else: first.append(x) return False try: return find(self, condition, random=random, settings=settings( database=None, verbosity=Verbosity.quiet, phases=tuple(set(Phase) - {Phase.shrink}), )) except (NoSuchExample, Unsatisfiable): # This can happen when a strategy has only one example. e.g. # st.just(x). In that case we wanted the first example after all. if first: return first[0] raise NoExamples(u'Could not find any valid examples in 100 tries')
def test_minimizes_ints_from_down_to_boundary(boundary): assert find(integers(min_value=boundary - 10), lambda x: x >= boundary) == boundary assert find(integers(min_value=boundary), lambda x: True) == boundary
def test_minimize_negative_int(): assert find(integers(), lambda x: x < 0) == -1 assert find(integers(), lambda x: x < -1) == -2
def test_minimizes_int_up_to_boundary(boundary): assert find(integers(), lambda x: x <= -boundary) == -boundary
def test_explicit_allow_nan(): find(floats(allow_nan=True), math.isnan)
def test_positive_negative_int(): assert find(integers(), lambda x: x > 0) == 1 assert find(integers(), lambda x: x > 1) == 2
def foo(): find(lists(integers()), lambda x: sum(x) >= 1000000, settings=settings(verbosity=Verbosity.verbose, database=None))
def test_one_sided_contains_infinity(): find(floats(min_value=1.0), math.isinf) find(floats(max_value=1.0), math.isinf)
def test_finds_boundary_floats(): def f(x): print(x) return True assert -1 <= find(floats(min_value=-1, max_value=1), f) <= 1
def test_floats_from_zero_have_reasonable_range(k): n = 10**k assert find(floats(min_value=0.0), lambda x: x >= n) == float(n) assert find(floats(max_value=0.0), lambda x: x <= -n) == float(-n)
def test_find_small_sum_float_list(): xs = find(lists(floats(), min_size=10), lambda x: sum(x) >= 1.0) assert sum(xs) <= 2.0
def test_bounds_are_respected(): assert find(floats(min_value=1.0), lambda x: True) == 1.0 assert find(floats(max_value=-1.0), lambda x: True) == -1.0
def test_find_small_number_in_large_range(): assert find(integers((-2**32), 2**32), lambda x: x >= 101) == 101
def test_minimal_fractional_float(): assert find(floats(), lambda x: x >= 1.5) in (1.5, 2.0)
def test_single_integer_range_is_range(): assert find(integers(1, 1), lambda x: True) == 1
def test_can_find_float_far_from_integral(): find( floats(), lambda x: not (math.isnan(x) or math.isinf(x) or is_integral(x * (2**32))))
def test_minimizes_integer_range_to_boundary(boundary): assert find(integers(boundary, boundary + 100), lambda x: True) == boundary
def test_errors_on_find(): s = st.lists(st.integers(), min_size=5, max_size=2) with pytest.raises(InvalidArgument): find(s, lambda x: True)
def test_can_find_standard_complex_numbers(): find(complex_numbers(), lambda x: x.imag != 0) == 0j find(complex_numbers(), lambda x: x.real != 0) == 1