繁体   English   中英

如何查看当前 pytest 配置?

[英]How can I see the current pytest configuration?

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

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

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

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

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

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

pytestconfig 的pytestconfigConfig的一部分,在文档中进行描述。

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

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

[pytest]
addopts = -vq

并且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

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

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

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

受@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

然后按照 Lucas 建议的 pytest 运行它(例如,此处使用-vv表示详细程度 2),

$ pytest -vv test_sample.py
...
verbose: 2
version: False
xmlpath: None
==================== 1 failed in 0.10s ===================
$

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM