【发布时间】:2014-08-23 08:52:27
【问题描述】:
我正在尝试测试一些使用 os.walk 的代码。我想创建一个临时的内存文件系统,我可以用 os.walk 将返回的示例(空)文件和目录填充它。这应该为我节省了模拟 os.walk 调用以模拟递归的复杂性。
具体来说,我要测试的代码是:
if recursive:
log.debug("Recursively searching for files under %s" % path)
for (dir_path, dirs, files) in os.walk(path):
log.debug("Found %d files in %s: %s" % (len(files), path, files))
for f in [os.path.join(dir_path, f) for f in files
if not re.search(exclude, f)]:
yield f
else:
log.debug("Non-recursively searching for files under %s" % path)
for (dir_path, dirs, files) in os.walk(path):
log.debug("Found %d files in %s: %s" % (len(files), path, files))
for f in [os.path.join(dir_path, f) for f in files
if not re.search(exclude, f)]:
yield f
这在 python 中可能吗?
【问题讨论】:
-
你想只模拟返回的列表,还是模拟完整的文件功能?
-
仅返回列表。
标签: python unit-testing testing mocking python-mock