I am writing python unittests and need to patch a function from a class not explicitly imported at the call site. I encounter an error unless I add the import (unused) to the file the call is from. I would like to know if there is a way around this unused import.
I have created an example with the same issue:
test_1.py
class Test1():
def test_1_func(self, arg):
print(f"This is test_1_func, called with {arg}")
test_2.py
from test_1 import Test1
class Test2():
def __init__(self):
self.test1 = Test1()
test_3.py
from test_2 import Test2
class Test3():
def __init__(self):
self.test2=Test2()
def test_3_func(self):
print("This is test_3_func")
self.test2.test1.test_1_func("ARGUMENT")
tests.py
import unittest
from mock import patch
from test_3 import Test3
class MyTestCase(unittest.TestCase):
@patch('test_3.Test1.test_1_func')
def test_test_3(self, mock_test_1_func):
Test3().test_3_func()
mock_test_1_func.assert_called_with("ARGUMENT")
When test_test_3
is run, I get the error:
ModuleNotFoundError: No module named 'test_3.Test1'; 'test_3' is not a package
However, when I add Test1
as an import in test_3.py
(as below) the test completes without error.
from test_1 import Test1
from test_2 import Test2
class Test3():
def __init__(self):
self.test2=Test2()
def test_3_func(self):
print("This is test_3_func")
self.test2.test1.test_1_func("ARGUMENT")