test_basic.py
This file
Path
tests/test_basic.py
166 lines · 4.8 KiB · Python
Last changed by
Made tests pass on 3.xArmin Ronacherbefore adoption14c9b1a
History of this fileTake it
Started a bit of test codeArmin Ronacherbefore adoption3f695c6
lines 1–11why, one step back1# -*- coding: utf-8 -*-2import click345def test_basic_functionality(runner):6 @click.command()7 def cli():8 """Hello World!"""9 click.echo('I EXECUTED')1011 result = runner.invoke(cli, ['--help'])Added some tests for some optionsArmin Ronacherbefore adoption7360097
line 12why, one step back12 assert not result.exceptionStarted a bit of test codeArmin Ronacherbefore adoption3f695c6
lines 13–18why, one step back13 assert 'Hello World!' in result.output14 assert 'Show this message and exit.' in result.output15 assert result.exit_code == 016 assert 'I EXECUTED' not in result.output1718 result = runner.invoke(cli, [])Added some tests for some optionsArmin Ronacherbefore adoption7360097
line 19why, one step back19 assert not result.exceptionStarted a bit of test codeArmin Ronacherbefore adoption3f695c6
lines 20–35why, one step back20 assert 'I EXECUTED' in result.output21 assert result.exit_code == 0222324def test_basic_group(runner):25 @click.group()26 def cli():27 """This is the root."""28 click.echo('ROOT EXECUTED')2930 @cli.command()31 def subcommand():32 """This is a subcommand."""33 click.echo('SUBCOMMAND EXECUTED')3435 result = runner.invoke(cli, ['--help'])Added some tests for some optionsArmin Ronacherbefore adoption7360097
line 36why, one step back36 assert not result.exceptionStarted a bit of test codeArmin Ronacherbefore adoption3f695c6
lines 37–42why, one step back37 assert 'This is the root' in result.output38 assert 'This is a subcommand.' in result.output39 assert result.exit_code == 040 assert 'ROOT EXECUTED' not in result.output4142 result = runner.invoke(cli, ['subcommand'])Added some tests for some optionsArmin Ronacherbefore adoption7360097
line 43why, one step back43 assert not result.exceptionStarted a bit of test codeArmin Ronacherbefore adoption3f695c6
lines 44–55why, one step back44 assert result.exit_code == 045 assert 'ROOT EXECUTED' in result.output46 assert 'SUBCOMMAND EXECUTED' in result.output474849def test_basic_option(runner):50 @click.command()51 @click.option('--foo', default='no value')52 def cli(foo):53 click.echo('FOO:[%s]' % foo)5455 result = runner.invoke(cli, [])Added some tests for some optionsArmin Ronacherbefore adoption7360097
line 56why, one step back56 assert not result.exceptionStarted a bit of test codeArmin Ronacherbefore adoption3f695c6
lines 57–59why, one step back57 assert 'FOO:[no value]' in result.output5859 result = runner.invoke(cli, ['--foo=42'])Added some tests for some optionsArmin Ronacherbefore adoption7360097
line 60why, one step back60 assert not result.exceptionStarted a bit of test codeArmin Ronacherbefore adoption3f695c6
lines 61–63why, one step back61 assert 'FOO:[42]' in result.output6263 result = runner.invoke(cli, ['--foo'])Added some tests for some optionsArmin Ronacherbefore adoption7360097
line 64why, one step back64 assert result.exceptionStarted a bit of test codeArmin Ronacherbefore adoption3f695c6
lines 65–67why, one step back65 assert '--foo option requires an argument' in result.output6667 result = runner.invoke(cli, ['--foo='])Added some tests for some optionsArmin Ronacherbefore adoption7360097
line 68why, one step back68 assert not result.exceptionStarted a bit of test codeArmin Ronacherbefore adoption3f695c6
lines 69–71why, one step back69 assert 'FOO:[]' in result.output7071 result = runner.invoke(cli, [u'--foo=\N{SNOWMAN}'])Added some tests for some optionsArmin Ronacherbefore adoption7360097
line 72why, one step back72 assert not result.exceptionStarted a bit of test codeArmin Ronacherbefore adoption3f695c6
line 73why, one step back73 assert u'FOO:[\N{SNOWMAN}]' in result.outputAdded some tests for some optionsArmin Ronacherbefore adoption7360097
lines 74–149why, one step back747576def test_int_option(runner):77 @click.command()78 @click.option('--foo', default=42)79 def cli(foo):80 click.echo('FOO:[%s]' % (foo * 2))8182 result = runner.invoke(cli, [])83 assert not result.exception84 assert 'FOO:[84]' in result.output8586 result = runner.invoke(cli, ['--foo=23'])87 assert not result.exception88 assert 'FOO:[46]' in result.output8990 result = runner.invoke(cli, ['--foo=bar'])91 assert result.exception92 assert 'Invalid value for "--foo": bar is not a valid integer' \93 in result.output949596def test_float_option(runner):97 @click.command()98 @click.option('--foo', default=42, type=click.FLOAT)99 def cli(foo):100 assert type(foo) is float101 click.echo('FOO:[%s]' % foo)102103 result = runner.invoke(cli, [])104 assert not result.exception105 assert 'FOO:[42.0]' in result.output106107 result = runner.invoke(cli, ['--foo=23.5'])108 assert not result.exception109 assert 'FOO:[23.5]' in result.output110111 result = runner.invoke(cli, ['--foo=bar'])112 assert result.exception113 assert 'Invalid value for "--foo": bar is not a valid float' \114 in result.output115116117def test_boolean_option(runner):118 for default in True, False:119 @click.command()120 @click.option('--with-foo/--without-foo', default=default)121 def cli(with_foo):122 click.echo(with_foo)123124 result = runner.invoke(cli, ['--with-foo'])125 assert not result.exception126 assert result.output == 'True\n'127 result = runner.invoke(cli, ['--without-foo'])128 assert not result.exception129 assert result.output == 'False\n'130 result = runner.invoke(cli, [])131 assert not result.exception132 assert result.output == '%s\n' % default133134 for default in True, False:135 @click.command()136 @click.option('--flag', is_flag=True, default=default)137 def cli(flag):138 click.echo(flag)139140 result = runner.invoke(cli, ['--flag'])141 assert not result.exception142 assert result.output == '%s\n' % (not default)143 result = runner.invoke(cli, [])144 assert not result.exception145 assert result.output == '%s\n' % (default)146147148def test_file_option(runner):149 @click.command()Made tests pass on 3.xArmin Ronacherbefore adoption14c9b1a
line 150why, one step back150 @click.option('--file', type=click.File('w'))Added some tests for some optionsArmin Ronacherbefore adoption7360097
lines 151–154why, one step back151 def input(file):152 file.write('Hello World!\n')153154 @click.command()Made tests pass on 3.xArmin Ronacherbefore adoption14c9b1a
line 155why, one step back155 @click.option('--file', type=click.File('r'))Added some tests for some optionsArmin Ronacherbefore adoption7360097
lines 156–166why, one step back156 def output(file):157 click.echo(file.read())158159 with runner.isolated_filesystem():160 result_in = runner.invoke(input, ['--file=example.txt'])161 result_out = runner.invoke(output, ['--file=example.txt'])162163 assert not result_in.exception164 assert result_in.output == ''165 assert not result_out.exception166 assert result_out.output == 'Hello World!\n\n'