AttributeError: __exit__ 当我尝试模拟内置函数时

7 投票
1 回答
4781 浏览
提问于 2025-04-18 11:44

我现在正在尝试在Python中模拟内置的open()方法,以便进行测试。不过,我总是遇到崩溃,并且出现了以下信息:

   File "/opt/home/venv/lib/python2.7/site-packages/nose-1.3.0-py2.7.egg/nose/result.py", line 187, in _exc_info_to_string
return _TextTestResult._exc_info_to_string(self, err, test)
 File "/opt/python-2.7.3/lib/python2.7/unittest/result.py", line 164, in _exc_info_to_string
msgLines = traceback.format_exception(exctype, value, tb)
 File "/opt/python-2.7.3/lib/python2.7/traceback.py", line 141, in format_exception
list = list + format_tb(tb, limit)
 File "/opt/python-2.7.3/lib/python2.7/traceback.py", line 76, in format_tb
return format_list(extract_tb(tb, limit))
  File "/opt/python-2.7.3/lib/python2.7/traceback.py", line 101, in extract_tb
line = linecache.getline(filename, lineno, f.f_globals)
  File "/opt/home/venv/lib/python2.7/linecache.py", line 14, in getline
lines = getlines(filename, module_globals)
 File "/opt/home/venv/lib/python2.7/linecache.py", line 40, in getlines
return updatecache(filename, module_globals)
 File "/opt/home/venv/lib/python2.7/linecache.py", line 127, in updatecache
with open(fullname, 'rU') as fp:
AttributeError: __exit__

这是我的测试代码:

m = mox.Mox()
m.StubOutWithMock(__builtin__, 'open')
mock_file = m.CreateMock(__builtin__.file)

open(mox.IgnoreArg(), mox.IgnoreArg()).AndReturn(mock_file)
mock_file.write(mox.IgnoreArg()).MultipleTimes()
mock_file.close()

write_file_method()

1 个回答

4

__exit__ 是在你尝试关闭文件时会被调用的方法。你的模拟文件只处理了 mock_file.close(),而没有处理 open()。你还需要模拟 close 方法。


补充:

再想想,为什么你想要模拟 open 呢?据我所知,你不应该去做这个方法。你测试的方法应该接收一个已经打开的流(比如说,不是文件名)。在实际的代码中,打开文件的责任在于使用者(例如 pickle.dump)。在你的测试中,你可以传入一个 StringIO,或者一个支持写入的模拟对象。


补充 2:

我建议把你的方法分成两部分,分别测试每一部分。

  • 创建文件:检查在调用这个方法之前文件是否不存在,调用之后文件是否存在。有人可能会觉得这样一个一行的简单方法不值得测试。
  • 写入文件:同上。创建一个 StringIO 并写入其中,这样你的测试就可以验证写入的内容是否正确。

撰写回答