python/tests: add DynamicTestCase setUpDynamicTestCases() infrastructure
authorStefan Metzmacher <metze@samba.org>
Mon, 20 Apr 2020 18:00:51 +0000 (20:00 +0200)
committerKarolin Seeger <kseeger@samba.org>
Mon, 26 Oct 2020 12:17:33 +0000 (12:17 +0000)
This can be used in order to run a sepcific test (coded just once)
with an autogenerated set of arguments.

BUG: https://bugzilla.samba.org/show_bug.cgi?id=14531

Pair-Programmed-With: Douglas Bagnall <douglas.bagnall@catalyst.net.nz>

Signed-off-by: Stefan Metzmacher <metze@samba.org>
Signed-off-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz>
(cherry picked from commit 80347deb544b38be6c6814e5d1b82e48ebe83fd1)

python/samba/tests/__init__.py

index 3f22eaa1c94c98c24a4e12f99a2dae65b3ab4676..9d5128bab0ebe878fc1e7e79fd3259ed05c715a4 100644 (file)
@@ -63,10 +63,37 @@ BINDIR = os.path.abspath(os.path.join(os.path.dirname(__file__),
 
 HEXDUMP_FILTER = bytearray([x if ((len(repr(chr(x))) == 3) and (x < 127)) else ord('.') for x in range(256)])
 
+def DynamicTestCase(cls):
+    cls.setUpDynamicTestCases()
+    return cls
 
 class TestCase(unittest.TestCase):
     """A Samba test case."""
 
+    @classmethod
+    def generate_dynamic_test(cls, fnname, suffix, *args):
+        """
+        fnname is something like "test_dynamic_sum"
+        suffix is something like "1plus2"
+        argstr could be (1, 2)
+
+        This would generate a test case called
+        "test_dynamic_sum_1plus2(self)" that
+        calls
+        self._test_dynamic_sum_with_args(1, 2)
+        """
+        def fn(self):
+            getattr(self, "_%s_with_args" % fnname)(*args)
+        setattr(cls, "%s_%s" % (fnname, suffix), fn)
+
+    @classmethod
+    def setUpDynamicTestCases(cls):
+        """This can be implemented in order to call cls.generate_dynamic_test()
+        In order to implement autogenerated testcase permutations.
+        """
+        msg = "%s needs setUpDynamicTestCases() if @DynamicTestCase is used!" % (cls)
+        raise Exception(msg)
+
     def setUp(self):
         super(TestCase, self).setUp()
         test_debug_level = os.getenv("TEST_DEBUG_LEVEL")