2

I´m currently writing tests for my application and therefore, I have to test some click.group commands I defined:

Let´s say I defined them like:

@click.group(cls=MyGroup)
@click.pass_context
def myapp(ctx):
    init_stuff()    

@myapp.command()
@click.option('--myOption')
def foo(myOption: str) -> None:
    do_stuff() # change some files, print, create other files

I know that I could use the CliRunner from click.testing. However, I just want to make sure, that the command is called, but I DONT WANT it to execute any code (for example by applying the CliRunner.invoke()).

How could this be done? I couldn´t come up with a solution using mocking with foo for example. Or do I have to execute code lets say using the isolated_filesystem() which CliRunner provides?

So the question is: What would be the most efficient way to test my commands when defined like shown above?

Many thanks in advance

2
  • 1
    So you want to unittest the click framework? Why would you suppose the framework is not working correctly? Commented May 17, 2020 at 5:11
  • 1
    @StephenRauch thanks for your comment. Y I should have clarified this. I dont want to test the framework; but now as I´m reading your comment, it seems testing only the calls is useless. I think I will actually test the commands instead of just their invocation with click. This seems much more reasonable. Thanks again ;) Commented May 17, 2020 at 20:58

1 Answer 1

1

You could add a --dry-run flag to your group or some commands, and save it it inside the context, and if the flag is enabled, do not execute any code. Then you can use CliRunner.invoke() with the --dry-run flag enabled and just check your invocations have happened, without actually executing the code.

Sign up to request clarification or add additional context in comments.

1 Comment

hey, thanks again ;). Yes my initial intention was, that I want the command to be called even if the command was not spelled correct: Like given fooz I implemented it to recognize it as foo. That´s what I wanted to test and seems your idea would be the way to go for this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.