1
0
mirror of https://github.com/samba-team/samba.git synced 2025-02-04 17:47:26 +03:00

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 <rob@catalyst.net.nz>
Reviewed-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz>
Reviewed-by: Andrew Bartlett <abartlet@samba.org>
This commit is contained in:
Rob van der Linde 2023-10-12 15:21:08 +13:00 committed by Andrew Bartlett
parent ed245e2887
commit d19e268221

View File

@ -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")