Checking the exit code of a CLI in a test

时光总嘲笑我的痴心妄想 提交于 2019-12-24 07:45:40

问题


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?


回答1:


You need to check value.code, like this:

assert e.value.code != 0


来源:https://stackoverflow.com/questions/57300169/checking-the-exit-code-of-a-cli-in-a-test

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!