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’sHOME: hermetic builders setHOME=/homeless-shelter, which would otherwise leak into those assertions.XDG_CACHE_HOMEis pinned too, soget_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 aFORCE_COLORorCLICOLOR_FORCEin the caller’s shell would otherwise leave ANSI codes in the output a test matches as plain text. A test that wants them passesenv=orforce_color=Truetoinvoke().The environment is patched directly (via
temporary_env()) rather than through themonkeypatchfixture on purpose: depending onmonkeypatchhere would tear it down afterisolated_filesystemremoved the working directory it tries to restore, breaking unrelated tests thatchdirwithin the runner. For the same reason, an autouse fixture that patches the environment must use its ownpytest.MonkeyPatch.context(), not themonkeypatchfixture, 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
--configsearch pattern derives fromclick.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(bothclick’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 pinsHOMEand 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 interceptsget_app_dirdirectly, covering any in-process invocation, without touchingHOME(a suite may need the real one elsewhere) and without leaking into subprocesses.To make a whole suite hermetic, alias it to an
autousefixture in yourconftest.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:
- Returns:
Pytest parameters covering each command-like decorator variant:
click.commandclick.command()cloup.commandcloup.command()click_extra.commandclick_extra.command()click.groupclick.group()cloup.groupcloup.group()click_extra.groupclick_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:
- Returns:
Pytest parameters covering each parameter-like decorator variant:
click.optionclick.option()cloup.optioncloup.option()click_extra.optionclick_extra.option()click.argumentclick.argument()cloup.argumentcloup.argument()click_extra.argumentclick_extra.argument()