Call a python unittest from another script and export all the error messages
You need to use a test runner
test runner A test runner is a component which orchestrates the execution of tests and provides the outcome to the user. The runner may use a graphical interface, a textual interface, or return a special value to indicate the results of executing the tests.
from unittest.case import TestCase
import unittest
from StringIO import StringIO
class MyTestCase(TestCase):
def testTrue(self):
'''
Always true
'''
assert True
def testFail(self):
'''
Always fails
'''
assert False
from pprint import pprint
stream = StringIO()
runner = unittest.TextTestRunner(stream=stream)
result = runner.run(unittest.makeSuite(MyTestCase))
print 'Tests run ', result.testsRun
print 'Errors ', result.errors
pprint(result.failures)
stream.seek(0)
print 'Test output\n', stream.read()
>>> Output:
>>> Tests run 2
>>> Errors []
>>> [(<__main__.MyTestCase testMethod=testFail>,
>>> 'Traceback (most recent call last):\n File "leanwx/test.py", line 15, in testFail\n assert False\nAssertionError\n')]
>>> Test output
>>> F.
>>> ======================================================================
>>> FAIL: testFail (__main__.MyTestCase)
>>> ----------------------------------------------------------------------
>>> Traceback (most recent call last):
>>> File "leanwx/test.py", line 15, in testFail
>>> assert False
>>> AssertionError
>>>
>>>----------------------------------------------------------------------
>>>Ran 2 tests in 0.001s
>>>
>>>FAILED (failures=1)
Assuming you have your unit tests located in the "test" directory:
# Tested in Python 3.8
# https://docs.python.org/3/library/unittest.html#module-unittest
from unittest import TestLoader, TestResult
from pathlib import Path
def run_tests():
test_loader = TestLoader()
test_result = TestResult()
# Use resolve() to get an absolute path
# https://docs.python.org/3/library/pathlib.html#pathlib.Path.resolve
test_directory = str(Path(__file__).resolve().parent / 'test')
test_suite = test_loader.discover(test_directory, pattern='test_*.py')
test_suite.run(result=test_result)
# See the docs for details on the TestResult object
# https://docs.python.org/3/library/unittest.html#unittest.TestResult
if test_result.wasSuccessful():
exit(0)
else:
# Here you can either print or log your test errors and failures
# test_result.errors or test_result.failures
exit(-1)
if __name__ == '__main__':
run_tests()