My directory structure looks like this:
myproject/
├── mymodule/
│ ├── foo.py
│ └── model/
│ └── functions.py
│
└── tests/
└── footest.py
my foo.py
file has a local import that looks like this:
from .model.functions import MyClass
I am trying to mock one of the MyClass methods in one of my tests but I can't figure out how to use the @patch
decorator to do it. Normally I do this:
class CheckLambdaHandler(unittest.TestCase):
@patch('requests.post')
def test_post_error(self, mocked_post):
# Test which uses requests.post
But when I try to do something similar with a local import, it breaks:
class CheckLambdaHandler(unittest.TestCase):
@patch('.model.functions.MyClass.function')
def test_post_error(self, mocked_post):
# Test which uses requests.post
Because I guess @patch does a string split on "." which means you cant use local imports?
ValueError: Empty module name
Anyone else know what to do here? Do I need to change my import? In the test? In the actual code?