Skip to content
Myr

testing.rst

This file

Path

docs/testing.rst

113 lines · 3.4 KiB

Last changed by

Merge pull request #378 from aspyatkin/patch-1Markus Unterwaditzerbefore adoptiona0d1ab8

History of this file

Take it

The walk stopped after 200 commits: lines older than 5cae326 point at it.

Merge pull request #110 from krockode/patch-1Armin Ronacherbefore adoption5cae326

lines 1–5why, one step back
1Testing Click Applications
2==========================34.. currentmodule:: click.testing5

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

line 6why, one step back
6For basic testing, Click provides the :mod:`click.testing` module which

Merge pull request #110 from krockode/patch-1Armin Ronacherbefore adoption5cae326

lines 7–9why, one step back
7provides test functionality that helps you invoke command line
8applications and check their behavior.9

Documentation fixesKevin Yapbefore adoption67523af

lines 10–12why, one step back
10These tools should really only be used for testing as they change
11the entire interpreter state for simplicity and are not in any way12thread-safe!

Merge pull request #110 from krockode/patch-1Armin Ronacherbefore adoption5cae326

lines 13–16why, one step back
13
14Basic Testing15-------------16

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

line 17why, one step back
17The basic functionality for testing Click applications is the

Merge pull request #110 from krockode/patch-1Armin Ronacherbefore adoption5cae326

lines 18–21why, one step back
18:class:`CliRunner` which can invoke commands as command line scripts. The
19:meth:`CliRunner.invoke` method runs the command line script in isolation20and captures the output as both bytes and binary data.21

Documentation fixesKevin Yapbefore adoption67523af

lines 22–23why, one step back
22The return value is a :class:`Result` object, which has the captured output
23data, exit code, and optional exception attached.

Merge pull request #110 from krockode/patch-1Armin Ronacherbefore adoption5cae326

lines 24–40why, one step back
24
25Example::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'40

Merge pull request #378 from aspyatkin/patch-1Markus Unterwaditzerbefore adoptiona0d1ab8

lines 41–63why, one step back
41For 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.output63

Merge pull request #110 from krockode/patch-1Armin Ronacherbefore adoption5cae326

lines 64–83why, one step back
64File System Isolation
65---------------------6667For basic command line tools that want to operate with the file system, the68:meth:`CliRunner.isolated_filesystem` method comes in useful which sets up69an empty folder and changes the current working directory to.7071Example::7273 import click74 from click.testing import CliRunner7576 def test_cat():77 @click.command()78 @click.argument('f', type=click.File())79 def cat(f):80 click.echo(f.read())8182 runner = CliRunner()83 with runner.isolated_filesystem():

Merge pull request #307 from bionikspoon/patch-3Markus Unterwaditzerbefore adoptionc3a6482

line 84why, one step back
84 with open('hello.txt', 'w') as f:

Merge pull request #110 from krockode/patch-1Armin Ronacherbefore adoption5cae326

lines 85–113why, one step back
85 f.write('Hello World!')
8687 result = runner.invoke(cat, ['hello.txt'])88 assert result.exit_code == 089 assert result.output == 'Hello World!\n'9091Input Streams92-------------9394The test wrapper can also be used to provide input data for the input95stream (stdin). This is very useful for testing prompts, for instance::9697 import click98 from click.testing import CliRunner99100 def test_prompts():101 @click.command()102 @click.option('--foo', prompt=True)103 def test(foo):104 click.echo('foo=%s' % foo)105106 runner = CliRunner()107 result = runner.invoke(test, input='wau wau\n')108 assert not result.exception109 assert result.output == 'Foo: wau wau\nfoo=wau wau\n'110111Note that prompts will be emulated so that they write the input data to112the output stream as well. If hidden input is expected then this113obviously does not happen.