click_extra.pytest module¶

Pytest fixtures and marks to help testing Click CLIs.

click_extra.pytest.runner()[source]¶

Runner fixture for click_extra.testing.CliRunner.

Pins HOME (and its platform-specific equivalents) to a subdirectory of the runner’s isolated filesystem so configuration-file discovery is deterministic and independent of the ambient environment. Without this, the handful of tests asserting on the config-search debug output depend on the caller’s HOME: hermetic builders set HOME=/homeless-shelter, which would otherwise leak into those assertions. XDG_CACHE_HOME is pinned too, so get_cache_dir() never sends a CLI under test to the cache of the user running the tests.

It also unsets every variable in COLOR_ENVVARS. The color option reads them, so a FORCE_COLOR or CLICOLOR_FORCE in the caller’s shell would otherwise leave ANSI codes in the output a test matches as plain text. A test that wants them passes env= or force_color=True to invoke().

The environment is patched directly (via temporary_env()) rather than through the monkeypatch fixture on purpose: depending on monkeypatch here would tear it down after isolated_filesystem removed the working directory it tries to restore, breaking unrelated tests that chdir within the runner. For the same reason, an autouse fixture that patches the environment must use its own pytest.MonkeyPatch.context(), not the monkeypatch fixture, which would outlive this one.

Warning

The pinning is scoped to each test requesting this fixture, but a cache filled from inside one is not: a module-global populated lazily during a test records what a home-less environment answered, and keeps serving that for the rest of the worker’s session. A binary resolved through a $HOME-dependent shim is the usual victim, answering an error rather than a version, after which every later test on that worker sees the tool as missing. Seed such a cache from a session-scoped fixture, which runs before the first test and outside this isolation.

click_extra.pytest.invoke(runner)[source]¶

Invoke fixture shorthand for click_extra.testing.CliRunner.invoke().

click_extra.pytest.isolated_app_dir(monkeypatch, tmp_path)[source]¶

Repoint configuration-file discovery at a fresh, empty directory.

The default --config search pattern derives from click.get_app_dir(), which resolves to the host configuration folder (~/Library/Application Support/<app> on macOS, ~/.config/<app> on Unix, %APPDATA%\<app> on Windows). Any configuration file living there bleeds into every in-process CLI invocation, making a test suite pass or fail depending on the developer’s personal configuration.

This fixture repoints get_app_dir (both click’s and the reference bound into click-extra’s config machinery) at a per-test temporary directory, whatever application name is requested. It returns that directory, so a test can also plant a configuration file in it to exercise the default discovery against a controlled file.

An explicit --config <path> bypasses the default search pattern and is left unaffected.

Note

The runner() fixture pins HOME and its platform equivalents inside an isolated filesystem, which also redirects the discovery paths built from the home directory, but only for tests requesting that fixture. This one intercepts get_app_dir directly, covering any in-process invocation, without touching HOME (a suite may need the real one elsewhere) and without leaking into subprocesses.

To make a whole suite hermetic, alias it to an autouse fixture in your conftest.py:

import pytest


@pytest.fixture(autouse=True)
def isolate_user_config(isolated_app_dir):
    return isolated_app_dir
click_extra.pytest.command_decorators(no_commands=False, no_groups=False, no_click=False, no_cloup=False, no_extra=False, with_parenthesis=True, with_types=False)[source]¶

Return the Pytest parameters testing every command-like decorator.

Return type:

tuple[ParameterSet, ...]

Returns:

Pytest parameters covering each command-like decorator variant:

  • click.command

  • click.command()

  • cloup.command

  • cloup.command()

  • click_extra.command

  • click_extra.command()

  • click.group

  • click.group()

  • cloup.group

  • cloup.group()

  • click_extra.group

  • click_extra.group()

click_extra.pytest.option_decorators(no_options=False, no_arguments=False, no_click=False, no_cloup=False, no_extra=False, with_parenthesis=True, with_types=False)[source]¶

Return the Pytest parameters testing every parameter-like decorator.

Return type:

tuple[ParameterSet, ...]

Returns:

Pytest parameters covering each parameter-like decorator variant:

  • click.option

  • click.option()

  • cloup.option

  • cloup.option()

  • click_extra.option

  • click_extra.option()

  • click.argument

  • click.argument()

  • cloup.argument

  • cloup.argument()

  • click_extra.argument

  • click_extra.argument()

click_extra.pytest.create_config(tmp_path)[source]¶

A generic fixture to produce a temporary configuration file.

click_extra.pytest.assert_output_regex()[source]¶

An assert-like utility for Pytest to compare CLI output against the regex.

Designed for the regexes defined above.