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 | 11 |
12 from test_result import TestResults | 12 from test_result import TestResults |
13 | 13 |
14 | 14 |
15 def CallPythonTest(test, device_id, shard_index): | 15 def CallPythonTest(test, options): |
16 """Invokes a test function and translates Python exceptions into test results. | 16 """Invokes a test function and translates Python exceptions into test results. |
17 | 17 |
18 This method invokes SetUp()/TearDown() on the test. It is intended to be | 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 | 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 | 20 Python exception means the test is marked as failed, and the test result will |
21 contain information about the exception. | 21 contain information about the exception. |
22 | 22 |
23 If SetUp() raises an exception, the test is not run. | 23 If SetUp() raises an exception, the test is not run. |
24 | 24 |
25 If TearDown() raises an exception, the test is treated as a failure. However, | 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 | 26 if the test itself raised an exception beforehand, that stack trace will take |
27 precedence whether or not TearDown() also raised an exception. | 27 precedence whether or not TearDown() also raised an exception. |
28 | 28 |
29 shard_index is not applicable in single-device scenarios, when test execution | 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 | 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. | 31 unique port numbers, for example. See also python_test_sharder. |
32 | 32 |
33 Args: | 33 Args: |
34 test: an object which is ostensibly a subclass of PythonTestBase. | 34 test: an object which is ostensibly a subclass of PythonTestBase. |
35 device_id: device ID against which the test will run. | 35 options: Options to use for setting up tests. |
36 shard_index: index # of the shard on which this test is running | |
37 | 36 |
38 Returns: | 37 Returns: |
39 A TestResults object which contains any results produced by the test or, in | 38 A TestResults object which contains any results produced by the test or, in |
40 the case of a Python exception, the Python exception info. | 39 the case of a Python exception, the Python exception info. |
41 """ | 40 """ |
42 | 41 |
43 start_date_ms = int(time.time()) * 1000 | 42 start_date_ms = int(time.time()) * 1000 |
44 failed = False | 43 failed = False |
45 | 44 |
46 try: | 45 try: |
47 test.SetUp(device_id, shard_index) | 46 test.SetUp(options) |
48 except Exception: | 47 except Exception: |
49 failed = True | 48 failed = True |
50 logging.exception( | 49 logging.exception( |
51 'Caught exception while trying to run SetUp() for test: ' + | 50 'Caught exception while trying to run SetUp() for test: ' + |
52 test.qualified_name) | 51 test.qualified_name) |
53 # Tests whose SetUp() method has failed are likely to fail, or at least | 52 # Tests whose SetUp() method has failed are likely to fail, or at least |
54 # yield invalid results. | 53 # yield invalid results. |
55 exc_info = sys.exc_info() | 54 exc_info = sys.exc_info() |
56 return TestResults.FromPythonException(test.qualified_name, start_date_ms, | 55 return TestResults.FromPythonException(test.qualified_name, start_date_ms, |
57 exc_info) | 56 exc_info) |
(...skipping 18 matching lines...) Expand all Loading... |
76 test.qualified_name) | 75 test.qualified_name) |
77 if not failed: | 76 if not failed: |
78 # Don't stomp the error during the test if TearDown blows up. This is a | 77 # 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 | 78 # trade-off: if the test fails, this will mask any problem with TearDown |
80 # until the test is fixed. | 79 # until the test is fixed. |
81 exc_info = sys.exc_info() | 80 exc_info = sys.exc_info() |
82 result = TestResults.FromPythonException(test.qualified_name, | 81 result = TestResults.FromPythonException(test.qualified_name, |
83 start_date_ms, exc_info) | 82 start_date_ms, exc_info) |
84 | 83 |
85 return result | 84 return result |
OLD | NEW |