test_testing.py
This file
Path
tests/test_testing.py
79 lines · 2.2 KiB · Python
Last changed by
Added docs for input stream testing.Armin Ronacherbefore adoption3e51448
History of this fileTake it
Added tests for stream wrapping.Armin Ronacherbefore adoption15f0e22
lines 1–57why, one step back1import click23from click.testing import CliRunner45from click._compat import PY267# Use the most reasonable io that users would use for the python version.8if PY2:9 from cStringIO import StringIO as ReasonableBytesIO10else:11 from io import BytesIO as ReasonableBytesIO121314def test_runner():15 @click.command()16 def test():17 i = click.get_binary_stream('stdin')18 o = click.get_binary_stream('stdout')19 while 1:20 chunk = i.read(4096)21 if not chunk:22 break23 o.write(chunk)24 o.flush()2526 runner = CliRunner()27 result = runner.invoke(test, input='Hello World!\n')28 assert not result.exception29 assert result.output == 'Hello World!\n'3031 runner = CliRunner(echo_stdin=True)32 result = runner.invoke(test, input='Hello World!\n')33 assert not result.exception34 assert result.output == 'Hello World!\nHello World!\n'353637def test_runner_with_stream():38 @click.command()39 def test():40 i = click.get_binary_stream('stdin')41 o = click.get_binary_stream('stdout')42 while 1:43 chunk = i.read(4096)44 if not chunk:45 break46 o.write(chunk)47 o.flush()4849 runner = CliRunner()50 result = runner.invoke(test, input=ReasonableBytesIO(b'Hello World!\n'))51 assert not result.exception52 assert result.output == 'Hello World!\n'5354 runner = CliRunner(echo_stdin=True)55 result = runner.invoke(test, input=ReasonableBytesIO(b'Hello World!\n'))56 assert not result.exception57 assert result.output == 'Hello World!\nHello World!\n'Added tests for promptsArmin Ronacherbefore adoptionb3965e8
lines 58–66why, one step back585960def test_prompts():61 @click.command()62 @click.option('--foo', prompt=True)63 def test(foo):64 click.echo('foo=%s' % foo)6566 runner = CliRunner()Added docs for input stream testing.Armin Ronacherbefore adoption3e51448
line 67why, one step back67 result = runner.invoke(test, input='wau wau\n')Added tests for promptsArmin Ronacherbefore adoptionb3965e8
lines 68–76why, one step back68 assert not result.exception69 assert result.output == 'Foo: wau wau\nfoo=wau wau\n'7071 @click.command()72 @click.option('--foo', prompt=True, hide_input=True)73 def test(foo):74 click.echo('foo=%s' % foo)7576 runner = CliRunner()Added docs for input stream testing.Armin Ronacherbefore adoption3e51448
line 77why, one step back77 result = runner.invoke(test, input='wau wau\n')Added tests for promptsArmin Ronacherbefore adoptionb3965e8
lines 78–79why, one step back78 assert not result.exception79 assert result.output == 'Foo: \nfoo=wau wau\n'