I am trying to create some tests for a Python program. I am using Python version 2.7.12 so I had to install mock
using sudo pip install mock
.
According to the documentation and several other sites, I should be able to use the following code to use patch:
import mock # Also could use from mock import patch
import unittest
class TestCases(unittest.TestCase):
@mock.patch('MyClass.ImportedClass') # If using from mock import patch should just be @patch
def test_patches(self, mock_ImportedClass):
# Test code...
However, the above code throws the following error:
AttributeError: 'module' object has no attribute 'patch'
After some experimenting in the terminal, it seems quite a few things don't work. For example
>>> import mock
>>> mock.MagicMock
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'MagicMock'
>>> mock.patch
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'patch'
>>> mock.Mock
<class mock.Mock at 0x7fdff56b0600>
Interestingly, I can access something called patch
using mock.Mock()
, but I don't know if this works in the same way and cannot find anything about it in the documentation.
My experience with Python is fairly limited so I'm not sure what might be wrong. I'm fairly certain the right package was installed. Any help on how I can get this working?