Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Python

unittest.TextTestRunner

Runs a case or suite and writes a human-readable test report to the configured text stream.

Version: Python 3.14; repository image, production runtime not attested

Syntax

unittest.TextTestRunner(stream=output, verbosity=1, failfast=False)

Runs a case or suite and writes a human-readable test report to the configured text stream.

The returned result exposes machine-readable counts. Testing those counts avoids relying on execution times embedded in the report.

run(test) creates and returns a result and writes the human-readable report to stream, which defaults to stderr. verbosity controls detail and failfast stops after the first failure or error. Inspect result.testsRun and wasSuccessful() instead of parsing elapsed seconds. The example finds OK in the captured output. buffer=True captures test stdout and stderr, discarding them for successful tests and including them on a failure or error. warnings applies a warnings filter such as 'ignore' for the duration of the run.

import unittest,io
class A(unittest.TestCase):
    def test_fail(self): self.fail('sample')
    def test_next(self): pass
out=io.StringIO()
r=unittest.TextTestRunner(stream=out,verbosity=2,failfast=True).run(unittest.defaultTestLoader.loadTestsFromTestCase(A))
assert r.testsRun==1 and len(r.failures)==1 and not r.wasSuccessful()
assert 'test_fail' in out.getvalue() and 'FAILED' in out.getvalue()
import warnings,contextlib,sys
class Capture(unittest.TestCase):
    def test_a(self): print('discard');warnings.warn('quiet')
    def test_b(self): print('retain',file=sys.stderr);self.fail('broken')
out=io.StringIO()
with contextlib.redirect_stderr(io.StringIO()),contextlib.redirect_stdout(io.StringIO()):
    result=unittest.TextTestRunner(stream=out,buffer=True,warnings='ignore').run(unittest.defaultTestLoader.loadTestsFromTestCase(Capture))
assert 'retain' in result.failures[0][1] and 'discard' not in out.getvalue()
assert 'UserWarning: quiet' not in out.getvalue()
class Warn(unittest.TestCase):
    def test_warn(self): warnings.warn('filter-probe')
for mode, visible in [('always',True),('ignore',False)]:
    capture=io.StringIO()
    with contextlib.redirect_stderr(capture):
        unittest.TextTestRunner(stream=io.StringIO(),buffer=False,warnings=mode).run(Warn('test_warn'))
    assert ('UserWarning: filter-probe' in capture.getvalue()) is visible

unittest.TextTestRunner.run

run(test) executes a case or suite, writes a report to the configured stream and returns a result object. Here testsRun and wasSuccessful() inspect the outcome independently of the duration printed.

import io,unittest
out=io.StringIO()
runner=unittest.TextTestRunner(stream=out,verbosity=0)
suite=unittest.TestSuite([unittest.FunctionTestCase(lambda:None)])
result=runner.run(suite)
assert result.testsRun==1 and result.wasSuccessful() and "OK" in out.getvalue()

Examples

import unittest
import io

class Check(unittest.TestCase):
    def test_total(self): self.assertEqual(2 + 3, 5)

out = io.StringIO()
r = unittest.TextTestRunner(stream=out).run(Check("test_total"))
print(r.testsRun, r.wasSuccessful(), "OK" in out.getvalue())

Result

1 True True

run(test) creates and returns a result and writes the human-readable report to stream, which defaults to stderr. verbosity controls detail and failfast stops after the first failure or error. Inspect result.testsRun and wasSuccessful() instead of parsing elapsed seconds. The example finds OK in the captured output. buffer=True captures test stdout and stderr, discarding them for successful tests and including them on a failure or error. warnings applies a warnings filter such as 'ignore' for the duration of the run.

Sources