Skip to content

pytestconfig

pytestconfig is a session-scoped fixture provides access to pytest’s global configuration object. It allows tests and fixtures to read command-line options, configuration values from pytest.ini, and other runtime settings. This is useful when test behavior needs to change based on how pytest was invoked.

python
def test_read_config(pytestconfig):
    # Get a built-in or custom command-line option
    verbose = pytestconfig.getoption("verbose")

    # Read a value from pytest.ini (if defined)
    cache_dir = pytestconfig.getini("cache_dir")

    assert isinstance(verbose, int)
    print(f"Verbose level: {verbose}, Cache dir: {cache_dir}")