从Shell脚本获取pytest的退出代码

6
我正在从Shell脚本中运行pytest测试。 脚本中相关行的样子如下:
pytest pytest_tests --param=$my_param

根据pytest文档,“运行pytest可能会导致六种不同的退出代码”(0-5)。我的问题是如何从脚本中获取此退出代码?我尝试了类似以下的内容:
exit_code = pytest pytest_tests --param=$my_param
echo $exit_code

但我得到了这个:

exit_code: command not found

我该如何获取它?或者有没有更好的方法在shell脚本中获取pytest结果?

1个回答

12

命令运行后,可以通过$? 变量获取其退出代码。尝试像这样做:

pytest pytest_tests --param=$my_param
echo Pytest exited $?

这在Bash中可行,并且应该在常规的sh Bourne shell和zsh中也可以工作。

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

my_var=$?

注意这里没有空格。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接