12

Can you give some clear examples of uses of the Mock() in Django unittests? I want to understand it more clearly.

Update: I've figured out some things, so I share it below.

asked Sep 26, 2011 at 13:53

2 Answers 2

23

Part 1: Basics

from mock import Mock

Mock object is an object that is a kind of a Dummy for the code that we want not to be executed, but for which we want to know some information (number of calls, call arguments). Also we might want to specify a return value for that code.

Let us define simple function:

def foo(value):
 return value + value

Now we're ready to create a Mock object for it:

mock_foo = Mock(foo, return_value='mock return value')

Now we can check it:

>>> foo(1)
2
>>> mock_foo(1)
'mock return value'

And get some information on calls:

>>> mock_foo.called
True
>>> mock_foo.call_count
1
>>> mock_foo.call_args
((1,), {})

Available attributes of Mock() instance are:

call_args func_code func_name
call_args_list func_defaults method_calls
call_count func_dict side_effect
called func_doc 
func_closure func_globals 

They're quite self-explanatory.

Part 2: @patch decorator

The @patch decorator allows us to easily create mock objects for imported objects (classes or methods). It is very useful while writing unit-tests.

Let us assume that we have following module foo.py:

class Foo(object):
 def foo(value):
 return value + value

Let us write a test for @patch decorator. We are going to patch method foo in class Foo from module foo. Do not forget imports.

from mock import patch
import foo
@patch('foo.Foo.foo')
def test(mock_foo):
 # We assign return value to the mock object
 mock_foo.return_value = 'mock return value'
 f = foo.Foo()
 return f.foo(1)

Now run it:

>>> test()
'mock return value'

Voila! Our method successfully overridden.

Eric Palakovich Carr
23.4k8 gold badges51 silver badges55 bronze badges
answered Oct 1, 2011 at 20:48
Sign up to request clarification or add additional context in comments.

1 Comment

Ah, finally an explanation on mock's and patch's that I can understand!!
1

The Mock site has some really nice API documentation that cover all of this. Note that patch replaces all instances of foo.Foo.foo where ever they are called. It is not the preferred way to mock something but can be the only way -- see open().

answered Dec 5, 2011 at 9:32

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.