test_testing.py
This file
Path
tests/test_testing.py
90 lines · 2.4 KiB · Python
Last changed by
Added getchar()Armin Ronacherbefore adoption88c97ef
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'Added getchar()Armin Ronacherbefore adoption88c97ef
lines 80–90why, one step back808182def test_getchar():83 @click.command()84 def continue_it():85 click.echo(click.getchar())8687 runner = CliRunner()88 result = runner.invoke(continue_it, input='y')89 assert not result.exception90 assert result.output == 'y\n'