I have a CLI application built that uses grouped commands and sub-commands. Everything is working. However, I want to ensure this continues to be the case in the future, so I want to ensure that all of my commands and sub-commands have loaded correctly.
My first thought to do this was to just run
commands = ["config", "othercommand"]
runner = CliRunner()
result = runner.invoke(cli.main)
for command in commands:
assert command in result.output
This has a few pitfalls, from my point of view.
- It requires that I update
commands
each time I add a new one. I'd love to be able to automatically generate this. - If I have a command that is a relatively common word that would appear in help test (ie.
config
), I could get a false result. - I don't know how I'd handle sub-commands of each of these in an elegant way.
My application is laid out like this:
@click.group()
def main():
"""My entry point"""
@click.group()
def config():
"""config group"""
@config.command()
def add_config():
"""this is a subcommand to config"""
@click.group()
def othercommand():
"""othercommand group"""
@othercommand.command()
def do_thing():
"""this is a subcommand to othercommand"""
My question: Is there a way to get a list of all commands (and sub-commands) that I can use and do this from my test suite? Preferably without all the surrounding help test so that I can eliminate false results.