Skip to content
Myr

test_testing.py

This file

Path

tests/test_testing.py

303 lines · 7.4 KiB · Python

Last changed by

Merge pull request #1490 from pallets/styleDavid Lordbefore adoption38ba2e6

History of this file

Take it

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

Fix typo (#817)scottbeldenbefore adoption950463b

lines 1–5why, one step back
1import os
2import sys34import pytest5

Merge pull request #1490 from pallets/styleDavid Lordbefore adoption38ba2e6

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

Fix typo (#817)scottbeldenbefore adoption950463b

lines 9–20why, one step back
9from click.testing import CliRunner
1011# 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 pull request #1490 from pallets/styleDavid Lordbefore adoption38ba2e6

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

Fix typo (#817)scottbeldenbefore adoption950463b

lines 23–30why, one step back
23 while 1:
24 chunk = i.read(4096)25 if not chunk:26 break27 o.write(chunk)28 o.flush()2930 runner = CliRunner()

Merge pull request #1490 from pallets/styleDavid Lordbefore adoption38ba2e6

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

Fix typo (#817)scottbeldenbefore adoption950463b

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

Merge pull request #1490 from pallets/styleDavid Lordbefore adoption38ba2e6

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

Fix typo (#817)scottbeldenbefore adoption950463b

lines 34–35why, one step back
34
35 runner = CliRunner(echo_stdin=True)

Merge pull request #1490 from pallets/styleDavid Lordbefore adoption38ba2e6

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

Fix typo (#817)scottbeldenbefore adoption950463b

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

Merge pull request #1490 from pallets/styleDavid Lordbefore adoption38ba2e6

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

Fix typo (#817)scottbeldenbefore adoption950463b

lines 39–43why, one step back
39
4041def test_runner_with_stream():42 @click.command()43 def test():

Merge pull request #1490 from pallets/styleDavid Lordbefore adoption38ba2e6

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

Fix typo (#817)scottbeldenbefore adoption950463b

lines 46–53why, one step back
46 while 1:
47 chunk = i.read(4096)48 if not chunk:49 break50 o.write(chunk)51 o.flush()5253 runner = CliRunner()

Merge pull request #1490 from pallets/styleDavid Lordbefore adoption38ba2e6

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

Fix typo (#817)scottbeldenbefore adoption950463b

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

Merge pull request #1490 from pallets/styleDavid Lordbefore adoption38ba2e6

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

Fix typo (#817)scottbeldenbefore adoption950463b

lines 57–58why, one step back
57
58 runner = CliRunner(echo_stdin=True)

Merge pull request #1490 from pallets/styleDavid Lordbefore adoption38ba2e6

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

Fix typo (#817)scottbeldenbefore adoption950463b

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

Merge pull request #1490 from pallets/styleDavid Lordbefore adoption38ba2e6

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

Fix typo (#817)scottbeldenbefore adoption950463b

lines 62–65why, one step back
62
6364def test_prompts():65 @click.command()

Merge pull request #1490 from pallets/styleDavid Lordbefore adoption38ba2e6

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

Fix typo (#817)scottbeldenbefore adoption950463b

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

Merge pull request #1490 from pallets/styleDavid Lordbefore adoption38ba2e6

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

Fix typo (#817)scottbeldenbefore adoption950463b

lines 69–70why, one step back
69
70 runner = CliRunner()

Merge pull request #1490 from pallets/styleDavid Lordbefore adoption38ba2e6

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

Fix typo (#817)scottbeldenbefore adoption950463b

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

Merge pull request #1490 from pallets/styleDavid Lordbefore adoption38ba2e6

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

Fix typo (#817)scottbeldenbefore adoption950463b

lines 74–75why, one step back
74
75 @click.command()

Merge pull request #1490 from pallets/styleDavid Lordbefore adoption38ba2e6

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

Fix typo (#817)scottbeldenbefore adoption950463b

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

Merge pull request #1490 from pallets/styleDavid Lordbefore adoption38ba2e6

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

Fix typo (#817)scottbeldenbefore adoption950463b

lines 79–80why, one step back
79
80 runner = CliRunner()

Merge pull request #1490 from pallets/styleDavid Lordbefore adoption38ba2e6

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

Fix typo (#817)scottbeldenbefore adoption950463b

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

Merge pull request #1490 from pallets/styleDavid Lordbefore adoption38ba2e6

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

Fix typo (#817)scottbeldenbefore adoption950463b

lines 84–91why, one step back
84
8586def test_getchar():87 @click.command()88 def continue_it():89 click.echo(click.getchar())9091 runner = CliRunner()

Merge pull request #1490 from pallets/styleDavid Lordbefore adoption38ba2e6

line 92why, one step back
92 result = runner.invoke(continue_it, input="y")

Fix typo (#817)scottbeldenbefore adoption950463b

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

Merge pull request #1490 from pallets/styleDavid Lordbefore adoption38ba2e6

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

Fix typo (#817)scottbeldenbefore adoption950463b

lines 95–120why, one step back
95
9697def 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 == 1119120

Merge pull request #1490 from pallets/styleDavid Lordbefore adoption38ba2e6

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

Fix typo (#817)scottbeldenbefore adoption950463b

lines 122–124why, one step back
122def test_with_color():
123 @click.command()124 def cli():

Merge pull request #1490 from pallets/styleDavid Lordbefore adoption38ba2e6

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

Fix typo (#817)scottbeldenbefore adoption950463b

lines 126–129why, one step back
126
127 runner = CliRunner()128129 result = runner.invoke(cli)

Merge pull request #1490 from pallets/styleDavid Lordbefore adoption38ba2e6

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

Fix typo (#817)scottbeldenbefore adoption950463b

lines 131–133why, one step back
131 assert not result.exception
132133 result = runner.invoke(cli, color=True)

Merge pull request #1490 from pallets/styleDavid Lordbefore adoption38ba2e6

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

Fix typo (#817)scottbeldenbefore adoption950463b

lines 135–145why, one step back
135 assert not result.exception
136137138def 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.exception

Merge pull request #1490 from pallets/styleDavid Lordbefore adoption38ba2e6

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

Fix typo (#817)scottbeldenbefore adoption950463b

lines 147–152why, one step back
147
148149def test_exit_code_and_output_from_sys_exit():150 # See issue #362151 @click.command()152 def cli_string():

Merge pull request #1490 from pallets/styleDavid Lordbefore adoption38ba2e6

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

Fix typo (#817)scottbeldenbefore adoption950463b

lines 155–156why, one step back
155
156 @click.command()

Merge pull request #1098 from sirosen/non-standalone-exitDavid Lordbefore adoption8df9a6b

lines 157–158why, one step back
157 @click.pass_context
158 def cli_string_ctx_exit(ctx):

Merge pull request #1490 from pallets/styleDavid Lordbefore adoption38ba2e6

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

Merge pull request #1098 from sirosen/non-standalone-exitDavid Lordbefore adoption8df9a6b

lines 161–162why, one step back
161
162 @click.command()

Fix typo (#817)scottbeldenbefore adoption950463b

line 163why, one step back
163 def cli_int():

Merge pull request #1490 from pallets/styleDavid Lordbefore adoption38ba2e6

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

Fix typo (#817)scottbeldenbefore adoption950463b

lines 165–167why, one step back
165 sys.exit(1)
166167 @click.command()

Merge pull request #1098 from sirosen/non-standalone-exitDavid Lordbefore adoption8df9a6b

lines 168–169why, one step back
168 @click.pass_context
169 def cli_int_ctx_exit(ctx):

Merge pull request #1490 from pallets/styleDavid Lordbefore adoption38ba2e6

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

Merge pull request #1098 from sirosen/non-standalone-exitDavid Lordbefore adoption8df9a6b

lines 171–173why, one step back
171 ctx.exit(1)
172173 @click.command()

Fix typo (#817)scottbeldenbefore adoption950463b

line 174why, one step back
174 def cli_float():

Merge pull request #1490 from pallets/styleDavid Lordbefore adoption38ba2e6

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

Fix typo (#817)scottbeldenbefore adoption950463b

lines 176–178why, one step back
176 sys.exit(1.0)
177178 @click.command()

Merge pull request #1098 from sirosen/non-standalone-exitDavid Lordbefore adoption8df9a6b

lines 179–180why, one step back
179 @click.pass_context
180 def cli_float_ctx_exit(ctx):

Merge pull request #1490 from pallets/styleDavid Lordbefore adoption38ba2e6

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

Merge pull request #1098 from sirosen/non-standalone-exitDavid Lordbefore adoption8df9a6b

lines 182–184why, one step back
182 ctx.exit(1.0)
183184 @click.command()

Fix typo (#817)scottbeldenbefore adoption950463b

line 185why, one step back
185 def cli_no_error():

Merge pull request #1490 from pallets/styleDavid Lordbefore adoption38ba2e6

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

Fix typo (#817)scottbeldenbefore adoption950463b

lines 187–191why, one step back
187
188 runner = CliRunner()189190 result = runner.invoke(cli_string)191 assert result.exit_code == 1

Merge pull request #1490 from pallets/styleDavid Lordbefore adoption38ba2e6

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

Fix typo (#817)scottbeldenbefore adoption950463b

line 193why, one step back
193

Merge pull request #1098 from sirosen/non-standalone-exitDavid Lordbefore adoption8df9a6b

lines 194–195why, one step back
194 result = runner.invoke(cli_string_ctx_exit)
195 assert result.exit_code == 1

Merge pull request #1490 from pallets/styleDavid Lordbefore adoption38ba2e6

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

Merge pull request #1098 from sirosen/non-standalone-exitDavid Lordbefore adoption8df9a6b

line 197why, one step back
197

Fix typo (#817)scottbeldenbefore adoption950463b

lines 198–199why, one step back
198 result = runner.invoke(cli_int)
199 assert result.exit_code == 1

Merge pull request #1490 from pallets/styleDavid Lordbefore adoption38ba2e6

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

Fix typo (#817)scottbeldenbefore adoption950463b

line 201why, one step back
201

Merge pull request #1098 from sirosen/non-standalone-exitDavid Lordbefore adoption8df9a6b

lines 202–203why, one step back
202 result = runner.invoke(cli_int_ctx_exit)
203 assert result.exit_code == 1

Merge pull request #1490 from pallets/styleDavid Lordbefore adoption38ba2e6

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

Merge pull request #1098 from sirosen/non-standalone-exitDavid Lordbefore adoption8df9a6b

line 205why, one step back
205

Fix typo (#817)scottbeldenbefore adoption950463b

line 206why, one step back
206 result = runner.invoke(cli_float)

Merge pull request #1098 from sirosen/non-standalone-exitDavid Lordbefore adoption8df9a6b

line 207why, one step back
207 assert result.exit_code == 1

Merge pull request #1490 from pallets/styleDavid Lordbefore adoption38ba2e6

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

Merge pull request #1098 from sirosen/non-standalone-exitDavid Lordbefore adoption8df9a6b

lines 209–210why, one step back
209
210 result = runner.invoke(cli_float_ctx_exit)

Fix typo (#817)scottbeldenbefore adoption950463b

line 211why, one step back
211 assert result.exit_code == 1

Merge pull request #1490 from pallets/styleDavid Lordbefore adoption38ba2e6

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

Fix typo (#817)scottbeldenbefore adoption950463b

lines 213–215why, one step back
213
214 result = runner.invoke(cli_no_error)215 assert result.exit_code == 0

Merge pull request #1490 from pallets/styleDavid Lordbefore adoption38ba2e6

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

Fix typo (#817)scottbeldenbefore adoption950463b

lines 217–221why, one step back
217
218219def test_env():220 @click.command()221 def cli_env():

Merge pull request #1490 from pallets/styleDavid Lordbefore adoption38ba2e6

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

Fix typo (#817)scottbeldenbefore adoption950463b

lines 223–227why, one step back
223
224 runner = CliRunner()225226 env_orig = dict(os.environ)227 env = dict(env_orig)

Merge pull request #1490 from pallets/styleDavid Lordbefore adoption38ba2e6

lines 228–229why, one step back
228 assert "TEST_CLICK_ENV" not in env
229 env["TEST_CLICK_ENV"] = "some_value"

Fix typo (#817)scottbeldenbefore adoption950463b

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

Merge pull request #1490 from pallets/styleDavid Lordbefore adoption38ba2e6

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

Fix typo (#817)scottbeldenbefore adoption950463b

lines 233–236why, one step back
233
234 assert os.environ == env_orig235236

CliRunner: add stdout / stderr, allow separating standard streams (#868)Dave Carlsonbefore adoptionfdceb39

lines 237–246why, one step back
237def 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)246

Merge pull request #1490 from pallets/styleDavid Lordbefore adoption38ba2e6

lines 247–249why, one step back
247 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 back
250
251 runner_mix = CliRunner(mix_stderr=True)252 result_mix = runner_mix.invoke(cli_stderr)253

Merge pull request #1490 from pallets/styleDavid Lordbefore adoption38ba2e6

lines 254–255why, one step back
254 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 back
256
257 with pytest.raises(ValueError):258 result_mix.stderr259

Merge pull request #1194 from Julian/stderr-really-separateDavid Lordbefore adoptioncc294de

lines 260–267why, one step back
260 @click.command()
261 def cli_empty_stderr():262 click.echo("stdout")263264 runner = CliRunner(mix_stderr=False)265266 result = runner.invoke(cli_empty_stderr)267

Merge pull request #1490 from pallets/styleDavid Lordbefore adoption38ba2e6

lines 268–270why, one step back
268 assert result.output == "stdout\n"
269 assert result.stdout == "stdout\n"270 assert result.stderr == ""

Merge pull request #1194 from Julian/stderr-really-separateDavid Lordbefore adoptioncc294de

line 271why, one step back
271

CliRunner: add stdout / stderr, allow separating standard streams (#868)Dave Carlsonbefore adoptionfdceb39

line 272why, one step back
272

Merge pull request #1490 from pallets/styleDavid Lordbefore adoption38ba2e6

lines 273–282why, one step back
273@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)

Fix typo (#817)scottbeldenbefore adoption950463b

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

Merge pull request #1490 from pallets/styleDavid Lordbefore adoption38ba2e6

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

Fix typo (#817)scottbeldenbefore adoption950463b

lines 286–292why, one step back
286 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_output

Merge pull request #999 from kporangehat/invoke_with_prog_nameDan Sullybefore adoption467fb8d

lines 293–302why, one step back
293
294295def 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.exception

Merge pull request #1490 from pallets/styleDavid Lordbefore adoption38ba2e6

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