I'm trying to use mock.patch to mock a couple of functions. I've cut my example down for readability. Here is test.py with my test case:
from unittest import mock
import pytest
@mock.patch("package1.myfunc")
def test_myfunc(mymock):
inst1 = MyClass()
inst1.myfunc()
And here is my source code in mycode.py
import package1
class MyClass:
def__init__(self):
pass
def myfunc(self): #wrapper
package1.myfunc()
Am I doing this correctly? Why am I getting an Attribute error? The reason why I didn't do anything else with "mymock" is because I simply want the function called to do nothing. Do I still need to add a return value to it?
Detailed error message:
exc_info = (<class 'AttributeError'>, AttributeError("<module 'package1' from '/.../site-packages/package1/__init__.py'> does not have the attribute 'myfunc'"), <traceback object at 0x7f2b86392640>)
patching = <unittest.mock._patch object at 0x7f2b86a479d0>
@wraps(func)
def patched(*args, **keywargs):
extra_args = []
entered_patchers = []
exc_info = tuple()
try:
for patching in patched.patchings:
> arg = patching.__enter__()
/.../python3.7/lib/python3.7/unittest/mock.py:1247:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
/.../python3.7/unittest/mock.py:1319: in __enter__
original, local = self.get_original()
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
self = <unittest.mock._patch object at 0x7f2b86a479d0>
def get_original(self):
target = self.getter()
name = self.attribute
original = DEFAULT
local = False
try:
[name] original = target.__dict__
except (AttributeError, KeyError):
original = getattr(target, name, DEFAULT)
else:
local = True
if name in _builtins and isinstance(target, ModuleType):
self.create = True
if not self.create and original is DEFAULT:
raise AttributeError(
> "%s does not have the attribute %r" % (target, name)
)
E AttributeError: <module 'package1' from '....'> does not have the attribute 'myfunc'
/.../python3.7/unittest/mock.py:1293: AttributeError
Aucun commentaire:
Enregistrer un commentaire