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:
objectSimplified version of the stdlib
_AssertRaisesContextthat 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_raises_contains_str(expected_exc: Type[BaseException], expected_text: str, msg: OptStr = None)[source]
- assert_parse_results(cmd_cls: CommandCls, argv: Argv, expected: Expected, msg: OptStr = None) Command[source]
- assert_env_parse_results(cmd_cls: CommandCls, argv: Argv, env: Env, expected: Expected, msg: OptStr = None) Command[source]
- assert_env_parse_results_cases(cmd_cls: CommandCls, cases: Iterable[EnvCase], msg: OptStr = None)[source]
- assert_parse_fails(cmd_cls: CommandCls, argv: Argv, expected_exc: ExcType = <class 'cli_command_parser.exceptions.UsageError'>, expected_pattern: OptStr = None, msg: OptStr = None, regex: bool = False)[source]
- assert_parse_fails_cases(cmd_cls: CommandCls, cases: ExcCases, exc: ExcType | None = None, msg: OptStr = None)[source]
- assert_argv_parse_fails_cases(cmd_cls: CommandCls, cases: Iterable[Argv], exc: ExcType = <class 'cli_command_parser.exceptions.UsageError'>, msg: OptStr = None)[source]
Convenience method for calling
assert_parse_fails_cases()with a default exception type.
- assert_call_fails(func: Callable, kwargs: Kwargs, exc: ExcType = <class 'Exception'>, expected_exc_msg: OptStr = None, msg: OptStr = None)[source]
- assert_call_fails_cases(func: Callable, cases: Iterable[CallExceptionCase], msg: OptStr = None)[source]
- assert_strings_equal(expected: str, actual: str, message: OptStr = None, diff_lines: int = 3, trim: bool = False)[source]
Assert that two strings are equal. Primarily intended for cases involving multi-line text.
- Parameters:
expected¶ – Expected string
actual¶ – Actual string that was produced by code that is being tested
message¶ – Message to be displayed if the strings do not match (default: a multi-line colored diff is displayed)
diff_lines¶ – Number of lines of context before/after differing lines that should be displayed in the diff
trim¶ – Whether the expected and actual strings should be normalized by removing trailing whitespace
- cli_command_parser.testing.format_diff(a: str, b: str, name_a: str = 'expected', name_b: str = ' actual', n: int = 3) str[source]
- class cli_command_parser.testing.RedirectStreams(stdin: IO | str | bytes | None = None)[source]
Bases:
AbstractContextManager
- cli_command_parser.testing.get_help_text(cmd: Type[Command] | Command, terminal_width: int = 199) str[source]
- cli_command_parser.testing.load_command(directory: Path, name: str, cmd_name: str, **kwargs) Iterator[Type[Command]][source]
- class cli_command_parser.testing.TemporaryDir(suffix=None, prefix=None, dir=None, ignore_cleanup_errors=False, *, delete=True)[source]
Bases:
TemporaryDirectory