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 import traceback | |
12 | |
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. | |
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.ResultType.FAIL, | |
37 start_date_ms, | |
38 duration_ms, | |
39 log=str(exc_type) + ' ' + log_msg) | |
40 | |
41 | |
42 def CallPythonTest(test, options): | |
43 """Invokes a test function and translates Python exceptions into test results. | |
44 | |
45 This method invokes SetUp()/TearDown() on the test. It is intended to be | |
46 resilient to exceptions in SetUp(), the test itself, and TearDown(). Any | |
47 Python exception means the test is marked as failed, and the test result will | |
48 contain information about the exception. | |
49 | |
50 If SetUp() raises an exception, the test is not run. | |
51 | |
52 If TearDown() raises an exception, the test is treated as a failure. However, | |
53 if the test itself raised an exception beforehand, that stack trace will take | |
54 precedence whether or not TearDown() also raised an exception. | |
55 | |
56 shard_index is not applicable in single-device scenarios, when test execution | |
57 is serial rather than parallel. Tests can use this to bring up servers with | |
58 unique port numbers, for example. See also python_test_sharder. | |
59 | |
60 Args: | |
61 test: an object which is ostensibly a subclass of PythonTestBase. | |
62 options: Options to use for setting up tests. | |
63 | |
64 Returns: | |
65 A TestRunResults object which contains any results produced by the test or, | |
66 in the case of a Python exception, the Python exception info. | |
67 """ | |
68 | |
69 start_date_ms = int(time.time()) * 1000 | |
70 failed = False | |
71 | |
72 try: | |
73 test.SetUp(options) | |
74 except Exception: | |
75 failed = True | |
76 logging.exception( | |
77 'Caught exception while trying to run SetUp() for test: ' + | |
78 test.qualified_name) | |
79 # Tests whose SetUp() method has failed are likely to fail, or at least | |
80 # yield invalid results. | |
81 exc_info = sys.exc_info() | |
82 results = base_test_result.TestRunResults() | |
83 results.AddResult(PythonExceptionTestResult( | |
84 test.qualified_name, start_date_ms, exc_info)) | |
85 return results | |
86 | |
87 try: | |
88 results = test.Run() | |
89 except Exception: | |
90 # Setting this lets TearDown() avoid stomping on our stack trace from Run() | |
91 # should TearDown() also raise an exception. | |
92 failed = True | |
93 logging.exception('Caught exception while trying to run test: ' + | |
94 test.qualified_name) | |
95 exc_info = sys.exc_info() | |
96 results = base_test_result.TestRunResults() | |
97 results.AddResult(PythonExceptionTestResult( | |
98 test.qualified_name, start_date_ms, exc_info)) | |
99 | |
100 try: | |
101 test.TearDown() | |
102 except Exception: | |
103 logging.exception( | |
104 'Caught exception while trying run TearDown() for test: ' + | |
105 test.qualified_name) | |
106 if not failed: | |
107 # Don't stomp the error during the test if TearDown blows up. This is a | |
108 # trade-off: if the test fails, this will mask any problem with TearDown | |
109 # until the test is fixed. | |
110 exc_info = sys.exc_info() | |
111 results = base_test_result.TestRunResults() | |
112 results.AddResult(PythonExceptionTestResult( | |
113 test.qualified_name, start_date_ms, exc_info)) | |
114 | |
115 return results | |
OLD | NEW |