1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/media/webrtc/trunk/build/android/pylib/python_test_caller.py Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,84 @@ 1.4 +# Copyright (c) 2012 The Chromium Authors. All rights reserved. 1.5 +# Use of this source code is governed by a BSD-style license that can be 1.6 +# found in the LICENSE file. 1.7 + 1.8 +"""Helper module for calling python-based tests.""" 1.9 + 1.10 + 1.11 +import logging 1.12 +import sys 1.13 +import time 1.14 + 1.15 +from test_result import TestResults 1.16 + 1.17 + 1.18 +def CallPythonTest(test, options): 1.19 + """Invokes a test function and translates Python exceptions into test results. 1.20 + 1.21 + This method invokes SetUp()/TearDown() on the test. It is intended to be 1.22 + resilient to exceptions in SetUp(), the test itself, and TearDown(). Any 1.23 + Python exception means the test is marked as failed, and the test result will 1.24 + contain information about the exception. 1.25 + 1.26 + If SetUp() raises an exception, the test is not run. 1.27 + 1.28 + If TearDown() raises an exception, the test is treated as a failure. However, 1.29 + if the test itself raised an exception beforehand, that stack trace will take 1.30 + precedence whether or not TearDown() also raised an exception. 1.31 + 1.32 + shard_index is not applicable in single-device scenarios, when test execution 1.33 + is serial rather than parallel. Tests can use this to bring up servers with 1.34 + unique port numbers, for example. See also python_test_sharder. 1.35 + 1.36 + Args: 1.37 + test: an object which is ostensibly a subclass of PythonTestBase. 1.38 + options: Options to use for setting up tests. 1.39 + 1.40 + Returns: 1.41 + A TestResults object which contains any results produced by the test or, in 1.42 + the case of a Python exception, the Python exception info. 1.43 + """ 1.44 + 1.45 + start_date_ms = int(time.time()) * 1000 1.46 + failed = False 1.47 + 1.48 + try: 1.49 + test.SetUp(options) 1.50 + except Exception: 1.51 + failed = True 1.52 + logging.exception( 1.53 + 'Caught exception while trying to run SetUp() for test: ' + 1.54 + test.qualified_name) 1.55 + # Tests whose SetUp() method has failed are likely to fail, or at least 1.56 + # yield invalid results. 1.57 + exc_info = sys.exc_info() 1.58 + return TestResults.FromPythonException(test.qualified_name, start_date_ms, 1.59 + exc_info) 1.60 + 1.61 + try: 1.62 + result = test.Run() 1.63 + except Exception: 1.64 + # Setting this lets TearDown() avoid stomping on our stack trace from Run() 1.65 + # should TearDown() also raise an exception. 1.66 + failed = True 1.67 + logging.exception('Caught exception while trying to run test: ' + 1.68 + test.qualified_name) 1.69 + exc_info = sys.exc_info() 1.70 + result = TestResults.FromPythonException(test.qualified_name, start_date_ms, 1.71 + exc_info) 1.72 + 1.73 + try: 1.74 + test.TearDown() 1.75 + except Exception: 1.76 + logging.exception( 1.77 + 'Caught exception while trying run TearDown() for test: ' + 1.78 + test.qualified_name) 1.79 + if not failed: 1.80 + # Don't stomp the error during the test if TearDown blows up. This is a 1.81 + # trade-off: if the test fails, this will mask any problem with TearDown 1.82 + # until the test is fixed. 1.83 + exc_info = sys.exc_info() 1.84 + result = TestResults.FromPythonException(test.qualified_name, 1.85 + start_date_ms, exc_info) 1.86 + 1.87 + return result