1 # Copyright 2013 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.
5 """Runs host-driven tests on a particular device."""
12 from pylib.base import base_test_result
13 from pylib.base import base_test_runner
14 from pylib.instrumentation import test_result
19 class HostDrivenExceptionTestResult(test_result.InstrumentationTestResult):
20 """Test result corresponding to a python exception in a host-driven test."""
22 def __init__(self, test_name, start_date_ms, exc_info):
23 """Constructs a HostDrivenExceptionTestResult object.
26 test_name: name of the test which raised an exception.
27 start_date_ms: the starting time for the test.
28 exc_info: exception info, ostensibly from sys.exc_info().
30 exc_type, exc_value, exc_traceback = exc_info
31 trace_info = ''.join(traceback.format_exception(exc_type, exc_value,
33 log_msg = 'Exception:\n' + trace_info
34 duration_ms = (int(time.time()) * 1000) - start_date_ms
36 super(HostDrivenExceptionTestResult, self).__init__(
38 base_test_result.ResultType.FAIL,
41 log=str(exc_type) + ' ' + log_msg)
44 class HostDrivenTestRunner(base_test_runner.BaseTestRunner):
45 """Orchestrates running a set of host-driven tests.
47 Any Python exceptions in the tests are caught and translated into a failed
48 result, rather than being re-raised on the main thread.
52 def __init__(self, device, shard_index, tool, push_deps,
54 """Creates a new HostDrivenTestRunner.
57 device: Attached android device.
58 shard_index: Shard index.
59 tool: Name of the Valgrind tool.
60 push_deps: If True, push all dependencies to the device.
61 cleanup_test_files: Whether or not to cleanup test files on device.
64 super(HostDrivenTestRunner, self).__init__(device, tool, push_deps,
67 # The shard index affords the ability to create unique port numbers (e.g.
68 # DEFAULT_PORT + shard_index) if the test so wishes.
69 self.shard_index = shard_index
72 def RunTest(self, test):
73 """Sets up and runs a test case.
76 test: An object which is ostensibly a subclass of HostDrivenTestCase.
79 A TestRunResults object which contains the result produced by the test
80 and, in the case of a failure, the test that should be retried.
83 assert isinstance(test, test_case.HostDrivenTestCase)
85 start_date_ms = int(time.time()) * 1000
86 exception_raised = False
89 test.SetUp(self.device, self.shard_index, self._push_deps,
90 self._cleanup_test_files)
93 'Caught exception while trying to run SetUp() for test: ' +
95 # Tests whose SetUp() method has failed are likely to fail, or at least
96 # yield invalid results.
97 exc_info = sys.exc_info()
98 results = base_test_result.TestRunResults()
99 results.AddResult(HostDrivenExceptionTestResult(
100 test.tagged_name, start_date_ms, exc_info))
106 # Setting this lets TearDown() avoid stomping on our stack trace from
107 # Run() should TearDown() also raise an exception.
108 exception_raised = True
109 logging.exception('Caught exception while trying to run test: ' +
111 exc_info = sys.exc_info()
112 results = base_test_result.TestRunResults()
113 results.AddResult(HostDrivenExceptionTestResult(
114 test.tagged_name, start_date_ms, exc_info))
120 'Caught exception while trying run TearDown() for test: ' +
122 if not exception_raised:
123 # Don't stomp the error during the test if TearDown blows up. This is a
124 # trade-off: if the test fails, this will mask any problem with TearDown
125 # until the test is fixed.
126 exc_info = sys.exc_info()
127 results = base_test_result.TestRunResults()
128 results.AddResult(HostDrivenExceptionTestResult(
129 test.tagged_name, start_date_ms, exc_info))
131 if not results.DidRunPass():