Merge pull request #3780 from nicoddemus/mock-integration-fix
Fix issue where fixtures would lose the decorated functionality
This commit is contained in:
@@ -1044,3 +1044,10 @@ def test_frame_leak_on_failing_test(testdir):
|
||||
)
|
||||
result = testdir.runpytest_subprocess()
|
||||
result.stdout.fnmatch_lines(["*1 failed, 1 passed in*"])
|
||||
|
||||
|
||||
def test_fixture_mock_integration(testdir):
|
||||
"""Test that decorators applied to fixture are left working (#3774)"""
|
||||
p = testdir.copy_example("acceptance/fixture_mock_integration.py")
|
||||
result = testdir.runpytest(p)
|
||||
result.stdout.fnmatch_lines("*1 passed*")
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
"""Reproduces issue #3774"""
|
||||
|
||||
import mock
|
||||
|
||||
import pytest
|
||||
|
||||
config = {"mykey": "ORIGINAL"}
|
||||
|
||||
|
||||
@pytest.fixture(scope="function")
|
||||
@mock.patch.dict(config, {"mykey": "MOCKED"})
|
||||
def my_fixture():
|
||||
return config["mykey"]
|
||||
|
||||
|
||||
def test_foobar(my_fixture):
|
||||
assert my_fixture == "MOCKED"
|
||||
@@ -1,8 +1,11 @@
|
||||
from __future__ import absolute_import, division, print_function
|
||||
import sys
|
||||
from functools import wraps
|
||||
|
||||
import six
|
||||
|
||||
import pytest
|
||||
from _pytest.compat import is_generator, get_real_func, safe_getattr
|
||||
from _pytest.compat import is_generator, get_real_func, safe_getattr, _PytestWrapper
|
||||
from _pytest.outcomes import OutcomeException
|
||||
|
||||
|
||||
@@ -38,6 +41,33 @@ def test_real_func_loop_limit():
|
||||
print(res)
|
||||
|
||||
|
||||
def test_get_real_func():
|
||||
"""Check that get_real_func correctly unwraps decorators until reaching the real function"""
|
||||
|
||||
def decorator(f):
|
||||
@wraps(f)
|
||||
def inner():
|
||||
pass
|
||||
|
||||
if six.PY2:
|
||||
inner.__wrapped__ = f
|
||||
return inner
|
||||
|
||||
def func():
|
||||
pass
|
||||
|
||||
wrapped_func = decorator(decorator(func))
|
||||
assert get_real_func(wrapped_func) is func
|
||||
|
||||
wrapped_func2 = decorator(decorator(wrapped_func))
|
||||
assert get_real_func(wrapped_func2) is func
|
||||
|
||||
# special case for __pytest_wrapped__ attribute: used to obtain the function up until the point
|
||||
# a function was wrapped by pytest itself
|
||||
wrapped_func2.__pytest_wrapped__ = _PytestWrapper(wrapped_func)
|
||||
assert get_real_func(wrapped_func2) is wrapped_func
|
||||
|
||||
|
||||
@pytest.mark.skipif(
|
||||
sys.version_info < (3, 4), reason="asyncio available in Python 3.4+"
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user