Testing Module

Helpers for unit tests

author:

Doug Skrypa

class cli_command_parser.testing.AssertRaisesWithStringContext(test_case: TestCase, expected_exc: Type[BaseException], text: OptStr = None, msg: OptStr = None)[source]

Bases: object

Simplified version of the stdlib _AssertRaisesContext that tests whether the raised exception’s string contains the given text rather than matching it against a regex pattern. This avoids the regex overhead when it isn’t necessary, which is the majority of the time for such checks in this project.

test_case
expected_exc
expected_text
msg
class cli_command_parser.testing.ParserTest(methodName='runTest')[source]

Bases: TestCase

assert_dict_equal(d1, d2, msg: str = None)[source]
assert_raises_contains_str(expected_exc: Type[BaseException], expected_text: str, msg: str = None)[source]
assert_parse_results(cmd_cls: CommandCls, argv: Argv, expected: Expected, msg: str = None) Command[source]
assert_parse_results_cases(cmd_cls: CommandCls, cases: Iterable[Case], msg: str = None)[source]
assert_env_parse_results(cmd_cls: CommandCls, argv: Argv, env: Env, expected: Expected, msg: str = None) Command[source]
assert_env_parse_results_cases(cmd_cls: CommandCls, cases: Iterable[EnvCase], msg: str = None)[source]
assert_parse_fails(cmd_cls: CommandCls, argv: Argv, expected_exc: ExcType = <class 'cli_command_parser.exceptions.UsageError'>, expected_pattern: str = None, msg: str = None, regex: bool = False)[source]
assert_parse_fails_cases(cmd_cls: CommandCls, cases: ExcCases, exc: ExcType = None, msg: str = None)[source]
assert_argv_parse_fails_cases(cmd_cls: CommandCls, cases: Iterable[Argv], exc: ExcType = <class 'cli_command_parser.exceptions.UsageError'>, msg: str = None)[source]

Convenience method for calling assert_parse_fails_cases() with a default exception type.

assert_call_fails(func: ~typing.Callable, kwargs: ~typing.Dict[str, ~typing.Any], exc: ~typing.Type[Exception] = <class 'Exception'>, expected_exc_msg: str = None, msg: str = None)[source]
assert_call_fails_cases(func: Callable, cases: Iterable[Tuple[Dict[str, Any], Type[Exception]] | Tuple[Dict[str, Any], Type[Exception], str]], msg: str = None)[source]
assert_strings_equal(expected: str, actual: str, message: str = None, diff_lines: int = 3, trim: bool = False)[source]
assert_str_starts_with_line(prefix: str, text: str)[source]
assert_str_contains(sub_text: str, text: str, diff_lines: int = 3)[source]
env_vars(case: str, **env_vars)[source]
cli_command_parser.testing.format_diff(a: str, b: str, name_a: str = 'expected', name_b: str = '  actual', n: int = 3) str[source]
cli_command_parser.testing.format_dict_diff(a: dict[str, Any], b: dict[str, Any]) str[source]
class cli_command_parser.testing.RedirectStreams(stdin: IO | str | bytes | None = None)[source]

Bases: AbstractContextManager

property stdout: str[source]
property stderr: str[source]
cli_command_parser.testing.get_usage_text(cmd: Type[Command]) str[source]
cli_command_parser.testing.get_help_text(cmd: Type[Command] | Command, terminal_width: int = 199) str[source]
cli_command_parser.testing.get_rst_text(cmd: Type[Command] | Command) str[source]
cli_command_parser.testing.sealed_mock(*args, **kwargs)[source]
cli_command_parser.testing.load_command(directory: Path, name: str, cmd_name: str, **kwargs) ContextManager[CommandCls][source]
class cli_command_parser.testing.TemporaryDir(suffix=None, prefix=None, dir=None, ignore_cleanup_errors=False)[source]

Bases: TemporaryDirectory