Skip to content
Myr

testing.rst

This file

Path

docs/testing.rst

90 lines · 2.7 KiB

Last changed by

Merge pull request #153 from iKevinY/click-renameArmin Ronacherbefore adoption327cfde

History of this file

Take 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'4041File System Isolation42---------------------4344For basic command line tools that want to operate with the file system, the45:meth:`CliRunner.isolated_filesystem` method comes in useful which sets up46an empty folder and changes the current working directory to.4748Example::4950    import click51    from click.testing import CliRunner5253    def test_cat():54        @click.command()55        @click.argument('f', type=click.File())56        def cat(f):57            click.echo(f.read())5859        runner = CliRunner()60        with runner.isolated_filesystem():61            with open('hello.txt') as f:62                f.write('Hello World!')6364            result = runner.invoke(cat, ['hello.txt'])65            assert result.exit_code == 066            assert result.output == 'Hello World!\n'6768Input Streams69-------------7071The test wrapper can also be used to provide input data for the input72stream (stdin).  This is very useful for testing prompts, for instance::7374    import click75    from click.testing import CliRunner7677    def test_prompts():78        @click.command()79        @click.option('--foo', prompt=True)80        def test(foo):81            click.echo('foo=%s' % foo)8283        runner = CliRunner()84        result = runner.invoke(test, input='wau wau\n')85        assert not result.exception86        assert result.output == 'Foo: wau wau\nfoo=wau wau\n'8788Note that prompts will be emulated so that they write the input data to89the output stream as well.  If hidden input is expected then this90obviously does not happen.