gpt4 book ai didi

python - 从conftest.py访问测试文件名

转载 作者:行者123 更新时间:2023-12-01 09:25:58 25 4
gpt4 key购买 nike

我正在尝试做什么

我正在使用 pytest 在 python 中编写一个小型框架,作为拆解的一部分,我正在截取屏幕截图。现在,我希望根据正在运行的测试来命名该屏幕截图,而不是 conftest.py例如,我现在的代码是:

driver.save_screenshot(os.path.basename(__file__)+'.png')

问题

这会打印名称“conftest.py”。如果可能的话,我想打印调用测试的名称。我猜使用请求?

最佳答案

您可以通过request.node.fspath访问当前文件路径。示例:

# conftest.py
import pytest

@pytest.fixture
def currpath(request):
return str(request.node.fspath)

关于python - 从conftest.py访问测试文件名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50393354/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com