我为此使用Mock(在py3.3 +上现在是unittest.mock):
from mock import patch
from PyQt4 import Qt
@patch.object(Qt.QMessage@R_702_2419@, 'aboutQt')
def testShowAboutQt(self, mock):
self.win.actionAboutQt.trigger()
self.assertTrue(mock.called)
对于您的情况,它可能看起来像这样:
import mock
from mock import patch
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)
Mock支持许多有用的功能,包括修补对象或模块的方式以及检查是否调用了正确的东西等。
(请当心!)
如果您输入错误的assert_called_with
(到assert_called_once
或assert_called_wiht
)您的测试可能仍在运行,因为Mock会认为这是一个模拟的函数并且很乐意进行,除非您使用autospec=true
。有关更多信息,请阅读assert_call_once:Threat或Menace。