• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
7class ResultType(object):
8  """Class enumerating test types."""
9  PASS = 'PASS'
10  FAIL = 'FAIL'
11  CRASH = 'CRASH'
12  TIMEOUT = 'TIMEOUT'
13  UNKNOWN = 'UNKNOWN'
14
15  @staticmethod
16  def GetTypes():
17    """Get a list of all test types."""
18    return [ResultType.PASS, ResultType.FAIL, ResultType.CRASH,
19            ResultType.TIMEOUT, ResultType.UNKNOWN]
20
21
22class BaseTestResult(object):
23  """Base class for a single test result."""
24
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 SetName(self, name):
53    """Set the test name.
54
55    Because we're putting this into a set, this should only be used if moving
56    this test result into another set.
57    """
58    self._name = name
59
60  def GetName(self):
61    """Get the test name."""
62    return self._name
63
64  def GetType(self):
65    """Get the test result type."""
66    return self._test_type
67
68  def GetLog(self):
69    """Get the test log."""
70    return self._log
71
72
73class TestRunResults(object):
74  """Set of results for a test run."""
75
76  def __init__(self):
77    self._results = set()
78
79  def GetLogs(self):
80    """Get the string representation of all test logs."""
81    s = []
82    for test_type in ResultType.GetTypes():
83      if test_type != ResultType.PASS:
84        for t in sorted(self._GetType(test_type)):
85          log = t.GetLog()
86          if log:
87            s.append('[%s] %s:' % (test_type, t))
88            s.append(log)
89    return '\n'.join(s)
90
91  def GetLongForm(self):
92    """Get the long string representation of this object."""
93    s = []
94    s.append('ALL (%d tests)' % len(self._results))
95    for test_type in ResultType.GetTypes():
96      tests = sorted(self._GetType(test_type))
97      if test_type == ResultType.PASS:
98        s.append('%s (%d tests)' % (test_type, len(tests)))
99      else:
100        s.append('%s (%d tests): %s' % (test_type, len(tests), tests))
101    return '\n'.join(s)
102
103  def GetShortForm(self):
104    """Get the short string representation of this object."""
105    s = []
106    s.append('ALL: %d' % len(self._results))
107    for test_type in ResultType.GetTypes():
108      s.append('%s: %d' % (test_type, len(self._GetType(test_type))))
109    return ''.join([x.ljust(15) for x in s])
110
111  def __str__(self):
112    return self.GetLongForm()
113
114  def AddResult(self, result):
115    """Add |result| to the set.
116
117    Args:
118      result: An instance of BaseTestResult.
119    """
120    assert isinstance(result, BaseTestResult)
121    self._results.add(result)
122
123  def AddResults(self, results):
124    """Add |results| to the set.
125
126    Args:
127      results: An iterable of BaseTestResult objects.
128    """
129    for t in results:
130      self.AddResult(t)
131
132  def AddTestRunResults(self, results):
133    """Add the set of test results from |results|.
134
135    Args:
136      results: An instance of TestRunResults.
137    """
138    assert isinstance(results, TestRunResults)
139    # pylint: disable=W0212
140    self._results.update(results._results)
141
142  def GetAll(self):
143    """Get the set of all test results."""
144    return self._results.copy()
145
146  def _GetType(self, test_type):
147    """Get the set of test results with the given test type."""
148    return set(t for t in self._results if t.GetType() == test_type)
149
150  def GetPass(self):
151    """Get the set of all passed test results."""
152    return self._GetType(ResultType.PASS)
153
154  def GetFail(self):
155    """Get the set of all failed test results."""
156    return self._GetType(ResultType.FAIL)
157
158  def GetCrash(self):
159    """Get the set of all crashed test results."""
160    return self._GetType(ResultType.CRASH)
161
162  def GetTimeout(self):
163    """Get the set of all timed out test results."""
164    return self._GetType(ResultType.TIMEOUT)
165
166  def GetUnknown(self):
167    """Get the set of all unknown test results."""
168    return self._GetType(ResultType.UNKNOWN)
169
170  def GetNotPass(self):
171    """Get the set of all non-passed test results."""
172    return self.GetAll() - self.GetPass()
173
174  def DidRunPass(self):
175    """Return whether the test run was successful."""
176    return not self.GetNotPass()
177