OLD | NEW |
(Empty) | |
| 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is governed by a BSD-style license that can be |
| 3 # found in the LICENSE file. |
| 4 |
| 5 """Helper module for calling python-based tests.""" |
| 6 |
| 7 |
| 8 import logging |
| 9 import sys |
| 10 import time |
| 11 |
| 12 from test_result import TestResults |
| 13 |
| 14 |
| 15 def CallPythonTest(test, device_id, shard_index): |
| 16 """Invokes a test function and translates Python exceptions into test results. |
| 17 |
| 18 This method invokes SetUp()/TearDown() on the test. It is intended to be |
| 19 resilient to exceptions in SetUp(), the test itself, and TearDown(). Any |
| 20 Python exception means the test is marked as failed, and the test result will |
| 21 contain information about the exception. |
| 22 |
| 23 If SetUp() raises an exception, the test is not run. |
| 24 |
| 25 If TearDown() raises an exception, the test is treated as a failure. However, |
| 26 if the test itself raised an exception beforehand, that stack trace will take |
| 27 precedence whether or not TearDown() also raised an exception. |
| 28 |
| 29 shard_index is not applicable in single-device scenarios, when test execution |
| 30 is serial rather than parallel. Tests can use this to bring up servers with |
| 31 unique port numbers, for example. See also python_test_sharder. |
| 32 |
| 33 Args: |
| 34 test: an object which is ostensibly a subclass of PythonTestBase. |
| 35 device_id: device ID against which the test will run. |
| 36 shard_index: index # of the shard on which this test is running |
| 37 |
| 38 Returns: |
| 39 A TestResults object which contains any results produced by the test or, in |
| 40 the case of a Python exception, the Python exception info. |
| 41 """ |
| 42 |
| 43 start_date_ms = int(time.time()) * 1000 |
| 44 failed = False |
| 45 |
| 46 try: |
| 47 test.SetUp(device_id, shard_index) |
| 48 except Exception: |
| 49 failed = True |
| 50 logging.exception( |
| 51 'Caught exception while trying to run SetUp() for test: ' + |
| 52 test.qualified_name) |
| 53 # Tests whose SetUp() method has failed are likely to fail, or at least |
| 54 # yield invalid results. |
| 55 exc_info = sys.exc_info() |
| 56 return TestResults.FromPythonException(test.qualified_name, start_date_ms, |
| 57 exc_info) |
| 58 |
| 59 try: |
| 60 result = test.Run() |
| 61 except Exception: |
| 62 # Setting this lets TearDown() avoid stomping on our stack trace from Run() |
| 63 # should TearDown() also raise an exception. |
| 64 failed = True |
| 65 logging.exception('Caught exception while trying to run test: ' + |
| 66 test.qualified_name) |
| 67 exc_info = sys.exc_info() |
| 68 result = TestResults.FromPythonException(test.qualified_name, start_date_ms, |
| 69 exc_info) |
| 70 |
| 71 try: |
| 72 test.TearDown() |
| 73 except Exception: |
| 74 logging.exception( |
| 75 'Caught exception while trying run TearDown() for test: ' + |
| 76 test.qualified_name) |
| 77 if not failed: |
| 78 # Don't stomp the error during the test if TearDown blows up. This is a |
| 79 # trade-off: if the test fails, this will mask any problem with TearDown |
| 80 # until the test is fixed. |
| 81 exc_info = sys.exc_info() |
| 82 result = TestResults.FromPythonException(test.qualified_name, |
| 83 start_date_ms, exc_info) |
| 84 |
| 85 return result |
OLD | NEW |