如何替换被测模块的文件访问引用 [英] How to replace file-access references for a module under test

查看:92
本文介绍了如何替换被测模块的文件访问引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

pyfakefs 听起来非常有用:最初是作为对Python核心模块的适度伪造实现而开发的,支持中等复杂的文件系统交互,并于2006年9月在Google范围内推出.此后,它获得了许多(经过充分测试的)贡献,以扩展其功能和实用性,并且已在900多个Google Python测试中使用. "

pyfakefs sounds very useful: it "was developed initially as a modest fake implementation of core Python modules to support moderately complex file system interactions, and was introduced Google-wide . . . in September 2006. Since then, it has received many (well-tested) contributions to extend its functionality and usefulness, and is used in over 900 Google Python tests."

文档似乎仅在源代码本身的文档字符串中可用.它解释说该模块提供了以下元素:

Documentation appears to currently only be available within docstrings of the source code itself. It explains that the module provides the following elements:

  • FakeFile:提供真实文件的外观.
  • FakeDirectory:提供真实目录的外观.
  • FakeFilesystem:提供真实目录层次结构的外观.
  • FakeOsModule:使用FakeFilesystem提供虚假的os模块替换.
  • FakePathModule:伪造的os.path模块替换.
  • FakeFileOpen:伪造的file()和open()函数替换.

文档没有解释如何在测试中有效使用这些元素.

Documentation does not, however, explain how to effectively use these elements in testing.

确保被测模块访问伪造的文件系统而不是真正的文件系统的正确方法是什么?

What is the right way to ensure that a module under test accesses a fake filesystem and not the real one?

推荐答案

请参见 http://github.com/jmcgeheeiv /pyfakefs 获取支持unittest和doctest的代码,以及一个实用的示例和教程.

See http://github.com/jmcgeheeiv/pyfakefs for code that supports both unittest and doctest, plus a practical example and tutorial.

这篇关于如何替换被测模块的文件访问引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆