Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save bossjones/38bf378e08949275f37a6202e895e602 to your computer and use it in GitHub Desktop.
Save bossjones/38bf378e08949275f37a6202e895e602 to your computer and use it in GitHub Desktop.
Return multiple items from a mocked function with Python's mock.
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()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment