From 278a8890a3eaeadf0637014afe713912ae7f1062 Mon Sep 17 00:00:00 2001 From: Chris Manchester Date: Mon, 6 Oct 2014 20:20:38 -0400 Subject: [PATCH] Bug 1078869 - Structured logging for mozbase unit tests. r=ahal --- .../mozbase/mozlog/tests/test_structured.py | 2 +- testing/mozbase/test.py | 32 +++++++------------ 2 files changed, 13 insertions(+), 21 deletions(-) diff --git a/testing/mozbase/mozlog/tests/test_structured.py b/testing/mozbase/mozlog/tests/test_structured.py index 2d5dbbcba637..e40ea43925ac 100644 --- a/testing/mozbase/mozlog/tests/test_structured.py +++ b/testing/mozbase/mozlog/tests/test_structured.py @@ -582,7 +582,7 @@ class TestCommandline(unittest.TestCase): parser = argparse.ArgumentParser() commandline.add_logging_group(parser) args = parser.parse_args(["--log-raw=-"]) - logger = commandline.setup_logging("test", args, {}) + logger = commandline.setup_logging("test_setup_logging", args, {}) self.assertEqual(len(logger.handlers), 1) def test_setup_logging_optparse(self): diff --git a/testing/mozbase/test.py b/testing/mozbase/test.py index dedede64a7fe..abfa5233eb24 100755 --- a/testing/mozbase/test.py +++ b/testing/mozbase/test.py @@ -17,28 +17,12 @@ import os import sys import unittest +from mozlog import structured from moztest.results import TestResultCollection - +from moztest.adapters.unit import StructuredTestRunner here = os.path.dirname(os.path.abspath(__file__)) - -class TBPLTextTestResult(unittest.TextTestResult): - """ - Format the failure outputs according to TBPL. See: - https://wiki.mozilla.org/Sheriffing/Job_Visibility_Policy#6.29_Outputs_failures_in_a_TBPL-starrable_format - """ - - def addFailure(self, test, err): - super(unittest.TextTestResult, self).addFailure(test, err) - self.stream.writeln() - self.stream.writeln('TEST-UNEXPECTED-FAIL | %s | %s' % (test.id(), err[1])) - - def addUnexpectedSuccess(self, test): - super(unittest.TextTestResult, self).addUnexpectedSuccess(test) - self.stream.writeln() - self.stream.writeln('TEST-UNEXPECTED-PASS | %s | Unexpected pass' % test.id()) - def unittests(path): """return the unittests in a .py file""" @@ -67,7 +51,13 @@ def main(args=sys.argv[1:]): parser.add_option('--list', dest='list_tests', action='store_true', default=False, help="list paths of tests to be run") + structured.commandline.add_logging_group(parser) options, args = parser.parse_args(args) + logger = structured.commandline.setup_logging("mozbase", + options, + { + "tbpl": sys.stdout + }) # read the manifest if args: @@ -89,6 +79,8 @@ def main(args=sys.argv[1:]): # gather the tests tests = manifest.active_tests(disabled=False, **mozinfo.info) tests = [test['path'] for test in tests] + logger.suite_start(tests) + if options.list_tests: # print test paths print '\n'.join(tests) @@ -101,10 +93,10 @@ def main(args=sys.argv[1:]): # run the tests suite = unittest.TestSuite(unittestlist) - runner = unittest.TextTestRunner(verbosity=2, # default=1 does not show success of unittests - resultclass=TBPLTextTestResult) + runner = StructuredTestRunner(logger=logger) unittest_results = runner.run(suite) results = TestResultCollection.from_unittest_results(None, unittest_results) + logger.suite_end() # exit according to results sys.exit(1 if results.num_failures else 0)