Thursday, June 20, 2013

MagicMock Returns different values each call

Recent discoveries about mock objects!

I'm trying to create a Python unittest that exercises a class method with a time() call in it.

That is I have this:

class DumObj(object):
def methodOne(self, intime):
while time.time() < intime:
... do stuff ...
so, I refactored this to:

class DumObj(object):
def nowTime(self):
return time.time()
def methodOne(self, intime):
while self.nowTime() < intime:
... do stuff ...
and have to test methodOne. How to get it to go through the loop once (or twice)?

I tried setting the return value to the output of a function:

import unittest
from mock import Mock, MagicMock
class TestDumObj(unittest.TestCase)
def test_methodOne(self):
d = DumObj()
retvals=[1,2,3,4,5]
def mf():
ret = retvals.pop()
return ret
d.nowTime = MagicMock(return_value=mf()) d.methodOne()

Doesn't work. Here's the interactive version:

>>> from mock import MagicMock
>>> retvals = [ 1, 2, 3, 4, 5 ]
>>> def mse(*args, **kwargs):
... ret = retvals.pop()
... return ret
...
>>> mm = MagicMock(return_value=mse())
>>> mm()
5
>>> mm()
5

Damn! I want MagicMock to return different values each time it's called. I want to have an array of return values for MagicMock, and each call it returns the next one. How to do this? Turns out it's using the side_effect param. Continuing from above:

>>> mm = MagicMock(side_effect=mse)
>>> mm()
4
>>> mm()
3
Yay! Solved: MagicMock returns different values each call. Now, tie it into the overall test:

import unittest
from mock import Mock, MagicMock
class TestDumObj(unittest.TestCase)
def test_methodOne(self):
d = DumObj()
retvals=[1,2,3,4,5]
def mse():
ret = retvals.pop()
return ret
d.nowTime = MagicMock(side_effect=mse) d.methodOne()

Works!

No comments: