From b496f3c79cbd77a71f15fe21cc6ee8f3fe9ad58d Mon Sep 17 00:00:00 2001 From: Jazz Kersell Date: Thu, 7 Apr 2022 23:46:40 -0400 Subject: [PATCH 1/2] gh-56809: Add an example of generating test for load_tests --- Doc/library/unittest.rst | 15 ++++++++++++--- Misc/ACKS | 1 + 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/Doc/library/unittest.rst b/Doc/library/unittest.rst index 9b8b75acce5..1bfc407baee 100644 --- a/Doc/library/unittest.rst +++ b/Doc/library/unittest.rst @@ -2328,15 +2328,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 a1df84c0d67..f1476958609 100644 --- a/Misc/ACKS +++ b/Misc/ACKS @@ -905,6 +905,7 @@ Dan Kenigsberg Randall Kern Robert Kern Jim Kerr +Jazz Kersell Magnus Kessler Lawrence Kesteloot Garvit Khatri From 12c2c6fb5f21fa6d7c4489e8339d90e8f7a747f5 Mon Sep 17 00:00:00 2001 From: Jazz Kersell Date: Tue, 19 Apr 2022 07:49:07 -0400 Subject: [PATCH 2/2] Add NEWS entry --- .../Documentation/2022-04-19-07-48-19.gh-issue-56809.HqcWei.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Documentation/2022-04-19-07-48-19.gh-issue-56809.HqcWei.rst 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