Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Python

unittest.TestLoader

Builds test suites by discovering methods according to the configured name prefix.

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

Syntax

unittest.TestLoader()

Builds test suites by discovering methods according to the configured name prefix.

Helper methods whose names do not match the prefix are ignored during discovery.

The default testMethodPrefix is 'test'. getTestCaseNames selects matching callable attributes and sorts them using sortTestMethodsUsing. loadTestsFromTestCase returns a suite of instances, one per selected method. With only test_one and helper, the suite contains one case. Change the prefix to select a different convention. discover imports modules matching a file pattern and calls a module's load_tests(loader, tests, pattern) hook when present. An import failure is recorded in loader.errors and represented in the suite by a synthetic test which reports the error at run time.

import unittest
class Check(unittest.TestCase):
    def test_z(self): pass
    def test_a(self): pass
    def check_b(self): pass
loader=unittest.TestLoader()
assert loader.getTestCaseNames(Check)==['test_a','test_z']
loader.testMethodPrefix='check'
assert [case._testMethodName for case in loader.loadTestsFromTestCase(Check)]==['check_b']
import tempfile,pathlib
with tempfile.TemporaryDirectory() as folder:
    p=pathlib.Path(folder)
    (p/'test_ref_broken.py').write_text("raise ImportError('missing')")
    (p/'test_ref_hook.py').write_text("import unittest\ndef load_tests(loader,tests,pattern):\n    assert pattern=='test*.py'\n    return unittest.TestSuite([unittest.FunctionTestCase(lambda: None)])")
    found=loader.discover(folder)
    assert found.countTestCases()==2 and len(loader.errors)==1
    r=unittest.TestResult()
    found.run(r)
    assert r.testsRun==2 and len(r.errors)==1

unittest.TestLoader.loadTestsFromTestCase

loadTestsFromTestCase takes a TestCase class and builds a suite of cases for matching methods. Loading does not run their bodies. Here test_a is included and helper is skipped.

import unittest
visits=[]
class Names(unittest.TestCase):
    def test_a(self): visits.append("a")
    def helper(self): visits.append("helper")
suite=unittest.TestLoader().loadTestsFromTestCase(Names)
assert suite.countTestCases()==1 and visits==[]
suite.run(unittest.TestResult())
assert visits==["a"]

Examples

import unittest
class Check(unittest.TestCase):
    def test_one(self): pass
    def helper(self): pass

loader = unittest.TestLoader()
print(loader.getTestCaseNames(Check))
print(loader.loadTestsFromTestCase(Check).countTestCases())

Result

['test_one']
1

The default testMethodPrefix is 'test'. getTestCaseNames selects matching callable attributes and sorts them using sortTestMethodsUsing. loadTestsFromTestCase returns a suite of instances, one per selected method. With only test_one and helper, the suite contains one case. Change the prefix to select a different convention. discover imports modules matching a file pattern and calls a module's load_tests(loader, tests, pattern) hook when present. An import failure is recorded in loader.errors and represented in the suite by a synthetic test which reports the error at run time.

Sources