Created
October 3, 2012 16:53
-
-
Save Gab-km/3828263 to your computer and use it in GitHub Desktop.
#124 「unittest.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
from tkinter import Frame, Button, LEFT, BOTH | |
class MyFrame(Frame): | |
def __init__(self, master=None): | |
Frame.__init__(self, master) | |
self.title_text = 'タイトル' | |
self.master.title(self.title_text) | |
self.b_change_title = Button(self, font=('Consolas', '24'), | |
text='タイトル変更', command=self.change_title) | |
self.b_change_title.pack(side=LEFT, fill=BOTH, padx=1) | |
def change_title(self): | |
self.title_text = self.reverse_title() | |
self.master.title(self.title_text) | |
def reverse_title(self): | |
return self.title_text[::-1] | |
if __name__ == '__main__': | |
f = MyFrame() | |
f.pack(fill=BOTH) | |
f.mainloop() |
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
from unittest import TestCase | |
from unittest.mock import MagicMock | |
from mini_tool import MyFrame | |
class MiniToolTest(TestCase): | |
def test_mockMiniTool(self): | |
sut = MyFrame() | |
sut.reverse_title = MagicMock(return_value='hoge') | |
sut.change_title() | |
sut.reverse_title.assert_called() | |
self.assertEqual(sut.title_text, 'hoge') |
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
from unittest import TestCase | |
from unittest.mock import MagicMock | |
class ProductionClass: | |
pass | |
class MockSample(TestCase): | |
def test_sampleCode(self): | |
thing = ProductionClass() | |
thing.method = MagicMock(return_value=3) | |
self.assertEqual(thing.method(3, 4, 5, key='value'), 3) | |
thing.method.assert_called_with(3, 4, 5, key='value') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment