I have a problem about figuring out paths when using mock in Python.
Suppose, that i have the following files
file1.py
def func1():
return 'X'
file2.py
from file1 import func1
class ClassA():
def func_that_uses_func1(self):
x = func1()
How could I patch the usage of func1 on ClassA? I have tried @mock.patch('file2.func1'), but i get an the error AttributeError: <class 'ClassA'> does not have the attribute 'func1'
I think you want to actually do your mock as @mock.patch('file2.func1')
. My example below might help:
from file2 import ClassA
from mock import patch
import unittest
class TestClassA(unittest.TestCase):
def setUp(self):
self.c = ClassA()
@patch('file2.func1')
def test_func(self, m_func1):
self.c.func_that_uses_func1()
self.assertEqual(m_func1.called, 1)
if __name__ == '__main__':
unittest.main()