Python Glossary Русский

all Python 3unittestterm

unittest.TestSuite

running tests

A container of tests: it groups test cases and other suites so they run with a single runner call or suite.run(result).

Syntax

suite = unittest.TestSuite()
suite.addTest(MyTests('test_x'))
suite.addTests(tests)

A suite is single-use: after run() it releases the tests it has run, and a second suite.run() fails with TypeError: 'NoneType' object is not callable, so rebuild the suite for another run. Tests run in the order they were added, not alphabetically as the loader sorts them. It is the suite that calls setUpClass and setUpModule, so a single test run outside a suite never gets them.

Examples

import unittest
class Tests(unittest.TestCase):
    def test_a(self): pass
    def test_b(self): pass
suite = unittest.TestSuite()
suite.addTest(Tests('test_b'))
suite.addTest(Tests('test_a'))
print([t.id().split('.')[-1] for t in suite])  # → ['test_b', 'test_a']
print(suite.countTestCases())  # → 2
import unittest
class Tests(unittest.TestCase):
    def test_a(self): pass
suite = unittest.TestSuite([Tests('test_a')])
suite.run(unittest.TestResult())
try:
    suite.run(unittest.TestResult())
except TypeError as e:
    print(e)  # → 'NoneType' object is not callable

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy