Skip to content
Myr

testing.rst

This file

Path

docs/testing.rst

145 lines · 3.9 KiB

Last changed by

Merge pull request #1330 from dposada/clarify-test-examplesDavid Lordbefore adoption073c9a4

History of this file

Take it

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

Update CHANGESArmin Ronacherbefore adoption7d1f4ab

lines 1–22why, one step back
1Testing Click Applications
2==========================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 output

Merge pull request #1330 from dposada/clarify-test-examplesDavid Lordbefore adoption073c9a4

line 23why, one step back
23data, exit code, and optional exception attached:

Update CHANGESArmin Ronacherbefore adoption7d1f4ab

line 24why, one step back
24

Merge pull request #1330 from dposada/clarify-test-examplesDavid Lordbefore adoption073c9a4

lines 25–26why, one step back
25.. code-block:: python
26 :caption: hello.py

Update CHANGESArmin Ronacherbefore adoption7d1f4ab

line 27why, one step back
27

Merge pull request #1330 from dposada/clarify-test-examplesDavid Lordbefore adoption073c9a4

line 28why, one step back
28 import click

Update CHANGESArmin Ronacherbefore adoption7d1f4ab

line 29why, one step back
29

Merge pull request #1330 from dposada/clarify-test-examplesDavid Lordbefore adoption073c9a4

lines 30–33why, one step back
30 @click.command()
31 @click.argument('name')32 def hello(name):33 click.echo('Hello %s!' % name)

Update CHANGESArmin Ronacherbefore adoption7d1f4ab

line 34why, one step back
34

Merge pull request #1330 from dposada/clarify-test-examplesDavid Lordbefore adoption073c9a4

lines 35–36why, one step back
35.. code-block:: python
36 :caption: test_hello.py

Update CHANGESArmin Ronacherbefore adoption7d1f4ab

line 37why, one step back
37

Merge pull request #1330 from dposada/clarify-test-examplesDavid Lordbefore adoption073c9a4

lines 38–39why, one step back
38 from click.testing import CliRunner
39 from hello import hello

Update CHANGESArmin Ronacherbefore adoption7d1f4ab

line 40why, one step back
40

Merge pull request #1330 from dposada/clarify-test-examplesDavid Lordbefore adoption073c9a4

lines 41–45why, one step back
41 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'

Update CHANGESArmin Ronacherbefore adoption7d1f4ab

line 46why, one step back
46

Merge pull request #1330 from dposada/clarify-test-examplesDavid Lordbefore adoption073c9a4

line 47why, one step back
47For subcommand testing, a subcommand name must be specified in the `args` parameter of :meth:`CliRunner.invoke` method:

Update CHANGESArmin Ronacherbefore adoption7d1f4ab

line 48why, one step back
48

Merge pull request #1330 from dposada/clarify-test-examplesDavid Lordbefore adoption073c9a4

lines 49–50why, one step back
49.. code-block:: python
50 :caption: sync.py

Merge pull request #1084 from simonw/patch-2Dan Sullybefore adoption7463db5

line 51why, one step back
51

Merge pull request #1330 from dposada/clarify-test-examplesDavid Lordbefore adoption073c9a4

lines 52–78why, one step back
52 import click
5354 @click.group()55 @click.option('--debug/--no-debug', default=False)56 def cli(debug):57 click.echo('Debug mode is %s' % ('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::78

Merge pull request #1084 from simonw/patch-2Dan Sullybefore adoption7463db5

lines 79–81why, one step back
79 runner = CliRunner()
80 result = runner.invoke(cli, ['--debug', 'sync'], terminal_width=60)81

Update CHANGESArmin Ronacherbefore adoption7d1f4ab

lines 82–86why, one step back
82File System Isolation
83---------------------8485For basic command line tools that want to operate with the file system, the86:meth:`CliRunner.isolated_filesystem` method comes in useful which sets up

Merge pull request #1330 from dposada/clarify-test-examplesDavid Lordbefore adoption073c9a4

line 87why, one step back
87an empty folder and changes the current working directory to:

Update CHANGESArmin Ronacherbefore adoption7d1f4ab

line 88why, one step back
88

Merge pull request #1330 from dposada/clarify-test-examplesDavid Lordbefore adoption073c9a4

lines 89–90why, one step back
89.. code-block:: python
90 :caption: cat.py

Update CHANGESArmin Ronacherbefore adoption7d1f4ab

line 91why, one step back
91

Merge pull request #1330 from dposada/clarify-test-examplesDavid Lordbefore adoption073c9a4

line 92why, one step back
92 import click

Update CHANGESArmin Ronacherbefore adoption7d1f4ab

line 93why, one step back
93

Merge pull request #1330 from dposada/clarify-test-examplesDavid Lordbefore adoption073c9a4

lines 94–97why, one step back
94 @click.command()
95 @click.argument('f', type=click.File())96 def cat(f):97 click.echo(f.read())

Update CHANGESArmin Ronacherbefore adoption7d1f4ab

line 98why, one step back
98

Merge pull request #1330 from dposada/clarify-test-examplesDavid Lordbefore adoption073c9a4

lines 99–100why, one step back
99.. code-block:: python
100 :caption: test_cat.py

Update CHANGESArmin Ronacherbefore adoption7d1f4ab

line 101why, one step back
101

Merge pull request #1330 from dposada/clarify-test-examplesDavid Lordbefore adoption073c9a4

lines 102–103why, one step back
102 from click.testing import CliRunner
103 from cat import cat

Update CHANGESArmin Ronacherbefore adoption7d1f4ab

line 104why, one step back
104

Merge pull request #1330 from dposada/clarify-test-examplesDavid Lordbefore adoption073c9a4

lines 105–114why, one step back
105 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'114

Update CHANGESArmin Ronacherbefore adoption7d1f4ab

lines 115–118why, one step back
115Input Streams
116-------------117118The test wrapper can also be used to provide input data for the input

Merge pull request #1330 from dposada/clarify-test-examplesDavid Lordbefore adoption073c9a4

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

Update CHANGESArmin Ronacherbefore adoption7d1f4ab

line 120why, one step back
120

Merge pull request #1330 from dposada/clarify-test-examplesDavid Lordbefore adoption073c9a4

lines 121–122why, one step back
121.. code-block:: python
122 :caption: prompt.py

Update CHANGESArmin Ronacherbefore adoption7d1f4ab

line 123why, one step back
123

Merge pull request #1330 from dposada/clarify-test-examplesDavid Lordbefore adoption073c9a4

line 124why, one step back
124 import click

Update CHANGESArmin Ronacherbefore adoption7d1f4ab

line 125why, one step back
125

Merge pull request #1330 from dposada/clarify-test-examplesDavid Lordbefore adoption073c9a4

lines 126–141why, one step back
126 @click.command()
127 @click.option('--foo', prompt=True)128 def prompt(foo):129 click.echo('foo=%s' % foo)130131.. code-block:: python132 :caption: test_prompt.py133134 from click.testing import CliRunner135 from prompt import prompt136137 def test_prompts():138 runner = CliRunner()139 result = runner.invoke(prompt, input='wau wau\n')140 assert not result.exception141 assert result.output == 'Foo: wau wau\nfoo=wau wau\n'

Update CHANGESArmin Ronacherbefore adoption7d1f4ab

lines 142–145why, one step back
142
143Note that prompts will be emulated so that they write the input data to144the output stream as well. If hidden input is expected then this145obviously does not happen.