test_testing.py
This file
Path
tests/test_testing.py
185 lines · 4.6 KiB · Python
Last changed by
Fixed broken tests and behavior on windows.Armin Ronacherbefore adoption262c275
History of this fileTake it
1import sys23import pytest4import click56from click.testing import CliRunner78from click._compat import PY2, WIN910# 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 == 1118119120@pytest.mark.skipif(WIN, reason='Test does not make sense on Windows.')121def test_with_color():122 @click.command()123 def cli():124 click.secho('hello world', fg='blue')125126 runner = CliRunner()127128 result = runner.invoke(cli)129 assert result.output == 'hello world\n'130 assert not result.exception131132 result = runner.invoke(cli, color=True)133 assert result.output == click.style('hello world', fg='blue') + '\n'134 assert not result.exception135136137def test_with_color_but_pause_not_blocking():138 @click.command()139 def cli():140 click.pause()141142 runner = CliRunner()143 result = runner.invoke(cli, color=True)144 assert not result.exception145 assert result.output == ''146147148def test_exit_code_and_output_from_sys_exit():149 # See issue #362150 @click.command()151 def cli_string():152 click.echo('hello world')153 sys.exit('error')154155 @click.command()156 def cli_int():157 click.echo('hello world')158 sys.exit(1)159160 @click.command()161 def cli_float():162 click.echo('hello world')163 sys.exit(1.0)164165 @click.command()166 def cli_no_error():167 click.echo('hello world')168169 runner = CliRunner()170171 result = runner.invoke(cli_string)172 assert result.exit_code == 1173 assert result.output == 'hello world\nerror\n'174175 result = runner.invoke(cli_int)176 assert result.exit_code == 1177 assert result.output == 'hello world\n'178179 result = runner.invoke(cli_float)180 assert result.exit_code == 1181 assert result.output == 'hello world\n1.0\n'182183 result = runner.invoke(cli_no_error)184 assert result.exit_code == 0185 assert result.output == 'hello world\n'