PyTest:自动删除使用 tmpdir_factory 创建的临时目录 [英] PyTest: Auto delete temporary directory created with tmpdir_factory
问题描述
我正在尝试使用 PyTest 的 tmpdir_factory
类似于 教程:
I'm trying to create a temporary directory with a specific name (eg, "data") for all tests within a module using PyTest's tmpdir_factory
similar to the tutorial:
@pytest.fixture(scope='module')
def project_file(self, tmpdir_factory):
return tmpdir_factory.mktemp("data")
我在模块内的一些测试中成功使用了临时目录.但是,运行测试后该目录仍然存在,当我再次运行它们时,我会失败,因为我无法创建名为data"的新临时目录.
I'm using the temporary directory in some tests within the module successfully. However, the directory still exists after running the tests and when I run them again, I get a failure because I cannot create a new temporary directory with name "data".
如何在pytest测试完成后自动删除临时目录data"?tmpdir
参数创建被删除的临时目录,但它没有名称,只有函数作用域.
How can I automatically delete the temporary directory "data" after the pytest tests finish? The tmpdir
argument creates temporary directory that is removed but it doesn't have a name and it only has function scope.
推荐答案
你可以在fixture完成后进行清理,例如:
You can cleanup after the fixture is done like:
@pytest.fixture(scope='module')
def project_file(self, tmpdir_factory):
my_tmpdir = tmpdir_factory.mktemp("data")
yield my_tmpdir
shutil.rmtree(str(my_tmpdir))
这篇关于PyTest:自动删除使用 tmpdir_factory 创建的临时目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!