您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

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

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

我为此使用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_onceassert_called_wiht)您的测试可能仍在运行,因为Mock会认为这是一个模拟的函数并且很乐意进行,除非您使用autospec=true。有关更多信息,请阅读assert_call_once:Threat或Menace

python 2022/1/1 18:49:51 有371人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶