我正在使用pytest
编写一些单元测试。
我知道我可以在任何测试或夹具中访问tmp_path
临时目录,但是在pytest_sessionstart()
方法中也可以访问它吗?
本质上,这是我想要达到的目标的一个例子
def pytest_sessionstart(session, tmp_path):
"""Create hello.txt before any test is ran and make available to all tests"""
p = tmp_path.join("hello.txt")
p.write("content")
谢谢
发布于 2021-07-21 21:51:53
为所有测试创建临时文件的推荐方法是将会话作用域夹具与内置的tmp_path_factory夹具一起使用。
来自[医]脓性文档:
# contents of conftest.py
import pytest
@pytest.fixture(scope="session")
def image_file(tmp_path_factory):
img = compute_expensive_image()
fn = tmp_path_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
https://stackoverflow.com/questions/68473205
复制相似问题