Testing
provide.testkit.cli.testing
¶
CLI Testing Utilities for Foundation.
Provides comprehensive testing support for CLI applications including context mocking, isolated runners, and configuration helpers.
Classes¶
CliTestCase
¶
Base class for CLI test cases with common utilities.
Functions¶
assert_json_output
¶
Assert that output is valid JSON matching expected.
Source code in provide/testkit/cli/testing.py
create_temp_file
¶
Create a temporary file that will be cleaned up.
Source code in provide/testkit/cli/testing.py
invoke
¶
setup_method
¶
MockContext
¶
Bases: CLIContext
Mock context for testing that tracks method calls.
Initialize mock context with tracking.
Source code in provide/testkit/cli/testing.py
Functions¶
click_testing_mode
¶
Pytest fixture to enable Click testing mode.
Sets CLICK_TESTING=1 environment variable for the duration of the test, then restores the original value. This fixture makes it easy to enable Click testing mode without manual environment variable management.
Usage
def test_my_cli(click_testing_mode): # Test CLI code here - CLICK_TESTING is automatically set pass
Source code in provide/testkit/cli/testing.py
create_test_cli
¶
create_test_cli(
name: str = "test-cli",
version: str = "1.0.0",
commands: list[Command] | None = None,
) -> click.Group
Create a test CLI group with standard options.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
CLI name |
'test-cli'
|
version
|
str
|
CLI version |
'1.0.0'
|
commands
|
list[Command] | None
|
Optional list of commands to add |
None
|
Returns:
| Type | Description |
|---|---|
Group
|
Click Group configured for testing |
Source code in provide/testkit/cli/testing.py
isolated_cli_runner
¶
Create an isolated test environment for CLI testing.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
env
|
dict[str, str] | None
|
Environment variables to set |
None
|
Yields:
| Type | Description |
|---|---|
CliRunner
|
CliRunner instance in isolated filesystem |
Source code in provide/testkit/cli/testing.py
temp_config_file
¶
Create a temporary configuration file for testing.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
content
|
dict[str, Any] | str
|
Configuration content (dict or string) |
required |
format
|
str
|
File format (json, toml, yaml) |
'json'
|
Yields:
| Type | Description |
|---|---|
Path
|
Path to temporary config file |