all Python 3unittestterm
unittest.TextTestRunner
running tests
A runner that executes a test suite, prints the progress and summary as text, and returns a TestResult object.
Syntax
runner = unittest.TextTestRunner(stream=None, verbosity=1, failfast=False)
result = runner.run(suite)
It prints to sys.stderr, not stdout: redirecting with > report.txt does not capture the report, so use stream= or 2>. Unlike unittest.main(), run() neither exits the program nor sets the exit code: failures exist only in the returned result, and for CI you set the exit code yourself with sys.exit(not result.wasSuccessful()). failfast=True stops the run at the first failure.
Examples
import sys
import unittest
class Tests(unittest.TestCase):
def test_sum(self):
self.assertEqual(sum([1, 2]), 3)
suite = unittest.TestLoader().loadTestsFromTestCase(Tests)
result = unittest.TextTestRunner(stream=sys.stdout, verbosity=2).run(suite)
# → test_sum (__main__.Tests.test_sum) ... ok / Ran 1 test in ? / OK
print(result.wasSuccessful()) # → True
See also
Open in the full glossarydocs.python.orgReport an inaccuracy