Skip to content

Instantly share code, notes, and snippets.

@metashock
Last active January 20, 2016 22:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save metashock/9456f66917376f0a38df to your computer and use it in GitHub Desktop.
Save metashock/9456f66917376f0a38df to your computer and use it in GitHub Desktop.
How to mock an external API with pytest_mock
class Api():
def persons(self):
return ['hek2mgl', 'lgm2keh']
class App(Api):
def hello_messages(self):
msg = []
for p in self.persons():
msg += ["Hello {}".format(p)]
return msg
def test_app(mocker):
mocker.patch('{}.Api.persons'.format(__name__),
return_value=['a','b'])
a = App()
assert a.hello_messages() == ([
"Hello a",
"Hello b"
])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment