从shell脚本获取pytest退出代码

问题描述 投票:1回答:1

我正在从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结果?

python shell pytest
1个回答
2
投票

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

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

这适用于Bash,并且应该在常规的sh Bourne shell和zsh中工作。

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

my_var=$?

注意缺少空格。

© www.soinside.com 2019 - 2024. All rights reserved.