Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 # found in the LICENSE file. | |
| 4 | |
| 5 """Module containing base test results classes.""" | |
| 6 | |
| 7 class TestType(object): | |
|
craigdh
2013/03/23 00:41:39
doc string
frankf
2013/03/23 01:32:59
Done.
| |
| 8 PASS = 'PASS' | |
| 9 FAIL = 'FAIL' | |
| 10 CRASH = 'CRASH' | |
| 11 TIMEOUT = 'TIMEOUT' | |
| 12 UNKNOWN = 'UNKNOWN' | |
| 13 | |
| 14 @staticmethod | |
| 15 def GetTypes(): | |
| 16 """Get a list of all test types.""" | |
| 17 return [TestType.PASS, TestType.FAIL, TestType.CRASH, TestType.TIMEOUT, | |
| 18 TestType.UNKNOWN] | |
| 19 | |
| 20 | |
| 21 class BaseTestResult(object): | |
| 22 """Base class for a single test result.""" | |
| 23 | |
|
craigdh
2013/03/23 00:41:39
no blank line
frankf
2013/03/23 01:32:59
Done, but pylint complains.
On 2013/03/23 00:41:3
| |
| 24 def __init__(self, name, test_type, log=''): | |
| 25 """Construct a BaseTestResult. | |
| 26 | |
| 27 Args: | |
| 28 name: Name of the test which defines uniqueness. | |
| 29 test_type: Type of the test result as defined in TestType. | |
| 30 log: An optional string listing any errors. | |
| 31 """ | |
| 32 assert name | |
| 33 assert test_type in TestType.GetTypes() | |
| 34 self._name = name | |
| 35 self._test_type = test_type | |
| 36 self._log = log | |
| 37 | |
| 38 def __str__(self): | |
| 39 return self._name | |
| 40 | |
| 41 def __repr__(self): | |
| 42 return self._name | |
| 43 | |
| 44 def __cmp__(self, other): | |
| 45 # pylint: disable=W0212 | |
| 46 return cmp(self._name, other._name) | |
| 47 | |
| 48 def __hash__(self): | |
| 49 return hash(self._name) | |
| 50 | |
| 51 def GetName(self): | |
| 52 """Get the test name.""" | |
| 53 return self._name | |
| 54 | |
| 55 def GetType(self): | |
| 56 """Get the test result type.""" | |
| 57 return self._test_type | |
| 58 | |
| 59 def GetLog(self): | |
| 60 """Get the test log.""" | |
| 61 return self._log | |
| 62 | |
| 63 | |
| 64 class TestRunResults(object): | |
| 65 """Set of results for a test run.""" | |
| 66 | |
|
craigdh
2013/03/23 00:41:39
ditto
frankf
2013/03/23 01:32:59
Done.
| |
| 67 def __init__(self): | |
| 68 self._results = set() | |
| 69 | |
| 70 def GetLogs(self): | |
| 71 """Get the string representation of all test logs.""" | |
| 72 s = [] | |
| 73 for test_type in TestType.GetTypes(): | |
| 74 if test_type != TestType.PASS: | |
| 75 tests = sorted(self._GetType(test_type)) | |
|
craigdh
2013/03/23 00:41:39
no need for intermediate variable tests
frankf
2013/03/23 01:32:59
Done.
| |
| 76 for t in tests: | |
| 77 s.append('[%s] %s:' % (test_type, t)) | |
| 78 s.append(t.GetLog()) | |
| 79 return '\n'.join(s) | |
| 80 | |
| 81 def GetLongForm(self): | |
| 82 """Get the long string representation of this object.""" | |
| 83 s = [] | |
| 84 s.append('ALL (%d tests)' % len(self._results)) | |
| 85 for test_type in TestType.GetTypes(): | |
| 86 tests = sorted(self._GetType(test_type)) | |
| 87 if test_type == TestType.PASS: | |
| 88 s.append('%s (%d tests)' % (test_type, len(tests))) | |
| 89 else: | |
| 90 s.append('%s (%d tests): %s' % (test_type, len(tests), tests)) | |
| 91 return '\n'.join(s) | |
| 92 | |
| 93 def GetShortForm(self): | |
| 94 """Get the short string representation of this object.""" | |
| 95 s = [] | |
| 96 s.append('ALL: %d' % len(self._results)) | |
| 97 for test_type in TestType.GetTypes(): | |
| 98 s.append('%s: %d' % (test_type, len(self._GetType(test_type)))) | |
| 99 return ', '.join(s) | |
| 100 | |
| 101 def __str__(self): | |
| 102 return self.GetLongForm() | |
| 103 | |
| 104 def AddResult(self, result): | |
| 105 """Add |result| to the set. | |
| 106 | |
| 107 Args: | |
| 108 result: An object derived from BaseTestResult. | |
|
craigdh
2013/03/23 00:41:39
I think "An instance of BaseTestResult." is better
frankf
2013/03/23 01:32:59
Done.
frankf
2013/03/23 01:32:59
Done.
| |
| 109 """ | |
| 110 | |
|
craigdh
2013/03/23 00:41:39
no blank line
frankf
2013/03/23 01:32:59
Done.
frankf
2013/03/23 01:32:59
Done.
| |
| 111 assert isinstance(result, BaseTestResult) | |
| 112 self._results.add(result) | |
| 113 | |
| 114 def AddResults(self, results): | |
| 115 """Add a set of results to this set. | |
|
craigdh
2013/03/23 00:41:39
doesn't have to be a set, can be any iterable
frankf
2013/03/23 01:32:59
Done.
frankf
2013/03/23 01:32:59
Done.
| |
| 116 | |
| 117 Args: | |
| 118 results: A set of objects derived from BaseTestResult. | |
|
craigdh
2013/03/23 00:41:39
ditto
frankf
2013/03/23 01:32:59
Done.
frankf
2013/03/23 01:32:59
Done.
| |
| 119 """ | |
| 120 for t in results: | |
| 121 self.AddResult(t) | |
| 122 | |
| 123 def AddTestRunResults(self, results): | |
| 124 """Add the set of test results from |results|. | |
| 125 | |
| 126 Args: | |
| 127 results: An instance of TestRunResults. | |
| 128 """ | |
| 129 assert isinstance(results, TestRunResults) | |
| 130 # pylint: disable=W0212 | |
| 131 self._results.update(results._results) | |
| 132 | |
| 133 def GetAll(self): | |
| 134 """Get the set of all test results.""" | |
| 135 return self._results.copy() | |
| 136 | |
| 137 def _GetType(self, test_type): | |
| 138 """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]) | |
|
craigdh
2013/03/23 00:41:39
nit: generator expressions are more scaleable than
frankf
2013/03/23 01:32:59
I want the return type to be a set though.
On 201
craigdh
2013/03/25 18:06:43
It still will be, there just won't be a (possibly
| |
| 140 | |
| 141 def GetPass(self): | |
| 142 """Get the set of all passed test results.""" | |
| 143 return self._GetType(TestType.PASS) | |
| 144 | |
| 145 def GetFail(self): | |
| 146 """Get the set of all failed test results.""" | |
| 147 return self._GetType(TestType.FAIL) | |
| 148 | |
| 149 def GetCrash(self): | |
| 150 """Get the set of all crashed test results.""" | |
| 151 return self._GetType(TestType.CRASH) | |
| 152 | |
| 153 def GetTimeout(self): | |
| 154 """Get the set of all timed out test results.""" | |
| 155 return self._GetType(TestType.TIMEOUT) | |
| 156 | |
| 157 def GetUnknown(self): | |
| 158 """Get the set of all unknown test results.""" | |
| 159 return self._GetType(TestType.UNKNOWN) | |
| 160 | |
| 161 def GetNotPass(self): | |
| 162 """Get the set of all non-passed test results.""" | |
| 163 return self.GetAll() - self.GetPass() | |
| 164 | |
| 165 def DidRunPass(self): | |
| 166 """Return whether the test run was succesfull""" | |
| 167 return not self.GetNotPass() | |
| OLD | NEW |