6

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?

1 Answer 1

12

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.