Skip to content
Myr

testing.rst

This file

Path

docs/testing.rst

158 lines · 4.3 KiB

Last changed by

Merge pull request #1715 from MLH-Fellowship/maaz/issue-395David Lordbefore adoptionb8a33bf

History of this file

Take it

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

Merge pull request #1098 from sirosen/non-standalone-exitDavid Lordbefore adoption8df9a6b

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 branch '7.x'David Lordbefore adoptiona438322

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

Merge pull request #1098 from sirosen/non-standalone-exitDavid Lordbefore adoption8df9a6b

line 24why, one step back
24

Merge branch '7.x'David Lordbefore adoptiona438322

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

Merge pull request #1098 from sirosen/non-standalone-exitDavid Lordbefore adoption8df9a6b

line 27why, one step back
27

Merge branch '7.x'David Lordbefore adoptiona438322

line 28why, one step back
28 import click

Merge pull request #1098 from sirosen/non-standalone-exitDavid Lordbefore adoption8df9a6b

line 29why, one step back
29

Merge branch '7.x'David Lordbefore adoptiona438322

lines 30–32why, one step back
30 @click.command()
31 @click.argument('name')32 def hello(name):

Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9

line 33why, one step back
33 click.echo(f'Hello {name}!')

Merge pull request #1098 from sirosen/non-standalone-exitDavid Lordbefore adoption8df9a6b

line 34why, one step back
34

Merge branch '7.x'David Lordbefore adoptiona438322

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

Merge pull request #1098 from sirosen/non-standalone-exitDavid Lordbefore adoption8df9a6b

line 37why, one step back
37

Merge branch '7.x'David Lordbefore adoptiona438322

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

Merge pull request #1098 from sirosen/non-standalone-exitDavid Lordbefore adoption8df9a6b

line 40why, one step back
40

Merge branch '7.x'David Lordbefore adoptiona438322

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'

Merge pull request #1098 from sirosen/non-standalone-exitDavid Lordbefore adoption8df9a6b

line 46why, one step back
46

Merge branch '7.x'David Lordbefore adoptiona438322

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

Merge pull request #1098 from sirosen/non-standalone-exitDavid Lordbefore adoption8df9a6b

line 48why, one step back
48

Merge branch '7.x'David Lordbefore adoptiona438322

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

Merge pull request #1098 from sirosen/non-standalone-exitDavid Lordbefore adoption8df9a6b

line 51why, one step back
51

Merge branch '7.x'David Lordbefore adoptiona438322

lines 52–56why, one step back
52 import click
5354 @click.group()55 @click.option('--debug/--no-debug', default=False)56 def cli(debug):

Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9

line 57why, one step back
57 click.echo(f"Debug mode is {'on' if debug else 'off'}")

Merge branch '7.x'David Lordbefore adoptiona438322

lines 58–78why, one step back
58
59 @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 #1098 from sirosen/non-standalone-exitDavid Lordbefore adoption8df9a6b

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

Merge branch '7.x'David Lordbefore adoption2269786

lines 85–87why, one step back
85For basic command line tools with file system operations, the
86:meth:`CliRunner.isolated_filesystem` method is useful for setting the87current working directory to a new, empty folder.

Merge pull request #1098 from sirosen/non-standalone-exitDavid Lordbefore adoption8df9a6b

line 88why, one step back
88

Merge branch '7.x'David Lordbefore adoptiona438322

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

Merge pull request #1098 from sirosen/non-standalone-exitDavid Lordbefore adoption8df9a6b

line 91why, one step back
91

Merge branch '7.x'David Lordbefore adoptiona438322

line 92why, one step back
92 import click

Merge pull request #1098 from sirosen/non-standalone-exitDavid Lordbefore adoption8df9a6b

line 93why, one step back
93

Merge branch '7.x'David Lordbefore adoptiona438322

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())

Merge pull request #1098 from sirosen/non-standalone-exitDavid Lordbefore adoption8df9a6b

line 98why, one step back
98

Merge branch '7.x'David Lordbefore adoptiona438322

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

Merge pull request #1098 from sirosen/non-standalone-exitDavid Lordbefore adoption8df9a6b

line 101why, one step back
101

Merge branch '7.x'David Lordbefore adoptiona438322

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

Merge pull request #1098 from sirosen/non-standalone-exitDavid Lordbefore adoption8df9a6b

line 104why, one step back
104

Merge branch '7.x'David Lordbefore adoptiona438322

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

Merge pull request #1715 from MLH-Fellowship/maaz/issue-395David Lordbefore adoptionb8a33bf

lines 115–127why, one step back
115Pass ``temp_dir`` to control where the temporary directory is created.
116The directory will not be removed by Click in this case. This is useful117to integrate with a framework like Pytest that manages temporary files.118119.. code-block:: python120121 def test_keep_dir(tmp_path):122 runner = CliRunner()123124 with runner.isolated_filesystem(temp_dir=tmp_path) as td:125 ...126127

Merge pull request #1098 from sirosen/non-standalone-exitDavid Lordbefore adoption8df9a6b

lines 128–131why, one step back
128Input Streams
129-------------130131The test wrapper can also be used to provide input data for the input

Merge branch '7.x'David Lordbefore adoptiona438322

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

Merge pull request #1098 from sirosen/non-standalone-exitDavid Lordbefore adoption8df9a6b

line 133why, one step back
133

Merge branch '7.x'David Lordbefore adoptiona438322

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

Merge pull request #1098 from sirosen/non-standalone-exitDavid Lordbefore adoption8df9a6b

line 136why, one step back
136

Merge branch '7.x'David Lordbefore adoptiona438322

line 137why, one step back
137 import click

Merge pull request #1098 from sirosen/non-standalone-exitDavid Lordbefore adoption8df9a6b

line 138why, one step back
138

Merge branch '7.x'David Lordbefore adoptiona438322

lines 139–141why, one step back
139 @click.command()
140 @click.option('--foo', prompt=True)141 def prompt(foo):

Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9

line 142why, one step back
142 click.echo(f"foo={foo}")

Merge branch '7.x'David Lordbefore adoptiona438322

lines 143–154why, one step back
143
144.. code-block:: python145 :caption: test_prompt.py146147 from click.testing import CliRunner148 from prompt import prompt149150 def test_prompts():151 runner = CliRunner()152 result = runner.invoke(prompt, input='wau wau\n')153 assert not result.exception154 assert result.output == 'Foo: wau wau\nfoo=wau wau\n'

Merge pull request #1098 from sirosen/non-standalone-exitDavid Lordbefore adoption8df9a6b

lines 155–158why, one step back
155
156Note that prompts will be emulated so that they write the input data to157the output stream as well. If hidden input is expected then this158obviously does not happen.