Pytest plugin for Click
Maintained. Last release 10 days ago, commits in 1 of the last 26 weeks. Weaker on commit activity and release cadence.
$
pip install pytest-clickpytest-click
Installation
pip install pytest-click
Usage
pytest-click comes with two fixtures: cli_runner and isolated_cli_runner.
import click
def test_cli(cli_runner):
@click.command()
@click.argument("name")
def hello(name):
click.echo(f"Hello {name}!")
result = cli_runner.invoke(hello, ["Peter"])
assert result.exit_code == 0
assert result.output == "Hello Peter!\n"
isolated_cli_runner runs each test inside CliRunner.isolated_filesystem():
import click
def test_fixture(isolated_cli_runner):
@click.command()
@click.argument("f", type=click.File())
def cat(f):
click.echo(f.read())
with open("hello.txt", "w") as f:
f.write("Hello World!")
result = isolated_cli_runner.invoke(cat, ["hello.txt"])
assert result.exit_code == 0
assert result.output == "Hello World!\n"
Both runners can be configured with the runner_setup mark:
import pytest
@pytest.mark.runner_setup(charset="cp1251", env={"test": 1}, echo_stdin=True)
def test_runner_setup(cli_runner): ...
All kwargs are passed to click.testing.CliRunner.