断言在Python单元测试中已调用方法

断言在Python单元测试中已调用方法,第1张

断言在Python单元测试中已调用方法

我为此使用Mock(在py3.3
+上现在是unittest.mock):

from mock import patchfrom PyQt4 import [email protected](Qt.QMessageBox, 'aboutQt')def testShowaboutQt(self, mock):    self.win.actionaboutQt.trigger()    self.assertTrue(mock.called)

对于您的情况,它可能看起来像这样:

import mockfrom mock import patchdef 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。



欢迎分享,转载请注明来源:内存溢出

原文地址: http://www.outofmemory.cn/zaji/5653769.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存