parametrize basic option tests
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by David Lord · committed by David Lord · parent 9072f18 · Browse files · 1 file·+74 added−64 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Trailers
- Co-authored-by: BALaka-18 <balaka2605@gmail.com>
Diff stats
1 file+74 added−64 removed
- tests:+74 added−64 removed
Provenance
- Commit
ad422c4- Tree
e049d2b- Author
David Lord <davidism@gmail.com>- Committer
David Lord <davidism@gmail.com>- Parent
9072f18
1 file changed+74 added−64 removed
modified:tests/test_basic.py+74 added-64 removed
1 import os2-import uuid2 from itertools import chain3 4 import pytest5 96 unmodified lines102 result = runner.invoke(cli, ["sub"])103 assert result.output == "sub"104 105 107-def test_basic_option(runner):106+@pytest.mark.parametrize(107+ ("args", "expect"),108+ [109+ ([], "S:[no value]"),110+ (["--s=42"], "S:[42]"),111+ (["--s"], "Error: Option '--s' requires an argument."),112+ (["--s="], "S:[]"),113+ (["--s=\N{SNOWMAN}"], "S:[\N{SNOWMAN}]"),114+ ],115+)116+def test_string_option(runner, args, expect):117 @click.command()109- @click.option("--foo", default="no value")110- def cli(foo):111- click.echo(f"FOO:[{foo}]")118+ @click.option("--s", default="no value")119+ def cli(s):120+ click.echo(f"S:[{s}]")121 113- result = runner.invoke(cli, [])114- assert not result.exception115- assert "FOO:[no value]" in result.output122+ result = runner.invoke(cli, args)123+ assert expect in result.output124 117- result = runner.invoke(cli, ["--foo=42"])118- assert not result.exception119- assert "FOO:[42]" in result.output125+ if expect.startswith("Error:"):126+ assert result.exception is not None127+ else:128+ assert result.exception is None129 121- result = runner.invoke(cli, ["--foo"])122- assert result.exception123- assert "Option '--foo' requires an argument." in result.output130 125- result = runner.invoke(cli, ["--foo="])126- assert not result.exception127- assert "FOO:[]" in result.output128-129- result = runner.invoke(cli, ["--foo=\N{SNOWMAN}"])130- assert not result.exception131- assert "FOO:[\N{SNOWMAN}]" in result.output132-133-134-def test_int_option(runner):131+@pytest.mark.parametrize(132+ ("args", "expect"),133+ [134+ ([], "I:[84]"),135+ (["--i=23"], "I:[46]"),136+ (["--i=x"], "Error: Invalid value for '--i': 'x' is not a valid integer."),137+ ],138+)139+def test_int_option(runner, args, expect):140 @click.command()136- @click.option("--foo", default=42)137- def cli(foo):138- click.echo(f"FOO:[{foo * 2}]")141+ @click.option("--i", default=42)142+ def cli(i):143+ click.echo(f"I:[{i * 2}]")144 140- result = runner.invoke(cli, [])141- assert not result.exception142- assert "FOO:[84]" in result.output145+ result = runner.invoke(cli, args)146+ assert expect in result.output147 144- result = runner.invoke(cli, ["--foo=23"])145- assert not result.exception146- assert "FOO:[46]" in result.output148+ if expect.startswith("Error:"):149+ assert result.exception is not None150+ else:151+ assert result.exception is None152 148- result = runner.invoke(cli, ["--foo=bar"])149- assert result.exception150- assert "Invalid value for '--foo': 'bar' is not a valid integer." in result.output153 152-153-def test_uuid_option(runner):154+@pytest.mark.parametrize(155+ ("args", "expect"),156+ [157+ ([], "U:[ba122011-349f-423b-873b-9d6a79c688ab]"),158+ (159+ ["--u=821592c1-c50e-4971-9cd6-e89dc6832f86"],160+ "U:[821592c1-c50e-4971-9cd6-e89dc6832f86]",161+ ),162+ (["--u=x"], "Error: Invalid value for '--u': 'x' is not a valid UUID."),163+ ],164+)165+def test_uuid_option(runner, args, expect):166 @click.command()167 @click.option(168 "--u", default="ba122011-349f-423b-873b-9d6a79c688ab", type=click.UUID169 )170 def cli(u):159- assert type(u) is uuid.UUID171 click.echo(f"U:[{u}]")172 162- result = runner.invoke(cli, [])163- assert not result.exception164- assert "U:[ba122011-349f-423b-873b-9d6a79c688ab]" in result.output173+ result = runner.invoke(cli, args)174+ assert expect in result.output175 166- result = runner.invoke(cli, ["--u=821592c1-c50e-4971-9cd6-e89dc6832f86"])167- assert not result.exception168- assert "U:[821592c1-c50e-4971-9cd6-e89dc6832f86]" in result.output176+ if expect.startswith("Error:"):177+ assert result.exception is not None178+ else:179+ assert result.exception is None180 170- result = runner.invoke(cli, ["--u=bar"])171- assert result.exception172- assert "Invalid value for '--u': 'bar' is not a valid UUID." in result.output181 174-175-def test_float_option(runner):182+@pytest.mark.parametrize(183+ ("args", "expect"),184+ [185+ ([], "F:[42.0]"),186+ ("--f=23.5", "F:[23.5]"),187+ ("--f=x", "Error: Invalid value for '--f': 'x' is not a valid float."),188+ ],189+)190+def test_float_option(runner, args, expect):191 @click.command()177- @click.option("--foo", default=42, type=click.FLOAT)178- def cli(foo):179- assert type(foo) is float180- click.echo(f"FOO:[{foo}]")192+ @click.option("--f", default=42.0)193+ def cli(f):194+ click.echo(f"F:[{f}]")195 182- result = runner.invoke(cli, [])183- assert not result.exception184- assert "FOO:[42.0]" in result.output196+ result = runner.invoke(cli, args)197+ assert expect in result.output198 186- result = runner.invoke(cli, ["--foo=23.5"])187- assert not result.exception188- assert "FOO:[23.5]" in result.output199+ if expect.startswith("Error:"):200+ assert result.exception is not None201+ else:202+ assert result.exception is None203 190- result = runner.invoke(cli, ["--foo=bar"])191- assert result.exception192- assert "Invalid value for '--foo': 'bar' is not a valid float." in result.output204 194-205 def test_boolean_option(runner):206 for default in True, False:207 208 @click.command()368 unmodified lines