Created
August 26, 2011 18:10
-
-
Save vpetro/1174019 to your computer and use it in GitHub Desktop.
Return multiple items from a mocked function with Python's mock.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import mock | |
def returnList(items): | |
def func(): | |
for item in items: | |
yield item | |
yield mock.DEFAULT | |
generator = func() | |
def effect(*args, **kwargs): | |
return generator.next() | |
return effect | |
m = mock.Mock() | |
m.side_effect = returnList([1,2,3]) | |
for i in ['a', 'b', 'c']: | |
print i, m() |
another way of doing the same thing
return_values = [1,2,3]
def f():
m2.return_value = return_values[m2.call_count-1]
return return_values[m2.call_count-1]
m2 = mock.Mock()
m2.side_effect = f
for i in ['a', 'b', 'c']:
print i, m2(), m2.return_value
Your main example rewritten to be Python 2 and 3 compatible:
try:
import mock
except ImportError:
import unittest.mock as mock
def return_list(items):
def func():
for item in items:
yield item
yield mock.DEFAULT
generator = func()
def effect(*args, **kwargs):
return next(generator)
return effect
m = mock.Mock()
m.side_effect = return_list([1, 2, 3])
for i in ['a', 'b', 'c']:
print(i, m())
Thanks for this gist, vpetro! Very helpful. You can use iter
to simplify a bit:
try:
import mock
except ImportError:
import unittest.mock as mock
def return_list(items):
def func():
return iter(items)
generator = func()
def effect(*args, **kwargs):
return next(generator)
return effect
m = mock.Mock()
m.side_effect = return_list([1, 2, 3])
for i in ['a', 'b', 'c']:
print(i, m())
Turns out you can also just set a side effect to any iterable, including a list:
m = mock.Mock()
m.side_effect = [1, 2, 3]
for i in ['a', 'b', 'c']:
print(i, m())
http://stackoverflow.com/questions/24897145/python-mock-multiple-return-values
Thanks for your great ideas, vpetro.
By the way, you can always use return_values.pop
instead of lambda: return_values.pop()
.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It is also possible to do the same using the code below: