Fix mock patchings detection when both mock and unittest.mock are present
This commit is contained in:
parent
e7bcc854d9
commit
b6166dccb4
|
@ -79,10 +79,11 @@ def num_mock_patch_args(function):
|
||||||
patchings = getattr(function, "patchings", None)
|
patchings = getattr(function, "patchings", None)
|
||||||
if not patchings:
|
if not patchings:
|
||||||
return 0
|
return 0
|
||||||
mock = sys.modules.get("mock", sys.modules.get("unittest.mock", None))
|
mock_modules = [sys.modules.get("mock"), sys.modules.get("unittest.mock")]
|
||||||
if mock is not None:
|
if any(mock_modules):
|
||||||
|
sentinels = [m.DEFAULT for m in mock_modules if m is not None]
|
||||||
return len([p for p in patchings
|
return len([p for p in patchings
|
||||||
if not p.attribute_name and p.new is mock.DEFAULT])
|
if not p.attribute_name and p.new in sentinels])
|
||||||
return len(patchings)
|
return len(patchings)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
Detect arguments injected by ``unittest.mock.patch`` decorator correctly when pypi ``mock.patch`` is installed and imported.
|
|
@ -147,6 +147,28 @@ class TestMockDecoration(object):
|
||||||
reprec = testdir.inline_run()
|
reprec = testdir.inline_run()
|
||||||
reprec.assertoutcome(passed=1)
|
reprec.assertoutcome(passed=1)
|
||||||
|
|
||||||
|
def test_unittest_mock_and_pypi_mock(self, testdir):
|
||||||
|
pytest.importorskip("unittest.mock")
|
||||||
|
pytest.importorskip("mock", "1.0.1")
|
||||||
|
testdir.makepyfile("""
|
||||||
|
import mock
|
||||||
|
import unittest.mock
|
||||||
|
class TestBoth(object):
|
||||||
|
@unittest.mock.patch("os.path.abspath")
|
||||||
|
def test_hello(self, abspath):
|
||||||
|
import os
|
||||||
|
os.path.abspath("hello")
|
||||||
|
abspath.assert_any_call("hello")
|
||||||
|
|
||||||
|
@mock.patch("os.path.abspath")
|
||||||
|
def test_hello_mock(self, abspath):
|
||||||
|
import os
|
||||||
|
os.path.abspath("hello")
|
||||||
|
abspath.assert_any_call("hello")
|
||||||
|
""")
|
||||||
|
reprec = testdir.inline_run()
|
||||||
|
reprec.assertoutcome(passed=2)
|
||||||
|
|
||||||
def test_mock(self, testdir):
|
def test_mock(self, testdir):
|
||||||
pytest.importorskip("mock", "1.0.1")
|
pytest.importorskip("mock", "1.0.1")
|
||||||
testdir.makepyfile("""
|
testdir.makepyfile("""
|
||||||
|
|
Loading…
Reference in New Issue