Add Apps.AppListSearchQueryLength UMA histogram.
[chromium-blink-merge.git] / build / android / pylib / base / base_test_result.py
blob58200f6d49535b64426b6d5160b01e7a3a010241
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.
5 """Module containing base test results classes."""
7 class ResultType(object):
8 """Class enumerating test types."""
9 PASS = 'PASS'
10 SKIP = 'SKIP'
11 FAIL = 'FAIL'
12 CRASH = 'CRASH'
13 TIMEOUT = 'TIMEOUT'
14 UNKNOWN = 'UNKNOWN'
16 @staticmethod
17 def GetTypes():
18 """Get a list of all test types."""
19 return [ResultType.PASS, ResultType.SKIP, ResultType.FAIL,
20 ResultType.CRASH, ResultType.TIMEOUT, ResultType.UNKNOWN]
23 class BaseTestResult(object):
24 """Base class for a single test result."""
26 def __init__(self, name, test_type, duration=0, log=''):
27 """Construct a BaseTestResult.
29 Args:
30 name: Name of the test which defines uniqueness.
31 test_type: Type of the test result as defined in ResultType.
32 duration: Time it took for the test to run in milliseconds.
33 log: An optional string listing any errors.
34 """
35 assert name
36 assert test_type in ResultType.GetTypes()
37 self._name = name
38 self._test_type = test_type
39 self._duration = duration
40 self._log = log
42 def __str__(self):
43 return self._name
45 def __repr__(self):
46 return self._name
48 def __cmp__(self, other):
49 # pylint: disable=W0212
50 return cmp(self._name, other._name)
52 def __hash__(self):
53 return hash(self._name)
55 def SetName(self, name):
56 """Set the test name.
58 Because we're putting this into a set, this should only be used if moving
59 this test result into another set.
60 """
61 self._name = name
63 def GetName(self):
64 """Get the test name."""
65 return self._name
67 def SetType(self, test_type):
68 """Set the test result type."""
69 assert test_type in ResultType.GetTypes()
70 self._test_type = test_type
72 def GetType(self):
73 """Get the test result type."""
74 return self._test_type
76 def GetDuration(self):
77 """Get the test duration."""
78 return self._duration
80 def SetLog(self, log):
81 """Set the test log."""
82 self._log = log
84 def GetLog(self):
85 """Get the test log."""
86 return self._log
89 class TestRunResults(object):
90 """Set of results for a test run."""
92 def __init__(self):
93 self._results = set()
95 def GetLogs(self):
96 """Get the string representation of all test logs."""
97 s = []
98 for test_type in ResultType.GetTypes():
99 if test_type != ResultType.PASS:
100 for t in sorted(self._GetType(test_type)):
101 log = t.GetLog()
102 if log:
103 s.append('[%s] %s:' % (test_type, t))
104 s.append(log)
105 return '\n'.join(s)
107 def GetGtestForm(self):
108 """Get the gtest string representation of this object."""
109 s = []
110 plural = lambda n, s, p: '%d %s' % (n, p if n != 1 else s)
111 tests = lambda n: plural(n, 'test', 'tests')
113 s.append('[==========] %s ran.' % (tests(len(self.GetAll()))))
114 s.append('[ PASSED ] %s.' % (tests(len(self.GetPass()))))
116 skipped = self.GetSkip()
117 if skipped:
118 s.append('[ SKIPPED ] Skipped %s, listed below:' % tests(len(skipped)))
119 for t in sorted(skipped):
120 s.append('[ SKIPPED ] %s' % str(t))
122 all_failures = self.GetFail().union(self.GetCrash(), self.GetTimeout(),
123 self.GetUnknown())
124 if all_failures:
125 s.append('[ FAILED ] %s, listed below:' % tests(len(all_failures)))
126 for t in sorted(self.GetFail()):
127 s.append('[ FAILED ] %s' % str(t))
128 for t in sorted(self.GetCrash()):
129 s.append('[ FAILED ] %s (CRASHED)' % str(t))
130 for t in sorted(self.GetTimeout()):
131 s.append('[ FAILED ] %s (TIMEOUT)' % str(t))
132 for t in sorted(self.GetUnknown()):
133 s.append('[ FAILED ] %s (UNKNOWN)' % str(t))
134 s.append('')
135 s.append(plural(len(all_failures), 'FAILED TEST', 'FAILED TESTS'))
136 return '\n'.join(s)
138 def GetShortForm(self):
139 """Get the short string representation of this object."""
140 s = []
141 s.append('ALL: %d' % len(self._results))
142 for test_type in ResultType.GetTypes():
143 s.append('%s: %d' % (test_type, len(self._GetType(test_type))))
144 return ''.join([x.ljust(15) for x in s])
146 def __str__(self):
147 return self.GetLongForm()
149 def AddResult(self, result):
150 """Add |result| to the set.
152 Args:
153 result: An instance of BaseTestResult.
155 assert isinstance(result, BaseTestResult)
156 self._results.add(result)
158 def AddResults(self, results):
159 """Add |results| to the set.
161 Args:
162 results: An iterable of BaseTestResult objects.
164 for t in results:
165 self.AddResult(t)
167 def AddTestRunResults(self, results):
168 """Add the set of test results from |results|.
170 Args:
171 results: An instance of TestRunResults.
173 assert isinstance(results, TestRunResults)
174 # pylint: disable=W0212
175 self._results.update(results._results)
177 def GetAll(self):
178 """Get the set of all test results."""
179 return self._results.copy()
181 def _GetType(self, test_type):
182 """Get the set of test results with the given test type."""
183 return set(t for t in self._results if t.GetType() == test_type)
185 def GetPass(self):
186 """Get the set of all passed test results."""
187 return self._GetType(ResultType.PASS)
189 def GetSkip(self):
190 """Get the set of all skipped test results."""
191 return self._GetType(ResultType.SKIP)
193 def GetFail(self):
194 """Get the set of all failed test results."""
195 return self._GetType(ResultType.FAIL)
197 def GetCrash(self):
198 """Get the set of all crashed test results."""
199 return self._GetType(ResultType.CRASH)
201 def GetTimeout(self):
202 """Get the set of all timed out test results."""
203 return self._GetType(ResultType.TIMEOUT)
205 def GetUnknown(self):
206 """Get the set of all unknown test results."""
207 return self._GetType(ResultType.UNKNOWN)
209 def GetNotPass(self):
210 """Get the set of all non-passed test results."""
211 return self.GetAll() - self.GetPass()
213 def DidRunPass(self):
214 """Return whether the test run was successful."""
215 return not self.GetNotPass() - self.GetSkip()