Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
with 文を mock する
# -*- coding: utf-8 -*-
import mock
import contextlib
@contextlib.contextmanager
def hoge():
yield u'ハリルホジッチ'
@contextlib.contextmanager
def hoge_patched():
yield u'パッチ着たハリルホジッチ'
if __name__ == '__main__':
with hoge() as x:
print(x)
# => ハリルホジッチ
with hoge_patched() as x:
print(x)
# => パッチ着たハリルホジッチ
with mock.patch('__main__.hoge',
side_effect=hoge_patched):
with hoge() as x:
print(x)
# => パッチ着たハリルホジッチ
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment