From 52202839fbf87f203d1b0fc56a48610408a10964 Mon Sep 17 00:00:00 2001 From: Prasanna Santhanam Date: Thu, 22 Mar 2012 22:49:48 +0530 Subject: [PATCH] testCase level logger and testClient class attr reviewed-by: Chirag Jog Each testCase class will use its own logger to distinguish log messages when multiple suites are run. This will aid debugging Every cloudstackTestCase will also contiain a class level attr of the testClient which was previously available only to setUp/tearDown. Now even setUpClass/tearDownClass can use this attribute. --- tools/testClient/TestCaseExecuteEngine.py | 31 +++++++++++++++-------- tools/testClient/cloudstackTestCase.py | 6 +++++ 2 files changed, 26 insertions(+), 11 deletions(-) diff --git a/tools/testClient/TestCaseExecuteEngine.py b/tools/testClient/TestCaseExecuteEngine.py index cceed9ca8bd..8057d5750dd 100644 --- a/tools/testClient/TestCaseExecuteEngine.py +++ b/tools/testClient/TestCaseExecuteEngine.py @@ -8,8 +8,6 @@ import os import sys import logging -module_logger = "testclient.testcase" - def testCaseLogger(message, logger=None): if logger is not None: logger.debug(message) @@ -18,18 +16,19 @@ class TestCaseExecuteEngine(object): def __init__(self, testclient, testCaseFolder, testcaseLogFile=None, testResultLogFile=None): self.testclient = testclient self.testCaseFolder = testCaseFolder - self.logger = None + self.logformat = logging.Formatter("%(asctime)s - %(levelname)s - %(name)s - %(message)s") + if testcaseLogFile is not None: - logger = logging.getLogger("testclient.testcase.TestCaseExecuteEngine") - fh = logging.FileHandler(testcaseLogFile) - fh.setFormatter(logging.Formatter("%(asctime)s - %(levelname)s - %(name)s - %(message)s")) - logger.addHandler(fh) - logger.setLevel(logging.DEBUG) - self.logger = logger + self.logfile = testcaseLogFile + self.logger = logging.getLogger("TestCaseExecuteEngine") + fh = logging.FileHandler(self.logfile) + fh.setFormatter(self.logformat) + self.logger.addHandler(fh) + self.logger.setLevel(logging.DEBUG) if testResultLogFile is not None: ch = logging.StreamHandler() ch.setLevel(logging.ERROR) - ch.setFormatter(logging.Formatter("%(asctime)s - %(levelname)s - %(name)s - %(message)s")) + ch.setFormatter(self.logformat) self.logger.addHandler(ch) fp = open(testResultLogFile, "w") self.testResultLogFile = fp @@ -41,8 +40,18 @@ class TestCaseExecuteEngine(object): if isinstance(test, unittest.BaseTestSuite): self.injectTestCase(test) else: + #logger bears the name of the test class + testcaselogger = logging.getLogger("testclient.testcase.%s"%test.__class__.__name__) + fh = logging.FileHandler(self.logfile) + fh.setFormatter(self.logformat) + testcaselogger.addHandler(fh) + testcaselogger.setLevel(logging.DEBUG) + + #inject testclient and logger into each unittest setattr(test, "testClient", self.testclient) - setattr(test, "debug", partial(testCaseLogger, logger=self.logger)) + setattr(test, "debug", partial(testCaseLogger, logger=testcaselogger)) + setattr(test.__class__, "clstestclient", self.testclient) + def run(self): loader = unittest.loader.TestLoader() suite = loader.discover(self.testCaseFolder) diff --git a/tools/testClient/cloudstackTestCase.py b/tools/testClient/cloudstackTestCase.py index 4595aefaa42..2424a670ac2 100644 --- a/tools/testClient/cloudstackTestCase.py +++ b/tools/testClient/cloudstackTestCase.py @@ -6,6 +6,12 @@ except ImportError: import cloudstackTestClient class cloudstackTestCase(unittest.case.TestCase): + clstestclient = None + def __init__(self, args): unittest.case.TestCase.__init__(self, args) self.testClient = cloudstackTestClient.cloudstackTestClient() + + @classmethod + def getClsTestClient(cls): + return cls.clstestclient