diff options
author | bulach@chromium.org <bulach@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-07-12 13:59:53 +0000 |
---|---|---|
committer | bulach@chromium.org <bulach@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-07-12 13:59:53 +0000 |
commit | a0c1fa879609e996816d6844215ae007f42a300c (patch) | |
tree | decae525b8387a75014755cf8c533286379fc445 /build/android/pylib/python_test_caller.py | |
parent | 361b9e8cf474ddad4f0bf5716c0804801a0ad633 (diff) | |
download | chromium_src-a0c1fa879609e996816d6844215ae007f42a300c.zip chromium_src-a0c1fa879609e996816d6844215ae007f42a300c.tar.gz chromium_src-a0c1fa879609e996816d6844215ae007f42a300c.tar.bz2 |
Android: adds instrumentation test runners.
Part of upstreaming build/android, this adds the instrumentation
test runners to allow us to run java-based tests.
BUG=136688
TEST=
Review URL: https://chromiumcodereview.appspot.com/10703165
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@146335 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'build/android/pylib/python_test_caller.py')
-rw-r--r-- | build/android/pylib/python_test_caller.py | 85 |
1 files changed, 85 insertions, 0 deletions
diff --git a/build/android/pylib/python_test_caller.py b/build/android/pylib/python_test_caller.py new file mode 100644 index 0000000..2cc5d7c --- /dev/null +++ b/build/android/pylib/python_test_caller.py @@ -0,0 +1,85 @@ +# Copyright (c) 2012 The Chromium Authors. All rights reserved. +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +"""Helper module for calling python-based tests.""" + + +import logging +import sys +import time + +from test_result import TestResults + + +def CallPythonTest(test, device_id, shard_index): + """Invokes a test function and translates Python exceptions into test results. + + This method invokes SetUp()/TearDown() on the test. It is intended to be + resilient to exceptions in SetUp(), the test itself, and TearDown(). Any + Python exception means the test is marked as failed, and the test result will + contain information about the exception. + + If SetUp() raises an exception, the test is not run. + + If TearDown() raises an exception, the test is treated as a failure. However, + if the test itself raised an exception beforehand, that stack trace will take + precedence whether or not TearDown() also raised an exception. + + shard_index is not applicable in single-device scenarios, when test execution + is serial rather than parallel. Tests can use this to bring up servers with + unique port numbers, for example. See also python_test_sharder. + + Args: + test: an object which is ostensibly a subclass of PythonTestBase. + device_id: device ID against which the test will run. + shard_index: index # of the shard on which this test is running + + Returns: + A TestResults object which contains any results produced by the test or, in + the case of a Python exception, the Python exception info. + """ + + start_date_ms = int(time.time()) * 1000 + failed = False + + try: + test.SetUp(device_id, shard_index) + except Exception: + failed = True + logging.exception( + 'Caught exception while trying to run SetUp() for test: ' + + test.qualified_name) + # Tests whose SetUp() method has failed are likely to fail, or at least + # yield invalid results. + exc_info = sys.exc_info() + return TestResults.FromPythonException(test.qualified_name, start_date_ms, + exc_info) + + try: + result = test.Run() + except Exception: + # Setting this lets TearDown() avoid stomping on our stack trace from Run() + # should TearDown() also raise an exception. + failed = True + logging.exception('Caught exception while trying to run test: ' + + test.qualified_name) + exc_info = sys.exc_info() + result = TestResults.FromPythonException(test.qualified_name, start_date_ms, + exc_info) + + try: + test.TearDown() + except Exception: + logging.exception( + 'Caught exception while trying run TearDown() for test: ' + + test.qualified_name) + if not failed: + # Don't stomp the error during the test if TearDown blows up. This is a + # trade-off: if the test fails, this will mask any problem with TearDown + # until the test is fixed. + exc_info = sys.exc_info() + result = TestResults.FromPythonException(test.qualified_name, + start_date_ms, exc_info) + + return result |