首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >pytest:如何在tmp_path ()中访问pytest_sessionstart()?

pytest:如何在tmp_path ()中访问pytest_sessionstart()?
EN

Stack Overflow用户
提问于 2021-07-21 16:25:37
回答 1查看 711关注 0票数 1

我正在使用pytest编写一些单元测试。

我知道我可以在任何测试或夹具中访问tmp_path临时目录,但是在pytest_sessionstart()方法中也可以访问它吗?

本质上,这是我想要达到的目标的一个例子

代码语言:javascript
运行
复制
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")

谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-07-21 21:51:53

为所有测试创建临时文件的推荐方法是将会话作用域夹具与内置的tmp_path_factory夹具一起使用。

来自[医]脓性文档

代码语言:javascript
运行
复制
# 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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68473205

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档