I tried to install a mock library for django from http://www.voidspace.org.uk/python/mock/
But when i type in
pip install -U mock
while in virtualenv, and then try to
import mock
from django project's shell i get:
ImportError: No module named mock
What can I do?
When mocking in a test, we are in a certain way making fun of our software or of the function we are testing, simulating the behaviour of a specific external functionality. In Python there is a package in the standard library that helps us apply mocks during our tests.
How do we mock in Python? Mocking in Python is done by using patch to hijack an API function or object creation call. When patch intercepts a call, it returns a MagicMock object by default. By setting properties on the MagicMock object, you can mock the API call to return any value you want or raise an Exception .
If you want to mock an object for the duration of your entire test function, you can use patch() as a function decorator. These functions are now in their own file, separate from their tests. Next, you'll re-create your tests in a file called tests.py .
Mock vs. MagicMock is a subclass of Mock . It contains all magic methods pre-created and ready to use (e.g. __str__ , __len__ , etc.). Therefore, you should use MagicMock when you need magic methods, and Mock if you don't need them.
Check to see if the module is in the path. To do this, in the Python shell:
>>> import sys
>>> print sys.path
If it was installed correctly, you should see the mock
directory in one of the directories printed out.
If you do not find the mock
directory, I'm assuming that pip
is not installing the module in the virtualenv
packages directory.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With