Skip to content
Myr

test_testing.py

This file

Path

tests/test_testing.py

259 lines · 6.3 KiB · Python

Last changed by

Merge branch 'master' into upstreamJosiahDubbefore adoptiona4b6bbd

History of this file

Take it

1import os2import sys34import pytest5import click67from click.testing import CliRunner89from click._compat import PY2, WIN1011# Use the most reasonable io that users would use for the python version.12if PY2:13    from cStringIO import StringIO as ReasonableBytesIO14else:15    from io import BytesIO as ReasonableBytesIO161718def test_runner():19    @click.command()20    def test():21        i = click.get_binary_stream('stdin')22        o = click.get_binary_stream('stdout')23        while 1:24            chunk = i.read(4096)25            if not chunk:26                break27            o.write(chunk)28            o.flush()2930    runner = CliRunner()31    result = runner.invoke(test, input='Hello World!\n')32    assert not result.exception33    assert result.output == 'Hello World!\n'3435    runner = CliRunner(echo_stdin=True)36    result = runner.invoke(test, input='Hello World!\n')37    assert not result.exception38    assert result.output == 'Hello World!\nHello World!\n'394041def test_runner_with_stream():42    @click.command()43    def test():44        i = click.get_binary_stream('stdin')45        o = click.get_binary_stream('stdout')46        while 1:47            chunk = i.read(4096)48            if not chunk:49                break50            o.write(chunk)51            o.flush()5253    runner = CliRunner()54    result = runner.invoke(test, input=ReasonableBytesIO(b'Hello World!\n'))55    assert not result.exception56    assert result.output == 'Hello World!\n'5758    runner = CliRunner(echo_stdin=True)59    result = runner.invoke(test, input=ReasonableBytesIO(b'Hello World!\n'))60    assert not result.exception61    assert result.output == 'Hello World!\nHello World!\n'626364def test_prompts():65    @click.command()66    @click.option('--foo', prompt=True)67    def test(foo):68        click.echo('foo=%s' % foo)6970    runner = CliRunner()71    result = runner.invoke(test, input='wau wau\n')72    assert not result.exception73    assert result.output == 'Foo: wau wau\nfoo=wau wau\n'7475    @click.command()76    @click.option('--foo', prompt=True, hide_input=True)77    def test(foo):78        click.echo('foo=%s' % foo)7980    runner = CliRunner()81    result = runner.invoke(test, input='wau wau\n')82    assert not result.exception83    assert result.output == 'Foo: \nfoo=wau wau\n'848586def test_getchar():87    @click.command()88    def continue_it():89        click.echo(click.getchar())9091    runner = CliRunner()92    result = runner.invoke(continue_it, input='y')93    assert not result.exception94    assert result.output == 'y\n'959697def test_catch_exceptions():98    class CustomError(Exception):99        pass100101    @click.command()102    def cli():103        raise CustomError(1)104105    runner = CliRunner()106107    result = runner.invoke(cli)108    assert isinstance(result.exception, CustomError)109    assert type(result.exc_info) is tuple110    assert len(result.exc_info) == 3111112    with pytest.raises(CustomError):113        runner.invoke(cli, catch_exceptions=False)114115    CustomError = SystemExit116117    result = runner.invoke(cli)118    assert result.exit_code == 1119120121@pytest.mark.skipif(WIN, reason='Test does not make sense on Windows.')122def test_with_color():123    @click.command()124    def cli():125        click.secho('hello world', fg='blue')126127    runner = CliRunner()128129    result = runner.invoke(cli)130    assert result.output == 'hello world\n'131    assert not result.exception132133    result = runner.invoke(cli, color=True)134    assert result.output == click.style('hello world', fg='blue') + '\n'135    assert not result.exception136137138def test_with_color_but_pause_not_blocking():139    @click.command()140    def cli():141        click.pause()142143    runner = CliRunner()144    result = runner.invoke(cli, color=True)145    assert not result.exception146    assert result.output == ''147148149def test_exit_code_and_output_from_sys_exit():150    # See issue #362151    @click.command()152    def cli_string():153        click.echo('hello world')154        sys.exit('error')155156    @click.command()157    def cli_int():158        click.echo('hello world')159        sys.exit(1)160161    @click.command()162    def cli_float():163        click.echo('hello world')164        sys.exit(1.0)165166    @click.command()167    def cli_no_error():168        click.echo('hello world')169170    runner = CliRunner()171172    result = runner.invoke(cli_string)173    assert result.exit_code == 1174    assert result.output == 'hello world\nerror\n'175176    result = runner.invoke(cli_int)177    assert result.exit_code == 1178    assert result.output == 'hello world\n'179180    result = runner.invoke(cli_float)181    assert result.exit_code == 1182    assert result.output == 'hello world\n1.0\n'183184    result = runner.invoke(cli_no_error)185    assert result.exit_code == 0186    assert result.output == 'hello world\n'187188189def test_env():190    @click.command()191    def cli_env():192        click.echo('ENV=%s' % os.environ['TEST_CLICK_ENV'])193194    runner = CliRunner()195196    env_orig = dict(os.environ)197    env = dict(env_orig)198    assert 'TEST_CLICK_ENV' not in env199    env['TEST_CLICK_ENV'] = 'some_value'200    result = runner.invoke(cli_env, env=env)201    assert result.exit_code == 0202    assert result.output == 'ENV=some_value\n'203204    assert os.environ == env_orig205206207def test_stderr():208    @click.command()209    def cli_stderr():210        click.echo("stdout")211        click.echo("stderr", err=True)212213    runner = CliRunner(mix_stderr=False)214215    result = runner.invoke(cli_stderr)216217    assert result.output == 'stdout\n'218    assert result.stdout == 'stdout\n'219    assert result.stderr == 'stderr\n'220221    runner_mix = CliRunner(mix_stderr=True)222    result_mix = runner_mix.invoke(cli_stderr)223224    assert result_mix.output == 'stdout\nstderr\n'225    assert result_mix.stdout == 'stdout\nstderr\n'226227    with pytest.raises(ValueError):228        result_mix.stderr229230231@pytest.mark.parametrize('args, expected_output', [232    (None, 'bar\n'),233    ([], 'bar\n'),234    ('', 'bar\n'),235    (['--foo', 'one two'], 'one two\n'),236    ('--foo "one two"', 'one two\n'),237])238def test_args(args, expected_output):239240    @click.command()241    @click.option('--foo', default='bar')242    def cli_args(foo):243        click.echo(foo)244245    runner = CliRunner()246    result = runner.invoke(cli_args, args=args)247    assert result.exit_code == 0248    assert result.output == expected_output249250251def test_setting_prog_name_in_extra():252    @click.command()253    def cli():254        click.echo("ok")255256    runner = CliRunner()257    result = runner.invoke(cli, prog_name="foobar")258    assert not result.exception259    assert result.output == 'ok\n'