CKTIS / Python
unittest.TestSuite
Groups test cases and nested suites for one run, accumulating results in the supplied result object.
Version: Python 3.14; repository image, production runtime not attested
Syntax
unittest.TestSuite(tests=())Groups test cases and nested suites for one run, accumulating results in the supplied result object.
A suite can be empty. Use the public case count before running because executed test references may be released.
Add a case or nested suite with addTest, or an iterable with addTests. countTestCases counts the contained individual tests recursively. run(result) shares the supplied result across them and returns it. The default suite can discard references after execution. The example counts one case and an empty suite before running successfully. Repeated iteration before a run returns the same tests. debug() runs them without capturing exceptions in a result, so a test error propagates.
import unittest
events=[]
class Check(unittest.TestCase):
def test_a(self): events.append('a')
def test_b(self): events.append('b')
inner=unittest.TestSuite([Check('test_a')])
outer=unittest.TestSuite()
outer.addTests([inner,Check('test_b')])
assert outer.countTestCases()==2
r=unittest.TestResult()
assert outer.run(r) is r and events==['a','b'] and r.testsRun==2
case=Check('test_a')
suite=unittest.TestSuite([case])
assert list(suite)==list(suite)==[case]
def bad(): raise ValueError('from case')
try: unittest.TestSuite([unittest.FunctionTestCase(bad)]).debug()
except ValueError as ex: assert str(ex)=='from case'
else: raise AssertionError
Examples
import unittest
class Check(unittest.TestCase):
def test_total(self): self.assertEqual(2 + 3, 5)
suite = unittest.TestSuite([Check("test_total")])
print(suite.countTestCases(), unittest.TestSuite().countTestCases())
r = unittest.TestResult()
suite.run(r)
print(r.testsRun, r.wasSuccessful())
Result
1 0 1 True
Add a case or nested suite with addTest, or an iterable with addTests. countTestCases counts the contained individual tests recursively. run(result) shares the supplied result across them and returns it. The default suite can discard references after execution. The example counts one case and an empty suite before running successfully. Repeated iteration before a run returns the same tests. debug() runs them without capturing exceptions in a result, so a test error propagates.