pytest-click

raw JSON →
1.1.0 verified Mon Apr 27 auth: no python

PyTest plugin for Click applications. Allows testing Click commands with a simple fixture-based interface. Current version: 1.1.0.

pip install pytest-click
error ModuleNotFoundError: No module named 'pytest_click'
cause Trying to import from pytest_click instead of click.testing.
fix
Install pytest-click: pip install pytest-click, then use from click.testing import CliRunner.
error AttributeError: module 'click' has no attribute 'CliRunner'
cause Importing `CliRunner` from `click` instead of `click.testing`. Older Click versions might not have `click.testing`; ensure Click is installed.
fix
Install click: pip install click, then use from click.testing import CliRunner.
error TypeError: invoke() missing 1 required positional argument: 'cli'
cause Calling `runner.invoke()` without passing the Click command object.
fix
Ensure you pass the command as first argument: runner.invoke(my_command, ['arg']).
gotcha pytest-click does not export any custom classes. The `CliRunner` class comes from `click.testing`. Common mistake: `from pytest_click import CliRunner` will fail.
fix Use `from click.testing import CliRunner`.
gotcha The `runner` fixture provided by pytest-click is set up with `mix_stderr=False` by default, which differs from Click's default `mix_stderr=True`. This may cause tests to pass locally but fail in CI if stderr is not handled correctly.
fix If you rely on stderr mixing, configure the runner fixture manually: `runner = CliRunner(mix_stderr=True)`.

Minimal test using CliRunner from click.testing.

import click
from click.testing import CliRunner

@click.command()
def hello():
    click.echo('Hello, World!')

def test_hello():
    runner = CliRunner()
    result = runner.invoke(hello, [])
    assert result.exit_code == 0
    assert 'Hello, World!' in result.output