Fix issue where fixtures would lose the decorated functionality

Fix #3774
This commit is contained in:
Bruno Oliveira
2018-08-04 15:14:00 -03:00
parent a76cc8f8c4
commit ef8ec01e39
5 changed files with 67 additions and 3 deletions

View File

@@ -234,6 +234,13 @@ def get_real_func(obj):
"""
start_obj = obj
for i in range(100):
# __pytest_wrapped__ is set by @pytest.fixture when wrapping the fixture function
# to trigger a warning if it gets called directly instead of by pytest: we don't
# want to unwrap further than this otherwise we lose useful wrappings like @mock.patch (#3774)
new_obj = getattr(obj, "__pytest_wrapped__", None)
if new_obj is not None:
obj = new_obj
break
new_obj = getattr(obj, "__wrapped__", None)
if new_obj is None:
break

View File

@@ -954,9 +954,6 @@ def _ensure_immutable_ids(ids):
def wrap_function_to_warning_if_called_directly(function, fixture_marker):
"""Wrap the given fixture function so we can issue warnings about it being called directly, instead of
used as an argument in a test function.
The warning is emitted only in Python 3, because I didn't find a reliable way to make the wrapper function
keep the original signature, and we probably will drop Python 2 in Pytest 4 anyway.
"""
is_yield_function = is_generator(function)
msg = FIXTURE_FUNCTION_CALL.format(name=fixture_marker.name or function.__name__)
@@ -982,6 +979,10 @@ def wrap_function_to_warning_if_called_directly(function, fixture_marker):
if six.PY2:
result.__wrapped__ = function
# keep reference to the original function in our own custom attribute so we don't unwrap
# further than this point and lose useful wrappings like @mock.patch (#3774)
result.__pytest_wrapped__ = function
return result