Python Glossary Русский

all Python 3until 3.13unittestfunction

unittest.getTestCaseNames()

removed

Returned the names of a class's test methods. Removed in Python 3.13 — use TestLoader().getTestCaseNames().

Syntax

unittest.getTestCaseNames(MyTest, 'test')  # до 3.13

The module-level unittest.getTestCaseNames(MyTest, 'test') returned the sorted names of the methods with the given prefix. It was removed because TestLoader has a method of the same name that honours the loader's settings. To migrate, create loader = unittest.TestLoader(), set loader.testMethodPrefix if your prefix is not 'test', then call loader.getTestCaseNames(MyTest) without a second argument.

Examples

# Python 3.13+
import unittest
try:
    unittest.getTestCaseNames
except AttributeError as e:
    print(type(e).__name__)  # → AttributeError
# Python 3.13+
import unittest
class MathTest(unittest.TestCase):
    def test_add(self):
        self.assertEqual(1 + 1, 2)
    def test_mul(self):
        self.assertEqual(2 * 3, 6)
print(unittest.TestLoader().getTestCaseNames(MathTest))  # → ['test_add', 'test_mul']
import unittest
class MathTest(unittest.TestCase):
    def test_add(self):
        self.assertEqual(1 + 1, 2)
    def test_mul(self):
        self.assertEqual(2 * 3, 6)
print(unittest.getTestCaseNames(MathTest, 'test'))  # → ['test_add', 'test_mul']

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy