Accessing test file name from conftest.py

做~自己de王妃 提交于 2019-12-02 06:52:15

问题


What I'm trying to do

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')

The problem

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


回答1:


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)


来源:https://stackoverflow.com/questions/50393354/accessing-test-file-name-from-conftest-py

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!