假设以下设置:
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类的更新方法编写单元测试,以检查是否调用了A类self.nodes中包含的每个B类节点的更新方法?
在更一般的设置中,让我们假设有多个类实现更新方法,并且可以是类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)
正如mocking a function within a class method所建议的那样。
编辑:如果我们假设这个特例:
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的更新方法是模拟的。如何衡量B.update在这种情况下在A.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)
更新功能现在确实运行了3次(我在控制台输出中看到它,因为“Update called。”被打印出三次),但更新方法的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)
我如何检查所有
B.update()
被叫所有A.nodes
?
# same everthing except this
self.assertEqual(mockb_update.call_count, 2)
在更新代码后,当
B.is_udpated
未被嘲笑时,遇到无限循环
在B.is_updated
或模拟类__init__
内模拟一个__init__
是一个比原始帖子更复杂的话题
这里有一些想法,B.is_updated
不能只是mock.patch
,它只有在B级启动后才可用。所以选择是
a)模拟B.__init__
,或类构造函数
b)模拟整个类B
,在你的情况下更容易,将is_updated
设置为True
,将结束无限循环。
结合@Gang和@jonrsharpe的答案,以下代码片段解决了我上面提到的问题:
如何为TestA.test_update()编写单元测试以查看是否调用了B.update()?看@Gangs的回答。
如何检查所有A.nodes的所有B.update()?看@Gangs的回答。
在OP更新代码后,当B.is_udpated没有被模拟时,运行到无限循环
正如@jonrsharpe建议的那样,解决方案是在节点中为每个B实例创建一个模拟对象,并分别检查函数调用:
class TestA(TestCase):
@mock.patch('module.B')
@mock.patch('module.B')
@mock.patch('module.B')
def test_update(self, mock_B1, mock_B2, mock_B3):
nodes = {}
nodes['1'] = mock_B1
nodes['2'] = mock_B2
nodes['3'] = mock_B3
a = A(nodes)
a.update()
with self.subTest():
self.assertEqual(mock_B1.update.call_count, 1)
with self.subTest():
self.assertEqual(mock_B2.update.call_count, 1)
with self.subTest():
self.assertEqual(mock_B3.update.call_count, 1)
另外,如果由于某种原因需要执行模拟函数(如果它们设置了一些影响运行时的标志或变量),可以编写如下测试:
def test_fit_skip_ancestors_all(self):
nodes = {}
nodes['1'] = mock_B1
nodes['2'] = mock_B2
nodes['3'] = mock_B3
a = A(nodes)
with mock.patch.object(A.nodes['1'],'update',wraps=A.nodes['1'].update) as mock_B1, \
mock.patch.object(A.nodes['2'], 'update', wraps=A.nodes['2'].update) as mock_B2, \
mock.patch.object(A.nodes['3'], 'update', wraps=A.nodes['3'].update) as mock_B3:
a.update()
with self.subTest():
self.assertEqual(mock_B1.call_count, 1)
with self.subTest():
self.assertEqual(mock_B2.call_count, 1)
with self.subTest():
self.assertEqual(mock_B3.call_count, 1)