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."""
10 class ResultType(object):
11 """Class enumerating test types."""
21 """Get a list of all test types."""
22 return [ResultType
.PASS
, ResultType
.SKIP
, ResultType
.FAIL
,
23 ResultType
.CRASH
, ResultType
.TIMEOUT
, ResultType
.UNKNOWN
]
26 class BaseTestResult(object):
27 """Base class for a single test result."""
29 def __init__(self
, name
, test_type
, duration
=0, log
=''):
30 """Construct a BaseTestResult.
33 name: Name of the test which defines uniqueness.
34 test_type: Type of the test result as defined in ResultType.
35 duration: Time it took for the test to run in milliseconds.
36 log: An optional string listing any errors.
39 assert test_type
in ResultType
.GetTypes()
41 self
._test
_type
= test_type
42 self
._duration
= duration
51 def __cmp__(self
, other
):
52 # pylint: disable=W0212
53 return cmp(self
._name
, other
._name
)
56 return hash(self
._name
)
58 def SetName(self
, name
):
61 Because we're putting this into a set, this should only be used if moving
62 this test result into another set.
67 """Get the test name."""
70 def SetType(self
, test_type
):
71 """Set the test result type."""
72 assert test_type
in ResultType
.GetTypes()
73 self
._test
_type
= test_type
76 """Get the test result type."""
77 return self
._test
_type
79 def GetDuration(self
):
80 """Get the test duration."""
83 def SetLog(self
, log
):
84 """Set the test log."""
88 """Get the test log."""
92 class TestRunResults(object):
93 """Set of results for a test run."""
97 self
._results
_lock
= threading
.RLock()
100 """Get the string representation of all test logs."""
101 with self
._results
_lock
:
103 for test_type
in ResultType
.GetTypes():
104 if test_type
!= ResultType
.PASS
:
105 for t
in sorted(self
._GetType
(test_type
)):
108 s
.append('[%s] %s:' % (test_type
, t
))
112 def GetGtestForm(self
):
113 """Get the gtest string representation of this object."""
114 with self
._results
_lock
:
116 plural
= lambda n
, s
, p
: '%d %s' % (n
, p
if n
!= 1 else s
)
117 tests
= lambda n
: plural(n
, 'test', 'tests')
119 s
.append('[==========] %s ran.' % (tests(len(self
.GetAll()))))
120 s
.append('[ PASSED ] %s.' % (tests(len(self
.GetPass()))))
122 skipped
= self
.GetSkip()
124 s
.append('[ SKIPPED ] Skipped %s, listed below:' % tests(len(skipped
)))
125 for t
in sorted(skipped
):
126 s
.append('[ SKIPPED ] %s' % str(t
))
128 all_failures
= self
.GetFail().union(self
.GetCrash(), self
.GetTimeout(),
131 s
.append('[ FAILED ] %s, listed below:' % tests(len(all_failures
)))
132 for t
in sorted(self
.GetFail()):
133 s
.append('[ FAILED ] %s' % str(t
))
134 for t
in sorted(self
.GetCrash()):
135 s
.append('[ FAILED ] %s (CRASHED)' % str(t
))
136 for t
in sorted(self
.GetTimeout()):
137 s
.append('[ FAILED ] %s (TIMEOUT)' % str(t
))
138 for t
in sorted(self
.GetUnknown()):
139 s
.append('[ FAILED ] %s (UNKNOWN)' % str(t
))
141 s
.append(plural(len(all_failures
), 'FAILED TEST', 'FAILED TESTS'))
144 def GetShortForm(self
):
145 """Get the short string representation of this object."""
146 with self
._results
_lock
:
148 s
.append('ALL: %d' % len(self
._results
))
149 for test_type
in ResultType
.GetTypes():
150 s
.append('%s: %d' % (test_type
, len(self
._GetType
(test_type
))))
151 return ''.join([x
.ljust(15) for x
in s
])
154 return self
.GetGtestForm()
156 def AddResult(self
, result
):
157 """Add |result| to the set.
160 result: An instance of BaseTestResult.
162 assert isinstance(result
, BaseTestResult
)
163 with self
._results
_lock
:
164 self
._results
.add(result
)
166 def AddResults(self
, results
):
167 """Add |results| to the set.
170 results: An iterable of BaseTestResult objects.
172 with self
._results
_lock
:
176 def AddTestRunResults(self
, results
):
177 """Add the set of test results from |results|.
180 results: An instance of TestRunResults.
182 assert isinstance(results
, TestRunResults
)
183 with self
._results
_lock
:
184 # pylint: disable=W0212
185 self
._results
.update(results
._results
)
188 """Get the set of all test results."""
189 with self
._results
_lock
:
190 return self
._results
.copy()
192 def _GetType(self
, test_type
):
193 """Get the set of test results with the given test type."""
194 with self
._results
_lock
:
195 return set(t
for t
in self
._results
if t
.GetType() == test_type
)
198 """Get the set of all passed test results."""
199 return self
._GetType
(ResultType
.PASS
)
202 """Get the set of all skipped test results."""
203 return self
._GetType
(ResultType
.SKIP
)
206 """Get the set of all failed test results."""
207 return self
._GetType
(ResultType
.FAIL
)
210 """Get the set of all crashed test results."""
211 return self
._GetType
(ResultType
.CRASH
)
213 def GetTimeout(self
):
214 """Get the set of all timed out test results."""
215 return self
._GetType
(ResultType
.TIMEOUT
)
217 def GetUnknown(self
):
218 """Get the set of all unknown test results."""
219 return self
._GetType
(ResultType
.UNKNOWN
)
221 def GetNotPass(self
):
222 """Get the set of all non-passed test results."""
223 return self
.GetAll() - self
.GetPass()
225 def DidRunPass(self
):
226 """Return whether the test run was successful."""
227 return not self
.GetNotPass() - self
.GetSkip()