I have a CLI in Python using typer. The main script simply assembles subcommands like so:
import typer
from mymodule import subcommand
my_cli = typer.Typer(no_args_is_help=True)
# CLI commands
my_cli.add_typer(subcommand, name="subcommand", no_args_is_help=True)
def main() -> None:
my_cli()
Each subcommand is a typer.Typer instance itself, with commands added via the @Typer.command() decorator, for example mymodule.py could be like:
import typer
subcommand = typer.Typer()
@subcommand.command()
def foo():
print("Bar")
I don't know what is the best way to go in terms of testing here. Typer allows testing by invoking the CLI:
from typer.testing import CliRunner
from main import my_cli
runner = CliRunner()
def test_app():
result = runner.invoke(my_cli, ["subcommand", "foo"])
assert result.exit_code == 0
There are already tests for the individual subcommands, so I don't want to trigger the actual logic (i.e. the function foo).
I tried to patch the methods and check if they are called correctly by the CLI, but I'm not able to find the correct way to patch.
What would be the best practice for testing this? Is it possible to patch the functions with this structure?