testing.rst
This file
Path
docs/testing.rst
118 lines · 3.7 KiB
Last changed by
Merge pull request #1084 from simonw/patch-2Dan Sullybefore adoption7463db5
History of this fileTake it
1Testing 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.2425Example::2627 import click28 from click.testing import CliRunner2930 def test_hello_world():31 @click.command()32 @click.argument('name')33 def hello(name):34 click.echo('Hello %s!' % name)3536 runner = CliRunner()37 result = runner.invoke(hello, ['Peter'])38 assert result.exit_code == 039 assert result.output == 'Hello Peter!\n'4041For subcommand testing, a subcommand name must be specified in the `args` parameter of :meth:`CliRunner.invoke` method.4243Example::4445 import click46 from click.testing import CliRunner47 48 def test_sync():49 @click.group()50 @click.option('--debug/--no-debug', default=False)51 def cli(debug):52 click.echo('Debug mode is %s' % ('on' if debug else 'off')) 53 54 @cli.command()55 def sync():56 click.echo('Syncing')57 58 runner = CliRunner()59 result = runner.invoke(cli, ['--debug', 'sync'])60 assert result.exit_code == 061 assert 'Debug mode is on' in result.output62 assert 'Syncing' in result.output6364Additional keyword arguments passed to ``.invoke()`` will be used to construct the initial Context object. For example, if you want to run your tests against a fixed terminal width you can use the following::6566 runner = CliRunner()67 result = runner.invoke(cli, ['--debug', 'sync'], terminal_width=60)6869File System Isolation70---------------------7172For basic command line tools that want to operate with the file system, the73:meth:`CliRunner.isolated_filesystem` method comes in useful which sets up74an empty folder and changes the current working directory to.7576Example::7778 import click79 from click.testing import CliRunner8081 def test_cat():82 @click.command()83 @click.argument('f', type=click.File())84 def cat(f):85 click.echo(f.read())8687 runner = CliRunner()88 with runner.isolated_filesystem():89 with open('hello.txt', 'w') as f:90 f.write('Hello World!')9192 result = runner.invoke(cat, ['hello.txt'])93 assert result.exit_code == 094 assert result.output == 'Hello World!\n'9596Input Streams97-------------9899The test wrapper can also be used to provide input data for the input100stream (stdin). This is very useful for testing prompts, for instance::101102 import click103 from click.testing import CliRunner104105 def test_prompts():106 @click.command()107 @click.option('--foo', prompt=True)108 def test(foo):109 click.echo('foo=%s' % foo)110111 runner = CliRunner()112 result = runner.invoke(test, input='wau wau\n')113 assert not result.exception114 assert result.output == 'Foo: wau wau\nfoo=wau wau\n'115116Note that prompts will be emulated so that they write the input data to117the output stream as well. If hidden input is expected then this118obviously does not happen.