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

8 views
Skip to first unread message

John Yeukhon Wong

unread,
Apr 10, 2012, 3:52:48 AM4/10/12
to django...@googlegroups.com, testing-...@lists.idyll.org
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!
Reply all
Reply to author
Forward
0 new messages