使用 mock_open ,我可以使用 with [...] as
构造从写入中捕获数据。然而,测试我所拥有的是正确的有点棘手。例如,我可以这样做:
>>> from mock import mock_open
>>> m = mock_open()
>>> with patch('__main__.open', m, create=True):
... with open('foo', 'w') as h:
... h.write('some stuff')
...
>>> m.mock_calls
[call('foo', 'w'),
call().__enter__(),
call().write('some stuff'),
call().__exit__(None, None, None)]
>>> m.assert_called_once_with('foo', 'w')
>>> handle = m()
>>> handle.write.assert_called_once_with('some stuff')
但我想比较一下我认为应该写的和已经写的。实际上是这样的:
>>> expected = 'some stuff'
>>> assert(expected == m.all_that_was_written)
我在 call
中遇到的问题是不同版本的 json(2.0.9 与 1.9)似乎打印的内容不同。不,我不能只更新到最新的 json。
我得到的实际错误是这样的:
E AssertionError: [call('Tool_000.json', 'w'),
call().__enter__(),
call().write('['),
call().write('\n '),
call().write('"1.0.0"'),
call().write(', \n '),
call().write('"2014-02-27 08:58:02"'),
call().write(', \n '),
call().write('"ook"'),
call().write('\n'),
call().write(']'),
call().__exit__(None, None, None)]
!=
[call('Tool_000.json', 'w'),
call().__enter__(),
call().write('[\n "1.0.0"'),
call().write(', \n "2014-02-27 08:58:02"'),
call().write(', \n "ook"'),
call().write('\n'),
call().write(']'),
call().__exit__(None, None, None)]
在效果上,调用不同但最终结果相同。
我正在测试的代码相当简单:
with open(get_new_file_name(), 'w') as fp:
json.dump(lst, fp)
因此,创建另一个传递文件指针的方法似乎有点过分了。
最佳答案
您可以修补 open()
以返回 StringIO
对象,然后检查内容。
with mock.patch('module_under_test.open', create=True) as mock_open:
stream = io.StringIO()
# patching to make getvalue() work after close() or __exit__()
stream.close = mock.Mock(return_value=None)
mock_open.return_value = stream
module_under_test.do_something() # this calls open()
contents = stream.getvalue()
assert(contents == expected)
编辑:为 stream.close
添加补丁以避免 stream.getvalue()
出现异常。
关于python - 如何修复调用中的 mock_open 差异而不是最终结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22201038/