API Documentation¶
This section describes the context managers and helper functions provided by the CLI test helpers package.
Utilities¶
- cli_test_helpers.shell(command, **kwargs)[source]¶
Execute a shell command capturing output and exit code.
This is a better version of
os.system()that captures output and returns a convenient namespace object with 3 values:exit_code:The command’s exit code
stdout:The command’s normal output
stderr:The command’s error output
Context Managers¶
- class cli_test_helpers.ArgvContext(*new_args)[source]¶
A simple context manager allowing to temporarily override
sys.argv.Use it to mimic the command line arguments of the CLI application. Note that the first argument (index
0) is always the script or application name.
- class cli_test_helpers.EnvironContext(**kwargs)[source]¶
A simple context manager allowing to temporarily set environment values.
This is syntactic sugar for unittest.mock.patch.dict as seen in the Python documentation, plus allowing to clear single environment variables.
- class cli_test_helpers.RandomDirectoryContext(*args, **kwargs)[source]¶
Change the execution directory to a random location, temporarily.
Use it to verify that your CLI application can operate from any location, and is independent from your development directory tree. Keyword arguments are optional and identical to the ones of tempfile.TemporaryDirectory of the Python standard library.
Mixins¶
Note
Used internally by all context managers, not meant for direct use.
- class cli_test_helpers.mixins.LoggingIsolationMixin[source]¶
Mixin that provides automatic logging configuration isolation.
Isolates logging configuration by temporarily clearing logging handlers when entering the context and restoring them when exiting. This allows code under test to call
logging.basicConfig()successfully even when test frameworks (like pytest) have already configured logging handlers.All context managers use this mixin.