2

When running python.main(), any failed unit tests do not return exit code 1 to python module that invoked the test.

Stated here, running pytest will not raise system exit, but is there a way to have failed unit tests behave the same or allow for a return of code (1) to the calling function?

1
  • 1
    exit(pytest.main()) Commented Jun 30, 2020 at 1:05

1 Answer 1

4

pytest.main() will return its exit code — which is an ExitCode enum, starting with pytest 5.0.0. If a test has failed, ExitCode.TESTS_FAILED will be returned; if all tests pass, ExitCode.OK will be returned.

Incidentally, the integer values of these enums are actually the exit codes used if run from the terminal. And here's the source of site-packages/pytest/__main__.py, which is executed if python -m pytest is used to invoke tests:

import pytest

if __name__ == "__main__":
    raise SystemExit(pytest.main())

The py.test (or pytest) entry point script is largely the same

import re
import sys
from pytest import main
if __name__ == '__main__':
    sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])
    sys.exit(main())
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.