class Stat(Sortable):
"""Generic statistic object used for storing all the statistic values."""
def __init__(self, name):
#: Human readable identifier of the object these statistics
#: belong to. `All Tests` for
#: :class:`~robot.model.totalstatistics.TotalStatistics`,
#: long name of the suite for
#: :class:`~robot.model.suitestatistics.SuiteStatistics`
#: or name of the tag for
#: :class:`~robot.model.tagstatistics.TagStatistics`
self.name = name
self.passed = 0
self.failed = 0
self.skipped = 0
self.elapsed = timedelta()
self._norm_name = normalize(name, ignore='_')
def get_attributes(self, include_label=False, include_elapsed=False,
exclude_empty=True, values_as_strings=False,
html_escape=False):
attrs = {'pass': self.passed, 'fail': self.failed, 'skip': self.skipped}
attrs.update(self._get_custom_attrs())
if include_label:
attrs['label'] = self.name
if include_elapsed:
attrs['elapsed'] = elapsed_time_to_string(self.elapsed, include_millis=False)
if exclude_empty:
attrs = dict((k, v) for k, v in attrs.items() if v not in ('', None))
if values_as_strings:
attrs = dict((k, str(v) if v is not None else '')
for k, v in attrs.items())
if html_escape:
attrs = dict((k, self._html_escape(v)) for k, v in attrs.items())
return attrs
def _get_custom_attrs(self):
return {}
def _html_escape(self, item):
return html_escape(item) if is_string(item) else item
@property
def total(self):
return self.passed + self.failed + self.skipped
def add_test(self, test):
self._update_stats(test)
self._update_elapsed(test)
def _update_stats(self, test):
if test.passed:
self.passed += 1
elif test.skipped:
self.skipped += 1
else:
self.failed += 1
def _update_elapsed(self, test):
self.elapsed += test.elapsed_time
@property
def _sort_key(self):
return self._norm_name
def __bool__(self):
return not self.failed
def visit(self, visitor):
visitor.visit_stat(self)