我使用
Mock为此:
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)
Mock支持很多有用的功能,包括补丁对象或模块,以及检查正确的东西被调用等等。
Caveat emptor!(Buyer beware!)
如果你错误的assert_called_with(assert_called_once或assert_called_wiht)你的测试可能仍然运行,因为Mock会认为这是一个模拟函数,并愉快地走,除非你使用autospec = true。更多信息阅读assert_called_once: Threat or Menace。