test_testing.py
This file
Path
tests/test_testing.py
303 lines · 7.4 KiB · Python
Last changed by
Merge branch '7.x'David Lordbefore adoption7239794
History of this fileTake it
The walk stopped after 200 commits: lines older than 2899795 point at it.
Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoption5f9d432
line 1why, one step back1import osMerge branch 'her0e1c1-fix-secho'Markus Unterwaditzerbefore adoption2899795
lines 2–5why, one step back2import sys34import pytest5Merge branch '7.x'David Lordbefore adoption7239794
lines 6–8why, one step back6import click7from click._compat import PY28from click._compat import WINMerge branch 'her0e1c1-fix-secho'Markus Unterwaditzerbefore adoption2899795
lines 9–20why, one step back9from click.testing import CliRunner1011# 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():Merge branch '7.x'David Lordbefore adoption7239794
lines 21–22why, one step back21 i = click.get_binary_stream("stdin")22 o = click.get_binary_stream("stdout")Merge branch 'her0e1c1-fix-secho'Markus Unterwaditzerbefore adoption2899795
lines 23–30why, one step back23 while 1:24 chunk = i.read(4096)25 if not chunk:26 break27 o.write(chunk)28 o.flush()2930 runner = CliRunner()Merge branch '7.x'David Lordbefore adoption7239794
line 31why, one step back31 result = runner.invoke(test, input="Hello World!\n")Merge branch 'her0e1c1-fix-secho'Markus Unterwaditzerbefore adoption2899795
line 32why, one step back32 assert not result.exceptionMerge branch '7.x'David Lordbefore adoption7239794
line 33why, one step back33 assert result.output == "Hello World!\n"Merge branch 'her0e1c1-fix-secho'Markus Unterwaditzerbefore adoption2899795
lines 34–35why, one step back3435 runner = CliRunner(echo_stdin=True)Merge branch '7.x'David Lordbefore adoption7239794
line 36why, one step back36 result = runner.invoke(test, input="Hello World!\n")Merge branch 'her0e1c1-fix-secho'Markus Unterwaditzerbefore adoption2899795
line 37why, one step back37 assert not result.exceptionMerge branch '7.x'David Lordbefore adoption7239794
line 38why, one step back38 assert result.output == "Hello World!\nHello World!\n"Merge branch 'her0e1c1-fix-secho'Markus Unterwaditzerbefore adoption2899795
lines 39–43why, one step back394041def test_runner_with_stream():42 @click.command()43 def test():Merge branch '7.x'David Lordbefore adoption7239794
lines 44–45why, one step back44 i = click.get_binary_stream("stdin")45 o = click.get_binary_stream("stdout")Merge branch 'her0e1c1-fix-secho'Markus Unterwaditzerbefore adoption2899795
lines 46–53why, one step back46 while 1:47 chunk = i.read(4096)48 if not chunk:49 break50 o.write(chunk)51 o.flush()5253 runner = CliRunner()Merge branch '7.x'David Lordbefore adoption7239794
line 54why, one step back54 result = runner.invoke(test, input=ReasonableBytesIO(b"Hello World!\n"))Merge branch 'her0e1c1-fix-secho'Markus Unterwaditzerbefore adoption2899795
line 55why, one step back55 assert not result.exceptionMerge branch '7.x'David Lordbefore adoption7239794
line 56why, one step back56 assert result.output == "Hello World!\n"Merge branch 'her0e1c1-fix-secho'Markus Unterwaditzerbefore adoption2899795
lines 57–58why, one step back5758 runner = CliRunner(echo_stdin=True)Merge branch '7.x'David Lordbefore adoption7239794
line 59why, one step back59 result = runner.invoke(test, input=ReasonableBytesIO(b"Hello World!\n"))Merge branch 'her0e1c1-fix-secho'Markus Unterwaditzerbefore adoption2899795
line 60why, one step back60 assert not result.exceptionMerge branch '7.x'David Lordbefore adoption7239794
line 61why, one step back61 assert result.output == "Hello World!\nHello World!\n"Merge branch 'her0e1c1-fix-secho'Markus Unterwaditzerbefore adoption2899795
lines 62–65why, one step back626364def test_prompts():65 @click.command()Merge branch '7.x'David Lordbefore adoption7239794
line 66why, one step back66 @click.option("--foo", prompt=True)Merge branch 'her0e1c1-fix-secho'Markus Unterwaditzerbefore adoption2899795
line 67why, one step back67 def test(foo):Merge branch '7.x'David Lordbefore adoption7239794
line 68why, one step back68 click.echo("foo={}".format(foo))Merge branch 'her0e1c1-fix-secho'Markus Unterwaditzerbefore adoption2899795
lines 69–70why, one step back6970 runner = CliRunner()Merge branch '7.x'David Lordbefore adoption7239794
line 71why, one step back71 result = runner.invoke(test, input="wau wau\n")Merge branch 'her0e1c1-fix-secho'Markus Unterwaditzerbefore adoption2899795
line 72why, one step back72 assert not result.exceptionMerge branch '7.x'David Lordbefore adoption7239794
line 73why, one step back73 assert result.output == "Foo: wau wau\nfoo=wau wau\n"Merge branch 'her0e1c1-fix-secho'Markus Unterwaditzerbefore adoption2899795
lines 74–75why, one step back7475 @click.command()Merge branch '7.x'David Lordbefore adoption7239794
line 76why, one step back76 @click.option("--foo", prompt=True, hide_input=True)Merge branch 'her0e1c1-fix-secho'Markus Unterwaditzerbefore adoption2899795
line 77why, one step back77 def test(foo):Merge branch '7.x'David Lordbefore adoption7239794
line 78why, one step back78 click.echo("foo={}".format(foo))Merge branch 'her0e1c1-fix-secho'Markus Unterwaditzerbefore adoption2899795
lines 79–80why, one step back7980 runner = CliRunner()Merge branch '7.x'David Lordbefore adoption7239794
line 81why, one step back81 result = runner.invoke(test, input="wau wau\n")Merge branch 'her0e1c1-fix-secho'Markus Unterwaditzerbefore adoption2899795
line 82why, one step back82 assert not result.exceptionMerge branch '7.x'David Lordbefore adoption7239794
line 83why, one step back83 assert result.output == "Foo: \nfoo=wau wau\n"Merge branch 'her0e1c1-fix-secho'Markus Unterwaditzerbefore adoption2899795
lines 84–91why, one step back848586def test_getchar():87 @click.command()88 def continue_it():89 click.echo(click.getchar())9091 runner = CliRunner()Merge branch '7.x'David Lordbefore adoption7239794
line 92why, one step back92 result = runner.invoke(continue_it, input="y")Merge branch 'her0e1c1-fix-secho'Markus Unterwaditzerbefore adoption2899795
line 93why, one step back93 assert not result.exceptionMerge branch '7.x'David Lordbefore adoption7239794
line 94why, one step back94 assert result.output == "y\n"Merge branch 'her0e1c1-fix-secho'Markus Unterwaditzerbefore adoption2899795
lines 95–120why, one step back959697def 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 == 1119120Merge branch '7.x'David Lordbefore adoption7239794
line 121why, one step back121@pytest.mark.skipif(WIN, reason="Test does not make sense on Windows.")Merge branch 'her0e1c1-fix-secho'Markus Unterwaditzerbefore adoption2899795
lines 122–124why, one step back122def test_with_color():123 @click.command()124 def cli():Merge branch '7.x'David Lordbefore adoption7239794
line 125why, one step back125 click.secho("hello world", fg="blue")Merge branch 'her0e1c1-fix-secho'Markus Unterwaditzerbefore adoption2899795
lines 126–129why, one step back126127 runner = CliRunner()128129 result = runner.invoke(cli)Merge branch '7.x'David Lordbefore adoption7239794
line 130why, one step back130 assert result.output == "hello world\n"Merge branch 'her0e1c1-fix-secho'Markus Unterwaditzerbefore adoption2899795
lines 131–133why, one step back131 assert not result.exception132133 result = runner.invoke(cli, color=True)Merge branch '7.x'David Lordbefore adoption7239794
line 134why, one step back134 assert result.output == "{}\n".format(click.style("hello world", fg="blue"))Merge branch 'her0e1c1-fix-secho'Markus Unterwaditzerbefore adoption2899795
lines 135–145why, one step back135 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.exceptionMerge branch '7.x'David Lordbefore adoption7239794
line 146why, one step back146 assert result.output == ""Merge branch 'her0e1c1-fix-secho'Markus Unterwaditzerbefore adoption2899795
lines 147–152why, one step back147148149def test_exit_code_and_output_from_sys_exit():150 # See issue #362151 @click.command()152 def cli_string():Merge branch '7.x'David Lordbefore adoption7239794
lines 153–154why, one step back153 click.echo("hello world")154 sys.exit("error")Merge branch 'her0e1c1-fix-secho'Markus Unterwaditzerbefore adoption2899795
lines 155–156why, one step back155156 @click.command()Merge pull request #1098 from sirosen/non-standalone-exitDavid Lordbefore adoption8df9a6b
lines 157–158why, one step back157 @click.pass_context158 def cli_string_ctx_exit(ctx):Merge branch '7.x'David Lordbefore adoption7239794
lines 159–160why, one step back159 click.echo("hello world")160 ctx.exit("error")Merge pull request #1098 from sirosen/non-standalone-exitDavid Lordbefore adoption8df9a6b
lines 161–162why, one step back161162 @click.command()Merge branch 'her0e1c1-fix-secho'Markus Unterwaditzerbefore adoption2899795
line 163why, one step back163 def cli_int():Merge branch '7.x'David Lordbefore adoption7239794
line 164why, one step back164 click.echo("hello world")Merge branch 'her0e1c1-fix-secho'Markus Unterwaditzerbefore adoption2899795
lines 165–167why, one step back165 sys.exit(1)166167 @click.command()Merge pull request #1098 from sirosen/non-standalone-exitDavid Lordbefore adoption8df9a6b
lines 168–169why, one step back168 @click.pass_context169 def cli_int_ctx_exit(ctx):Merge branch '7.x'David Lordbefore adoption7239794
line 170why, one step back170 click.echo("hello world")Merge pull request #1098 from sirosen/non-standalone-exitDavid Lordbefore adoption8df9a6b
lines 171–173why, one step back171 ctx.exit(1)172173 @click.command()Merge branch 'her0e1c1-fix-secho'Markus Unterwaditzerbefore adoption2899795
line 174why, one step back174 def cli_float():Merge branch '7.x'David Lordbefore adoption7239794
line 175why, one step back175 click.echo("hello world")Merge branch 'her0e1c1-fix-secho'Markus Unterwaditzerbefore adoption2899795
lines 176–178why, one step back176 sys.exit(1.0)177178 @click.command()Merge pull request #1098 from sirosen/non-standalone-exitDavid Lordbefore adoption8df9a6b
lines 179–180why, one step back179 @click.pass_context180 def cli_float_ctx_exit(ctx):Merge branch '7.x'David Lordbefore adoption7239794
line 181why, one step back181 click.echo("hello world")Merge pull request #1098 from sirosen/non-standalone-exitDavid Lordbefore adoption8df9a6b
lines 182–184why, one step back182 ctx.exit(1.0)183184 @click.command()Merge branch 'her0e1c1-fix-secho'Markus Unterwaditzerbefore adoption2899795
line 185why, one step back185 def cli_no_error():Merge branch '7.x'David Lordbefore adoption7239794
line 186why, one step back186 click.echo("hello world")Merge branch 'her0e1c1-fix-secho'Markus Unterwaditzerbefore adoption2899795
lines 187–191why, one step back187188 runner = CliRunner()189190 result = runner.invoke(cli_string)191 assert result.exit_code == 1Merge branch '7.x'David Lordbefore adoption7239794
line 192why, one step back192 assert result.output == "hello world\nerror\n"Merge branch 'her0e1c1-fix-secho'Markus Unterwaditzerbefore adoption2899795
line 193why, one step back193Merge pull request #1098 from sirosen/non-standalone-exitDavid Lordbefore adoption8df9a6b
lines 194–195why, one step back194 result = runner.invoke(cli_string_ctx_exit)195 assert result.exit_code == 1Merge branch '7.x'David Lordbefore adoption7239794
line 196why, one step back196 assert result.output == "hello world\nerror\n"Merge pull request #1098 from sirosen/non-standalone-exitDavid Lordbefore adoption8df9a6b
line 197why, one step back197Merge branch 'her0e1c1-fix-secho'Markus Unterwaditzerbefore adoption2899795
lines 198–199why, one step back198 result = runner.invoke(cli_int)199 assert result.exit_code == 1Merge branch '7.x'David Lordbefore adoption7239794
line 200why, one step back200 assert result.output == "hello world\n"Merge branch 'her0e1c1-fix-secho'Markus Unterwaditzerbefore adoption2899795
line 201why, one step back201Merge pull request #1098 from sirosen/non-standalone-exitDavid Lordbefore adoption8df9a6b
lines 202–203why, one step back202 result = runner.invoke(cli_int_ctx_exit)203 assert result.exit_code == 1Merge branch '7.x'David Lordbefore adoption7239794
line 204why, one step back204 assert result.output == "hello world\n"Merge pull request #1098 from sirosen/non-standalone-exitDavid Lordbefore adoption8df9a6b
line 205why, one step back205Merge branch 'her0e1c1-fix-secho'Markus Unterwaditzerbefore adoption2899795
line 206why, one step back206 result = runner.invoke(cli_float)Merge pull request #1098 from sirosen/non-standalone-exitDavid Lordbefore adoption8df9a6b
line 207why, one step back207 assert result.exit_code == 1Merge branch '7.x'David Lordbefore adoption7239794
line 208why, one step back208 assert result.output == "hello world\n1.0\n"Merge pull request #1098 from sirosen/non-standalone-exitDavid Lordbefore adoption8df9a6b
lines 209–210why, one step back209210 result = runner.invoke(cli_float_ctx_exit)Merge branch 'her0e1c1-fix-secho'Markus Unterwaditzerbefore adoption2899795
line 211why, one step back211 assert result.exit_code == 1Merge branch '7.x'David Lordbefore adoption7239794
line 212why, one step back212 assert result.output == "hello world\n1.0\n"Merge branch 'her0e1c1-fix-secho'Markus Unterwaditzerbefore adoption2899795
lines 213–215why, one step back213214 result = runner.invoke(cli_no_error)215 assert result.exit_code == 0Merge branch '7.x'David Lordbefore adoption7239794
line 216why, one step back216 assert result.output == "hello world\n"Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoption5f9d432
lines 217–221why, one step back217218219def test_env():220 @click.command()221 def cli_env():Merge branch '7.x'David Lordbefore adoption7239794
line 222why, one step back222 click.echo("ENV={}".format(os.environ["TEST_CLICK_ENV"]))Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoption5f9d432
lines 223–227why, one step back223224 runner = CliRunner()225226 env_orig = dict(os.environ)227 env = dict(env_orig)Merge branch '7.x'David Lordbefore adoption7239794
lines 228–229why, one step back228 assert "TEST_CLICK_ENV" not in env229 env["TEST_CLICK_ENV"] = "some_value"Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoption5f9d432
lines 230–231why, one step back230 result = runner.invoke(cli_env, env=env)231 assert result.exit_code == 0Merge branch '7.x'David Lordbefore adoption7239794
line 232why, one step back232 assert result.output == "ENV=some_value\n"Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoption5f9d432
lines 233–234why, one step back233234 assert os.environ == env_origMerge pull request #664 from menegazzo/masterMarkus Unterwaditzerbefore adoptiond3f36e8
lines 235–236why, one step back235236CliRunner: add stdout / stderr, allow separating standard streams (#868)Dave Carlsonbefore adoptionfdceb39
lines 237–246why, 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)246Merge branch '7.x'David Lordbefore adoption7239794
lines 247–249why, one step back247 assert result.output == "stdout\n"248 assert result.stdout == "stdout\n"249 assert result.stderr == "stderr\n"CliRunner: add stdout / stderr, allow separating standard streams (#868)Dave Carlsonbefore adoptionfdceb39
lines 250–253why, one step back250251 runner_mix = CliRunner(mix_stderr=True)252 result_mix = runner_mix.invoke(cli_stderr)253Merge branch '7.x'David Lordbefore adoption7239794
lines 254–255why, one step back254 assert result_mix.output == "stdout\nstderr\n"255 assert result_mix.stdout == "stdout\nstderr\n"CliRunner: add stdout / stderr, allow separating standard streams (#868)Dave Carlsonbefore adoptionfdceb39
lines 256–259why, one step back256257 with pytest.raises(ValueError):258 result_mix.stderr259Merge branch '7.x'David Lordbefore adoption2269786
lines 260–267why, 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)267Merge branch '7.x'David Lordbefore adoption7239794
lines 268–270why, one step back268 assert result.output == "stdout\n"269 assert result.stdout == "stdout\n"270 assert result.stderr == ""Merge branch '7.x'David Lordbefore adoption2269786
line 271why, one step back271CliRunner: add stdout / stderr, allow separating standard streams (#868)Dave Carlsonbefore adoptionfdceb39
line 272why, one step back272Merge branch '7.x'David Lordbefore adoption7239794
lines 273–282why, one step back273@pytest.mark.parametrize(274 "args, expected_output",275 [276 (None, "bar\n"),277 ([], "bar\n"),278 ("", "bar\n"),279 (["--foo", "one two"], "one two\n"),280 ('--foo "one two"', "one two\n"),281 ],282)Merge pull request #664 from menegazzo/masterMarkus Unterwaditzerbefore adoptiond3f36e8
lines 283–284why, one step back283def test_args(args, expected_output):284 @click.command()Merge branch '7.x'David Lordbefore adoption7239794
line 285why, one step back285 @click.option("--foo", default="bar")Merge pull request #664 from menegazzo/masterMarkus Unterwaditzerbefore adoptiond3f36e8
lines 286–292why, one step back286 def cli_args(foo):287 click.echo(foo)288289 runner = CliRunner()290 result = runner.invoke(cli_args, args=args)291 assert result.exit_code == 0292 assert result.output == expected_outputMerge pull request #999 from kporangehat/invoke_with_prog_nameDan Sullybefore adoption467fb8d
lines 293–302why, one step back293294295def test_setting_prog_name_in_extra():296 @click.command()297 def cli():298 click.echo("ok")299300 runner = CliRunner()301 result = runner.invoke(cli, prog_name="foobar")302 assert not result.exceptionMerge branch '7.x'David Lordbefore adoption7239794
line 303why, one step back303 assert result.output == "ok\n"