临时目录和文件
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 是并行执行测试的插件,这里讲的是临时目录与并行执行的时候需要注意的点,当我们遇到这种情况的时候再仔细研究也可以