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 fileTake it
The walk stopped after 200 commits: lines older than e539622 point at it.
Linked to termui exampleArmin Ronacherbefore adoptione539622
lines 1–5why, one step back1Testing Click Applications2==========================34.. currentmodule:: click.testing5Merge pull request #153 from iKevinY/click-renameArmin Ronacherbefore adoption327cfde
line 6why, one step back6For basic testing, Click provides the :mod:`click.testing` module whichLinked to termui exampleArmin Ronacherbefore adoptione539622
lines 7–9why, one step back7provides test functionality that helps you invoke command line8applications and check their behavior.9Documentation fixesKevin Yapbefore adoption67523af
lines 10–12why, one step back10These tools should really only be used for testing as they change11the entire interpreter state for simplicity and are not in any way12thread-safe!Linked to termui exampleArmin Ronacherbefore adoptione539622
lines 13–16why, one step back1314Basic Testing15-------------16Merge pull request #153 from iKevinY/click-renameArmin Ronacherbefore adoption327cfde
line 17why, one step back17The basic functionality for testing Click applications is theLinked to termui exampleArmin Ronacherbefore adoptione539622
lines 18–21why, one step back18: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.21Documentation fixesKevin Yapbefore adoption67523af
lines 22–23why, one step back22The return value is a :class:`Result` object, which has the captured output23data, exit code, and optional exception attached.Linked to termui exampleArmin Ronacherbefore adoptione539622
lines 24–43why, one step back2425Example::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---------------------43Merge pull request #109 from iKevinY/doc-fixesArmin Ronacherbefore adoptiond428e69
line 44why, one step back44For basic command line tools that want to operate with the file system, theLinked to termui exampleArmin Ronacherbefore adoptione539622
lines 45–60why, one step back45: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():Merge pull request #307 from bionikspoon/patch-3Markus Unterwaditzerbefore adoptionc3a6482
line 61why, one step back61 with open('hello.txt', 'w') as f:Linked to termui exampleArmin Ronacherbefore adoptione539622
lines 62–71why, one step back62 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 inputMerge pull request #109 from iKevinY/doc-fixesArmin Ronacherbefore adoptiond428e69
line 72why, one step back72stream (stdin). This is very useful for testing prompts, for instance::Linked to termui exampleArmin Ronacherbefore adoptione539622
lines 73–90why, one step back7374 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.