consolekit.testing
Test helpers.
New in version 0.9.0.
Attention
This module has the following additional requirements:
coincidence>=0.1.0 pytest>=6.0.0 pytest-regressions>=2.0.2
These can be installed as follows:
python -m pip install consolekit[testing]
Classes:
|
Provides functionality to invoke and test a Click script in an isolated environment. |
|
Holds the captured result of an invoked CLI script. |
Functions:
Returns a click runner for this test function. |
-
class
CliRunner
(charset='UTF-8', env=None, *, echo_stdin=False, mix_stderr=True)[source] Bases:
CliRunner
Provides functionality to invoke and test a Click script in an isolated environment.
This only works in single-threaded systems without any concurrency as it changes the global interpreter state.
- Parameters
charset (
str
) – The character set for the input and output data. Default'UTF-8'
.env (
Optional
[Mapping
[str
,str
]]) – A dictionary with environment variables to override. DefaultNone
.echo_stdin (
bool
) – IfTrue
, then reading from stdin writes to stdout. This is useful for showing examples in some circumstances. Note that regular prompts will automatically echo the input. DefaultFalse
.mix_stderr (
bool
) – IfFalse
, then stdout and stderr are preserved as independent streams. This is useful for Unix-philosophy apps that have predictable stdout and noisy stderr, such that each may be measured independently. DefaultTrue
.
-
invoke
(cli, args=None, input=None, env=None, *, catch_exceptions=False, color=False, **extra)[source] Invokes a command in an isolated environment.
The arguments are forwarded directly to the command line script, the
extra
keyword arguments are passed to themain()
function of the command.- Parameters
cli (
BaseCommand
) – The command to invoke.args (
Union
[str
,Iterable
[str
],None
]) – The arguments to invoke. It may be given as an iterable or a string. When given as string it will be interpreted as a Unix shell command. More details atshlex.split()
. DefaultNone
.input (
Union
[bytes
,str
,IO
,None
]) – The input data forsys.stdin
. DefaultNone
.env (
Optional
[Mapping
[str
,str
]]) – The environment overrides. DefaultNone
.catch_exceptions (
bool
) – Whether to catch any other exceptions thanSystemExit
. DefaultFalse
.color (
bool
) – whether the output should contain color codes. The application can still override this explicitly. DefaultFalse
.**extra – The keyword arguments to pass to
click.Command.main()
.
- Return type
-
class
Result
(runner, stdout_bytes, stderr_bytes, exit_code, exception, exc_info=None)[source] Bases:
Result
Holds the captured result of an invoked CLI script.
- Parameters
runner (
CliRunner
) – The runner that created the result.stdout_bytes (
bytes
) – The standard output as bytes.stderr_bytes (
Optional
[bytes
]) – The standard error as bytes, orNone
if not available.exit_code (
int
) – The command’s exit code.exception (
Optional
[BaseException
]) – The exception that occurred, if any.exc_info (
Optional
[Tuple
[Type
[BaseException
],BaseException
,traceback
]]) – The traceback, if an exception occurred. DefaultNone
.
Methods:
check_stdout
(file_regression[, extension])Perform a regression check on the standard output from the command.
Attributes:
The (standard) output as a string.
The standard error as a string.
The standard output as a string.