From: Rob van der Linde Date: Thu, 12 Oct 2023 02:21:08 +0000 (+1300) Subject: python: tests: function to generate a unique name from caller X-Git-Tag: talloc-2.4.2~990 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=d19e268221efca4079469c015f0fe3f2d0719f23;p=thirdparty%2Fsamba.git python: tests: function to generate a unique name from caller Uses the caller function to generate a unique name from the test function name. Unique name is converted to camel case Signed-off-by: Rob van der Linde Reviewed-by: Douglas Bagnall Reviewed-by: Andrew Bartlett --- diff --git a/python/samba/tests/__init__.py b/python/samba/tests/__init__.py index 27788a9fa45..5ba18c9fdcb 100644 --- a/python/samba/tests/__init__.py +++ b/python/samba/tests/__init__.py @@ -61,6 +61,8 @@ HEXDUMP_FILTER = bytearray([x if ((len(repr(chr(x))) == 3) and (x < 127)) else o LDB_ERR_LUT = {v: k for k, v in vars(ldb).items() if k.startswith('ERR_')} +RE_CAMELCASE = re.compile(r"([_\-])+") + def ldb_err(v): if isinstance(v, ldb.LdbError): @@ -165,6 +167,23 @@ class TestCase(unittest.TestCase): msg = "%s needs setUpDynamicTestCases() if @DynamicTestCase is used!" % (cls) raise Exception(msg) + def unique_name(self): + """Generate a unique name from within a test for creating objects. + + Used to ensure each test generates uniquely named objects that don't + interfere with other tests. + """ + # name of calling function + name = self.id().rsplit(".", 1)[1] + + # remove test_ prefix + if name.startswith("test_"): + name = name[5:] + + # finally, convert to camelcase + name = RE_CAMELCASE.sub(" ", name).title().replace(" ", "") + return "".join([name[0].lower(), name[1:]]) + def setUp(self): super(TestCase, self).setUp() test_debug_level = os.getenv("TEST_DEBUG_LEVEL")