1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
|
# This file is part of Hypothesis, which may be found at
# https://github.com/HypothesisWorks/hypothesis/
#
# Copyright the Hypothesis Authors.
# Individual contributors are listed in AUTHORS.rst and the git log.
#
# This Source Code Form is subject to the terms of the Mozilla Public License,
# v. 2.0. If a copy of the MPL was not distributed with this file, You can
# obtain one at https://mozilla.org/MPL/2.0/.
import re
import pytest
from hypothesis import HealthCheck, given, reject, settings, strategies as st
from hypothesis.errors import InvalidArgument, Unsatisfiable
def test_contains_the_test_function_name_in_the_exception_string():
look_for_one = settings(max_examples=1, suppress_health_check=list(HealthCheck))
@given(st.integers())
@look_for_one
def this_has_a_totally_unique_name(x):
reject()
with pytest.raises(
Unsatisfiable, match=re.escape(this_has_a_totally_unique_name.__name__)
):
this_has_a_totally_unique_name()
class Foo:
@given(st.integers())
@look_for_one
def this_has_a_unique_name_and_lives_on_a_class(self, x):
reject()
with pytest.raises(
Unsatisfiable,
match=re.escape(Foo.this_has_a_unique_name_and_lives_on_a_class.__name__),
):
Foo().this_has_a_unique_name_and_lives_on_a_class()
def test_signature_mismatch_error_message():
# Regression test for issue #1978
@settings(max_examples=2)
@given(x=st.integers())
def bad_test():
pass
with pytest.raises(
InvalidArgument,
match=r"bad_test\(\) got an unexpected keyword argument 'x', "
r"from `x=integers\(\)` in @given",
):
bad_test()
@given(data=st.data(), keys=st.lists(st.integers(), unique=True))
def test_fixed_dict_preserves_iteration_order(data, keys):
d = data.draw(st.fixed_dictionaries({k: st.none() for k in keys}))
assert all(a == b for a, b in zip(keys, d)), f"keys={keys}, d.keys()={d.keys()}"
|