I am working with Python unit tests and am trying to create a basic test.
In doing so, I have created a ‘Person’ class and am attempting to mock one of its methods. However, when I run the file, python says that ‘Person’ cannot be found… despite this class being in the exact same file as the test itself.
Any ideas as to whats going on?
from unittest.mock import patch, Mock
class Person():
name = None
age = None
profession = None
def __init__(self, name:str, age:int, profession:str):
self.name = name
self.age = age
self.profession = profession
def getProfession(self):
return self.profession
@patch('Person.getProfession')
def simple_test(mock_get):
mockProfession = Mock()
profession = "CEO"
mockProfession.return_value = profession
mock_get.return_value = mockProfession
person = Person("Jim Jimmerson", 44, "TEACHER")
assert person.getProfession() == "CEO"
simple_test()
stacktrace:
in exec_module
exec(co, module.__dict__)
test_call_lambda.py:47: in <module>
simple_test()
C:Python311Libunittestmock.py:1372: in patched
with self.decoration_helper(patched,
C:Python311Libcontextlib.py:137: in __enter__
return next(self.gen)
C:Python311Libunittestmock.py:1354: in decoration_helper
arg = exit_stack.enter_context(patching)
C:Python311Libcontextlib.py:505: in enter_context
result = _enter(cm)
C:Python311Libunittestmock.py:1427: in __enter__
self.target = self.getter()
C:Python311Libpkgutil.py:700: in resolve_name
mod = importlib.import_module(modname)
C:Python311Libimportlib__init__.py:126: in import_module
return _bootstrap._gcd_import(name[level:], package, level)
<frozen importlib._bootstrap>:1204: in _gcd_import
???
<frozen importlib._bootstrap>:1176: in _find_and_load
???
<frozen importlib._bootstrap>:1140: in _find_and_load_unlocked
???
E ModuleNotFoundError: No module named 'Person'