Last active
January 20, 2016 22:55
-
-
Save metashock/9456f66917376f0a38df to your computer and use it in GitHub Desktop.
How to mock an external API with pytest_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
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