Using `typer.testing.CliRunner`
Overview
typer.testing.CliRunner
is a great tool for testing typer
applications.
Checklist:
- Initialize with
mix_stderr=False
- Ensure that each argument of
invoke
is a string. - When asserting the value of
stdout
orstderr
, add a trailing line break.
Initialization
By default, CliRunner
is created with mix_stderr=True
. This means, that the content of stdout
and stderr
will both appear in result.stdout
.
Recommended practice: Verify separately the content of stdout
and stderr
. => Create the CliRunner
instance with mix_stderr=False
Verifying the content of stdout
and stderr
separately after invoking a command:
from pycon_catalogue.cli.cli import app
runner = CliRunner(mix_stderr=False)
def test_success(cmd_parts):
result = runner.invoke(app, "create", "--name", "something")
assert result.exit_code == 0
assert result.stdout
# stderr
assert "New talk created. 🪅" in result.stderr
Invoke: Use only string Arguments
Example:
Pitfall:
This will lead to an error during invoking the command:
This is especially easy to oversee when testing error scenarios.