Skip to content

Instantly share code, notes, and snippets.

@aj07mm
Forked from vpetro/gist:1174019
Created October 18, 2018 16:32
Show Gist options
  • Save aj07mm/c746d8ee0329e48acb62a211b955b1d0 to your computer and use it in GitHub Desktop.
Save aj07mm/c746d8ee0329e48acb62a211b955b1d0 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