简体   繁体   中英

How can I see the current pytest configuration?

pytest gathers configuration settings from various files and command-line options , but there doesn't appear to be a command-line option to show pytest's current settings, and I can't work out how to easily interrogate pytest for this info. pytest --help (or pytest -h ) shows what the available options are but doesn't show their current values.

I've tried running pytest with PYTEST_DEBUG set (eg $ env PYTEST_DEBUG=1 pytest --setup-only ): This generates a huge quantity of debug info but it doesn't appear to include the configuration settings, or at least not in a digestible format.

I see that there is a Config object but I can't work out how to interrogate it to write a small program to output its content (I think it needs higher-level pytest-fu than I possess); I think this object may be the right place to look, assuming there isn't a command-line option to display the current settings that I've missed.

After looking through the pytest docs, there doesn't appear to be a direct way to enumerate all the config options automatically. Here are some ways to get at the config values though so hopefully these are helpful.

If you have a specific unit test and know particular values there is an example in the pytest docs that show how to diagnose if those values are set.

Also, the config docs describe the config file search and precedence. Configuration settings come from several places for pytest, including cmd line options, ini files, and env variables.

The arguments to pytestconfig are the parts of Config and are described here in the documentation.

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

Now if I run this in my directory with pytest test_sample.py (no cmd line arguments). The contents of test_sample.py are given above. The contents of the pytest.ini file are

[pytest]
addopts = -vq

and the PYTEST_ADDOPTS is not set I see:

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

and using a different call of pytest test_sample.py --verbose you'll see

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

Here I'm abbreviating the output somewhat to the relevant info (eg I'm not showing the test failure info). If you don't have direct access to the filesystem where the unit tests are running you can always read in the file found in pytestconfig.inipath and print the contents to stdout.

Inspired by @Lucas Roberts answer I've taken a closer look at pytest's pytestconfig fixture which has various interesting attributes, particularly option (also known_args_namespace with similar contents). So if I understand pytestconfig correctly, a rough-and-ready solution to print the currently-in-effect options would be,

# 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

then run this as Lucas suggests with pytest (here with -vv for verbosity 2 for example),

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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