【发布时间】:2015-11-05 13:31:13
【问题描述】:
我真的很喜欢mock 的哨兵价值观。在您只想编写涵盖一行的最小单元测试的情况下,不要使用随机无意义的数字是一种好方法。
但是,以下
from mock import sentinel, patch
def test_multiply_stuff():
with patch('module.data_source1',return_value=sentinel.source1):
with patch('module.data_source1',return_value=sentinel.source1):
assert function(module.data_source1,
module_data2) == sentinel.source1 * sentinel.source2
不起作用。你会得到
TypeError: unsupported operand type(s) for *: '_SentinelObject' and '_SentinelObject'
我明白为什么:对哨兵对象的操作不能计算为表达式是有道理的。
是否有一些技术可以做到这一点(最好在mock 内)?
我可以使用一些技巧吗?还是仅使用模范数字是您能做的最好的事情?
【问题讨论】:
标签: python unit-testing testing mocking