Skip to content
Myr

Added docs for input stream testing.

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Armin Ronacher · committed by Armin Ronacher · parent b3965e8 · Browse files · 2 files+27 added−3 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+27 added−3 removed

  • tests:+2 added−2 removed
  • other:+25 added−1 removed

2 files+27 added−3 removed

modified:docs/testing.rst+25 added-1 removed
48 unmodified lines
4950 import click51 from click.testing import CliRunner5253 def test_hello_world():53 def test_cat():54 @click.command()55 @click.argument('f', type=click.File())56 def cat(f):57 click.echo(f.read())
5 unmodified lines
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 input72stream (stdin). This is very useful for testing prompts for instance::7374 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.
modified:tests/test_testing.py+2 added-2 removed
62 unmodified lines
63 def test(foo):64 click.echo('foo=%s' % foo)6566 runner = CliRunner()67 result = runner.invoke(test, input='wau wau')67 result = runner.invoke(test, input='wau wau\n')68 assert not result.exception69 assert result.output == 'Foo: wau wau\nfoo=wau wau\n'7071 @click.command()
1 unmodified line
73 def test(foo):74 click.echo('foo=%s' % foo)7576 runner = CliRunner()77 result = runner.invoke(test, input='wau wau')77 result = runner.invoke(test, input='wau wau\n')78 assert not result.exception79 assert result.output == 'Foo: \nfoo=wau wau\n'