Skip to content
Advertisement

pytest throws exception while using ClickArguments SystemExit: 0

I am trying to run a test case for a python script. Test case is successful when I don’t use Click decorator and argument in python script method.

But when I am using it, it gives SystemExit: 0.

commands.py:

JavaScript

tests/test_commands.py:

JavaScript

When I run the test case:

JavaScript

then the test fails with:

JavaScript

The full output is:

JavaScript

Advertisement

Answer

Don’t run click commands directly; they will trigger sys.exit(0) when done, which is the normal and correct way to end a command-line tool, but not very useful when trying to test the command.

Instead, follow the Testing Click Applications chapter and use CliRunner() object to run your commands:

JavaScript

Note that I passed in a list of string arguments; click will parse those into the correct structure.

I assigned result.return_value to response here, but know that that’ll be None; click commands are not supposed to return anything really, because they normally would communicate with the user via the terminal or by interacting with the filesystem, etc.:

JavaScript

You could perhaps use print() or click.echo() to write something to stdout and then test for that output via result.stdout, though.

Advertisement