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 | |
8 class ResultType(object): | |
9 """Class enumerating test types.""" | |
craigdh
2013/03/25 23:29:20
update docstring
| |
10 PASS = 'PASS' | |
11 FAIL = 'FAIL' | |
12 CRASH = 'CRASH' | |
13 TIMEOUT = 'TIMEOUT' | |
14 UNKNOWN = 'UNKNOWN' | |
15 | |
16 @staticmethod | |
17 def GetTypes(): | |
18 """Get a list of all test types.""" | |
19 return [ResultType.PASS, ResultType.FAIL, ResultType.CRASH, | |
20 ResultType.TIMEOUT, ResultType.UNKNOWN] | |
21 | |
22 | |
23 class BaseTestResult(object): | |
24 """Base class for a single test result.""" | |
25 def __init__(self, name, test_type, log=''): | |
26 """Construct a BaseTestResult. | |
27 | |
28 Args: | |
29 name: Name of the test which defines uniqueness. | |
30 test_type: Type of the test result as defined in ResultType. | |
31 log: An optional string listing any errors. | |
32 """ | |
33 assert name | |
34 assert test_type in ResultType.GetTypes() | |
35 self._name = name | |
36 self._test_type = test_type | |
37 self._log = log | |
38 | |
39 def __str__(self): | |
40 return self._name | |
41 | |
42 def __repr__(self): | |
43 return self._name | |
44 | |
45 def __cmp__(self, other): | |
46 # pylint: disable=W0212 | |
47 return cmp(self._name, other._name) | |
48 | |
49 def __hash__(self): | |
50 return hash(self._name) | |
51 | |
52 def GetName(self): | |
53 """Get the test name.""" | |
54 return self._name | |
55 | |
56 def GetType(self): | |
57 """Get the test result type.""" | |
58 return self._test_type | |
59 | |
60 def GetLog(self): | |
61 """Get the test log.""" | |
62 return self._log | |
63 | |
64 | |
65 class TestRunResults(object): | |
66 """Set of results for a test run.""" | |
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 ResultType.GetTypes(): | |
74 if test_type != ResultType.PASS: | |
75 for t in sorted(self._GetType(test_type)): | |
76 s.append('[%s] %s:' % (test_type, t)) | |
77 s.append(t.GetLog()) | |
78 return '\n'.join(s) | |
79 | |
80 def GetLongForm(self): | |
81 """Get the long string representation of this object.""" | |
82 s = [] | |
83 s.append('ALL (%d tests)' % len(self._results)) | |
84 for test_type in ResultType.GetTypes(): | |
85 tests = sorted(self._GetType(test_type)) | |
86 if test_type == ResultType.PASS: | |
87 s.append('%s (%d tests)' % (test_type, len(tests))) | |
88 else: | |
89 s.append('%s (%d tests): %s' % (test_type, len(tests), tests)) | |
90 return '\n'.join(s) | |
91 | |
92 def GetShortForm(self): | |
93 """Get the short string representation of this object.""" | |
94 s = [] | |
95 s.append('ALL: %d' % len(self._results)) | |
96 for test_type in ResultType.GetTypes(): | |
97 s.append('%s: %d' % (test_type, len(self._GetType(test_type)))) | |
98 return ''.join([x.ljust(15) for x in s]) | |
craigdh
2013/03/25 23:29:20
nit: no need for the [ and ]
| |
99 | |
100 def __str__(self): | |
101 return self.GetLongForm() | |
102 | |
103 def AddResult(self, result): | |
104 """Add |result| to the set. | |
105 | |
106 Args: | |
107 result: An instance of BaseTestResult. | |
108 """ | |
109 assert isinstance(result, BaseTestResult) | |
110 self._results.add(result) | |
111 | |
112 def AddResults(self, results): | |
113 """Add |results| to the set. | |
114 | |
115 Args: | |
116 results: An iterable of BaseTestResult objects. | |
117 """ | |
118 for t in results: | |
119 self.AddResult(t) | |
120 | |
121 def AddTestRunResults(self, results): | |
122 """Add the set of test results from |results|. | |
123 | |
124 Args: | |
125 results: An instance of TestRunResults. | |
126 """ | |
127 assert isinstance(results, TestRunResults) | |
128 # pylint: disable=W0212 | |
129 self._results.update(results._results) | |
130 | |
131 def GetAll(self): | |
132 """Get the set of all test results.""" | |
133 return self._results.copy() | |
134 | |
135 def _GetType(self, test_type): | |
136 """Get the set of test results with the given test type.""" | |
137 return set(t for t in self._results if t.GetType() == test_type) | |
138 | |
139 def GetPass(self): | |
140 """Get the set of all passed test results.""" | |
141 return self._GetType(ResultType.PASS) | |
142 | |
143 def GetFail(self): | |
144 """Get the set of all failed test results.""" | |
145 return self._GetType(ResultType.FAIL) | |
146 | |
147 def GetCrash(self): | |
148 """Get the set of all crashed test results.""" | |
149 return self._GetType(ResultType.CRASH) | |
150 | |
151 def GetTimeout(self): | |
152 """Get the set of all timed out test results.""" | |
153 return self._GetType(ResultType.TIMEOUT) | |
154 | |
155 def GetUnknown(self): | |
156 """Get the set of all unknown test results.""" | |
157 return self._GetType(ResultType.UNKNOWN) | |
158 | |
159 def GetNotPass(self): | |
160 """Get the set of all non-passed test results.""" | |
161 return self.GetAll() - self.GetPass() | |
162 | |
163 def DidRunPass(self): | |
164 """Return whether the test run was successful.""" | |
165 return not self.GetNotPass() | |
OLD | NEW |