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."""
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.
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.
36 assert test_type
in ResultType
.GetTypes()
38 self
._test
_type
= test_type
39 self
._duration
= duration
48 def __cmp__(self
, other
):
49 # pylint: disable=W0212
50 return cmp(self
._name
, other
._name
)
53 return hash(self
._name
)
55 def SetName(self
, name
):
58 Because we're putting this into a set, this should only be used if moving
59 this test result into another set.
64 """Get the test 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
73 """Get the test result type."""
74 return self
._test
_type
76 def GetDuration(self
):
77 """Get the test duration."""
80 def SetLog(self
, log
):
81 """Set the test log."""
85 """Get the test log."""
89 class TestRunResults(object):
90 """Set of results for a test run."""
96 """Get the string representation of all test logs."""
98 for test_type
in ResultType
.GetTypes():
99 if test_type
!= ResultType
.PASS
:
100 for t
in sorted(self
._GetType
(test_type
)):
103 s
.append('[%s] %s:' % (test_type
, t
))
107 def GetGtestForm(self
):
108 """Get the gtest string representation of this object."""
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()
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(),
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
))
135 s
.append(plural(len(all_failures
), 'FAILED TEST', 'FAILED TESTS'))
138 def GetShortForm(self
):
139 """Get the short string representation of this object."""
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
])
147 return self
.GetLongForm()
149 def AddResult(self
, result
):
150 """Add |result| to the set.
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.
162 results: An iterable of BaseTestResult objects.
167 def AddTestRunResults(self
, results
):
168 """Add the set of test results from |results|.
171 results: An instance of TestRunResults.
173 assert isinstance(results
, TestRunResults
)
174 # pylint: disable=W0212
175 self
._results
.update(results
._results
)
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
)
186 """Get the set of all passed test results."""
187 return self
._GetType
(ResultType
.PASS
)
190 """Get the set of all skipped test results."""
191 return self
._GetType
(ResultType
.SKIP
)
194 """Get the set of all failed test results."""
195 return self
._GetType
(ResultType
.FAIL
)
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()