This commit is contained in:
Jazz Kersell 2025-12-08 06:11:45 +02:00 committed by GitHub
commit bbdf454608
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 14 additions and 3 deletions

View file

@ -2426,15 +2426,24 @@ The third argument is used when loading packages as part of test discovery.
A typical ``load_tests`` function that loads tests from a specific set of A typical ``load_tests`` function that loads tests from a specific set of
:class:`TestCase` classes may look like:: :class:`TestCase` classes may look like::
test_cases = (TestCase1, TestCase2, TestCase3)
def load_tests(loader, tests, pattern): def load_tests(loader, tests, pattern):
suite = TestSuite() suite = TestSuite()
for test_class in test_cases: for test_class in make_testcase_classes():
tests = loader.loadTestsFromTestCase(test_class) tests = loader.loadTestsFromTestCase(test_class)
suite.addTests(tests) suite.addTests(tests)
return suite return suite
This could be used to load a set of tests that are dynamically generated to
run against different backends, for example::
def make_testcase_classes():
for backend in backends:
yield type(
'{}Test'.format(backend.name),
(TheBaseClass, unittest.TestCase),
{'backend': backend}
)
If discovery is started in a directory containing a package, either from the If discovery is started in a directory containing a package, either from the
command line or by calling :meth:`TestLoader.discover`, then the package command line or by calling :meth:`TestLoader.discover`, then the package
:file:`__init__.py` will be checked for ``load_tests``. If that function does :file:`__init__.py` will be checked for ``load_tests``. If that function does

View file

@ -963,6 +963,7 @@ Dan Kenigsberg
Randall Kern Randall Kern
Robert Kern Robert Kern
Jim Kerr Jim Kerr
Jazz Kersell
Magnus Kessler Magnus Kessler
Lawrence Kesteloot Lawrence Kesteloot
Garvit Khatri Garvit Khatri

View file

@ -0,0 +1 @@
Add an example of how to generate test cases to load with load_tests