typer.testing
1from typing import IO, Any, Mapping, Optional, Sequence, Union 2 3from click.testing import CliRunner as ClickCliRunner # noqa 4from click.testing import Result 5from typer.main import Typer 6from typer.main import get_command as _get_command 7 8 9class CliRunner(ClickCliRunner): 10 def invoke( # type: ignore 11 self, 12 app: Typer, 13 args: Optional[Union[str, Sequence[str]]] = None, 14 input: Optional[Union[bytes, str, IO[Any]]] = None, 15 env: Optional[Mapping[str, str]] = None, 16 catch_exceptions: bool = True, 17 color: bool = False, 18 **extra: Any, 19 ) -> Result: 20 use_cli = _get_command(app) 21 return super().invoke( 22 use_cli, 23 args=args, 24 input=input, 25 env=env, 26 catch_exceptions=catch_exceptions, 27 color=color, 28 **extra, 29 )
10class CliRunner(ClickCliRunner): 11 def invoke( # type: ignore 12 self, 13 app: Typer, 14 args: Optional[Union[str, Sequence[str]]] = None, 15 input: Optional[Union[bytes, str, IO[Any]]] = None, 16 env: Optional[Mapping[str, str]] = None, 17 catch_exceptions: bool = True, 18 color: bool = False, 19 **extra: Any, 20 ) -> Result: 21 use_cli = _get_command(app) 22 return super().invoke( 23 use_cli, 24 args=args, 25 input=input, 26 env=env, 27 catch_exceptions=catch_exceptions, 28 color=color, 29 **extra, 30 )
The CLI runner provides functionality to invoke a Click command line script for unittesting purposes in a isolated environment. This only works in single-threaded systems without any concurrency as it changes the global interpreter state.
Parameters
- charset: the character set for the input and output data.
- env: a dictionary with environment variables for overriding.
- echo_stdin: if this is set to
True
, 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. - mix_stderr: if this is set to
False
, 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
11 def invoke( # type: ignore 12 self, 13 app: Typer, 14 args: Optional[Union[str, Sequence[str]]] = None, 15 input: Optional[Union[bytes, str, IO[Any]]] = None, 16 env: Optional[Mapping[str, str]] = None, 17 catch_exceptions: bool = True, 18 color: bool = False, 19 **extra: Any, 20 ) -> Result: 21 use_cli = _get_command(app) 22 return super().invoke( 23 use_cli, 24 args=args, 25 input=input, 26 env=env, 27 catch_exceptions=catch_exceptions, 28 color=color, 29 **extra, 30 )
Invokes a command in an isolated environment. The arguments are
forwarded directly to the command line script, the extra
keyword
arguments are passed to the ~clickpkg.Command.main()
function of
the command.
This returns a Result
object.
Parameters
- cli: the command to invoke
- args: 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 at
shlex.split()
. - input: the input data for
sys.stdin
. - env: the environment overrides.
- catch_exceptions: Whether to catch any other exceptions than
SystemExit
. - extra: the keyword arguments to pass to
main()
. - color: whether the output should contain color codes. The application can still override this explicitly.
Changed in version 8.0:
The result object has the return_value
attribute with
the value returned from the invoked command.
Changed in version 4.0:
Added the color
parameter.
Changed in version 3.0:
Added the catch_exceptions
parameter.
Changed in version 3.0:
The result object has the exc_info
attribute with the
traceback if available.
Inherited Members
- click.testing.CliRunner
- CliRunner
- charset
- env
- echo_stdin
- mix_stderr
- get_default_prog_name
- make_env
- isolation
- isolated_filesystem