OLD | NEW |
---|---|
1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 """Helper module for calling python-based tests.""" | 5 """Helper module for calling python-based tests.""" |
6 | 6 |
7 | 7 |
8 import logging | 8 import logging |
9 import sys | 9 import sys |
10 import time | 10 import time |
11 import traceback | |
11 | 12 |
12 from pylib.base.test_result import TestResults | 13 from pylib.base import base_test_result |
14 from pylib.instrumentation import test_result | |
15 | |
16 | |
17 class PythonExceptionTestResult(test_result.InstrumentationTestResult): | |
18 """Helper class for creating a test result from python exception.""" | |
19 | |
20 def __init__(self, test_name, start_date_ms, exc_info): | |
21 """Constructs an PythonExceptionTestResult object. | |
22 | |
23 Args: | |
24 test_name: name of the test which raised an exception. | |
craigdh
2013/03/23 00:41:39
this patch set isn't consistent about capitalizati
| |
25 start_date_ms: the starting time for the test. | |
26 exc_info: exception info, ostensibly from sys.exc_info(). | |
27 """ | |
28 exc_type, exc_value, exc_traceback = exc_info | |
29 trace_info = ''.join(traceback.format_exception(exc_type, exc_value, | |
30 exc_traceback)) | |
31 log_msg = 'Exception:\n' + trace_info | |
32 duration_ms = (int(time.time()) * 1000) - start_date_ms | |
33 | |
34 super(PythonExceptionTestResult, self).__init__( | |
35 'PythonWrapper#' + test_name, | |
36 base_test_result.TestType.FAIL, | |
37 start_date_ms, | |
38 duration_ms, | |
39 log=str(exc_type) + ' ' + log_msg) | |
13 | 40 |
14 | 41 |
15 def CallPythonTest(test, options): | 42 def CallPythonTest(test, options): |
16 """Invokes a test function and translates Python exceptions into test results. | 43 """Invokes a test function and translates Python exceptions into test results. |
17 | 44 |
18 This method invokes SetUp()/TearDown() on the test. It is intended to be | 45 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 | 46 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 | 47 Python exception means the test is marked as failed, and the test result will |
21 contain information about the exception. | 48 contain information about the exception. |
22 | 49 |
23 If SetUp() raises an exception, the test is not run. | 50 If SetUp() raises an exception, the test is not run. |
24 | 51 |
25 If TearDown() raises an exception, the test is treated as a failure. However, | 52 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 | 53 if the test itself raised an exception beforehand, that stack trace will take |
27 precedence whether or not TearDown() also raised an exception. | 54 precedence whether or not TearDown() also raised an exception. |
28 | 55 |
29 shard_index is not applicable in single-device scenarios, when test execution | 56 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 | 57 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. | 58 unique port numbers, for example. See also python_test_sharder. |
32 | 59 |
33 Args: | 60 Args: |
34 test: an object which is ostensibly a subclass of PythonTestBase. | 61 test: an object which is ostensibly a subclass of PythonTestBase. |
35 options: Options to use for setting up tests. | 62 options: Options to use for setting up tests. |
36 | 63 |
37 Returns: | 64 Returns: |
38 A TestResults object which contains any results produced by the test or, in | 65 A TestRunResults object which contains any results produced by the test or, |
39 the case of a Python exception, the Python exception info. | 66 in the case of a Python exception, the Python exception info. |
40 """ | 67 """ |
41 | 68 |
42 start_date_ms = int(time.time()) * 1000 | 69 start_date_ms = int(time.time()) * 1000 |
43 failed = False | 70 failed = False |
44 | 71 |
45 try: | 72 try: |
46 test.SetUp(options) | 73 test.SetUp(options) |
47 except Exception: | 74 except Exception: |
48 failed = True | 75 failed = True |
49 logging.exception( | 76 logging.exception( |
50 'Caught exception while trying to run SetUp() for test: ' + | 77 'Caught exception while trying to run SetUp() for test: ' + |
51 test.qualified_name) | 78 test.qualified_name) |
52 # Tests whose SetUp() method has failed are likely to fail, or at least | 79 # Tests whose SetUp() method has failed are likely to fail, or at least |
53 # yield invalid results. | 80 # yield invalid results. |
54 exc_info = sys.exc_info() | 81 exc_info = sys.exc_info() |
55 return TestResults.FromPythonException(test.qualified_name, start_date_ms, | 82 results = base_test_result.TestRunResults() |
56 exc_info) | 83 results.AddResult(PythonExceptionTestResult( |
84 test.qualified_name, start_date_ms, exc_info)) | |
85 return results | |
57 | 86 |
58 try: | 87 try: |
59 result = test.Run() | 88 results = test.Run() |
60 except Exception: | 89 except Exception: |
61 # Setting this lets TearDown() avoid stomping on our stack trace from Run() | 90 # Setting this lets TearDown() avoid stomping on our stack trace from Run() |
62 # should TearDown() also raise an exception. | 91 # should TearDown() also raise an exception. |
63 failed = True | 92 failed = True |
64 logging.exception('Caught exception while trying to run test: ' + | 93 logging.exception('Caught exception while trying to run test: ' + |
65 test.qualified_name) | 94 test.qualified_name) |
66 exc_info = sys.exc_info() | 95 exc_info = sys.exc_info() |
67 result = TestResults.FromPythonException(test.qualified_name, start_date_ms, | 96 results = base_test_result.TestRunResults() |
68 exc_info) | 97 results.AddResult(PythonExceptionTestResult( |
98 test.qualified_name, start_date_ms, exc_info)) | |
69 | 99 |
70 try: | 100 try: |
71 test.TearDown() | 101 test.TearDown() |
72 except Exception: | 102 except Exception: |
73 logging.exception( | 103 logging.exception( |
74 'Caught exception while trying run TearDown() for test: ' + | 104 'Caught exception while trying run TearDown() for test: ' + |
75 test.qualified_name) | 105 test.qualified_name) |
76 if not failed: | 106 if not failed: |
77 # Don't stomp the error during the test if TearDown blows up. This is a | 107 # Don't stomp the error during the test if TearDown blows up. This is a |
78 # trade-off: if the test fails, this will mask any problem with TearDown | 108 # trade-off: if the test fails, this will mask any problem with TearDown |
79 # until the test is fixed. | 109 # until the test is fixed. |
80 exc_info = sys.exc_info() | 110 exc_info = sys.exc_info() |
81 result = TestResults.FromPythonException(test.qualified_name, | 111 results = base_test_result.TestRunResults() |
82 start_date_ms, exc_info) | 112 results.AddResult(PythonExceptionTestResult( |
113 test.qualified_name, start_date_ms, exc_info)) | |
83 | 114 |
84 return result | 115 return results |
OLD | NEW |