Pytest:获取所有测试的地址

Ram*_*hum 13 python pytest

当我跑去pytest --collect-only获取我的测试列表时,我会以类似的格式获取它们<Function: test_whatever>.但是,当我pytest -k ...用来运行特定测试时,我需要以格式输入测试的"地址" foo::test_whatever.是否有可能以相同的格式-k获取所有测试的所有地址列表?

Sha*_*rad 3

在conftest.py中,您可以覆盖“集合”挂钩以打印有关收集的测试“项目”的信息。

您可以引入自己的命令行选项(例如 --collect-only)。如果指定了此选项,则打印测试项目(以您喜欢的方式)并退出。

下面的示例conftest.py(在本地测试):

import pytest

def pytest_addoption(parser):
    parser.addoption("--my_test_dump", action="store", default=None,
        help="Print test items in my custom format")

def pytest_collection_finish(session):
    if session.config.option.my_test_dump is not None:
        for item in session.items:
            print('{}::{}'.format(item.fspath, item.name))
        pytest.exit('Done!')
Run Code Online (Sandbox Code Playgroud)

有关 pytest 挂钩的更多信息,请参阅:

http://doc.pytest.org/en/latest/_modules/_pytest/hookspec.html