[TIP] Should I always provide a return value when writing my test using mock library?

John Yeukhon Wong gokoproject at gmail.com
Tue Apr 10 00:52:48 PDT 2012


I am Cc this to testing-in-python mailing list. Hope this makes sense.

Suppose I have a function called "render_reverse" which takes two 
arguments: function name, and args list, and it returns "reverse(f, *args)"

If I called render_reverse('happy_birthday', {'args': [username]}), I would 
get this:   /greeting/birthday/username/

I am going to patch the `reverse` function that's local to render_reverse, 
and inside the test, should I always provide a return value like this?

with patch('myproject.myapps.mylibrary.reverse') as mock_reverse:
     mock_f = MagicMock(name='f')
     mock_kwargs = MagicMock(name='kwargs')
     mock_reverse.return_value = ' /greeting/birthday/johnsmith/'
     response = mock_reverse(mock_f, mock_kwargs)

self.assert......


What is the best practice in general? How do I determine whether I want to 
provide a return value or not? In almost any cases, how do I know things go 
well? Sometimes I can't differentiate unittest from integration / system 
test (I want to see other codes ikn the same function execute and throw 
back the right result!!!!)

Thanks! 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.idyll.org/pipermail/testing-in-python/attachments/20120410/9dea4a4f/attachment.htm>


More information about the testing-in-python mailing list