提供模拟对象的python库

时间:2016-02-19 04:21:22

标签: python

我在工作中使用python来开发一些相互复杂的库,它们之间具有相互依赖性。我向我的团队建议我们内部库的开发人员提供他们的功能类和函数的模拟版本。

举个例子,假设你有一个ssh类,你必须反复模拟ssh调用。一种方法是使用不同的返回值来模拟类似ssh.run_command的东西(使用标准的MagicMock)。 IMO更好的方法是拥有像

这样的东西
# mock using regexp
ssh.run_command.mock_pattern(r'ls .*', output='one two three', rc=0)
ssh.run_command.mock_pattern(r'kill \d+', output='', rc=0)
myfunc() # run a function that uses ssh somehow
ssh.run_command.assert_all_match() # assert all inputs matched a pattern

基本上,我建议这些库定义“helper mocks”,以便更容易为它们编写单元测试。

我无法找到有关此主题的资源,甚至是提供此类功能的python库 - 有没有人知道这种工具?

谢谢!

0 个答案:

没有答案