From 4251323ac8c739ce8de670fd77c9d4399f9f66ce Mon Sep 17 00:00:00 2001 From: Billy O'Mahony Date: Tue, 23 Jun 2015 12:10:04 +0100 Subject: vsperf: Add --xunit support Generate xmlrunner output for use by Jenkins, but without actually using xmlrunner to discover & run tests. JIRA: VSPERF-27 Change-Id: I000099c841f3755043104f251837e3d4c214c6bb Signed-off-by: Billy O'mahony Reviewed-by: Maryam Tahhan Reviewed-by: Al Morton Reviewed-by: Eugene Snider Reviewed-by: Maryam Tahhan Reviewed-by: Gurpreet Singh Reviewed-by: Tv Rao --- vsperf | 45 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) (limited to 'vsperf') diff --git a/vsperf b/vsperf index 0747a205..cb02d6db 100755 --- a/vsperf +++ b/vsperf @@ -24,6 +24,8 @@ import argparse import time import datetime import shutil +import unittest +import xmlrunner sys.dont_write_bytecode = True @@ -130,6 +132,10 @@ def parse_arguments(): group.add_argument('--trafficgen', help='traffic generator to use') group.add_argument('--sysmetrics', help='system metrics logger to use') group = parser.add_argument_group('test behavior options') + group.add_argument('--xunit', action='store_true', + help='enable xUnit-formatted output') + group.add_argument('--xunit-dir', action=_ValidateDirAction, + help='output directory of xUnit-formatted output') group.add_argument('--load-env', action='store_true', help='enable loading of settings from the environment') group.add_argument('--conf-file', action=_ValidateFileAction, @@ -231,6 +237,34 @@ def apply_filter(tests, tc_filter): return result +class MockTestCase(unittest.TestCase): + """Allow use of xmlrunner to generate Jenkins compatible output without + using xmlrunner to actually run tests. + + Usage: + suite = unittest.TestSuite() + suite.addTest(MockTestCase('Test1 passed ', True, 'Test1')) + suite.addTest(MockTestCase('Test2 failed because...', False, 'Test2')) + xmlrunner.XMLTestRunner(...).run(suite) + """ + + def __init__(self, msg, is_pass, test_name): + #remember the things + self.msg = msg + self.is_pass = is_pass + + #dynamically create a test method with the right name + #but point the method at our generic test method + setattr(MockTestCase, test_name, self.generic_test) + + super(MockTestCase, self).__init__(test_name) + + def generic_test(self): + """Provide a generic function that raises or not based + on how self.is_pass was set in the constructor""" + self.assertTrue(self.is_pass, self.msg) + + def main(): """Main function. """ @@ -317,15 +351,24 @@ def main(): logger.info("Creating result directory: " + results_path) os.makedirs(results_path) + suite = unittest.TestSuite() + # run tests for test in all_tests: try: test.run() + suite.addTest(MockTestCase('', True, test.name)) #pylint: disable=broad-except - except (Exception) as _: + except (Exception) as ex: logger.exception("Failed to run test: %s", test.name) + suite.addTest(MockTestCase(str(ex), False, test.name)) logger.info("Continuing with next test...") + if settings.getValue('XUNIT'): + xmlrunner.XMLTestRunner( + output=settings.getValue('XUNIT_DIR'), outsuffix="", + verbosity=0).run(suite) + #remove directory if no result files were created. if os.path.exists(results_path): if os.listdir(results_path) == []: -- cgit 1.2.3-korg