diff --git a/Doc/library/unittest.rst b/Doc/library/unittest.rst index 0bc0a953fd9..8c68cbbef32 100644 --- a/Doc/library/unittest.rst +++ b/Doc/library/unittest.rst @@ -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 :class:`TestCase` classes may look like:: - test_cases = (TestCase1, TestCase2, TestCase3) - def load_tests(loader, tests, pattern): suite = TestSuite() - for test_class in test_cases: + for test_class in make_testcase_classes(): tests = loader.loadTestsFromTestCase(test_class) suite.addTests(tests) 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 command line or by calling :meth:`TestLoader.discover`, then the package :file:`__init__.py` will be checked for ``load_tests``. If that function does diff --git a/Misc/ACKS b/Misc/ACKS index e3927ff0b33..ce541247ff0 100644 --- a/Misc/ACKS +++ b/Misc/ACKS @@ -963,6 +963,7 @@ Dan Kenigsberg Randall Kern Robert Kern Jim Kerr +Jazz Kersell Magnus Kessler Lawrence Kesteloot Garvit Khatri diff --git a/Misc/NEWS.d/next/Documentation/2022-04-19-07-48-19.gh-issue-56809.HqcWei.rst b/Misc/NEWS.d/next/Documentation/2022-04-19-07-48-19.gh-issue-56809.HqcWei.rst new file mode 100644 index 00000000000..d3ff4e87584 --- /dev/null +++ b/Misc/NEWS.d/next/Documentation/2022-04-19-07-48-19.gh-issue-56809.HqcWei.rst @@ -0,0 +1 @@ +Add an example of how to generate test cases to load with load_tests