Skip to content

Instantly share code, notes, and snippets.

@tomoh1r
Created April 14, 2015 13:58
Show Gist options
  • Save tomoh1r/dd871a9e2154dbfedfdf to your computer and use it in GitHub Desktop.
Save tomoh1r/dd871a9e2154dbfedfdf to your computer and use it in GitHub Desktop.
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