Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
michael@0 | 2 | # Use of this source code is governed by a BSD-style license that can be |
michael@0 | 3 | # found in the LICENSE file. |
michael@0 | 4 | |
michael@0 | 5 | """Helper module for calling python-based tests.""" |
michael@0 | 6 | |
michael@0 | 7 | |
michael@0 | 8 | import logging |
michael@0 | 9 | import sys |
michael@0 | 10 | import time |
michael@0 | 11 | |
michael@0 | 12 | from test_result import TestResults |
michael@0 | 13 | |
michael@0 | 14 | |
michael@0 | 15 | def CallPythonTest(test, options): |
michael@0 | 16 | """Invokes a test function and translates Python exceptions into test results. |
michael@0 | 17 | |
michael@0 | 18 | This method invokes SetUp()/TearDown() on the test. It is intended to be |
michael@0 | 19 | resilient to exceptions in SetUp(), the test itself, and TearDown(). Any |
michael@0 | 20 | Python exception means the test is marked as failed, and the test result will |
michael@0 | 21 | contain information about the exception. |
michael@0 | 22 | |
michael@0 | 23 | If SetUp() raises an exception, the test is not run. |
michael@0 | 24 | |
michael@0 | 25 | If TearDown() raises an exception, the test is treated as a failure. However, |
michael@0 | 26 | if the test itself raised an exception beforehand, that stack trace will take |
michael@0 | 27 | precedence whether or not TearDown() also raised an exception. |
michael@0 | 28 | |
michael@0 | 29 | shard_index is not applicable in single-device scenarios, when test execution |
michael@0 | 30 | is serial rather than parallel. Tests can use this to bring up servers with |
michael@0 | 31 | unique port numbers, for example. See also python_test_sharder. |
michael@0 | 32 | |
michael@0 | 33 | Args: |
michael@0 | 34 | test: an object which is ostensibly a subclass of PythonTestBase. |
michael@0 | 35 | options: Options to use for setting up tests. |
michael@0 | 36 | |
michael@0 | 37 | Returns: |
michael@0 | 38 | A TestResults object which contains any results produced by the test or, in |
michael@0 | 39 | the case of a Python exception, the Python exception info. |
michael@0 | 40 | """ |
michael@0 | 41 | |
michael@0 | 42 | start_date_ms = int(time.time()) * 1000 |
michael@0 | 43 | failed = False |
michael@0 | 44 | |
michael@0 | 45 | try: |
michael@0 | 46 | test.SetUp(options) |
michael@0 | 47 | except Exception: |
michael@0 | 48 | failed = True |
michael@0 | 49 | logging.exception( |
michael@0 | 50 | 'Caught exception while trying to run SetUp() for test: ' + |
michael@0 | 51 | test.qualified_name) |
michael@0 | 52 | # Tests whose SetUp() method has failed are likely to fail, or at least |
michael@0 | 53 | # yield invalid results. |
michael@0 | 54 | exc_info = sys.exc_info() |
michael@0 | 55 | return TestResults.FromPythonException(test.qualified_name, start_date_ms, |
michael@0 | 56 | exc_info) |
michael@0 | 57 | |
michael@0 | 58 | try: |
michael@0 | 59 | result = test.Run() |
michael@0 | 60 | except Exception: |
michael@0 | 61 | # Setting this lets TearDown() avoid stomping on our stack trace from Run() |
michael@0 | 62 | # should TearDown() also raise an exception. |
michael@0 | 63 | failed = True |
michael@0 | 64 | logging.exception('Caught exception while trying to run test: ' + |
michael@0 | 65 | test.qualified_name) |
michael@0 | 66 | exc_info = sys.exc_info() |
michael@0 | 67 | result = TestResults.FromPythonException(test.qualified_name, start_date_ms, |
michael@0 | 68 | exc_info) |
michael@0 | 69 | |
michael@0 | 70 | try: |
michael@0 | 71 | test.TearDown() |
michael@0 | 72 | except Exception: |
michael@0 | 73 | logging.exception( |
michael@0 | 74 | 'Caught exception while trying run TearDown() for test: ' + |
michael@0 | 75 | test.qualified_name) |
michael@0 | 76 | if not failed: |
michael@0 | 77 | # Don't stomp the error during the test if TearDown blows up. This is a |
michael@0 | 78 | # trade-off: if the test fails, this will mask any problem with TearDown |
michael@0 | 79 | # until the test is fixed. |
michael@0 | 80 | exc_info = sys.exc_info() |
michael@0 | 81 | result = TestResults.FromPythonException(test.qualified_name, |
michael@0 | 82 | start_date_ms, exc_info) |
michael@0 | 83 | |
michael@0 | 84 | return result |