CLI testingΒΆ
Todo
Write example and tutorial.
click_extra.testing APIΒΆ
classDiagram
AssertionError <|-- RegexLineMismatch
BytesIO <|-- BytesIOCopy
CliRunner <|-- ExtraCliRunner
CLI testing and simulation of their execution.
- click_extra.testing.PROMPT = '$ 'ΒΆ
Prompt used to simulate the CLI execution.
Hint
Use ASCII characters to avoid issues with Windows terminals.
- click_extra.testing.INDENT = ' 'ΒΆ
Constants for rendering of CLI execution.
- click_extra.testing.args_cleanup(*args)[source]ΒΆ
Flatten recursive iterables, remove all
None, and cast each element to strings.Helps serialize
pathlib.Pathand other objects.It also allows for nested iterables and
Nonevalues as CLI arguments for convenience. We just need to flatten and filters them out.
- click_extra.testing.format_cli_prompt(cmd_args, extra_env=None)[source]ΒΆ
Simulate the console prompt used to invoke the CLI.
- Return type:
- click_extra.testing.render_cli_run(args, result, env=None)[source]ΒΆ
Generates the full simulation of CLI execution, including output.
Mostly used to print debug traces to user or in test results.
- Return type:
- click_extra.testing.print_cli_run(args, result, env=None)[source]ΒΆ
Prints the full simulation of CLI execution, including output.
- Return type:
- click_extra.testing.INVOKE_ARGS = {'args', 'catch_exceptions', 'cli', 'color', 'env', 'input', 'self'}ΒΆ
Parameter IDs of
click.testing.CliRunner.invoke().We need to collect them to help us identify which extra parameters passed to
invoke()collides with its original signature.Warning
This has been reported upstream to Click project but has been rejected and not considered an issue worth fixing.
- class click_extra.testing.BytesIOCopy(copy_to)[source]ΒΆ
Bases:
BytesIOPatch
io.BytesIOto let the written stream be copied to another.Caution
This has been proposed upstream to Click project but has not been merged yet.
- class click_extra.testing.ExtraCliRunner(charset='utf-8', env=None, echo_stdin=False, catch_exceptions=True)[source]ΒΆ
Bases:
CliRunnerAugment
click.testing.CliRunnerwith extra features and bug fixes.- force_color: bool = FalseΒΆ
Global class attribute to override the
colorparameter ininvoke.Note
This was initially developed to force the initialization of the runner during the setup of Sphinx new directives. This was the only way we found, as to patch some code we had to operate at the class level.
- invoke(cli, *args, input=None, env=None, catch_exceptions=True, color=None, **extra)[source]ΒΆ
Same as
click.testing.CliRunner.invoke()with extra features.The first positional parameter is the CLI to invoke. The remaining positional parameters of the function are the CLI arguments. All other parameters are required to be named.
The CLI arguments can be nested iterables of arbitrary depth. This is useful for argument composition of test cases with @pytest.mark.parametrize.
Allow forcing of the
colorproperty at the class-level viaforce_colorattribute.Adds a special case in the form of
color="forced"parameter, which allows colored output to be kept, while forcing the initialization ofContext.color = True. This is not allowed in current implementation ofclick.testing.CliRunner.invoke()because of colliding parameters.Strips all ANSI codes from results if
colorwas explicirely set toFalse.Always prints a simulation of the CLI execution as the user would see it in its terminal. Including colors.
Pretty-prints a formatted exception traceback if the command fails.
- Parameters:
cli (
Command) β CLI to invoke.*args β
can be nested iterables composed of
str,pathlib.Pathobjects andNonevalues. The nested structure will be flattened andNonevalues will be filtered out. Then all elements will be casted tostr. Seeargs_cleanup()for details.input (
str|bytes|IO|None) β same asclick.testing.CliRunner.invoke().env (
Mapping[str,str|None] |None) β same asclick.testing.CliRunner.invoke().catch_exceptions (
bool) β same asclick.testing.CliRunner.invoke().color (
Union[bool,Literal['forced'],None]) β If a boolean, the parameter will be passed as-is toclick.testing.CliRunner.isolation(). If"forced", the parameter will be passed asTruetoclick.testing.CliRunner.isolation()and an extracolor=Trueparameter will be passed to the invoked CLI.**extra β
same as
click.testing.CliRunner.invoke(), but colliding parameters are allowed and properly passed on to the invoked CLI.
- Return type:
- env: cabc.Mapping[str, str | None]ΒΆ
- click_extra.testing.unescape_regex(text)[source]ΒΆ
De-obfuscate a regex for better readability.
This is like the reverse of
re.escape().- Return type:
- exception click_extra.testing.RegexLineMismatch(regex_line, content_line, line_number)[source]ΒΆ
Bases:
AssertionErrorRaised when a regex line does not match the corresponding content line.
- click_extra.testing.regex_fullmatch_line_by_line(regex, content)[source]ΒΆ
Check that the
contentmatches the givenregex.If the
regexdoes not fully match thecontent, raise anAssertionError, with a message showing the first mismatching line.This is useful when comparing large walls of text, such as CLI output.
- Return type: