From a0efa709580d9c92f11b9927d938c1ee93f5100b Mon Sep 17 00:00:00 2001 From: Prasanna Santhanam Date: Thu, 10 May 2012 16:53:12 +0530 Subject: [PATCH] support for running tests in a given script --- tools/marvin/marvin/TestCaseExecuteEngine.py | 30 +++++++++++++----- tools/marvin/marvin/deployAndRun.py | 33 +++++++++++++++----- 2 files changed, 48 insertions(+), 15 deletions(-) diff --git a/tools/marvin/marvin/TestCaseExecuteEngine.py b/tools/marvin/marvin/TestCaseExecuteEngine.py index 77ca95b694c..94ba8168624 100644 --- a/tools/marvin/marvin/TestCaseExecuteEngine.py +++ b/tools/marvin/marvin/TestCaseExecuteEngine.py @@ -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) diff --git a/tools/marvin/marvin/deployAndRun.py b/tools/marvin/marvin/deployAndRun.py index b91453c22a9..01f106d5243 100644 --- a/tools/marvin/marvin/deployAndRun.py +++ b/tools/marvin/marvin/deployAndRun.py @@ -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() \ No newline at end of file + 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()