kravb
kravb

Reputation: 558

Capture exit code for pytest from python

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?

Upvotes: 2

Views: 5095

Answers (1)

theY4Kman
theY4Kman

Reputation: 6105

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())

Upvotes: 4

Related Questions