Skip to content
Myr

test_testing.py

This file

Path

tests/test_testing.py

115 lines · 2.9 KiB · Python

Last changed by

Added exc_info to result.Armin Ronacherbefore adoptionf1a8460

History of this file

Take it

1import pytest2import click34from click.testing import CliRunner56from click._compat import PY278# Use the most reasonable io that users would use for the python version.9if PY2:10    from cStringIO import StringIO as ReasonableBytesIO11else:12    from io import BytesIO as ReasonableBytesIO131415def test_runner():16    @click.command()17    def test():18        i = click.get_binary_stream('stdin')19        o = click.get_binary_stream('stdout')20        while 1:21            chunk = i.read(4096)22            if not chunk:23                break24            o.write(chunk)25            o.flush()2627    runner = CliRunner()28    result = runner.invoke(test, input='Hello World!\n')29    assert not result.exception30    assert result.output == 'Hello World!\n'3132    runner = CliRunner(echo_stdin=True)33    result = runner.invoke(test, input='Hello World!\n')34    assert not result.exception35    assert result.output == 'Hello World!\nHello World!\n'363738def test_runner_with_stream():39    @click.command()40    def test():41        i = click.get_binary_stream('stdin')42        o = click.get_binary_stream('stdout')43        while 1:44            chunk = i.read(4096)45            if not chunk:46                break47            o.write(chunk)48            o.flush()4950    runner = CliRunner()51    result = runner.invoke(test, input=ReasonableBytesIO(b'Hello World!\n'))52    assert not result.exception53    assert result.output == 'Hello World!\n'5455    runner = CliRunner(echo_stdin=True)56    result = runner.invoke(test, input=ReasonableBytesIO(b'Hello World!\n'))57    assert not result.exception58    assert result.output == 'Hello World!\nHello World!\n'596061def test_prompts():62    @click.command()63    @click.option('--foo', prompt=True)64    def test(foo):65        click.echo('foo=%s' % foo)6667    runner = CliRunner()68    result = runner.invoke(test, input='wau wau\n')69    assert not result.exception70    assert result.output == 'Foo: wau wau\nfoo=wau wau\n'7172    @click.command()73    @click.option('--foo', prompt=True, hide_input=True)74    def test(foo):75        click.echo('foo=%s' % foo)7677    runner = CliRunner()78    result = runner.invoke(test, input='wau wau\n')79    assert not result.exception80    assert result.output == 'Foo: \nfoo=wau wau\n'818283def test_getchar():84    @click.command()85    def continue_it():86        click.echo(click.getchar())8788    runner = CliRunner()89    result = runner.invoke(continue_it, input='y')90    assert not result.exception91    assert result.output == 'y\n'929394def test_catch_exceptions():95    class CustomError(Exception):96        pass9798    @click.command()99    def cli():100        raise CustomError(1)101102    runner = CliRunner()103104    result = runner.invoke(cli)105    assert isinstance(result.exception, CustomError)106    assert type(result.exc_info) is tuple107    assert len(result.exc_info) == 3108109    with pytest.raises(CustomError):110        runner.invoke(cli, catch_exceptions=False)111112    CustomError = SystemExit113114    result = runner.invoke(cli)115    assert result.exit_code == 1