pytest官方文档 6.2 中文翻译版(第八章):临时目录和文件

该博客主要介绍了pytest测试框架中关于临时目录和文件的使用,包括tmp_path、tmp_path_factory、tmpdir和tmpdir_factory四个夹具的详细说明,以及根临时目录的默认设置和管理。这些夹具用于为每个测试创建独立的临时目录,方便测试过程中的文件操作。文章还提到了pytest-xdist并行执行测试时的注意事项。
摘要由CSDN通过智能技术生成

临时目录和文件

tmp_path 夹具

你可以使用 tmp_path夹具为每一个测试调用创建一个独立的临时目录,这个目录的创建是基于 根临时目录的。
译者注:根临时目录是所有临时目录的父目录,可以使用命令行指定,也有默认值
tmp_path 是 pathlib.Path 的一个实例,下面是使用的例子:

# content of test_tmp_path.py
CONTENT = "content"


def test_create_file(tmp_path):
	d = tmp_path / "sub" d.mkdir()
	p = d / "hello.txt" p.write_text(CONTENT)
	assert p.read_text() == CONTENT
	assert len(list(tmp_path.iterdir())) == 1
	assert 0

运行这个测试,我们给了一个给定的assert 0 是为了查看测试运行的值:

$ pytest test_tmp_path.py
=========================== test session starts ============================
platform linux -- Python 3.x.y, pytest-6.x.y, py-1.x.y, pluggy-0.x.y
cachedir: $PYTHON_PREFIX/.pytest_cache
rootdir: $REGENDOC_TMPDIR
collected 1 item
test_tmp_path.py F [100%]
================================= FAILURES =================================
_____________________________ test_create_file _____________________________
tmp_path = PosixPath('PYTEST_TMPDIR/test_create_file0')
def test_create_file(tmp_path):
d = tmp_path / "sub"
d.mkdir()
p = d / "hello.txt"
p.write_text(CONTENT)
assert p.read_text() == CONTENT
assert len(list(tmp_path.iterdir())) == 1
> assert 0
E assert 0
test_tmp_path.py:13: AssertionError
========================= short test summary info ==========================
FAILED test_tmp_path.py::test_create_file - assert 0
============================ 1 failed in 0.12s =============================

8.2 tmp_path_factory 夹具

tmp_path_factory 是一个 session-scoped 的夹具,它可以被其他夹具或者测试调用来创建任意的临时目录。

它是被设计出来替换 tmpdir_factory 的,它返回一个 pathlib.Path 对象。

查看 tmp_path_factory API 以查看更多的细节。

8.3 tmpdir 夹具

你可以使用 tmpdir 夹具为每一个测试调用创建一个独立的临时目录,这个牡蛎的创建是基于 根临时目录的。
tmpdir返回 py.path.local 对象,提供了 os.path 方法等,下面是使用的例子:

# content of test_tmpdir.py
def test_create_file(tmpdir):
	p = tmpdir.mkdir("sub").join("hello.txt") p.write("content")
	assert p.read() == "content"
	assert len(tmpdir.listdir()) == 1
	assert 0

运行这个测试,我们给了一个给定的assert 0 是为了查看测试运行的值:

$ pytest test_tmpdir.py
=========================== test session starts ============================
platform linux -- Python 3.x.y, pytest-6.x.y, py-1.x.y, pluggy-0.x.y
cachedir: $PYTHON_PREFIX/.pytest_cache
rootdir: $REGENDOC_TMPDIR
collected 1 item
test_tmpdir.py F [100%]
================================= FAILURES =================================
_____________________________ test_create_file _____________________________
tmpdir = local('PYTEST_TMPDIR/test_create_file0')
def test_create_file(tmpdir):
p = tmpdir.mkdir("sub").join("hello.txt")
p.write("content")
assert p.read() == "content"
assert len(tmpdir.listdir()) == 1
> assert 0
E assert 0
test_tmpdir.py:9: AssertionError
========================= short test summary info ==========================
FAILED test_tmpdir.py::test_create_file - assert 0
============================ 1 failed in 0.12s =============================

8.4 tmpdir_factory 夹具

tmpdir_factory 是一个 session-scoped 的夹具,它可以被其他夹具或者测试调用来创建任意的临时目录。

例如,你的测试集需要一个程序生成的大图,你不需要去为每一个测试创建一个自己的 tmpdir,只需要创建一个 单会话 的路径就可以了:

# contents of conftest.py
import pytest


@pytest.fixture(scope="session")
def image_file(tmpdir_factory):
	img = compute_expensive_image()
	fn = tmpdir_factory.mktemp("data").join("img.png")
	img.save(str(fn))
	return fn
	
# contents of test_image.py
def test_histogram(image_file):
	img = load_image(image_file)
	# compute and test histogram

从 tmpdir_factory API 中获取更多的细节

8.5 默认的 根临时目录

临时目录会默认创建在系统的临时目录下,名称的格式是 pytest-NUM, NUM是一个每次测试运行之后递增的数字。此外,根据这个数字,3次以前的临时目录将会被删除。

你可以设置默认的临时文件目录,用下面的命令:

pytest --basetemp=mydir

警告:临时目录中的内容将会被彻底删除,所以在使用临时目录的时候一定注意使用的场景和目的。

当我们使用 pytest-xdist 来在本地机器构建测试的时候,需要注意自动配置一个临时目录给子进程,这样的话所有的临时目录都会在一个基本目录之下了。
译者注:pytest-xdist 是并行执行测试的插件,这里讲的是临时目录与并行执行的时候需要注意的点,当我们遇到这种情况的时候再仔细研究也可以

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值