Skip to content
Myr

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 file

Take it

1import 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'585960def test_prompts():61    @click.command()62    @click.option('--foo', prompt=True)63    def test(foo):64        click.echo('foo=%s' % foo)6566    runner = CliRunner()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()72    @click.option('--foo', prompt=True, hide_input=True)73    def test(foo):74        click.echo('foo=%s' % foo)7576    runner = CliRunner()77    result = runner.invoke(test, input='wau wau\n')78    assert not result.exception79    assert result.output == 'Foo: \nfoo=wau wau\n'808182def 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'