如何查看当前的 pytest 配置?

And*_*rds 9 python pytest

pytest 从各种文件和命令行选项收集配置设置,但似乎没有命令行选项来显示 pytest 的当前设置,而且我无法弄清楚如何轻松地询问 pytest 以获取此信息。pytest --help(或pytest -h)显示可用选项,但不显示其当前值。

我尝试使用PYTEST_DEBUGset (例如$ env PYTEST_DEBUG=1 pytest --setup-only)运行 pytest:这会生成大量调试信息,但它似乎不包含配置设置,或者至少不包含可消化的格式。

我看到有一个Config对象,但我不知道如何询问它来编写一个小程序来输出其内容(我认为它需要比我拥有的更高级别的 pytest-fu );我认为这个对象可能是查看的正确位置,假设没有命令行选项来显示我错过的当前设置。

And*_*rds 5

受到@Lucas Roberts 回答的启发,我仔细研究了 pytest 的pytestconfig固定装置,它具有各种有趣的属性,特别是option(也known_args_namespace具有类似的内容)。因此,如果我理解pytestconfig正确,打印当前有效选项的粗略解决方案将是,

# test_sample.py
import pytest
import os

def test_answer(pytestconfig):
    for item_name in dir(pytestconfig.option):
        if not item_name.startswith('_'):
            print(f'{item_name}: {getattr(pytestconfig.option,item_name)}')
    assert 0  # to see what was printed
Run Code Online (Sandbox Code Playgroud)

然后按照 Lucas 建议的 pytest 运行(这里以-vvfor verbosity 2 为例),

$ pytest -vv test_sample.py
...
verbose: 2
version: False
xmlpath: None
==================== 1 failed in 0.10s ===================
$
Run Code Online (Sandbox Code Playgroud)


Luc*_*rts 4

查看 pytest 文档后,似乎没有直接的方法来自动枚举所有配置选项。这里有一些获取配置值的方法,希望这些对您有所帮助。

如果您有特定的单元测试并且知道特定值,则 pytest 文档中有一个示例,该示例显示如何诊断这些值是否已设置。

此外,配置文档描述了配置文件搜索和优先级。pytest 的配置设置来自多个位置,包括 cmd 行选项、ini 文件和环境变量。

的参数pytestconfig是 和 的部分,在文档中进行Config了描述。

import pytest
import os

def test_answer(pytestconfig):
    if pytestconfig.getoption("verbose") > 0:
        print("verbose")
    print(pytestconfig.inipath)
    print(pytestconfig.invocation_params.args)
    print(os.getenv('PYTEST_ADDOPTS', None))
    print(pytestconfig.getini('addopts'))
    assert 0  # to see what was printed
Run Code Online (Sandbox Code Playgroud)

现在,如果我在我的目录中运行它pytest test_sample.py(没有命令行参数)。内容已test_sample.py在上面给出。文件的内容pytest.ini

[pytest]
addopts = -vq
Run Code Online (Sandbox Code Playgroud)

我看到未PYTEST_ADDOPTS设置:

---------------------------------------------------------------------------------------------------- Captured stdout call ----------------------------------------------------------------------------------------------------
/Users/rlucas/Desktop/pytest.ini
('test_sample.py',)
None
['-vq']
================================================================================================== short test summary info ===================================================================================================
FAILED test_sample.py::test_answer - assert 0
Run Code Online (Sandbox Code Playgroud)

并使用不同的调用pytest test_sample.py --verbose你会看到

---------------------------------------------------------------------------------------------------- Captured stdout call ----------------------------------------------------------------------------------------------------
verbose
/Users/rlucas/Desktop/pytest.ini
('test_sample.py', '--verbose')
None
['-vq']
================================================================================================== short test summary info ===================================================================================================

Run Code Online (Sandbox Code Playgroud)

在这里,我将输出稍微缩写为相关信息(例如,我没有显示测试失败信息)。如果您无法直接访问运行单元测试的文件系统,您始终可以读取找到的文件pytestconfig.inipath并将内容打印到标准输出。