在临时目录中执行代码块
temp_dir的Python项目详细描述
切换到新创建的临时目录以执行代码块。
离开代码块时,临时目录及其所有内容 删除并还原原始工作目录。
旨在作为一个工具来简化“集成”测试。
该包仅包含两个函数:in_temp_dir将与with语句一起使用 在温度目录中修饰函数。
示例
函数装饰符:
@within_temp_dir def f(): return os.getcwd() class TestScript(unittest.TestCase): @within_temp_dir def test_output_file_created(self): self.create_input_file() call_script('input') self.assertTrue(os.path.exists('output'))
上下文管理器:
with in_temp_dir(): with open('tempfile', 'w') as f: f.write(...) process_file('tempfile')
其他地方的类似功能
类似的功能可在
- 测试支持温度CWD (http://docs.python.org/3/library/test.html#test.support.temp_cwd) 在python发行版中 -温度cwd非常接近这个模块, 不幸的是它不在标准库中, 但与回归测试一起打包。 temp_cwd的来源显示了一些特殊情况 并在可以猜测的目录中运行代码
- tempdir(https://bitbucket.org/another_thomas/tempdir)
- 临时目录(https://github.com/thelinuxkid/tempdirs) -用于创建任意数量的临时目录
- 路径.py(https://github.com/jaraco/path.py/blob/master/path.py) -文件末尾的tempdir