Skip to content
Myr

test_basic.py

This file

Path

tests/test_basic.py

205 lines · 6.0 KiB · Python

Last changed by

Added tests for choice typeArmin Ronacherbefore adoption4a8cf56

History of this file

Take it

Started a bit of test codeArmin Ronacherbefore adoption3f695c6

line 1why, one step back
1# -*- coding: utf-8 -*-

Added UUID type supportArmin Ronacherbefore adoption4ed961d

line 2why, one step back
2import uuid

Started a bit of test codeArmin Ronacherbefore adoption3f695c6

lines 3–12why, one step back
3import click
456def test_basic_functionality(runner):7 @click.command()8 def cli():9 """Hello World!"""10 click.echo('I EXECUTED')1112 result = runner.invoke(cli, ['--help'])

Added some tests for some optionsArmin Ronacherbefore adoption7360097

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

Started a bit of test codeArmin Ronacherbefore adoption3f695c6

lines 14–19why, one step back
14 assert 'Hello World!' in result.output
15 assert 'Show this message and exit.' in result.output16 assert result.exit_code == 017 assert 'I EXECUTED' not in result.output1819 result = runner.invoke(cli, [])

Added some tests for some optionsArmin Ronacherbefore adoption7360097

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

Started a bit of test codeArmin Ronacherbefore adoption3f695c6

lines 21–36why, one step back
21 assert 'I EXECUTED' in result.output
22 assert result.exit_code == 0232425def test_basic_group(runner):26 @click.group()27 def cli():28 """This is the root."""29 click.echo('ROOT EXECUTED')3031 @cli.command()32 def subcommand():33 """This is a subcommand."""34 click.echo('SUBCOMMAND EXECUTED')3536 result = runner.invoke(cli, ['--help'])

Added some tests for some optionsArmin Ronacherbefore adoption7360097

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

Started a bit of test codeArmin Ronacherbefore adoption3f695c6

lines 38–43why, one step back
38 assert 'This is the root' in result.output
39 assert 'This is a subcommand.' in result.output40 assert result.exit_code == 041 assert 'ROOT EXECUTED' not in result.output4243 result = runner.invoke(cli, ['subcommand'])

Added some tests for some optionsArmin Ronacherbefore adoption7360097

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

Started a bit of test codeArmin Ronacherbefore adoption3f695c6

lines 45–56why, one step back
45 assert result.exit_code == 0
46 assert 'ROOT EXECUTED' in result.output47 assert 'SUBCOMMAND EXECUTED' in result.output484950def test_basic_option(runner):51 @click.command()52 @click.option('--foo', default='no value')53 def cli(foo):54 click.echo('FOO:[%s]' % foo)5556 result = runner.invoke(cli, [])

Added some tests for some optionsArmin Ronacherbefore adoption7360097

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

Started a bit of test codeArmin Ronacherbefore adoption3f695c6

lines 58–60why, one step back
58 assert 'FOO:[no value]' in result.output
5960 result = runner.invoke(cli, ['--foo=42'])

Added some tests for some optionsArmin Ronacherbefore adoption7360097

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

Started a bit of test codeArmin Ronacherbefore adoption3f695c6

lines 62–64why, one step back
62 assert 'FOO:[42]' in result.output
6364 result = runner.invoke(cli, ['--foo'])

Added some tests for some optionsArmin Ronacherbefore adoption7360097

line 65why, one step back
65 assert result.exception

Started a bit of test codeArmin Ronacherbefore adoption3f695c6

lines 66–68why, one step back
66 assert '--foo option requires an argument' in result.output
6768 result = runner.invoke(cli, ['--foo='])

Added some tests for some optionsArmin Ronacherbefore adoption7360097

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

Started a bit of test codeArmin Ronacherbefore adoption3f695c6

lines 70–72why, one step back
70 assert 'FOO:[]' in result.output
7172 result = runner.invoke(cli, [u'--foo=\N{SNOWMAN}'])

Added some tests for some optionsArmin Ronacherbefore adoption7360097

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

Started a bit of test codeArmin Ronacherbefore adoption3f695c6

line 74why, one step back
74 assert u'FOO:[\N{SNOWMAN}]' in result.output

Added some tests for some optionsArmin Ronacherbefore adoption7360097

lines 75–93why, one step back
75
7677def test_int_option(runner):78 @click.command()79 @click.option('--foo', default=42)80 def cli(foo):81 click.echo('FOO:[%s]' % (foo * 2))8283 result = runner.invoke(cli, [])84 assert not result.exception85 assert 'FOO:[84]' in result.output8687 result = runner.invoke(cli, ['--foo=23'])88 assert not result.exception89 assert 'FOO:[46]' in result.output9091 result = runner.invoke(cli, ['--foo=bar'])92 assert result.exception93 assert 'Invalid value for "--foo": bar is not a valid integer' \

Added UUID type supportArmin Ronacherbefore adoption4ed961d

lines 94–115why, one step back
94 in result.output
959697def test_uuid_option(runner):98 @click.command()99 @click.option('--u', default='ba122011-349f-423b-873b-9d6a79c688ab',100 type=click.UUID)101 def cli(u):102 assert type(u) is uuid.UUID103 click.echo('U:[%s]' % u)104105 result = runner.invoke(cli, [])106 assert not result.exception107 assert 'U:[ba122011-349f-423b-873b-9d6a79c688ab]' in result.output108109 result = runner.invoke(cli, ['--u=821592c1-c50e-4971-9cd6-e89dc6832f86'])110 assert not result.exception111 assert 'U:[821592c1-c50e-4971-9cd6-e89dc6832f86]' in result.output112113 result = runner.invoke(cli, ['--u=bar'])114 assert result.exception115 assert 'Invalid value for "--u": bar is not a valid UUID value' \

Added some tests for some optionsArmin Ronacherbefore adoption7360097

lines 116–172why, one step back
116 in result.output
117118119def test_float_option(runner):120 @click.command()121 @click.option('--foo', default=42, type=click.FLOAT)122 def cli(foo):123 assert type(foo) is float124 click.echo('FOO:[%s]' % foo)125126 result = runner.invoke(cli, [])127 assert not result.exception128 assert 'FOO:[42.0]' in result.output129130 result = runner.invoke(cli, ['--foo=23.5'])131 assert not result.exception132 assert 'FOO:[23.5]' in result.output133134 result = runner.invoke(cli, ['--foo=bar'])135 assert result.exception136 assert 'Invalid value for "--foo": bar is not a valid float' \137 in result.output138139140def test_boolean_option(runner):141 for default in True, False:142 @click.command()143 @click.option('--with-foo/--without-foo', default=default)144 def cli(with_foo):145 click.echo(with_foo)146147 result = runner.invoke(cli, ['--with-foo'])148 assert not result.exception149 assert result.output == 'True\n'150 result = runner.invoke(cli, ['--without-foo'])151 assert not result.exception152 assert result.output == 'False\n'153 result = runner.invoke(cli, [])154 assert not result.exception155 assert result.output == '%s\n' % default156157 for default in True, False:158 @click.command()159 @click.option('--flag', is_flag=True, default=default)160 def cli(flag):161 click.echo(flag)162163 result = runner.invoke(cli, ['--flag'])164 assert not result.exception165 assert result.output == '%s\n' % (not default)166 result = runner.invoke(cli, [])167 assert not result.exception168 assert result.output == '%s\n' % (default)169170171def test_file_option(runner):172 @click.command()

Made tests pass on 3.xArmin Ronacherbefore adoption14c9b1a

line 173why, one step back
173 @click.option('--file', type=click.File('w'))

Added some tests for some optionsArmin Ronacherbefore adoption7360097

lines 174–177why, one step back
174 def input(file):
175 file.write('Hello World!\n')176177 @click.command()

Made tests pass on 3.xArmin Ronacherbefore adoption14c9b1a

line 178why, one step back
178 @click.option('--file', type=click.File('r'))

Added some tests for some optionsArmin Ronacherbefore adoption7360097

lines 179–189why, one step back
179 def output(file):
180 click.echo(file.read())181182 with runner.isolated_filesystem():183 result_in = runner.invoke(input, ['--file=example.txt'])184 result_out = runner.invoke(output, ['--file=example.txt'])185186 assert not result_in.exception187 assert result_in.output == ''188 assert not result_out.exception189 assert result_out.output == 'Hello World!\n\n'

Added tests for choice typeArmin Ronacherbefore adoption4a8cf56

lines 190–205why, one step back
190
191192def test_choice_option(runner):193 @click.command()194 @click.option('--method', type=click.Choice(['foo', 'bar', 'baz']))195 def cli(method):196 click.echo(method)197198 result = runner.invoke(cli, ['--method=foo'])199 assert not result.exception200 assert result.output == 'foo\n'201202 result = runner.invoke(cli, ['--method=meh'])203 assert result.exit_code == 2204 assert 'Invalid value for "--method": invalid choice: meh. ' \205 '(choose from foo, bar, baz)' in result.output