Skip to content
Myr

test_testing.py

This file

Path

tests/test_testing.py

289 lines · 7.1 KiB · Python

Last changed by

Merge pull request #1098 from sirosen/non-standalone-exitDavid Lordbefore adoption8df9a6b

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    @click.pass_context158    def cli_string_ctx_exit(ctx):159        click.echo('hello world')160        ctx.exit('error')161162    @click.command()163    def cli_int():164        click.echo('hello world')165        sys.exit(1)166167    @click.command()168    @click.pass_context169    def cli_int_ctx_exit(ctx):170        click.echo('hello world')171        ctx.exit(1)172173    @click.command()174    def cli_float():175        click.echo('hello world')176        sys.exit(1.0)177178    @click.command()179    @click.pass_context180    def cli_float_ctx_exit(ctx):181        click.echo('hello world')182        ctx.exit(1.0)183184    @click.command()185    def cli_no_error():186        click.echo('hello world')187188    runner = CliRunner()189190    result = runner.invoke(cli_string)191    assert result.exit_code == 1192    assert result.output == 'hello world\nerror\n'193194    result = runner.invoke(cli_string_ctx_exit)195    assert result.exit_code == 1196    assert result.output == 'hello world\nerror\n'197198    result = runner.invoke(cli_int)199    assert result.exit_code == 1200    assert result.output == 'hello world\n'201202    result = runner.invoke(cli_int_ctx_exit)203    assert result.exit_code == 1204    assert result.output == 'hello world\n'205206    result = runner.invoke(cli_float)207    assert result.exit_code == 1208    assert result.output == 'hello world\n1.0\n'209210    result = runner.invoke(cli_float_ctx_exit)211    assert result.exit_code == 1212    assert result.output == 'hello world\n1.0\n'213214    result = runner.invoke(cli_no_error)215    assert result.exit_code == 0216    assert result.output == 'hello world\n'217218219def test_env():220    @click.command()221    def cli_env():222        click.echo('ENV=%s' % os.environ['TEST_CLICK_ENV'])223224    runner = CliRunner()225226    env_orig = dict(os.environ)227    env = dict(env_orig)228    assert 'TEST_CLICK_ENV' not in env229    env['TEST_CLICK_ENV'] = 'some_value'230    result = runner.invoke(cli_env, env=env)231    assert result.exit_code == 0232    assert result.output == 'ENV=some_value\n'233234    assert os.environ == env_orig235236237def test_stderr():238    @click.command()239    def cli_stderr():240        click.echo("stdout")241        click.echo("stderr", err=True)242243    runner = CliRunner(mix_stderr=False)244245    result = runner.invoke(cli_stderr)246247    assert result.output == 'stdout\n'248    assert result.stdout == 'stdout\n'249    assert result.stderr == 'stderr\n'250251    runner_mix = CliRunner(mix_stderr=True)252    result_mix = runner_mix.invoke(cli_stderr)253254    assert result_mix.output == 'stdout\nstderr\n'255    assert result_mix.stdout == 'stdout\nstderr\n'256257    with pytest.raises(ValueError):258        result_mix.stderr259260261@pytest.mark.parametrize('args, expected_output', [262    (None, 'bar\n'),263    ([], 'bar\n'),264    ('', 'bar\n'),265    (['--foo', 'one two'], 'one two\n'),266    ('--foo "one two"', 'one two\n'),267])268def test_args(args, expected_output):269270    @click.command()271    @click.option('--foo', default='bar')272    def cli_args(foo):273        click.echo(foo)274275    runner = CliRunner()276    result = runner.invoke(cli_args, args=args)277    assert result.exit_code == 0278    assert result.output == expected_output279280281def test_setting_prog_name_in_extra():282    @click.command()283    def cli():284        click.echo("ok")285286    runner = CliRunner()287    result = runner.invoke(cli, prog_name="foobar")288    assert not result.exception289    assert result.output == 'ok\n'