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.
This commit is contained in:
Prasanna Santhanam 2012-03-22 22:49:48 +05:30
parent 56dd6e81ac
commit 52202839fb
2 changed files with 26 additions and 11 deletions

View File

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

View File

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