Search code examples
pythonpytestexit-code

Checking the exit code of a CLI in a test


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?


Solution

  • You need to check value.code, like this:

    assert e.value.code != 0