Skip to content

Commit ef8ec01

Browse files
committed
Fix issue where fixtures would lose the decorated functionality
Fix #3774
1 parent a76cc8f commit ef8ec01

File tree

5 files changed

+67
-3
lines changed

5 files changed

+67
-3
lines changed

src/_pytest/compat.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,13 @@ def get_real_func(obj):
234234
"""
235235
start_obj = obj
236236
for i in range(100):
237+
# __pytest_wrapped__ is set by @pytest.fixture when wrapping the fixture function
238+
# to trigger a warning if it gets called directly instead of by pytest: we don't
239+
# want to unwrap further than this otherwise we lose useful wrappings like @mock.patch (#3774)
240+
new_obj = getattr(obj, "__pytest_wrapped__", None)
241+
if new_obj is not None:
242+
obj = new_obj
243+
break
237244
new_obj = getattr(obj, "__wrapped__", None)
238245
if new_obj is None:
239246
break

src/_pytest/fixtures.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -954,9 +954,6 @@ def _ensure_immutable_ids(ids):
954954
def wrap_function_to_warning_if_called_directly(function, fixture_marker):
955955
"""Wrap the given fixture function so we can issue warnings about it being called directly, instead of
956956
used as an argument in a test function.
957-
958-
The warning is emitted only in Python 3, because I didn't find a reliable way to make the wrapper function
959-
keep the original signature, and we probably will drop Python 2 in Pytest 4 anyway.
960957
"""
961958
is_yield_function = is_generator(function)
962959
msg = FIXTURE_FUNCTION_CALL.format(name=fixture_marker.name or function.__name__)
@@ -982,6 +979,10 @@ def result(*args, **kwargs):
982979
if six.PY2:
983980
result.__wrapped__ = function
984981

982+
# keep reference to the original function in our own custom attribute so we don't unwrap
983+
# further than this point and lose useful wrappings like @mock.patch (#3774)
984+
result.__pytest_wrapped__ = function
985+
985986
return result
986987

987988

testing/acceptance_test.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1044,3 +1044,10 @@ def test2():
10441044
)
10451045
result = testdir.runpytest_subprocess()
10461046
result.stdout.fnmatch_lines(["*1 failed, 1 passed in*"])
1047+
1048+
1049+
def test_fixture_mock_integration(testdir):
1050+
"""Test that decorators applied to fixture are left working (#3774)"""
1051+
p = testdir.copy_example("acceptance/fixture_mock_integration.py")
1052+
result = testdir.runpytest(p)
1053+
result.stdout.fnmatch_lines("*1 passed*")
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
"""Reproduces issue #3774"""
2+
3+
import mock
4+
5+
import pytest
6+
7+
config = {"mykey": "ORIGINAL"}
8+
9+
10+
@pytest.fixture(scope="function")
11+
@mock.patch.dict(config, {"mykey": "MOCKED"})
12+
def my_fixture():
13+
return config["mykey"]
14+
15+
16+
def test_foobar(my_fixture):
17+
assert my_fixture == "MOCKED"

testing/test_compat.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
from __future__ import absolute_import, division, print_function
22
import sys
3+
from functools import wraps
4+
5+
import six
36

47
import pytest
58
from _pytest.compat import is_generator, get_real_func, safe_getattr
@@ -26,6 +29,8 @@ def __repr__(self):
2629
return "<Evil left={left}>".format(left=self.left)
2730

2831
def __getattr__(self, attr):
32+
if attr == "__pytest_wrapped__":
33+
raise AttributeError
2934
if not self.left:
3035
raise RuntimeError("its over")
3136
self.left -= 1
@@ -38,6 +43,33 @@ def __getattr__(self, attr):
3843
print(res)
3944

4045

46+
def test_get_real_func():
47+
"""Check that get_real_func correctly unwraps decorators until reaching the real function"""
48+
49+
def decorator(f):
50+
@wraps(f)
51+
def inner():
52+
pass
53+
54+
if six.PY2:
55+
inner.__wrapped__ = f
56+
return inner
57+
58+
def func():
59+
pass
60+
61+
wrapped_func = decorator(decorator(func))
62+
assert get_real_func(wrapped_func) is func
63+
64+
wrapped_func2 = decorator(decorator(wrapped_func))
65+
assert get_real_func(wrapped_func2) is func
66+
67+
# special case for __pytest_wrapped__ attribute: used to obtain the function up until the point
68+
# a function was wrapped by pytest itself
69+
wrapped_func2.__pytest_wrapped__ = wrapped_func
70+
assert get_real_func(wrapped_func2) is wrapped_func
71+
72+
4173
@pytest.mark.skipif(
4274
sys.version_info < (3, 4), reason="asyncio available in Python 3.4+"
4375
)

0 commit comments

Comments
 (0)