On this page
unittest.TestLoader
Prev: unittest.TestCase | Up: unittest | Next: unittest.TestResult
Rel: unittest.addModuleCleanup, unittest.doModuleCleanups, unittest.enterModuleContext, unittest.expectedFailure
Ref: docs.python.org — unittest.TestLoader
Tags: #public #python3 #stdlib #unittest
Import
import unittest
from unittest import TestLoader
Signature & Overview
unittest.TestLoader()
This class is responsible for loading tests according to various criteria and returning them wrapped in a TestSuite
Methods & Attributes
TestLoader.discover(self, start_dir, pattern='test*.py', top_level_dir=None)— Find and return all test modules from the specified start directory, recursing into subdirectories to find them and return all tests found within them. Only test files that match the pattern willTestLoader.getTestCaseNames(self, testCaseClass)— Return a sorted sequence of method names found within testCaseClassTestLoader.loadTestsFromModule(self, module, *, pattern=None)— Return a suite of all test cases contained in the given moduleTestLoader.loadTestsFromName(self, name, module=None)— Return a suite of all test cases given a string specifier.TestLoader.loadTestsFromNames(self, names, module=None)— Return a suite of all test cases found using the given sequence of string specifiers. See 'loadTestsFromName()'.TestLoader.loadTestsFromTestCase(self, testCaseClass)— Return a suite of all test cases contained in testCaseClassTestLoader.sortTestMethodsUsing(x, y)— Return -1 if x < y, 0 if x == y and 1 if x > yTestLoader.suiteClass(tests=())— A test suite is a composite test consisting of a number of TestCases.
Usage
import unittest
class Sample(unittest.TestCase):
def test_ok(self) -> None:
self.assertTrue(True)
loader = unittest.TestLoader()
suite = loader.loadTestsFromTestCase(Sample)
assert suite.countTestCases() == 1
Gotchas
- Use
loader.discover(start_dir, pattern="test_*.py"): Matches the same discovery algorithm aspython3 -m unittest discover.
REPL Inspect
from unittest import TestLoader
help(TestLoader)
Linked from
Related notes
- tuts/python3/stdlib_module/unittest/TestCase Linked here; links to this page; shared tags: #stdlib, #unittest
- tuts/python3/stdlib_module/unittest/TestResult Linked here; links to this page; shared tags: #stdlib, #unittest
- tuts/python3/stdlib_module/unittest Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/unittest/addModuleCleanup Linked here; shared tags: #stdlib, #unittest
- tuts/python3/stdlib_module/unittest/doModuleCleanups Linked here; shared tags: #stdlib, #unittest
- tuts/python3/stdlib_module/unittest/enterModuleContext Linked here; shared tags: #stdlib, #unittest
- tuts/python3/stdlib_module/unittest/expectedFailure Linked here; shared tags: #stdlib, #unittest
- tuts/python3/stdlib_module/unittest/FunctionTestCase shared tags: #stdlib, #unittest
- tuts/python3/stdlib_module/unittest/installHandler shared tags: #stdlib, #unittest
- tuts/python3/stdlib_module/unittest/IsolatedAsyncioTestCase shared tags: #stdlib, #unittest