testing.rst
This file
The walk stopped after 200 commits: lines older than 40e7756 point at it.
Merge pull request #1799 from pallets/dependabot/pip/mypy-0.812dependabot-preview[bot]before adoption40e7756
lines 1–158why, one step back1Testing Click Applications2==========================34.. currentmodule:: click.testing56For basic testing, Click provides the :mod:`click.testing` module which7provides test functionality that helps you invoke command line8applications and check their behavior.910These tools should really only be used for testing as they change11the entire interpreter state for simplicity and are not in any way12thread-safe!1314Basic Testing15-------------1617The basic functionality for testing Click applications is the18:class:`CliRunner` which can invoke commands as command line scripts. The19:meth:`CliRunner.invoke` method runs the command line script in isolation20and captures the output as both bytes and binary data.2122The return value is a :class:`Result` object, which has the captured output23data, exit code, and optional exception attached:2425.. code-block:: python26 :caption: hello.py2728 import click2930 @click.command()31 @click.argument('name')32 def hello(name):33 click.echo(f'Hello {name}!')3435.. code-block:: python36 :caption: test_hello.py3738 from click.testing import CliRunner39 from hello import hello4041 def test_hello_world():42 runner = CliRunner()43 result = runner.invoke(hello, ['Peter'])44 assert result.exit_code == 045 assert result.output == 'Hello Peter!\n'4647For subcommand testing, a subcommand name must be specified in the `args` parameter of :meth:`CliRunner.invoke` method:4849.. code-block:: python50 :caption: sync.py5152 import click5354 @click.group()55 @click.option('--debug/--no-debug', default=False)56 def cli(debug):57 click.echo(f"Debug mode is {'on' if debug else 'off'}")5859 @cli.command()60 def sync():61 click.echo('Syncing')6263.. code-block:: python64 :caption: test_sync.py6566 from click.testing import CliRunner67 from sync import cli6869 def test_sync():70 runner = CliRunner()71 result = runner.invoke(cli, ['--debug', 'sync'])72 assert result.exit_code == 073 assert 'Debug mode is on' in result.output74 assert 'Syncing' in result.output7576Additional keyword arguments passed to ``.invoke()`` will be used to construct the initial Context object.77For example, if you want to run your tests against a fixed terminal width you can use the following::7879 runner = CliRunner()80 result = runner.invoke(cli, ['--debug', 'sync'], terminal_width=60)8182File System Isolation83---------------------8485For basic command line tools with file system operations, the86:meth:`CliRunner.isolated_filesystem` method is useful for setting the87current working directory to a new, empty folder.8889.. code-block:: python90 :caption: cat.py9192 import click9394 @click.command()95 @click.argument('f', type=click.File())96 def cat(f):97 click.echo(f.read())9899.. code-block:: python100 :caption: test_cat.py101102 from click.testing import CliRunner103 from cat import cat104105 def test_cat():106 runner = CliRunner()107 with runner.isolated_filesystem():108 with open('hello.txt', 'w') as f:109 f.write('Hello World!')110111 result = runner.invoke(cat, ['hello.txt'])112 assert result.exit_code == 0113 assert result.output == 'Hello World!\n'114115Pass ``temp_dir`` to control where the temporary directory is created.116The directory will not be removed by Click in this case. This is useful117to integrate with a framework like Pytest that manages temporary files.118119.. code-block:: python120121 def test_keep_dir(tmp_path):122 runner = CliRunner()123124 with runner.isolated_filesystem(temp_dir=tmp_path) as td:125 ...126127128Input Streams129-------------130131The test wrapper can also be used to provide input data for the input132stream (stdin). This is very useful for testing prompts, for instance:133134.. code-block:: python135 :caption: prompt.py136137 import click138139 @click.command()140 @click.option('--foo', prompt=True)141 def prompt(foo):142 click.echo(f"foo={foo}")143144.. code-block:: python145 :caption: test_prompt.py146147 from click.testing import CliRunner148 from prompt import prompt149150 def test_prompts():151 runner = CliRunner()152 result = runner.invoke(prompt, input='wau wau\n')153 assert not result.exception154 assert result.output == 'Foo: wau wau\nfoo=wau wau\n'155156Note that prompts will be emulated so that they write the input data to157the output stream as well. If hidden input is expected then this158obviously does not happen.