【发布时间】:2021-01-26 13:13:54
【问题描述】:
我有两个文件,分别是“example.py”和“test.py”
example.py 是这样的:
class Example(object):
print('hello world')
def greeting(self, words):
print(words)
example.py 被导入到 test.py 中:
import example
这将导致代码 'print('hello world') 被执行。
在 unittest.mock 中是否有一种方法可以模拟()或补丁()类示例来停止 print('hello world') 被执行但仍然保持方法 greeting() 工作?
【问题讨论】:
-
将 print 关联到方法(甚至构造函数)然后将其 return_value 修补为 None 不是更合适吗?
-
@Younes 问题是代码在我无法修改的库中
标签: python unit-testing mocking