Unit test script returns exit code = 0 even if tests fail Unit test script returns exit code = 0 even if tests fail python-3.x python-3.x

Unit test script returns exit code = 0 even if tests fail


The code is not using unittest.main. You need to check the result using TestResult.wasSuccessful and call sys.exit manually.

import sys....ret = not runner.run(suite).wasSuccessful()sys.exit(ret)


I had some trouble getting TextTestRunner results. For those like me, here is how it works:

"""Run all tests inside of *_test.py modules located in the same directory."""import sysimport unittestif __name__ == '__main__':    test_suite = unittest.defaultTestLoader.discover('.', '*_test.py')    test_runner = unittest.TextTestRunner(resultclass=unittest.TextTestResult)    result = test_runner.run(test_suite)    sys.exit(not result.wasSuccessful())