Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get pytest exit code from shell script

I'm running pytest test from shell script. The relevant line in the script looks something like:

pytest pytest_tests --param=$my_param

According to pytest documentation, "Running pytest can result in six different exit codes" (0-5). My question is how can I get this exit code from the script? I tried something like

exit_code = pytest pytest_tests --param=$my_param
echo $exit_code

But I got this:

exit_code: command not found

How can I get it? Or is there a better way to get pytest results in the shell script?

like image 847
user2880391 Avatar asked Jan 27 '23 11:01

user2880391


1 Answers

After a command runs its exit code should be available via the $? variable. Try something like this:

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

This works in Bash, and should work in the regular sh Bourne shell and zsh as well.

If you need to assign this to another variable, use

my_var=$?

Note the lack of spaces.

like image 72
Chris Avatar answered Jan 29 '23 02:01

Chris