【发布时间】:2015-01-14 23:05:41
【问题描述】:
我正在尝试测试一些记录日志的代码。
logfile = open(file_name, 'w')
logfile.write("blah1")
logfile.write("blah2")
我想断言 blah1 和 blah2 都被写入。我的测试函数如下所示:
def test_it(self):
logger.open = mock_open()
logger.time.time = Mock(return_value=12345)
logger.run_me()
logger.open.assert_called_once_with('test_12345.log', 'w');
file_handle_mock = logger.open()
file_handle_mock.write.assert_called_with("blah1")
file_handle_mock.write.assert_called_with("blah2")
但它给了我一个错误:
AssertionError: Expected call: write('blah1')
Actual call: write('blah2')
如何正确测试对 write 函数的多次调用?
版本: Python 2.7.6 模拟==1.0.1
【问题讨论】:
标签: python unit-testing python-2.7 mocking