Skip to content
Myr

test_testing.py

This file

Path

tests/test_testing.py

185 lines · 4.5 KiB · Python

Last changed by

Merge pull request #363 from torfuspolymorphus/fix_362Markus Unterwaditzerbefore adoption189d616

History of this file

Take it

1import sys23import pytest4import click56from click.testing import CliRunner78from click._compat import PY2910# Use the most reasonable io that users would use for the python version.11if PY2:12    from cStringIO import StringIO as ReasonableBytesIO13else:14    from io import BytesIO as ReasonableBytesIO151617def test_runner():18    @click.command()19    def test():20        i = click.get_binary_stream('stdin')21        o = click.get_binary_stream('stdout')22        while 1:23            chunk = i.read(4096)24            if not chunk:25                break26            o.write(chunk)27            o.flush()2829    runner = CliRunner()30    result = runner.invoke(test, input='Hello World!\n')31    assert not result.exception32    assert result.output == 'Hello World!\n'3334    runner = CliRunner(echo_stdin=True)35    result = runner.invoke(test, input='Hello World!\n')36    assert not result.exception37    assert result.output == 'Hello World!\nHello World!\n'383940def test_runner_with_stream():41    @click.command()42    def test():43        i = click.get_binary_stream('stdin')44        o = click.get_binary_stream('stdout')45        while 1:46            chunk = i.read(4096)47            if not chunk:48                break49            o.write(chunk)50            o.flush()5152    runner = CliRunner()53    result = runner.invoke(test, input=ReasonableBytesIO(b'Hello World!\n'))54    assert not result.exception55    assert result.output == 'Hello World!\n'5657    runner = CliRunner(echo_stdin=True)58    result = runner.invoke(test, input=ReasonableBytesIO(b'Hello World!\n'))59    assert not result.exception60    assert result.output == 'Hello World!\nHello World!\n'616263def test_prompts():64    @click.command()65    @click.option('--foo', prompt=True)66    def test(foo):67        click.echo('foo=%s' % foo)6869    runner = CliRunner()70    result = runner.invoke(test, input='wau wau\n')71    assert not result.exception72    assert result.output == 'Foo: wau wau\nfoo=wau wau\n'7374    @click.command()75    @click.option('--foo', prompt=True, hide_input=True)76    def test(foo):77        click.echo('foo=%s' % foo)7879    runner = CliRunner()80    result = runner.invoke(test, input='wau wau\n')81    assert not result.exception82    assert result.output == 'Foo: \nfoo=wau wau\n'838485def test_getchar():86    @click.command()87    def continue_it():88        click.echo(click.getchar())8990    runner = CliRunner()91    result = runner.invoke(continue_it, input='y')92    assert not result.exception93    assert result.output == 'y\n'949596def test_catch_exceptions():97    class CustomError(Exception):98        pass99100    @click.command()101    def cli():102        raise CustomError(1)103104    runner = CliRunner()105106    result = runner.invoke(cli)107    assert isinstance(result.exception, CustomError)108    assert type(result.exc_info) is tuple109    assert len(result.exc_info) == 3110111    with pytest.raises(CustomError):112        runner.invoke(cli, catch_exceptions=False)113114    CustomError = SystemExit115116    result = runner.invoke(cli)117    assert result.exit_code == 1118119120def test_with_color():121    @click.command()122    def cli():123        click.secho('hello world', fg='blue')124125    runner = CliRunner()126127    result = runner.invoke(cli)128    assert result.output == 'hello world\n'129    assert not result.exception130131    result = runner.invoke(cli, color=True)132    assert result.output == click.style('hello world', fg='blue') + '\n'133    assert not result.exception134135136def test_with_color_but_pause_not_blocking():137    @click.command()138    def cli():139        click.pause()140141    runner = CliRunner()142    result = runner.invoke(cli, color=True)143    assert not result.exception144    assert result.output == ''145146147def test_exit_code_and_output_from_sys_exit():148    # See issue #362149    @click.command()150    def cli_string():151        click.echo('hello world')152        sys.exit('error')153154    @click.command()155    def cli_int():156        click.echo('hello world')157        sys.exit(1)158159    @click.command()160    def cli_float():161        click.echo('hello world')162        sys.exit(1.0)163164    @click.command()165    def cli_no_error():166        click.echo('hello world')167168    runner = CliRunner()169170    result = runner.invoke(cli_string)171    assert result.exit_code == 1172    assert result.output == 'hello world\nerror\n'173174    result = runner.invoke(cli_int)175    assert result.exit_code == 1176    assert result.output == 'hello world\n'177178    result = runner.invoke(cli_float)179    assert result.exit_code == 1180    assert result.output == 'hello world\n1.0\n'181182    result = runner.invoke(cli_no_error)183    assert result.exit_code == 0184    assert result.output == 'hello world\n'185