diff options
-rw-r--r-- | requirements.txt | 1 | ||||
-rwxr-xr-x | vsperf | 45 |
2 files changed, 45 insertions, 1 deletions
diff --git a/requirements.txt b/requirements.txt index f7738754..32410729 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,3 +2,4 @@ pexpect==3.3 linux-metrics==0.1.4 tox==1.8.1 jinja2==2.7.3 +xmlrunner==1.7.7 @@ -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) == []: |