support for running tests in a given script

This commit is contained in:
Prasanna Santhanam 2012-05-10 16:53:12 +05:30
parent dba1c01cb5
commit a0efa70958
2 changed files with 48 additions and 15 deletions

View File

@ -25,10 +25,16 @@ def testCaseLogger(message, logger=None):
logger.debug(message)
class TestCaseExecuteEngine(object):
def __init__(self, testclient, testCaseFolder, testcaseLogFile=None, testResultLogFile=None):
def __init__(self, testclient, testcaseLogFile=None, testResultLogFile=None):
"""
Initialize the testcase execution engine, just the basics here
@var testcaseLogFile: client log file
@var testResultLogFile: summary report file
"""
self.testclient = testclient
self.testCaseFolder = testCaseFolder
self.logformat = logging.Formatter("%(asctime)s - %(levelname)s - %(name)s - %(message)s")
self.loader = unittest.loader.TestLoader()
self.suite = None
if testcaseLogFile is not None:
self.logfile = testcaseLogFile
@ -46,7 +52,18 @@ class TestCaseExecuteEngine(object):
self.testResultLogFile = fp
else:
self.testResultLogFile = sys.stdout
def loadTestsFromDir(self, testDirectory):
""" Load the test suites from a package with multiple test files """
self.suite = self.loader.discover(testDirectory)
self.injectTestCase(self.suite)
def loadTestsFromFile(self, file_name):
""" Load the tests from a single script/module """
if os.path.isfile(file_name):
self.suite = self.loader.discover(os.path.dirname(file_name), os.path.basename(file_name))
self.injectTestCase(self.suite)
def injectTestCase(self, testSuites):
for test in testSuites:
if isinstance(test, unittest.BaseTestSuite):
@ -67,8 +84,5 @@ class TestCaseExecuteEngine(object):
self.testclient.createNewApiClient(test.UserName, test.DomainName, test.AcctType)
def run(self):
loader = unittest.loader.TestLoader()
suite = loader.discover(self.testCaseFolder)
self.injectTestCase(suite)
unittest.TextTestRunner(stream=self.testResultLogFile, verbosity=2).run(suite)
if self.suite:
unittest.TextTestRunner(stream=self.testResultLogFile, verbosity=2).run(self.suite)

View File

@ -12,21 +12,22 @@
# Automatically generated by addcopyright.py at 04/03/2012
import deployDataCenter
import TestCaseExecuteEngine
import NoseTestExecuteEngine
from optparse import OptionParser
import os
if __name__ == "__main__":
parser = OptionParser()
parser.add_option("-c", "--config", action="store", default="./datacenterCfg", dest="config", help="the path where the json config file generated, by default is ./datacenterCfg")
parser.add_option("-d", "--directory", dest="testCaseFolder", help="the test case directory")
parser.add_option("-r", "--result", dest="result", help="test result log file")
parser.add_option("-t", dest="testcaselog", help="test case log file")
parser.add_option("-t", "--client", dest="testcaselog", help="test case log file")
parser.add_option("-l", "--load", dest="load", action="store_true", help="only load config, do not deploy, it will only run testcase")
parser.add_option("-f", "--file", dest="module", help="run tests in the given file")
parser.add_option("-n", "--nose", dest="nose", action="store_true", help="run tests using nose")
(options, args) = parser.parse_args()
if options.testCaseFolder is None:
parser.print_usage()
exit(1)
testResultLogFile = None
if options.result is not None:
testResultLogFile = options.result
@ -40,5 +41,23 @@ if __name__ == "__main__":
else:
deploy.deploy()
testcaseEngine = TestCaseExecuteEngine.TestCaseExecuteEngine(deploy.testClient, options.testCaseFolder, testCaseLogFile, testResultLogFile)
testcaseEngine.run()
if options.testCaseFolder is None:
if options.module is None:
parser.print_usage()
exit(1)
else:
if options.nose:
engine = NoseTestExecuteEngine.NoseTestExecuteEngine(deploy.testClient, testCaseLogFile, testResultLogFile)
engine.runTestsFromFile(options.module)
else:
engine = TestCaseExecuteEngine.TestCaseExecuteEngine(deploy.testClient, testCaseLogFile, testResultLogFile)
engine.loadTestsFromFile(options.module)
engine.run()
else:
if options.nose:
engine = NoseTestExecuteEngine.NoseTestExecuteEngine(deploy.testClient, clientLog=testCaseLogFile, resultLog=testResultLogFile, workingdir=options.testCaseFolder)
engine.runTests()
else:
engine = TestCaseExecuteEngine.TestCaseExecuteEngine(deploy.testClient, testCaseLogFile, testResultLogFile)
engine.loadTestsFromDir(options.testCaseFolder)
engine.run()