test_testing.py
This file
Path
tests/test_testing.py
297 lines · 7.2 KiB · Python
Last changed by
Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9
History of this fileTake it
The walk stopped after 200 commits: lines older than 764541f point at it.
Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoption5f9d432
line 1why, one step back1import osMerge branch '6.x-maintenance'Markus Unterwaditzerbefore adoption764541f
line 2why, one step back2import sysMerge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9
line 3why, one step back3from io import BytesIOMerge branch '6.x-maintenance'Markus Unterwaditzerbefore adoption764541f
lines 4–6why, one step back45import pytest6Merge branch '7.x'David Lordbefore adoption7239794
lines 7–8why, one step back7import click8from click._compat import WINMerge branch '6.x-maintenance'Markus Unterwaditzerbefore adoption764541f
lines 9–14why, one step back9from click.testing import CliRunner101112def test_runner():13 @click.command()14 def test():Merge branch '7.x'David Lordbefore adoption7239794
lines 15–16why, one step back15 i = click.get_binary_stream("stdin")16 o = click.get_binary_stream("stdout")Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoption764541f
lines 17–24why, one step back17 while 1:18 chunk = i.read(4096)19 if not chunk:20 break21 o.write(chunk)22 o.flush()2324 runner = CliRunner()Merge branch '7.x'David Lordbefore adoption7239794
line 25why, one step back25 result = runner.invoke(test, input="Hello World!\n")Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoption764541f
line 26why, one step back26 assert not result.exceptionMerge branch '7.x'David Lordbefore adoption7239794
line 27why, one step back27 assert result.output == "Hello World!\n"Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoption764541f
lines 28–29why, one step back2829 runner = CliRunner(echo_stdin=True)Merge branch '7.x'David Lordbefore adoption7239794
line 30why, one step back30 result = runner.invoke(test, input="Hello World!\n")Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoption764541f
line 31why, one step back31 assert not result.exceptionMerge branch '7.x'David Lordbefore adoption7239794
line 32why, one step back32 assert result.output == "Hello World!\nHello World!\n"Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoption764541f
lines 33–37why, one step back333435def test_runner_with_stream():36 @click.command()37 def test():Merge branch '7.x'David Lordbefore adoption7239794
lines 38–39why, one step back38 i = click.get_binary_stream("stdin")39 o = click.get_binary_stream("stdout")Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoption764541f
lines 40–47why, one step back40 while 1:41 chunk = i.read(4096)42 if not chunk:43 break44 o.write(chunk)45 o.flush()4647 runner = CliRunner()Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9
line 48why, one step back48 result = runner.invoke(test, input=BytesIO(b"Hello World!\n"))Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoption764541f
line 49why, one step back49 assert not result.exceptionMerge branch '7.x'David Lordbefore adoption7239794
line 50why, one step back50 assert result.output == "Hello World!\n"Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoption764541f
lines 51–52why, one step back5152 runner = CliRunner(echo_stdin=True)Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9
line 53why, one step back53 result = runner.invoke(test, input=BytesIO(b"Hello World!\n"))Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoption764541f
line 54why, one step back54 assert not result.exceptionMerge branch '7.x'David Lordbefore adoption7239794
line 55why, one step back55 assert result.output == "Hello World!\nHello World!\n"Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoption764541f
lines 56–59why, one step back565758def test_prompts():59 @click.command()Merge branch '7.x'David Lordbefore adoption7239794
line 60why, one step back60 @click.option("--foo", prompt=True)Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoption764541f
line 61why, one step back61 def test(foo):Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9
line 62why, one step back62 click.echo(f"foo={foo}")Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoption764541f
lines 63–64why, one step back6364 runner = CliRunner()Merge branch '7.x'David Lordbefore adoption7239794
line 65why, one step back65 result = runner.invoke(test, input="wau wau\n")Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoption764541f
line 66why, one step back66 assert not result.exceptionMerge branch '7.x'David Lordbefore adoption7239794
line 67why, one step back67 assert result.output == "Foo: wau wau\nfoo=wau wau\n"Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoption764541f
lines 68–69why, one step back6869 @click.command()Merge branch '7.x'David Lordbefore adoption7239794
line 70why, one step back70 @click.option("--foo", prompt=True, hide_input=True)Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoption764541f
line 71why, one step back71 def test(foo):Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9
line 72why, one step back72 click.echo(f"foo={foo}")Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoption764541f
lines 73–74why, one step back7374 runner = CliRunner()Merge branch '7.x'David Lordbefore adoption7239794
line 75why, one step back75 result = runner.invoke(test, input="wau wau\n")Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoption764541f
line 76why, one step back76 assert not result.exceptionMerge branch '7.x'David Lordbefore adoption7239794
line 77why, one step back77 assert result.output == "Foo: \nfoo=wau wau\n"Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoption764541f
lines 78–85why, one step back787980def test_getchar():81 @click.command()82 def continue_it():83 click.echo(click.getchar())8485 runner = CliRunner()Merge branch '7.x'David Lordbefore adoption7239794
line 86why, one step back86 result = runner.invoke(continue_it, input="y")Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoption764541f
line 87why, one step back87 assert not result.exceptionMerge branch '7.x'David Lordbefore adoption7239794
line 88why, one step back88 assert result.output == "y\n"Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoption764541f
lines 89–114why, one step back899091def test_catch_exceptions():92 class CustomError(Exception):93 pass9495 @click.command()96 def cli():97 raise CustomError(1)9899 runner = CliRunner()100101 result = runner.invoke(cli)102 assert isinstance(result.exception, CustomError)103 assert type(result.exc_info) is tuple104 assert len(result.exc_info) == 3105106 with pytest.raises(CustomError):107 runner.invoke(cli, catch_exceptions=False)108109 CustomError = SystemExit110111 result = runner.invoke(cli)112 assert result.exit_code == 1113114Merge branch '7.x'David Lordbefore adoption7239794
line 115why, one step back115@pytest.mark.skipif(WIN, reason="Test does not make sense on Windows.")Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoption764541f
lines 116–118why, one step back116def test_with_color():117 @click.command()118 def cli():Merge branch '7.x'David Lordbefore adoption7239794
line 119why, one step back119 click.secho("hello world", fg="blue")Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoption764541f
lines 120–123why, one step back120121 runner = CliRunner()122123 result = runner.invoke(cli)Merge branch '7.x'David Lordbefore adoption7239794
line 124why, one step back124 assert result.output == "hello world\n"Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoption764541f
lines 125–127why, one step back125 assert not result.exception126127 result = runner.invoke(cli, color=True)Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9
line 128why, one step back128 assert result.output == f"{click.style('hello world', fg='blue')}\n"Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoption764541f
lines 129–139why, one step back129 assert not result.exception130131132def test_with_color_but_pause_not_blocking():133 @click.command()134 def cli():135 click.pause()136137 runner = CliRunner()138 result = runner.invoke(cli, color=True)139 assert not result.exceptionMerge branch '7.x'David Lordbefore adoption7239794
line 140why, one step back140 assert result.output == ""Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoption764541f
lines 141–146why, one step back141142143def test_exit_code_and_output_from_sys_exit():144 # See issue #362145 @click.command()146 def cli_string():Merge branch '7.x'David Lordbefore adoption7239794
lines 147–148why, one step back147 click.echo("hello world")148 sys.exit("error")Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoption764541f
lines 149–150why, one step back149150 @click.command()Merge pull request #1098 from sirosen/non-standalone-exitDavid Lordbefore adoption8df9a6b
lines 151–152why, one step back151 @click.pass_context152 def cli_string_ctx_exit(ctx):Merge branch '7.x'David Lordbefore adoption7239794
lines 153–154why, one step back153 click.echo("hello world")154 ctx.exit("error")Merge pull request #1098 from sirosen/non-standalone-exitDavid Lordbefore adoption8df9a6b
lines 155–156why, one step back155156 @click.command()Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoption764541f
line 157why, one step back157 def cli_int():Merge branch '7.x'David Lordbefore adoption7239794
line 158why, one step back158 click.echo("hello world")Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoption764541f
lines 159–161why, one step back159 sys.exit(1)160161 @click.command()Merge pull request #1098 from sirosen/non-standalone-exitDavid Lordbefore adoption8df9a6b
lines 162–163why, one step back162 @click.pass_context163 def cli_int_ctx_exit(ctx):Merge branch '7.x'David Lordbefore adoption7239794
line 164why, one step back164 click.echo("hello world")Merge pull request #1098 from sirosen/non-standalone-exitDavid Lordbefore adoption8df9a6b
lines 165–167why, one step back165 ctx.exit(1)166167 @click.command()Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoption764541f
line 168why, one step back168 def cli_float():Merge branch '7.x'David Lordbefore adoption7239794
line 169why, one step back169 click.echo("hello world")Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoption764541f
lines 170–172why, one step back170 sys.exit(1.0)171172 @click.command()Merge pull request #1098 from sirosen/non-standalone-exitDavid Lordbefore adoption8df9a6b
lines 173–174why, one step back173 @click.pass_context174 def cli_float_ctx_exit(ctx):Merge branch '7.x'David Lordbefore adoption7239794
line 175why, one step back175 click.echo("hello world")Merge pull request #1098 from sirosen/non-standalone-exitDavid Lordbefore adoption8df9a6b
lines 176–178why, one step back176 ctx.exit(1.0)177178 @click.command()Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoption764541f
line 179why, one step back179 def cli_no_error():Merge branch '7.x'David Lordbefore adoption7239794
line 180why, one step back180 click.echo("hello world")Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoption764541f
lines 181–185why, one step back181182 runner = CliRunner()183184 result = runner.invoke(cli_string)185 assert result.exit_code == 1Merge branch '7.x'David Lordbefore adoption7239794
line 186why, one step back186 assert result.output == "hello world\nerror\n"Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoption764541f
line 187why, one step back187Merge pull request #1098 from sirosen/non-standalone-exitDavid Lordbefore adoption8df9a6b
lines 188–189why, one step back188 result = runner.invoke(cli_string_ctx_exit)189 assert result.exit_code == 1Merge branch '7.x'David Lordbefore adoption7239794
line 190why, one step back190 assert result.output == "hello world\nerror\n"Merge pull request #1098 from sirosen/non-standalone-exitDavid Lordbefore adoption8df9a6b
line 191why, one step back191Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoption764541f
lines 192–193why, one step back192 result = runner.invoke(cli_int)193 assert result.exit_code == 1Merge branch '7.x'David Lordbefore adoption7239794
line 194why, one step back194 assert result.output == "hello world\n"Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoption764541f
line 195why, one step back195Merge pull request #1098 from sirosen/non-standalone-exitDavid Lordbefore adoption8df9a6b
lines 196–197why, one step back196 result = runner.invoke(cli_int_ctx_exit)197 assert result.exit_code == 1Merge branch '7.x'David Lordbefore adoption7239794
line 198why, one step back198 assert result.output == "hello world\n"Merge pull request #1098 from sirosen/non-standalone-exitDavid Lordbefore adoption8df9a6b
line 199why, one step back199Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoption764541f
line 200why, one step back200 result = runner.invoke(cli_float)Merge pull request #1098 from sirosen/non-standalone-exitDavid Lordbefore adoption8df9a6b
line 201why, one step back201 assert result.exit_code == 1Merge branch '7.x'David Lordbefore adoption7239794
line 202why, one step back202 assert result.output == "hello world\n1.0\n"Merge pull request #1098 from sirosen/non-standalone-exitDavid Lordbefore adoption8df9a6b
lines 203–204why, one step back203204 result = runner.invoke(cli_float_ctx_exit)Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoption764541f
line 205why, one step back205 assert result.exit_code == 1Merge branch '7.x'David Lordbefore adoption7239794
line 206why, one step back206 assert result.output == "hello world\n1.0\n"Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoption764541f
lines 207–209why, one step back207208 result = runner.invoke(cli_no_error)209 assert result.exit_code == 0Merge branch '7.x'David Lordbefore adoption7239794
line 210why, one step back210 assert result.output == "hello world\n"Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoption5f9d432
lines 211–215why, one step back211212213def test_env():214 @click.command()215 def cli_env():Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9
line 216why, one step back216 click.echo(f"ENV={os.environ['TEST_CLICK_ENV']}")Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoption5f9d432
lines 217–221why, one step back217218 runner = CliRunner()219220 env_orig = dict(os.environ)221 env = dict(env_orig)Merge branch '7.x'David Lordbefore adoption7239794
lines 222–223why, one step back222 assert "TEST_CLICK_ENV" not in env223 env["TEST_CLICK_ENV"] = "some_value"Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoption5f9d432
lines 224–225why, one step back224 result = runner.invoke(cli_env, env=env)225 assert result.exit_code == 0Merge branch '7.x'David Lordbefore adoption7239794
line 226why, one step back226 assert result.output == "ENV=some_value\n"Merge branch '6.x-maintenance'Markus Unterwaditzerbefore adoption5f9d432
lines 227–228why, one step back227228 assert os.environ == env_origMerge pull request #664 from menegazzo/masterMarkus Unterwaditzerbefore adoptiond3f36e8
lines 229–230why, one step back229230CliRunner: add stdout / stderr, allow separating standard streams (#868)Dave Carlsonbefore adoptionfdceb39
lines 231–240why, one step back231def test_stderr():232 @click.command()233 def cli_stderr():234 click.echo("stdout")235 click.echo("stderr", err=True)236237 runner = CliRunner(mix_stderr=False)238239 result = runner.invoke(cli_stderr)240Merge branch '7.x'David Lordbefore adoption7239794
lines 241–243why, one step back241 assert result.output == "stdout\n"242 assert result.stdout == "stdout\n"243 assert result.stderr == "stderr\n"CliRunner: add stdout / stderr, allow separating standard streams (#868)Dave Carlsonbefore adoptionfdceb39
lines 244–247why, one step back244245 runner_mix = CliRunner(mix_stderr=True)246 result_mix = runner_mix.invoke(cli_stderr)247Merge branch '7.x'David Lordbefore adoption7239794
lines 248–249why, one step back248 assert result_mix.output == "stdout\nstderr\n"249 assert result_mix.stdout == "stdout\nstderr\n"CliRunner: add stdout / stderr, allow separating standard streams (#868)Dave Carlsonbefore adoptionfdceb39
lines 250–253why, one step back250251 with pytest.raises(ValueError):252 result_mix.stderr253Merge branch '7.x'David Lordbefore adoption2269786
lines 254–261why, one step back254 @click.command()255 def cli_empty_stderr():256 click.echo("stdout")257258 runner = CliRunner(mix_stderr=False)259260 result = runner.invoke(cli_empty_stderr)261Merge branch '7.x'David Lordbefore adoption7239794
lines 262–264why, one step back262 assert result.output == "stdout\n"263 assert result.stdout == "stdout\n"264 assert result.stderr == ""Merge branch '7.x'David Lordbefore adoption2269786
line 265why, one step back265CliRunner: add stdout / stderr, allow separating standard streams (#868)Dave Carlsonbefore adoptionfdceb39
line 266why, one step back266Merge branch '7.x'David Lordbefore adoption7239794
lines 267–276why, one step back267@pytest.mark.parametrize(268 "args, expected_output",269 [270 (None, "bar\n"),271 ([], "bar\n"),272 ("", "bar\n"),273 (["--foo", "one two"], "one two\n"),274 ('--foo "one two"', "one two\n"),275 ],276)Merge pull request #664 from menegazzo/masterMarkus Unterwaditzerbefore adoptiond3f36e8
lines 277–278why, one step back277def test_args(args, expected_output):278 @click.command()Merge branch '7.x'David Lordbefore adoption7239794
line 279why, one step back279 @click.option("--foo", default="bar")Merge pull request #664 from menegazzo/masterMarkus Unterwaditzerbefore adoptiond3f36e8
lines 280–286why, one step back280 def cli_args(foo):281 click.echo(foo)282283 runner = CliRunner()284 result = runner.invoke(cli_args, args=args)285 assert result.exit_code == 0286 assert result.output == expected_outputMerge pull request #999 from kporangehat/invoke_with_prog_nameDan Sullybefore adoption467fb8d
lines 287–296why, one step back287288289def test_setting_prog_name_in_extra():290 @click.command()291 def cli():292 click.echo("ok")293294 runner = CliRunner()295 result = runner.invoke(cli, prog_name="foobar")296 assert not result.exceptionMerge branch '7.x'David Lordbefore adoption7239794
line 297why, one step back297 assert result.output == "ok\n"