all Python 3unittestterm
unittest.TextTestResult
running tests
A TestResult subclass that prints dots, the letters F/E/s or '... ok' lines to a stream as the run goes; TextTestRunner uses it.
Syntax
class MyResult(unittest.TextTestResult): ...
unittest.TextTestRunner(resultclass=MyResult)
You rarely create it directly: a subclass is handed to the runner through resultclass to change the output or gather your own statistics by overriding addSuccess, addFailure and friends; remember to call super(), or the test vanishes from the totals. verbosity controls how much is printed: 0 is silent, 1 prints dots, 2 prints a line per test. Before Python 2.7 and in 3.0-3.1 the class was named _TextTestResult.
Examples
import sys
import unittest
class Counting(unittest.TextTestResult):
passed = 0
def addSuccess(self, test):
super().addSuccess(test)
Counting.passed += 1
class Tests(unittest.TestCase):
def test_a(self): pass
def test_b(self): pass
runner = unittest.TextTestRunner(stream=sys.stdout, verbosity=0, resultclass=Counting)
result = runner.run(unittest.TestLoader().loadTestsFromTestCase(Tests))
print(type(result).__name__, Counting.passed) # → Counting 2
See also
Open in the full glossarydocs.python.orgReport an inaccuracy