python测试用例模拟什么? [英] What to mock for python test cases?
本文介绍了python测试用例模拟什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想了解一般编写测试用例时需要模拟什么以及不需要模拟什么.
I want to understand what needs to be mocked and what not when writing test cases in general.
例如,我们将模拟 I/O 操作,但是从另一个模块导入的函数呢?我们也应该嘲笑他们吗?
For example, we will mock I/O operations, but what about functions imported from another module. Are we supposed to mock them as well?
推荐答案
模拟应该是有原因的.很好的理由是:
Mocking should be done for a reason. Good reasons are:
- 您无法轻松地使依赖组件 (DOC) 的行为符合您的测试预期.
- 调用 DOC 是否会导致任何非极端行为(日期/时间、随机性、网络连接)?
- 测试设置过于复杂和/或维护密集型(例如,需要外部文件)
- 原始 DOC 为您的测试代码带来了可移植性问题.
- 使用原始 DOC 会导致构建/执行时间过长吗?
- 是否存在 DOC 稳定性(成熟度)问题导致测试不可靠,或者更糟糕的是,DOC 是否尚未可用?
例如,您(通常)不会模拟像 sin
或 cos
这样的标准库数学函数,因为它们没有任何上述问题.
For example, you (typically) don't mock standard library math functions like sin
or cos
, because they don't have any of the abovementioned problems.
这篇关于python测试用例模拟什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文