[Pytest-commit] commit/pytest: hpk42: Merged in ticosax/pytest/mock-unittest-252 (pull request #184)
commits-noreply at bitbucket.org
commits-noreply at bitbucket.org
Mon Jul 28 10:31:35 CEST 2014
1 new commit in pytest:
https://bitbucket.org/hpk42/pytest/commits/7b861c1de21d/
Changeset: 7b861c1de21d
User: hpk42
Date: 2014-07-28 10:31:31
Summary: Merged in ticosax/pytest/mock-unittest-252 (pull request #184)
Injection of fixture doesn't work when decorated with unittest.mock.patch
Affected #: 2 files
diff -r c49801c86150891478719d8ca94505da2cf3f4e8 -r 7b861c1de21df974bf015ee9b254bf266d73d300 _pytest/python.py
--- a/_pytest/python.py
+++ b/_pytest/python.py
@@ -1861,6 +1861,29 @@
return ("<FixtureDef name=%r scope=%r baseid=%r >" %
(self.argname, self.scope, self.baseid))
+def handle_mock_module_patching(function, startindex):
+ """
+ Special treatment when test_function is decorated
+ by mock.patch
+ """
+ for candidate_module_name in ('mock', 'unittest.mock'):
+ # stdlib comes last, because mock might be also installed
+ # as a third party with upgraded version compare to
+ # unittest.mock
+ try:
+ mock = sys.modules[candidate_module_name]
+ except KeyError:
+ pass
+ else:
+ for patching in getattr(function, "patchings", []):
+ if (not patching.attribute_name
+ and patching.new is mock.DEFAULT):
+ startindex += 1
+ break
+ else:
+ startindex += len(getattr(function, "patchings", []))
+ return startindex
+
def getfuncargnames(function, startindex=None):
# XXX merge with main.py's varnames
#assert not inspect.isclass(function)
@@ -1870,13 +1893,7 @@
if startindex is None:
startindex = inspect.ismethod(function) and 1 or 0
if realfunction != function:
- mock = sys.modules.get('mock')
- if mock is not None:
- for patching in getattr(function, "patchings", []):
- if not patching.attribute_name and patching.new is mock.DEFAULT:
- startindex += 1
- else:
- startindex += len(getattr(function, "patchings", []))
+ startindex = handle_mock_module_patching(function, startindex)
function = realfunction
argnames = inspect.getargs(py.code.getrawcode(function))[0]
defaults = getattr(function, 'func_defaults',
diff -r c49801c86150891478719d8ca94505da2cf3f4e8 -r 7b861c1de21df974bf015ee9b254bf266d73d300 testing/python/integration.py
--- a/testing/python/integration.py
+++ b/testing/python/integration.py
@@ -112,6 +112,26 @@
reprec = testdir.inline_run()
reprec.assertoutcome(passed=1)
+ def test_unittest_mock_and_fixture(self, testdir):
+ pytest.importorskip("unittest.mock")
+ testdir.makepyfile("""
+ import os.path
+ import unittest.mock
+ import pytest
+
+ @pytest.fixture
+ def inject_me():
+ pass
+
+ @unittest.mock.patch.object(os.path, "abspath",
+ new=unittest.mock.MagicMock)
+ def test_hello(inject_me):
+ import os
+ os.path.abspath("hello")
+ """)
+ reprec = testdir.inline_run()
+ reprec.assertoutcome(passed=1)
+
def test_mock(self, testdir):
pytest.importorskip("mock", "1.0.1")
testdir.makepyfile("""
Repository URL: https://bitbucket.org/hpk42/pytest/
--
This is a commit notification from bitbucket.org. You are receiving
this because you have the service enabled, addressing the recipient of
this email.
More information about the pytest-commit
mailing list