|
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, options): |
|
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 options: Options to use for setting up tests. |
|
36 |
|
37 Returns: |
|
38 A TestResults object which contains any results produced by the test or, in |
|
39 the case of a Python exception, the Python exception info. |
|
40 """ |
|
41 |
|
42 start_date_ms = int(time.time()) * 1000 |
|
43 failed = False |
|
44 |
|
45 try: |
|
46 test.SetUp(options) |
|
47 except Exception: |
|
48 failed = True |
|
49 logging.exception( |
|
50 'Caught exception while trying to run SetUp() for test: ' + |
|
51 test.qualified_name) |
|
52 # Tests whose SetUp() method has failed are likely to fail, or at least |
|
53 # yield invalid results. |
|
54 exc_info = sys.exc_info() |
|
55 return TestResults.FromPythonException(test.qualified_name, start_date_ms, |
|
56 exc_info) |
|
57 |
|
58 try: |
|
59 result = test.Run() |
|
60 except Exception: |
|
61 # Setting this lets TearDown() avoid stomping on our stack trace from Run() |
|
62 # should TearDown() also raise an exception. |
|
63 failed = True |
|
64 logging.exception('Caught exception while trying to run test: ' + |
|
65 test.qualified_name) |
|
66 exc_info = sys.exc_info() |
|
67 result = TestResults.FromPythonException(test.qualified_name, start_date_ms, |
|
68 exc_info) |
|
69 |
|
70 try: |
|
71 test.TearDown() |
|
72 except Exception: |
|
73 logging.exception( |
|
74 'Caught exception while trying run TearDown() for test: ' + |
|
75 test.qualified_name) |
|
76 if not failed: |
|
77 # 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 |
|
79 # until the test is fixed. |
|
80 exc_info = sys.exc_info() |
|
81 result = TestResults.FromPythonException(test.qualified_name, |
|
82 start_date_ms, exc_info) |
|
83 |
|
84 return result |