python - 使用 Python 单元测试库(unittest、mock),如何断言 B 类的方法是否在 A 类的方法中被调用?
问题描述
假设以下设置:
class A:
def __init__(self, nodes):
self.nodes=nodes
def update(self, bool_a=True):
if bool_a:
for n in self.nodes:
if hasattr(self.nodes[n], 'update'):
self.nodes[n].update()
class B:
def __init__(self, int_attr=5):
self.int_attr=int_attr
def update(self):
self.int_attr = 0
让我们假设 A 类中的节点列表实际上是 B 类实例的列表。
如何为A类的update方法编写单元测试,检查是否调用了A类的self.nodes中包含的每个B类节点的update方法?
在更一般的设置中,让我们假设有多个类实现了更新方法,并且可以是类 A 的 self.nodes 中的节点。如何检查 self.nodes 成员的所有更新方法是否被调用?
我尝试了以下方法,但未成功:
mock_obj = MagicMock()
@patch('module.A.update', return_value=mock_obj)
def test_update(self, mock_obj):
nodes = {}
nodes['first'] = B(int_attr=1)
nodes['second'] = B(int_attr=2)
test_A = module.A(nodes=nodes)
test_A.update(bool_A=True)
self.assertTrue(mock_obj.called)
正如在类方法中模拟函数中所建议的那样。
编辑:如果我们假设这种特殊情况:
import unittest
import mock
from unittest import TestCase
class A:
def __init__(self, nodes):
self.nodes=nodes
def update(self, bool_a=True):
if bool_a:
to_update = [n for n in self.nodes]
while len(to_update) > 0:
if hasattr(self.nodes[to_update[-1]], 'update'):
self.nodes[to_update[-1]].update()
print('Update called.')
if self.nodes[to_update[-1]].is_updated:
to_update.pop()
class B:
def __init__(self, int_attr=5):
self.int_attr=int_attr
self.is_updated = False
def update(self):
self.int_attr = 0
self.is_updated = True
class TestEnsemble(TestCase):
def setUp(self):
self.b1 = B(1)
self.b2 = B(2)
self.b3 = B(3)
self.nodes = {}
self.nodes['1'] = self.b1
self.nodes['2'] = self.b2
self.nodes['3'] = self.b3
self.a = A(self.nodes)
@mock.patch('module.B.update')
def test_update(self, mock_update):
mock_update.return_value = None
self.a.update()
with self.subTest():
self.assertEqual(mock_update.call_count, 3)
在这种情况下运行 unittest 会导致无限循环,因为 is_updated 属性永远不会设置为 True ,因为 B 类的更新方法被模拟了。在这种情况下,如何测量在 A.update 中调用 B.update 的时间量?
更新:试过这个:
@mock.patch('dummy_script.B')
def test_update(self, mock_B):
self.a.update()
with self.subTest():
self.assertEqual(mock_B.update.call_count, 3)
update 函数现在确实运行了 3 次(我在控制台输出中看到它,因为“Update called.”被打印了 3 次),但是 update 方法的 call_count 保持为零。我在检查错误的属性/对象吗?
解决方案
如何编写单元测试
TestA.test_update()
以查看是否B.update()
被调用?
这只是提供一些想法。
import mock
import unittest
import A
import B
class TestB(unittest.TestCase):
# only mock away update method of class B, this is python2 syntax
@mock.patch.object(B, 'update')
def test_update(self, mockb_update):
# B.update() does not return anything
mockb_update.return_value = None
nodes = {}
nodes['first'] = B(int_attr=1)
nodes['second'] = B(int_attr=2)
test_A = A(nodes)
test_A.update(bool_A=True)
self.assertTrue(mockb_update.called)
我如何检查 all
B.update()
were called for allA.nodes
?
# same everthing except this
self.assertEqual(mockb_update.call_count, 2)
B.is_udpated
OP更新代码后未模拟时进入无限循环
模拟B.is_updated
内部__init__
或模拟类__init__
是一个比原始帖子更复杂的主题
这里有一些想法,B.is_updated
不能只是mock.patch
,它只有在B类启动后才可用。所以选择是
a) mockB.__init__
或类构造函数
b)模拟整个班级B
,在您的情况下更容易设置is_updated
为True
,将结束无限循环。
推荐阅读
- php - 测试时 pdo->exec 未定义
- multithreading - Groovy 中的匿名内部类未捕获最终的外部变量状态
- python - 如何处理“登录python字符串
- python - 取 3 个数字并打印平均值但给我错误
- javascript - 在编译时在 vue js 中传递道具
- c# - 将 ConfigurationElementCollection 中对象的属性复制到数组中?
- angular - 无法在输入上打印值 0.00,小数被切片
- c++ - 带有 switch 语句的 do-while 循环 - 无限循环错误
- node.js - 为 Electron 应用程序重建 SQLite3 时出错
- javascript - Three.js 中点网格的“液化”表面