从conftest.py访问测试文件名 [英] Accessing test file name from conftest.py

查看:148
本文介绍了从conftest.py访问测试文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要做什么

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

I'm writing a small framework in python using pytest, and as part of the teardown I am taking a screenshot. Now, I want that screenshot to be named according to the running test, not conftest.py So, for example, my code right now is:

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

问题

这将打印名称"conftest.py". 如果可能,我想打印呼叫测试名称.我在猜测使用请求吗?

This prints the name "conftest.py". I want to print the calling test name if possible. I am guessing using requests?

推荐答案

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

You can access the current file path via request.node.fspath. Example:

# conftest.py
import pytest

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

这篇关于从conftest.py访问测试文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆