Created
May 11, 2013 06:35
-
-
Save drillbits/5559101 to your computer and use it in GitHub Desktop.
デコレータが邪魔してうまくテストできない例(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
# -*- coding: utf-8 -*- | |
import sys | |
import unittest | |
class WorkTest(unittest.TestCase): | |
def _makeOne(self): | |
from working import work | |
return work | |
def _callFUT(self, num): | |
"""Function Under Test.""" | |
func = self._makeOne() | |
return func(num) | |
def setUp(self): | |
"""Patch stdout.""" | |
import io | |
self.default_out = sys.stdout | |
self.dummy_out = io.StringIO() | |
sys.stdout = self.dummy_out | |
def tearDown(self): | |
"""Revert stdout.""" | |
sys.stdout = self.default_out | |
def test_one(self): | |
self._callFUT(3) | |
self.dummy_out.seek(0) | |
self.assertEqual( | |
self.dummy_out.getvalue(), | |
'ワイワイ!\nワイワイ!\nワイワイ!\n' | |
) | |
if __name__ == '__main__': | |
unittest.main() |
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
# -*- coding: utf-8 -*- | |
from datetime import datetime | |
def teiji(hours, minutes): | |
"""定時出社チェック""" | |
def _teiji(func): | |
def wrapper(*args, **kwargs): | |
now = datetime.now() | |
teiji_date = datetime( | |
now.year, | |
now.month, | |
now.day, | |
hours, | |
minutes | |
) | |
if now < teiji_date: | |
func(*args, **kwargs) | |
else: | |
print('定時出社大失敗!') | |
return wrapper | |
return _teiji | |
@teiji(9, 0) | |
def work(num): | |
for i in range(num): | |
print('ワイワイ!') | |
if __name__ == '__main__': | |
work(3) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment