| OLD | NEW |
| 1 # Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 # Copyright (c) 2013 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 """Module containing base test results classes.""" | 5 """Module containing base test results classes.""" |
| 6 | 6 |
| 7 from pylib import constants |
| 8 |
| 7 | 9 |
| 8 class ResultType(object): | 10 class ResultType(object): |
| 9 """Class enumerating test types.""" | 11 """Class enumerating test types.""" |
| 10 PASS = 'PASS' | 12 PASS = 'PASS' |
| 11 FAIL = 'FAIL' | 13 FAIL = 'FAIL' |
| 12 CRASH = 'CRASH' | 14 CRASH = 'CRASH' |
| 13 TIMEOUT = 'TIMEOUT' | 15 TIMEOUT = 'TIMEOUT' |
| 14 UNKNOWN = 'UNKNOWN' | 16 UNKNOWN = 'UNKNOWN' |
| 15 | 17 |
| 16 @staticmethod | 18 @staticmethod |
| 17 def GetTypes(): | 19 def GetTypes(): |
| 18 """Get a list of all test types.""" | 20 """Get a list of all test types.""" |
| 19 return [ResultType.PASS, ResultType.FAIL, ResultType.CRASH, | 21 return [ResultType.PASS, ResultType.FAIL, ResultType.CRASH, |
| 20 ResultType.TIMEOUT, ResultType.UNKNOWN] | 22 ResultType.TIMEOUT, ResultType.UNKNOWN] |
| 21 | 23 |
| 22 | 24 |
| 23 class BaseTestResult(object): | 25 class BaseTestResult(object): |
| 24 """Base class for a single test result.""" | 26 """Base class for a single test result.""" |
| 27 |
| 25 def __init__(self, name, test_type, log=''): | 28 def __init__(self, name, test_type, log=''): |
| 26 """Construct a BaseTestResult. | 29 """Construct a BaseTestResult. |
| 27 | 30 |
| 28 Args: | 31 Args: |
| 29 name: Name of the test which defines uniqueness. | 32 name: Name of the test which defines uniqueness. |
| 30 test_type: Type of the test result as defined in ResultType. | 33 test_type: Type of the test result as defined in ResultType. |
| 31 log: An optional string listing any errors. | 34 log: An optional string listing any errors. |
| 32 """ | 35 """ |
| 33 assert name | 36 assert name |
| 34 assert test_type in ResultType.GetTypes() | 37 assert test_type in ResultType.GetTypes() |
| (...skipping 22 matching lines...) Expand all Loading... |
| 57 """Get the test result type.""" | 60 """Get the test result type.""" |
| 58 return self._test_type | 61 return self._test_type |
| 59 | 62 |
| 60 def GetLog(self): | 63 def GetLog(self): |
| 61 """Get the test log.""" | 64 """Get the test log.""" |
| 62 return self._log | 65 return self._log |
| 63 | 66 |
| 64 | 67 |
| 65 class TestRunResults(object): | 68 class TestRunResults(object): |
| 66 """Set of results for a test run.""" | 69 """Set of results for a test run.""" |
| 70 |
| 67 def __init__(self): | 71 def __init__(self): |
| 68 self._results = set() | 72 self._results = set() |
| 73 self.exit_code = 0 |
| 69 | 74 |
| 70 def GetLogs(self): | 75 def GetLogs(self): |
| 71 """Get the string representation of all test logs.""" | 76 """Get the string representation of all test logs.""" |
| 72 s = [] | 77 s = [] |
| 73 for test_type in ResultType.GetTypes(): | 78 for test_type in ResultType.GetTypes(): |
| 74 if test_type != ResultType.PASS: | 79 if test_type != ResultType.PASS: |
| 75 for t in sorted(self._GetType(test_type)): | 80 for t in sorted(self._GetType(test_type)): |
| 76 log = t.GetLog() | 81 log = t.GetLog() |
| 77 if log: | 82 if log: |
| 78 s.append('[%s] %s:' % (test_type, t)) | 83 s.append('[%s] %s:' % (test_type, t)) |
| (...skipping 24 matching lines...) Expand all Loading... |
| 103 return self.GetLongForm() | 108 return self.GetLongForm() |
| 104 | 109 |
| 105 def AddResult(self, result): | 110 def AddResult(self, result): |
| 106 """Add |result| to the set. | 111 """Add |result| to the set. |
| 107 | 112 |
| 108 Args: | 113 Args: |
| 109 result: An instance of BaseTestResult. | 114 result: An instance of BaseTestResult. |
| 110 """ | 115 """ |
| 111 assert isinstance(result, BaseTestResult) | 116 assert isinstance(result, BaseTestResult) |
| 112 self._results.add(result) | 117 self._results.add(result) |
| 118 if result.GetType() not in [ResultType.PASS, ResultType.UNKNOWN]: |
| 119 self.exit_code = constants.ERROR_EXIT_CODE |
| 113 | 120 |
| 114 def AddResults(self, results): | 121 def AddResults(self, results): |
| 115 """Add |results| to the set. | 122 """Add |results| to the set. |
| 116 | 123 |
| 117 Args: | 124 Args: |
| 118 results: An iterable of BaseTestResult objects. | 125 results: An iterable of BaseTestResult objects. |
| 119 """ | 126 """ |
| 120 for t in results: | 127 for t in results: |
| 121 self.AddResult(t) | 128 self.AddResult(t) |
| 122 | 129 |
| 123 def AddTestRunResults(self, results): | 130 def AddTestRunResults(self, results): |
| 124 """Add the set of test results from |results|. | 131 """Add the set of test results from |results|. |
| 125 | 132 |
| 126 Args: | 133 Args: |
| 127 results: An instance of TestRunResults. | 134 results: An instance of TestRunResults. |
| 128 """ | 135 """ |
| 129 assert isinstance(results, TestRunResults) | 136 assert isinstance(results, TestRunResults) |
| 130 # pylint: disable=W0212 | 137 # pylint: disable=W0212 |
| 131 self._results.update(results._results) | 138 self._results.update(results._results) |
| 139 self.exit_code = self.exit_code or results.exit_code |
| 132 | 140 |
| 133 def GetAll(self): | 141 def GetAll(self): |
| 134 """Get the set of all test results.""" | 142 """Get the set of all test results.""" |
| 135 return self._results.copy() | 143 return self._results.copy() |
| 136 | 144 |
| 137 def _GetType(self, test_type): | 145 def _GetType(self, test_type): |
| 138 """Get the set of test results with the given test type.""" | 146 """Get the set of test results with the given test type.""" |
| 139 return set(t for t in self._results if t.GetType() == test_type) | 147 return set(t for t in self._results if t.GetType() == test_type) |
| 140 | 148 |
| 141 def GetPass(self): | 149 def GetPass(self): |
| (...skipping 16 matching lines...) Expand all Loading... |
| 158 """Get the set of all unknown test results.""" | 166 """Get the set of all unknown test results.""" |
| 159 return self._GetType(ResultType.UNKNOWN) | 167 return self._GetType(ResultType.UNKNOWN) |
| 160 | 168 |
| 161 def GetNotPass(self): | 169 def GetNotPass(self): |
| 162 """Get the set of all non-passed test results.""" | 170 """Get the set of all non-passed test results.""" |
| 163 return self.GetAll() - self.GetPass() | 171 return self.GetAll() - self.GetPass() |
| 164 | 172 |
| 165 def DidRunPass(self): | 173 def DidRunPass(self): |
| 166 """Return whether the test run was successful.""" | 174 """Return whether the test run was successful.""" |
| 167 return not self.GetNotPass() | 175 return not self.GetNotPass() |
| OLD | NEW |