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