test_testing.py
This file
Path
tests/test_testing.py
301 lines · 7.3 KiB · Python
Last changed by
Merge pull request #1194 from Julian/stderr-really-separateDavid Lordbefore adoptioncc294de
History of this fileTake it
The walk stopped after 200 commits: lines older than 430c50a point at it.
Merge pull request #681 from pgkelley4/masterMarkus Unterwaditzerbefore adoption430c50a
lines 1–156why, one step back1import 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()Merge pull request #1098 from sirosen/non-standalone-exitDavid Lordbefore adoption8df9a6b
lines 157–162why, one step back157 @click.pass_context158 def cli_string_ctx_exit(ctx):159 click.echo('hello world')160 ctx.exit('error')161162 @click.command()Merge pull request #681 from pgkelley4/masterMarkus Unterwaditzerbefore adoption430c50a
lines 163–167why, one step back163 def cli_int():164 click.echo('hello world')165 sys.exit(1)166167 @click.command()Merge pull request #1098 from sirosen/non-standalone-exitDavid Lordbefore adoption8df9a6b
lines 168–173why, one step back168 @click.pass_context169 def cli_int_ctx_exit(ctx):170 click.echo('hello world')171 ctx.exit(1)172173 @click.command()Merge pull request #681 from pgkelley4/masterMarkus Unterwaditzerbefore adoption430c50a
lines 174–178why, one step back174 def cli_float():175 click.echo('hello world')176 sys.exit(1.0)177178 @click.command()Merge pull request #1098 from sirosen/non-standalone-exitDavid Lordbefore adoption8df9a6b
lines 179–184why, one step back179 @click.pass_context180 def cli_float_ctx_exit(ctx):181 click.echo('hello world')182 ctx.exit(1.0)183184 @click.command()Merge pull request #681 from pgkelley4/masterMarkus Unterwaditzerbefore adoption430c50a
lines 185–193why, one step back185 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'193Merge pull request #1098 from sirosen/non-standalone-exitDavid Lordbefore adoption8df9a6b
lines 194–197why, one step back194 result = runner.invoke(cli_string_ctx_exit)195 assert result.exit_code == 1196 assert result.output == 'hello world\nerror\n'197Merge pull request #681 from pgkelley4/masterMarkus Unterwaditzerbefore adoption430c50a
lines 198–201why, one step back198 result = runner.invoke(cli_int)199 assert result.exit_code == 1200 assert result.output == 'hello world\n'201Merge pull request #1098 from sirosen/non-standalone-exitDavid Lordbefore adoption8df9a6b
lines 202–205why, one step back202 result = runner.invoke(cli_int_ctx_exit)203 assert result.exit_code == 1204 assert result.output == 'hello world\n'205Merge pull request #681 from pgkelley4/masterMarkus Unterwaditzerbefore adoption430c50a
line 206why, one step back206 result = runner.invoke(cli_float)Merge pull request #1098 from sirosen/non-standalone-exitDavid Lordbefore adoption8df9a6b
lines 207–210why, one step back207 assert result.exit_code == 1208 assert result.output == 'hello world\n1.0\n'209210 result = runner.invoke(cli_float_ctx_exit)Merge pull request #681 from pgkelley4/masterMarkus Unterwaditzerbefore adoption430c50a
lines 211–236why, one step back211 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_orig235236CliRunner: add stdout / stderr, allow separating standard streams (#868)Dave Carlsonbefore adoptionfdceb39
lines 237–259why, one step back237def 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.stderr259Merge pull request #1194 from Julian/stderr-really-separateDavid Lordbefore adoptioncc294de
lines 260–271why, one step back260 @click.command()261 def cli_empty_stderr():262 click.echo("stdout")263264 runner = CliRunner(mix_stderr=False)265266 result = runner.invoke(cli_empty_stderr)267268 assert result.output == 'stdout\n'269 assert result.stdout == 'stdout\n'270 assert result.stderr == ''271CliRunner: add stdout / stderr, allow separating standard streams (#868)Dave Carlsonbefore adoptionfdceb39
line 272why, one step back272Merge pull request #681 from pgkelley4/masterMarkus Unterwaditzerbefore adoption430c50a
lines 273–290why, one step back273@pytest.mark.parametrize('args, expected_output', [274 (None, 'bar\n'),275 ([], 'bar\n'),276 ('', 'bar\n'),277 (['--foo', 'one two'], 'one two\n'),278 ('--foo "one two"', 'one two\n'),279])280def test_args(args, expected_output):281282 @click.command()283 @click.option('--foo', default='bar')284 def cli_args(foo):285 click.echo(foo)286287 runner = CliRunner()288 result = runner.invoke(cli_args, args=args)289 assert result.exit_code == 0290 assert result.output == expected_outputMerge pull request #999 from kporangehat/invoke_with_prog_nameDan Sullybefore adoption467fb8d
lines 291–301why, one step back291292293def test_setting_prog_name_in_extra():294 @click.command()295 def cli():296 click.echo("ok")297298 runner = CliRunner()299 result = runner.invoke(cli, prog_name="foobar")300 assert not result.exception301 assert result.output == 'ok\n'