I am writing automated tests for a command line tool. Essentially, I want to invoke the CLI with various options and test the exit code and/or output.
My test looks like this:
from mymodule.cli_tool import main
def test_args(capfd):
with pytest.raises(SystemExit) as e:
main(args=['--junk_option'])
# check the exit code to make sure it is non-zero
???
How do I check the exit code?
You need to check value.code
, like this:
assert e.value.code != 0