Python:如何在我的测试套件中制作临时文件?

时间:2023-01-13 10:04:12

(I'm using Python 2.6 and nose.)

(我正在使用Python 2.6和鼻子。)

I'm writing tests for my Python app. I want one test to open a new file, close it, and then delete it. Naturally, I prefer that this will happen inside a temporary directory, because I don't want to trash the user's filesystem. And, it needs to be cross-OS.

我正在为我的Python应用程序编写测试。我想要一个测试打开一个新文件,关闭它,然后删除它。当然,我更喜欢这会发生在临时目录中,因为我不想丢弃用户的文件系统。而且,它需要跨OS。

How do I do it?

我该怎么做?

3 个解决方案

#1


12  

See the tempfile module in the standard library -- should be all you need.

请参阅标准库中的tempfile模块 - 应该是您所需要的。

#2


25  

FWIW using py.test you can write:

使用py.test的FWIW你可以写:

def test_function(tmpdir):
    # tmpdir is a unique-per-test-function invocation temporary directory

Each test function using the "tmpdir" function argument will get a clean empty directory, created as a sub directory of "/tmp/pytest-NUM" (linux, win32 has different path) where NUM is increased for each test run. The last three directories are kept to ease inspection and older ones are automatically deleted. You can also set the base temp directory with py.test --basetemp=mytmpdir.

使用“tmpdir”函数参数的每个测试函数将获得一个干净的空目录,创建为“/ tmp / pytest-NUM”的子目录(linux,win32具有不同的路径),其中每次测试运行增加NUM。保留最后三个目录以便于检查,并自动删除旧的目录。您还可以使用py.test --basetemp = mytmpdir设置基本临时目录。

The tmpdir object is a py.path.local object which can also use like this:

tmpdir对象是一个py.path.local对象,它也可以像这样使用:

sub = tmpdir.mkdir("sub")
sub.join("testfile.txt").write("content")

But it's also fine to just convert it to a "string" path:

但是将它转换为“字符串”路径也很好:

tmpdir = str(tmpdir)

#3


4  

Instead of using tempfile directly I suggest using a context manager wrapper for it - the context manager takes care of removing the directory in all cases (success/failure/exception) with basically no boilerplate.

我没有直接使用tempfile,而是建议使用上下文管理器包装器 - 上下文管理器负责在所有情况下(成功/失败/异常)删除目录,基本上没有样板。

Here is how it can be used:

以下是它的使用方法:

from tempdir import TempDir
...

# in some test:
with TempDir() as d:
    temp_file_name = os.path.join(d.name, 'your_temp_file.name')
    # create file...
    # ...
    # asserts...

I have been using a home grown version (the implementation is rather short - under 20 lines) up to the point, when I needed to use it somewhere else as well, so I looked around if there is a package ready to install, and indeed there is: tempdir

我一直在使用自己开发的版本(实现相当短 - 在20行以下),当我需要在其他地方使用它时,所以我环顾四周是否有一个包准备安装,的确有:tempdir

#1


12  

See the tempfile module in the standard library -- should be all you need.

请参阅标准库中的tempfile模块 - 应该是您所需要的。

#2


25  

FWIW using py.test you can write:

使用py.test的FWIW你可以写:

def test_function(tmpdir):
    # tmpdir is a unique-per-test-function invocation temporary directory

Each test function using the "tmpdir" function argument will get a clean empty directory, created as a sub directory of "/tmp/pytest-NUM" (linux, win32 has different path) where NUM is increased for each test run. The last three directories are kept to ease inspection and older ones are automatically deleted. You can also set the base temp directory with py.test --basetemp=mytmpdir.

使用“tmpdir”函数参数的每个测试函数将获得一个干净的空目录,创建为“/ tmp / pytest-NUM”的子目录(linux,win32具有不同的路径),其中每次测试运行增加NUM。保留最后三个目录以便于检查,并自动删除旧的目录。您还可以使用py.test --basetemp = mytmpdir设置基本临时目录。

The tmpdir object is a py.path.local object which can also use like this:

tmpdir对象是一个py.path.local对象,它也可以像这样使用:

sub = tmpdir.mkdir("sub")
sub.join("testfile.txt").write("content")

But it's also fine to just convert it to a "string" path:

但是将它转换为“字符串”路径也很好:

tmpdir = str(tmpdir)

#3


4  

Instead of using tempfile directly I suggest using a context manager wrapper for it - the context manager takes care of removing the directory in all cases (success/failure/exception) with basically no boilerplate.

我没有直接使用tempfile,而是建议使用上下文管理器包装器 - 上下文管理器负责在所有情况下(成功/失败/异常)删除目录,基本上没有样板。

Here is how it can be used:

以下是它的使用方法:

from tempdir import TempDir
...

# in some test:
with TempDir() as d:
    temp_file_name = os.path.join(d.name, 'your_temp_file.name')
    # create file...
    # ...
    # asserts...

I have been using a home grown version (the implementation is rather short - under 20 lines) up to the point, when I needed to use it somewhere else as well, so I looked around if there is a package ready to install, and indeed there is: tempdir

我一直在使用自己开发的版本(实现相当短 - 在20行以下),当我需要在其他地方使用它时,所以我环顾四周是否有一个包准备安装,的确有:tempdir