【发布时间】:2017-08-10 14:58:16
【问题描述】:
我正在测试一个函数,其中两个
def foo():
with open('input.dat', 'r') as f:
....
with open('output.dat', 'w') as f:
....
被使用。但我只想模拟写部分。有可能这样做吗?还是应该使用其他策略来测试这样的功能?
with patch('__builtin__.open') as m:
foo()
将无法读取数据。
提前致谢。
【问题讨论】:
-
@quamrana 虽然我的问题是关于条件模拟,但作为一种特殊情况,它比引用问题中的一般解决方案更方便。
标签: python unit-testing testing file-io mocking