Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(524)

Unified Diff: telemetry/telemetry/testing/browser_test_case.py

Issue 2072713003: [telemetry] Print browser standard output and log upon BrowserTestCase failures (Closed) Base URL: git@github.com:catapult-project/catapult.git@master
Patch Set: Make the metaclass private Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « telemetry/telemetry/internal/backends/chrome_inspector/tracing_backend_unittest.py ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: telemetry/telemetry/testing/browser_test_case.py
diff --git a/telemetry/telemetry/testing/browser_test_case.py b/telemetry/telemetry/testing/browser_test_case.py
index 85e6797223392acc4cf2c743cc83a9c130bf3ed9..710904b07ef7c31110f1ba308c35888d7561272c 100644
--- a/telemetry/telemetry/testing/browser_test_case.py
+++ b/telemetry/telemetry/testing/browser_test_case.py
@@ -2,7 +2,11 @@
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
+from functools import wraps
+import logging
import os
+import sys
+import types
import unittest
from telemetry.internal.browser import browser_finder
@@ -13,6 +17,52 @@ current_browser_options = None
current_browser = None
+class _MetaBrowserTestCase(type):
+ """Metaclass for BrowserTestCase.
+
+ The metaclass wraps all test* methods of all subclasses of BrowserTestCase to
+ print browser standard output and log upon failure.
+ """
+
+ def __new__(mcs, name, bases, dct):
+ new_dct = {}
+ for attributeName, attribute in dct.iteritems():
+ if (isinstance(attribute, types.FunctionType) and
+ attributeName.startswith('test')):
+ attribute = mcs._PrintBrowserStandardOutputAndLogOnFailure(attribute)
+ new_dct[attributeName] = attribute
+ return type.__new__(mcs, name, bases, new_dct)
+
+ @staticmethod
+ def _PrintBrowserStandardOutputAndLogOnFailure(method):
+ @wraps(method)
+ def WrappedMethod(self):
+ try: # pylint: disable=broad-except
+ method(self)
+ except Exception:
+ exc_info = sys.exc_info()
+
+ logging.info('*************** BROWSER STANDARD OUTPUT ***************')
+ try: # pylint: disable=broad-except
+ logging.info(self._browser.GetStandardOutput())
+ except Exception:
+ logging.exception('Failed to get browser standard output:')
+ logging.info('*********** END OF BROWSER STANDARD OUTPUT ************')
+
+ logging.info('********************* BROWSER LOG *********************')
+ try: # pylint: disable=broad-except
+ logging.info(self._browser.GetLogFileContents())
+ except Exception:
+ logging.exception('Failed to get browser log:')
+ logging.info('***************** END OF BROWSER LOG ******************')
+
+ # Re-raise the original exception. Note that we can't just use 'raise'
+ # without any arguments because an exception might have been thrown when
+ # the browser standard output was retrieved.
+ raise exc_info[0], exc_info[1], exc_info[2]
+ return WrappedMethod
+
+
def teardown_browser():
global current_browser
global current_browser_options
@@ -24,6 +74,8 @@ def teardown_browser():
class BrowserTestCase(unittest.TestCase):
+ __metaclass__ = _MetaBrowserTestCase
+
@classmethod
def setUpClass(cls):
cls._platform = None
« no previous file with comments | « telemetry/telemetry/internal/backends/chrome_inspector/tracing_backend_unittest.py ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698