Skip to content

Instantly share code, notes, and snippets.

@raimusyndrome
Created October 7, 2015 08:47
Show Gist options
  • Save raimusyndrome/8b714755f58d136dcf15 to your computer and use it in GitHub Desktop.
Save raimusyndrome/8b714755f58d136dcf15 to your computer and use it in GitHub Desktop.
pythonでMockを使用するテストの雛形。
#
# -*- coding: utf-8 -*-
import unittest
import mock
import contextlib as ctxlib
class TestPySample(unittest.TestCase):
def test_single_mock_test(self):
"""
少数のモックを適用する場合
"""
# モックの定義
with mock.patch('mypkg.myclass.method1', return_value=True):
# 以下テスト本体
pass
return
def test_many_mock_test(self):
"""
多数のモックを適用する場合
"""
with ctxlib.ExitStack() as stack:
# モックの定義
stack.enter_context(mock.patch('mypkg.myclass.method1', return_value=None))
stack.enter_context(mock.patch('mypkg.myclass.method3', side_effect=Exception))
stack.enter_context(mock.patch('mypkg.mywriter', return_value=mock.Mock()))
# 以下テスト本体
return
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment