Skip to content
Myr

test_testing.py

This file

Path

tests/test_testing.py

336 lines · 8.1 KiB · Python

Last changed by

Merge pull request #1715 from MLH-Fellowship/maaz/issue-395David Lordbefore adoptionb8a33bf

History of this file

Take it

The walk stopped after 200 commits: lines older than df0f81d point at it.

Merge pull request #1059 from stopthatcow/feature/1047David Lordbefore adoptiondf0f81d

lines 1–2why, one step back
1import os
2import sys

Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9

line 3why, one step back
3from io import BytesIO

Merge pull request #1059 from stopthatcow/feature/1047David Lordbefore adoptiondf0f81d

lines 4–6why, one step back
4
5import pytest6

Merge branch '7.x'David Lordbefore adoption7239794

lines 7–8why, one step back
7import click
8from click._compat import WIN

Merge pull request #1059 from stopthatcow/feature/1047David Lordbefore adoptiondf0f81d

lines 9–14why, one step back
9from click.testing import CliRunner
101112def test_runner():13 @click.command()14 def test():

Merge branch '7.x'David Lordbefore adoption7239794

lines 15–16why, one step back
15 i = click.get_binary_stream("stdin")
16 o = click.get_binary_stream("stdout")

Merge pull request #1059 from stopthatcow/feature/1047David Lordbefore adoptiondf0f81d

lines 17–24why, one step back
17 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 back
25 result = runner.invoke(test, input="Hello World!\n")

Merge pull request #1059 from stopthatcow/feature/1047David Lordbefore adoptiondf0f81d

line 26why, one step back
26 assert not result.exception

Merge branch '7.x'David Lordbefore adoption7239794

line 27why, one step back
27 assert result.output == "Hello World!\n"

Merge pull request #1059 from stopthatcow/feature/1047David Lordbefore adoptiondf0f81d

lines 28–29why, one step back
28
29 runner = CliRunner(echo_stdin=True)

Merge branch '7.x'David Lordbefore adoption7239794

line 30why, one step back
30 result = runner.invoke(test, input="Hello World!\n")

Merge pull request #1059 from stopthatcow/feature/1047David Lordbefore adoptiondf0f81d

line 31why, one step back
31 assert not result.exception

Merge branch '7.x'David Lordbefore adoption7239794

line 32why, one step back
32 assert result.output == "Hello World!\nHello World!\n"

Merge pull request #1059 from stopthatcow/feature/1047David Lordbefore adoptiondf0f81d

lines 33–37why, one step back
33
3435def test_runner_with_stream():36 @click.command()37 def test():

Merge branch '7.x'David Lordbefore adoption7239794

lines 38–39why, one step back
38 i = click.get_binary_stream("stdin")
39 o = click.get_binary_stream("stdout")

Merge pull request #1059 from stopthatcow/feature/1047David Lordbefore adoptiondf0f81d

lines 40–47why, one step back
40 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 back
48 result = runner.invoke(test, input=BytesIO(b"Hello World!\n"))

Merge pull request #1059 from stopthatcow/feature/1047David Lordbefore adoptiondf0f81d

line 49why, one step back
49 assert not result.exception

Merge branch '7.x'David Lordbefore adoption7239794

line 50why, one step back
50 assert result.output == "Hello World!\n"

Merge pull request #1059 from stopthatcow/feature/1047David Lordbefore adoptiondf0f81d

lines 51–52why, one step back
51
52 runner = CliRunner(echo_stdin=True)

Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9

line 53why, one step back
53 result = runner.invoke(test, input=BytesIO(b"Hello World!\n"))

Merge pull request #1059 from stopthatcow/feature/1047David Lordbefore adoptiondf0f81d

line 54why, one step back
54 assert not result.exception

Merge branch '7.x'David Lordbefore adoption7239794

line 55why, one step back
55 assert result.output == "Hello World!\nHello World!\n"

Merge pull request #1059 from stopthatcow/feature/1047David Lordbefore adoptiondf0f81d

lines 56–59why, one step back
56
5758def test_prompts():59 @click.command()

Merge branch '7.x'David Lordbefore adoption7239794

line 60why, one step back
60 @click.option("--foo", prompt=True)

Merge pull request #1059 from stopthatcow/feature/1047David Lordbefore adoptiondf0f81d

line 61why, one step back
61 def test(foo):

Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9

line 62why, one step back
62 click.echo(f"foo={foo}")

Merge pull request #1059 from stopthatcow/feature/1047David Lordbefore adoptiondf0f81d

lines 63–64why, one step back
63
64 runner = CliRunner()

Merge branch '7.x'David Lordbefore adoption7239794

line 65why, one step back
65 result = runner.invoke(test, input="wau wau\n")

Merge pull request #1059 from stopthatcow/feature/1047David Lordbefore adoptiondf0f81d

line 66why, one step back
66 assert not result.exception

Merge branch '7.x'David Lordbefore adoption7239794

line 67why, one step back
67 assert result.output == "Foo: wau wau\nfoo=wau wau\n"

Merge pull request #1059 from stopthatcow/feature/1047David Lordbefore adoptiondf0f81d

lines 68–69why, one step back
68
69 @click.command()

Merge branch '7.x'David Lordbefore adoption7239794

line 70why, one step back
70 @click.option("--foo", prompt=True, hide_input=True)

Merge pull request #1059 from stopthatcow/feature/1047David Lordbefore adoptiondf0f81d

line 71why, one step back
71 def test(foo):

Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9

line 72why, one step back
72 click.echo(f"foo={foo}")

Merge pull request #1059 from stopthatcow/feature/1047David Lordbefore adoptiondf0f81d

lines 73–74why, one step back
73
74 runner = CliRunner()

Merge branch '7.x'David Lordbefore adoption7239794

line 75why, one step back
75 result = runner.invoke(test, input="wau wau\n")

Merge pull request #1059 from stopthatcow/feature/1047David Lordbefore adoptiondf0f81d

line 76why, one step back
76 assert not result.exception

Merge branch '7.x'David Lordbefore adoption7239794

line 77why, one step back
77 assert result.output == "Foo: \nfoo=wau wau\n"

Merge pull request #1059 from stopthatcow/feature/1047David Lordbefore adoptiondf0f81d

lines 78–85why, one step back
78
7980def 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 back
86 result = runner.invoke(continue_it, input="y")

Merge pull request #1059 from stopthatcow/feature/1047David Lordbefore adoptiondf0f81d

line 87why, one step back
87 assert not result.exception

Merge branch '7.x'David Lordbefore adoption7239794

line 88why, one step back
88 assert result.output == "y\n"

Merge pull request #1059 from stopthatcow/feature/1047David Lordbefore adoptiondf0f81d

lines 89–114why, one step back
89
9091def 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 == 1113114

Merge branch '7.x'David Lordbefore adoption7239794

line 115why, one step back
115@pytest.mark.skipif(WIN, reason="Test does not make sense on Windows.")

Merge pull request #1059 from stopthatcow/feature/1047David Lordbefore adoptiondf0f81d

lines 116–118why, one step back
116def test_with_color():
117 @click.command()118 def cli():

Merge branch '7.x'David Lordbefore adoption7239794

line 119why, one step back
119 click.secho("hello world", fg="blue")

Merge pull request #1059 from stopthatcow/feature/1047David Lordbefore adoptiondf0f81d

lines 120–123why, one step back
120
121 runner = CliRunner()122123 result = runner.invoke(cli)

Merge branch '7.x'David Lordbefore adoption7239794

line 124why, one step back
124 assert result.output == "hello world\n"

Merge pull request #1059 from stopthatcow/feature/1047David Lordbefore adoptiondf0f81d

lines 125–127why, one step back
125 assert not result.exception
126127 result = runner.invoke(cli, color=True)

Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9

line 128why, one step back
128 assert result.output == f"{click.style('hello world', fg='blue')}\n"

Merge pull request #1059 from stopthatcow/feature/1047David Lordbefore adoptiondf0f81d

lines 129–139why, one step back
129 assert not result.exception
130131132def 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.exception

Merge branch '7.x'David Lordbefore adoption7239794

line 140why, one step back
140 assert result.output == ""

Merge pull request #1059 from stopthatcow/feature/1047David Lordbefore adoptiondf0f81d

lines 141–146why, one step back
141
142143def 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 back
147 click.echo("hello world")
148 sys.exit("error")

Merge pull request #1059 from stopthatcow/feature/1047David Lordbefore adoptiondf0f81d

lines 149–152why, one step back
149
150 @click.command()151 @click.pass_context152 def cli_string_ctx_exit(ctx):

Merge branch '7.x'David Lordbefore adoption7239794

lines 153–154why, one step back
153 click.echo("hello world")
154 ctx.exit("error")

Merge pull request #1059 from stopthatcow/feature/1047David Lordbefore adoptiondf0f81d

lines 155–157why, one step back
155
156 @click.command()157 def cli_int():

Merge branch '7.x'David Lordbefore adoption7239794

line 158why, one step back
158 click.echo("hello world")

Merge pull request #1059 from stopthatcow/feature/1047David Lordbefore adoptiondf0f81d

lines 159–163why, one step back
159 sys.exit(1)
160161 @click.command()162 @click.pass_context163 def cli_int_ctx_exit(ctx):

Merge branch '7.x'David Lordbefore adoption7239794

line 164why, one step back
164 click.echo("hello world")

Merge pull request #1059 from stopthatcow/feature/1047David Lordbefore adoptiondf0f81d

lines 165–168why, one step back
165 ctx.exit(1)
166167 @click.command()168 def cli_float():

Merge branch '7.x'David Lordbefore adoption7239794

line 169why, one step back
169 click.echo("hello world")

Merge pull request #1059 from stopthatcow/feature/1047David Lordbefore adoptiondf0f81d

lines 170–174why, one step back
170 sys.exit(1.0)
171172 @click.command()173 @click.pass_context174 def cli_float_ctx_exit(ctx):

Merge branch '7.x'David Lordbefore adoption7239794

line 175why, one step back
175 click.echo("hello world")

Merge pull request #1059 from stopthatcow/feature/1047David Lordbefore adoptiondf0f81d

lines 176–179why, one step back
176 ctx.exit(1.0)
177178 @click.command()179 def cli_no_error():

Merge branch '7.x'David Lordbefore adoption7239794

line 180why, one step back
180 click.echo("hello world")

Merge pull request #1059 from stopthatcow/feature/1047David Lordbefore adoptiondf0f81d

lines 181–185why, one step back
181
182 runner = CliRunner()183184 result = runner.invoke(cli_string)185 assert result.exit_code == 1

Merge branch '7.x'David Lordbefore adoption7239794

line 186why, one step back
186 assert result.output == "hello world\nerror\n"

Merge pull request #1059 from stopthatcow/feature/1047David Lordbefore adoptiondf0f81d

lines 187–189why, one step back
187
188 result = runner.invoke(cli_string_ctx_exit)189 assert result.exit_code == 1

Merge branch '7.x'David Lordbefore adoption7239794

line 190why, one step back
190 assert result.output == "hello world\nerror\n"

Merge pull request #1059 from stopthatcow/feature/1047David Lordbefore adoptiondf0f81d

lines 191–193why, one step back
191
192 result = runner.invoke(cli_int)193 assert result.exit_code == 1

Merge branch '7.x'David Lordbefore adoption7239794

line 194why, one step back
194 assert result.output == "hello world\n"

Merge pull request #1059 from stopthatcow/feature/1047David Lordbefore adoptiondf0f81d

lines 195–197why, one step back
195
196 result = runner.invoke(cli_int_ctx_exit)197 assert result.exit_code == 1

Merge branch '7.x'David Lordbefore adoption7239794

line 198why, one step back
198 assert result.output == "hello world\n"

Merge pull request #1059 from stopthatcow/feature/1047David Lordbefore adoptiondf0f81d

lines 199–201why, one step back
199
200 result = runner.invoke(cli_float)201 assert result.exit_code == 1

Merge branch '7.x'David Lordbefore adoption7239794

line 202why, one step back
202 assert result.output == "hello world\n1.0\n"

Merge pull request #1059 from stopthatcow/feature/1047David Lordbefore adoptiondf0f81d

lines 203–205why, one step back
203
204 result = runner.invoke(cli_float_ctx_exit)205 assert result.exit_code == 1

Merge branch '7.x'David Lordbefore adoption7239794

line 206why, one step back
206 assert result.output == "hello world\n1.0\n"

Merge pull request #1059 from stopthatcow/feature/1047David Lordbefore adoptiondf0f81d

lines 207–209why, one step back
207
208 result = runner.invoke(cli_no_error)209 assert result.exit_code == 0

Merge branch '7.x'David Lordbefore adoption7239794

line 210why, one step back
210 assert result.output == "hello world\n"

Merge pull request #1059 from stopthatcow/feature/1047David Lordbefore adoptiondf0f81d

lines 211–215why, one step back
211
212213def test_env():214 @click.command()215 def cli_env():

Merge pull request #1532 from pallets/drop-python2David Lordbefore adoption97de2c9

line 216why, one step back
216 click.echo(f"ENV={os.environ['TEST_CLICK_ENV']}")

Merge pull request #1059 from stopthatcow/feature/1047David Lordbefore adoptiondf0f81d

lines 217–221why, one step back
217
218 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 back
222 assert "TEST_CLICK_ENV" not in env
223 env["TEST_CLICK_ENV"] = "some_value"

Merge pull request #1059 from stopthatcow/feature/1047David Lordbefore adoptiondf0f81d

lines 224–225why, one step back
224 result = runner.invoke(cli_env, env=env)
225 assert result.exit_code == 0

Merge branch '7.x'David Lordbefore adoption7239794

line 226why, one step back
226 assert result.output == "ENV=some_value\n"

Merge pull request #1059 from stopthatcow/feature/1047David Lordbefore adoptiondf0f81d

lines 227–240why, one step back
227
228 assert os.environ == env_orig229230231def 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)240

Merge branch '7.x'David Lordbefore adoption7239794

lines 241–243why, one step back
241 assert result.output == "stdout\n"
242 assert result.stdout == "stdout\n"243 assert result.stderr == "stderr\n"

Merge pull request #1059 from stopthatcow/feature/1047David Lordbefore adoptiondf0f81d

lines 244–247why, one step back
244
245 runner_mix = CliRunner(mix_stderr=True)246 result_mix = runner_mix.invoke(cli_stderr)247

Merge branch '7.x'David Lordbefore adoption7239794

lines 248–249why, one step back
248 assert result_mix.output == "stdout\nstderr\n"
249 assert result_mix.stdout == "stdout\nstderr\n"

Merge pull request #1059 from stopthatcow/feature/1047David Lordbefore adoptiondf0f81d

lines 250–253why, one step back
250
251 with pytest.raises(ValueError):252 result_mix.stderr253

Merge branch '7.x'David Lordbefore adoption2269786

lines 254–261why, one step back
254 @click.command()
255 def cli_empty_stderr():256 click.echo("stdout")257258 runner = CliRunner(mix_stderr=False)259260 result = runner.invoke(cli_empty_stderr)261

Merge branch '7.x'David Lordbefore adoption7239794

lines 262–264why, one step back
262 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 back
265

Merge pull request #1059 from stopthatcow/feature/1047David Lordbefore adoptiondf0f81d

line 266why, one step back
266

Merge branch '7.x'David Lordbefore adoption7239794

lines 267–276why, one step back
267@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 #1059 from stopthatcow/feature/1047David Lordbefore adoptiondf0f81d

lines 277–278why, one step back
277def test_args(args, expected_output):
278 @click.command()

Merge branch '7.x'David Lordbefore adoption7239794

line 279why, one step back
279 @click.option("--foo", default="bar")

Merge pull request #1059 from stopthatcow/feature/1047David Lordbefore adoptiondf0f81d

lines 280–296why, one step back
280 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_output287288289def 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.exception

Merge branch '7.x'David Lordbefore adoption7239794

line 297why, one step back
297 assert result.output == "ok\n"

Merge pull request #1312 from tom-dalton-fanduel/allow-testing-command-result-captureDavid Lordbefore adoption990ca8e

lines 298–310why, one step back
298
299300def test_command_standalone_mode_returns_value():301 @click.command()302 def cli():303 click.echo("ok")304 return "Hello, World!"305306 runner = CliRunner()307 result = runner.invoke(cli, standalone_mode=False)308 assert result.output == "ok\n"309 assert result.return_value == "Hello, World!"310 assert result.exit_code == 0

Merge pull request #1497 from jyscao/clirunner_stdin_fixDavid Lordbefore adoption2571539

lines 311–321why, one step back
311
312313def test_file_stdin_attrs(runner):314 @click.command()315 @click.argument("f", type=click.File())316 def cli(f):317 click.echo(f.name)318 click.echo(f.mode, nl=False)319320 result = runner.invoke(cli, ["-"])321 assert result.output == "<stdin>\nr"

Merge pull request #1715 from MLH-Fellowship/maaz/issue-395David Lordbefore adoptionb8a33bf

lines 322–336why, one step back
322
323324def test_isolated_runner(runner):325 with runner.isolated_filesystem() as d:326 assert os.path.exists(d)327328 assert not os.path.exists(d)329330331def test_isolated_runner_custom_tempdir(runner, tmp_path):332 with runner.isolated_filesystem(temp_dir=tmp_path) as d:333 assert os.path.exists(d)334335 assert os.path.exists(d)336 os.rmdir(d)