| 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 |
from functools import wraps |
| 12 |
|
| 13 |
import pytest |
| 14 |
|
| 15 |
from hypothesis import given, strategies as st |
| 16 |
from hypothesis.errors import InvalidArgument |
| 17 |
|
| 18 |
|
| 19 |
def always_passes(*args, **kwargs): |
| 20 |
"""Stand-in for a fixed version of an inner test. |
| 21 |
|
| 22 |
For example, pytest-trio would take the inner test, wrap it in an |
| 23 |
async-to-sync converter, and use the new func (not always_passes). |
| 24 |
""" |
| 25 |
|
| 26 |
|
| 27 |
@given(st.integers()) |
| 28 |
def test_can_replace_inner_test(x): |
| 29 |
raise AssertionError("This should be replaced") |
| 30 |
|
| 31 |
|
| 32 |
test_can_replace_inner_test.hypothesis.inner_test = always_passes |
| 33 |
|
| 34 |
|
| 35 |
def decorator(func): |
| 36 |
"""An example of a common decorator pattern.""" |
| 37 |
|
| 38 |
@wraps(func) |
| 39 |
def inner(*args, **kwargs): |
| 40 |
return func(*args, **kwargs) |
| 41 |
|
| 42 |
return inner |
| 43 |
|
| 44 |
|
| 45 |
@decorator |
| 46 |
@given(st.integers()) |
| 47 |
def test_can_replace_when_decorated(x): |
| 48 |
raise AssertionError("This should be replaced") |
| 49 |
|
| 50 |
|
| 51 |
test_can_replace_when_decorated.hypothesis.inner_test = always_passes |
| 52 |
|
| 53 |
|
| 54 |
@pytest.mark.parametrize("x", [1, 2]) |
| 55 |
@given(y=st.integers()) |
| 56 |
def test_can_replace_when_parametrized(x, y): |
| 57 |
raise AssertionError("This should be replaced") |
| 58 |
|
| 59 |
|
| 60 |
test_can_replace_when_parametrized.hypothesis.inner_test = always_passes |
| 61 |
|
| 62 |
|
| 63 |
def test_can_replace_when_original_is_invalid(): |
| 64 |
# Invalid: @given with too many positional arguments |
| 65 |
@given(st.integers(), st.integers()) |
| 66 |
def invalid_test(x): |
| 67 |
raise AssertionError |
| 68 |
|
| 69 |
invalid_test.hypothesis.inner_test = always_passes |
| 70 |
|
| 71 |
# Even after replacing the inner test, calling the wrapper should still |
| 72 |
# fail. |
| 73 |
with pytest.raises(InvalidArgument, match="Too many positional arguments"): |
| 74 |
invalid_test() |
| 75 |
|
| 76 |
|
| 77 |
def test_inner_is_original_even_when_invalid(): |
| 78 |
def invalid_test(x): |
| 79 |
raise AssertionError |
| 80 |
|
| 81 |
original = invalid_test |
| 82 |
|
| 83 |
# Invalid: @given with no arguments |
| 84 |
invalid_test = given()(invalid_test) |
| 85 |
|
| 86 |
# Verify that the test is actually invalid |
| 87 |
with pytest.raises( |
| 88 |
InvalidArgument, |
| 89 |
match="given must be called with at least one argument", |
| 90 |
): |
| 91 |
invalid_test() |
| 92 |
|
| 93 |
assert invalid_test.hypothesis.inner_test == original |
| 94 |
|
| 95 |
|
| 96 |
def test_invokes_inner_function_with_args_by_name(): |
| 97 |
# Regression test for https://github.com/HypothesisWorks/hypothesis/issues/3245 |
| 98 |
@given(st.integers()) |
| 99 |
def test(x): |
| 100 |
pass |
| 101 |
|
| 102 |
f = test.hypothesis.inner_test |
| 103 |
test.hypothesis.inner_test = wraps(f)(lambda **kw: f(**kw)) |
| 104 |
test() |