从 shell 脚本中获取 pytest 退出代码

use*_*391 4 python shell pytest

我正在从 shell 脚本运行 pytest 测试。脚本中的相关行类似于:

pytest pytest_tests --param=$my_param
Run Code Online (Sandbox Code Playgroud)

根据 pytest 文档,“运行 pytest 可能会导致六个不同的退出代码”(0-5)。我的问题是如何从脚本中获取此退出代码?我试过类似的东西

exit_code = pytest pytest_tests --param=$my_param
echo $exit_code
Run Code Online (Sandbox Code Playgroud)

但我得到了这个:

exit_code: command not found
Run Code Online (Sandbox Code Playgroud)

我怎么才能得到它?或者有没有更好的方法在shell脚本中获得pytest结果?

Chr*_*ris 8

命令运行后,它的退出代码应该可以通过$?变量获得。尝试这样的事情:

pytest pytest_tests --param=$my_param
echo Pytest exited $?
Run Code Online (Sandbox Code Playgroud)

这适用于 Bash,也应该适用于常规的shBourne shell 和 zsh。

如果您需要将此分配给另一个变量,请使用

my_var=$?
Run Code Online (Sandbox Code Playgroud)

请注意缺少空格。