断言在Pythonunit testing中调用了一个方法
假设我在Pythonunit testing中有以下代码:
aw = aps.Request("nv1") aw2 = aps.Request("nv2", aw)
有没有一种简单的方法来断言在testing的第二行中调用了一个特定的方法(在我的例子中是aw.Clear()
)? 例如有这样的事情:
#pseudocode: assertMethodIsCalled(aw.Clear, lambda: aps.Request("nv2", aw))
我使用模拟这个:
from mock import patch from PyQt4 import Qt @patch.object(Qt.QMessageBox, 'aboutQt') def testShowAboutQt(self, mock): self.win.actionAboutQt.trigger() self.assertTrue(mock.called)
对于你的情况,它可能是这样的:
import mock def testClearWasCalled(self): aw = aps.Request("nv1") with patch.object(aw, 'Clear') as mock: aw2 = aps.Request("nv2", aw) mock.assert_called_with(42) # or mock.assert_called_once_with(42)
模拟支持不less有用的function,包括修补对象或模块的方法,以及检查是否调用了正确的东西等等。
买者自负! (买家小心!)
如果你错误地input了assert_called_with
( assert_called_once
或者assert_called_wiht
),你的testing仍然可能会运行,因为Mock会认为这是一个模拟的函数,并且很乐意继续,除非你使用autospec=true
。 欲了解更多信息,请阅读assert_called_once:威胁或威胁 。
我不知道有什么内置的。 实现起来非常简单:
class assertMethodIsCalled(object): def __init__(self, obj, method): self.obj = obj self.method = method def called(self, *args, **kwargs): self.method_called = True self.orig_method(*args, **kwargs) def __enter__(self): self.orig_method = getattr(self.obj, self.method) setattr(self.obj, self.method, self.called) self.method_called = False def __exit__(self, exc_type, exc_value, traceback): assert getattr(self.obj, self.method) == self.called, "method %s was modified during assertMethodIsCalled" % self.method setattr(self.obj, self.method, self.orig_method) # If an exception was thrown within the block, we've already failed. if traceback is None: assert self.method_called, "method %s of %s was not called" % (self.method, self.obj) class test(object): def a(self): print "test" def b(self): self.a() obj = test() with assertMethodIsCalled(obj, "a"): obj.b()
这要求对象本身不会修改self.b,这几乎总是正确的。
是的,如果你正在使用Python 3.3+。 您可以使用内置的unittest.mock
来声明调用的方法。 对于Python 2.6+使用滚动backport Mock
,这是相同的事情。
以下是您的案例中的一个简单示例:
from unittest.mock import MagicMock aw = aps.Request("nv1") aw.Clear = MagicMock() aw2 = aps.Request("nv2", aw) assert aw.Clear.called
是的,我可以给你提纲,但我的Python有点生疏,我太忙了,不能详细解释。
基本上,你需要把一个代理的方法,将调用原件,例如:
class fred(object): def blog(self): print "We Blog" class methCallLogger(object): def __init__(self, meth): self.meth = meth def __call__(self, code=None): self.meth() # would also log the fact that it invoked the method #example f = fred() f.blog = methCallLogger(f.blog)
这个关于可调用的StackOverflow答案可能有助于你理解上面的内容。
更详细地说:
虽然答案被接受,但由于与Glenn有趣的讨论,并有几分钟的自由,我想扩大我的答案:
# helper class defined elsewhere class methCallLogger(object): def __init__(self, meth): self.meth = meth self.was_called = False def __call__(self, code=None): self.meth() self.was_called = True #example class fred(object): def blog(self): print "We Blog" f = fred() g = fred() f.blog = methCallLogger(f.blog) g.blog = methCallLogger(g.blog) f.blog() assert(f.blog.was_called) assert(not g.blog.was_called)
你可以模拟aw.Clear
,手动或使用像pymox这样的testing框架。 手动,你会用这样的事情来做:
class MyTest(TestCase): def testClear(): old_clear = aw.Clear clear_calls = 0 aw.Clear = lambda: clear_calls += 1 aps.Request('nv2', aw) assert clear_calls == 1 aw.Clear = old_clear
使用pymox,你会这样做:
class MyTest(mox.MoxTestBase): def testClear(): aw = self.m.CreateMock(aps.Request) aw.Clear() self.mox.ReplayAll() aps.Request('nv2', aw)