|
|
@ -413,15 +413,17 @@ with ``get_json``. |
|
|
|
Testing CLI Commands |
|
|
|
Testing CLI Commands |
|
|
|
-------------------- |
|
|
|
-------------------- |
|
|
|
|
|
|
|
|
|
|
|
Click comes with `utilities for testing`_ your CLI commands. |
|
|
|
Click comes with `utilities for testing`_ your CLI commands. A |
|
|
|
|
|
|
|
:class:`~click.testing.CliRunner` runs commands in isolation and |
|
|
|
|
|
|
|
captures the output in a :class:`~click.testing.Result` object. |
|
|
|
|
|
|
|
|
|
|
|
Use :meth:`CliRunner.invoke <click.testing.CliRunner.invoke>` to call |
|
|
|
Flask provides :meth:`~flask.Flask.test_cli_runner` to create a |
|
|
|
commands in the same way they would be called from the command line. The |
|
|
|
:class:`~flask.testing.FlaskCliRunner` that passes the Flask app to the |
|
|
|
:class:`~click.testing.CliRunner` runs the command in isolation and |
|
|
|
CLI automatically. Use its :meth:`~flask.testing.FlaskCliRunner.invoke` |
|
|
|
captures the output in a :class:`~click.testing.Result` object. :: |
|
|
|
method to call commands in the same way they would be called from the |
|
|
|
|
|
|
|
command line. :: |
|
|
|
|
|
|
|
|
|
|
|
import click |
|
|
|
import click |
|
|
|
from click.testing import CliRunner |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@app.cli.command('hello') |
|
|
|
@app.cli.command('hello') |
|
|
|
@click.option('--name', default='World') |
|
|
|
@click.option('--name', default='World') |
|
|
@ -429,14 +431,22 @@ captures the output in a :class:`~click.testing.Result` object. :: |
|
|
|
click.echo(f'Hello, {name}!') |
|
|
|
click.echo(f'Hello, {name}!') |
|
|
|
|
|
|
|
|
|
|
|
def test_hello(): |
|
|
|
def test_hello(): |
|
|
|
runner = CliRunner() |
|
|
|
runner = app.test_cli_runner() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# invoke the command directly |
|
|
|
result = runner.invoke(hello_command, ['--name', 'Flask']) |
|
|
|
result = runner.invoke(hello_command, ['--name', 'Flask']) |
|
|
|
assert 'Hello, Flask' in result.output |
|
|
|
assert 'Hello, Flask' in result.output |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# or by name |
|
|
|
|
|
|
|
result = runner.invoke(args=['hello']) |
|
|
|
|
|
|
|
assert 'World' in result.output |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
In the example above, invoking the command by name is useful because it |
|
|
|
|
|
|
|
verifies that the command was correctly registered with the app. |
|
|
|
|
|
|
|
|
|
|
|
If you want to test how your command parses parameters, without running |
|
|
|
If you want to test how your command parses parameters, without running |
|
|
|
the command, use the command's :meth:`~click.BaseCommand.make_context` |
|
|
|
the command, use its :meth:`~click.BaseCommand.make_context` method. |
|
|
|
method. This is useful for testing complex validation rules and custom |
|
|
|
This is useful for testing complex validation rules and custom types. :: |
|
|
|
types. :: |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def upper(ctx, param, value): |
|
|
|
def upper(ctx, param, value): |
|
|
|
if value is not None: |
|
|
|
if value is not None: |
|
|
|