Skip to content

Testing

SnkCliRunner dataclass

Dynamically creates a CLI Runner for testing.

Parameters:

Name Type Description Default
cli CLI

The CLI object to be tested.

required

Attributes:

Name Type Description
runner CliRunner

The CliRunner object used for testing.

Source code in src/snk_cli/testing.py
@dataclass
class SnkCliRunner:
    """
    Dynamically creates a CLI Runner for testing.

    Args:
      cli (CLI): The CLI object to be tested.

    Attributes:
      runner (CliRunner): The CliRunner object used for testing.
    """

    cli: CLI
    runner = CliRunner(mix_stderr=False)

    def invoke(self, args: List[str]) -> Result:
        old_argv = sys.argv
        sys.argv = ['cli'] + args  # ensure that the CLI is invoked with the correct arguments
        result = self.runner.invoke(self.cli.app, args)
        sys.argv = old_argv
        return result