Skip to content
Myr

testing.rst

This file

Path

docs/testing.rst

90 lines · 2.7 KiB

Last changed by

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

History of this file

Take it

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

Customize required in arguments for nargs of -1Armin Ronacherbefore adoptionce1a418

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

Customize required in arguments for nargs of -1Armin Ronacherbefore adoptionce1a418

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!

Customize required in arguments for nargs of -1Armin Ronacherbefore adoptionce1a418

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

Customize required in arguments for nargs of -1Armin Ronacherbefore adoptionce1a418

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.

Customize required in arguments for nargs of -1Armin Ronacherbefore adoptionce1a418

lines 24–43why, 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'4041File System Isolation42---------------------43

Merge pull request #109 from iKevinY/doc-fixesArmin Ronacherbefore adoptiond428e69

line 44why, one step back
44For basic command line tools that want to operate with the file system, the

Customize required in arguments for nargs of -1Armin Ronacherbefore adoptionce1a418

lines 45–60why, one step back
45:meth:`CliRunner.isolated_filesystem` method comes in useful which sets up
46an 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():

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

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

Customize required in arguments for nargs of -1Armin Ronacherbefore adoptionce1a418

lines 62–71why, one step back
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 input

Merge pull request #109 from iKevinY/doc-fixesArmin Ronacherbefore adoptiond428e69

line 72why, one step back
72stream (stdin). This is very useful for testing prompts, for instance::

Customize required in arguments for nargs of -1Armin Ronacherbefore adoptionce1a418

lines 73–90why, one step back
73
74 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.