Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use mock in django?

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?

like image 483
santino Avatar asked Nov 13 '11 23:11

santino


People also ask

What is mock test in Django?

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 you use mock method in Python?

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 .

How do you mock an object in Python?

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 .

Should I use mock MagicMock?

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.


1 Answers

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.

like image 142
Belmin Fernandez Avatar answered Nov 02 '22 10:11

Belmin Fernandez