从conftest.py访问测试文件名

use*_*607 3 python fixtures pytest

我正在尝试做什么

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

driver.save_screenshot(os.path.basename(__file__)+'.png')
Run Code Online (Sandbox Code Playgroud)

问题

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

hoe*_*ing 5

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

# conftest.py
import pytest

@pytest.fixture
def currpath(request):
    return str(request.node.fspath)
Run Code Online (Sandbox Code Playgroud)