Robot Framework
stats.py
Go to the documentation of this file.
1 # Copyright 2008-2015 Nokia Networks
2 # Copyright 2016- Robot Framework Foundation
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 
16 from robot.utils import (Sortable, elapsed_time_to_string, html_escape,
17  is_string, normalize)
18 
19 from .tags import TagPattern
20 
21 
22 
23 class Stat(Sortable):
24 
25  def __init__(self, name):
26  #: Human readable identifier of the object these statistics
27  #: belong to. `All Tests` for
28  #: :class:`~robot.model.totalstatistics.TotalStatistics`,
29  #: long name of the suite for
30  #: :class:`~robot.model.suitestatistics.SuiteStatistics`
31  #: or name of the tag for
32  #: :class:`~robot.model.tagstatistics.TagStatistics`
33  self.namename = name
34  #: Number of passed tests.
35  self.passedpassed = 0
36  #: Number of failed tests.
37  self.failedfailed = 0
38  #: Number of skipped tests.
39  self.skippedskipped = 0
40  #: Number of milliseconds it took to execute.
41  self.elapsedelapsed = 0
42  self._norm_name_norm_name = normalize(name, ignore='_')
43 
44  def get_attributes(self, include_label=False, include_elapsed=False,
45  exclude_empty=True, values_as_strings=False,
46  html_escape=False):
47  attrs = {'pass': self.passedpassed, 'fail': self.failedfailed, 'skip': self.skippedskipped}
48  attrs.update(self._get_custom_attrs_get_custom_attrs())
49  if include_label:
50  attrs['label'] = self.namename
51  if include_elapsed:
52  attrs['elapsed'] = elapsed_time_to_string(self.elapsedelapsed,
53  include_millis=False)
54  if exclude_empty:
55  attrs = dict((k, v) for k, v in attrs.items() if v not in ('', None))
56  if values_as_strings:
57  attrs = dict((k, str(v) if v is not None else '')
58  for k, v in attrs.items())
59  if html_escape:
60  attrs = dict((k, self._html_escape_html_escape(v)) for k, v in attrs.items())
61  return attrs
62 
63  def _get_custom_attrs(self):
64  return {}
65 
66  def _html_escape(self, item):
67  return html_escape(item) if is_string(item) else item
68 
69  @property
70  total = property
71 
72  def total(self):
73  return self.passedpassed + self.failedfailed + self.skippedskipped
74 
75  def add_test(self, test):
76  self._update_stats_update_stats(test)
77  self._update_elapsed_update_elapsed(test)
78 
79  def _update_stats(self, test):
80  if test.passed:
81  self.passedpassed += 1
82  elif test.skipped:
83  self.skippedskipped += 1
84  else:
85  self.failedfailed += 1
86 
87  def _update_elapsed(self, test):
88  self.elapsedelapsed += test.elapsedtime
89 
90  @property
91  _sort_key = property
92 
93  def _sort_key(self):
94  return self._norm_name_norm_name
95 
96  def __bool__(self):
97  return not self.failedfailed
98 
99  def visit(self, visitor):
100  visitor.visit_stat(self)
101 
102 
103 
105  type = 'total'
106 
107 
108 
110  type = 'suite'
111 
112  def __init__(self, suite):
113  Stat.__init__(self, suite.longname)
114  #: Identifier of the suite, e.g. `s1-s2`.
115  self.idid = suite.id
116  #: Number of milliseconds it took to execute this suite,
117  #: including sub-suites.
118  self.elapsedelapsedelapsed = suite.elapsedtime
119  self._name_name = suite.name
120 
121  def _get_custom_attrs(self):
122  return {'id': self.idid, 'name': self._name_name}
123 
124  def _update_elapsed(self, test):
125  pass
126 
127  def add_stat(self, other):
128  self.passed += other.passed
129  self.failed += other.failed
130  self.skipped += other.skipped
131 
132 
133 
134 class TagStat(Stat):
135  type = 'tag'
136 
137  def __init__(self, name, doc='', links=None, combined=None):
138  Stat.__init__(self, name)
139  #: Documentation of tag as a string.
140  self.docdoc = doc
141  #: List of tuples in which the first value is the link URL and
142  #: the second is the link title. An empty list by default.
143  self.linkslinks = links or []
144  #: Pattern as a string if the tag is combined, ``None`` otherwise.
145  self.combinedcombined = combined
146 
147  @property
148 
151  info = property
152 
153  def info(self):
154  if self.combinedcombined:
155  return 'combined'
156  return ''
157 
158  def _get_custom_attrs(self):
159  return {'doc': self.docdoc, 'links': self._get_links_as_string_get_links_as_string(),
160  'info': self.infoinfoinfo, 'combined': self.combinedcombined}
161 
163  return ':::'.join('%s:%s' % (title, url) for url, title in self.linkslinks)
164 
165  @property
166  _sort_key = property
167 
168  def _sort_key(self):
169  return (not self.combinedcombined, self._norm_name_norm_name)
170 
171 
173 
174  def __init__(self, pattern, name=None, doc='', links=None):
175  TagStat.__init__(self, name or pattern, doc, links, combined=pattern)
176  self.patternpattern = TagPattern(pattern)
177 
178  def match(self, tags):
179  return self.patternpattern.match(tags)
def __init__(self, pattern, name=None, doc='', links=None)
Definition: stats.py:174
Generic statistic object used for storing all the statistic values.
Definition: stats.py:23
def add_test(self, test)
Definition: stats.py:75
def _html_escape(self, item)
Definition: stats.py:66
def __init__(self, name)
Definition: stats.py:25
def visit(self, visitor)
Definition: stats.py:99
def _update_stats(self, test)
Definition: stats.py:79
def __bool__(self)
Definition: stats.py:96
def get_attributes(self, include_label=False, include_elapsed=False, exclude_empty=True, values_as_strings=False, html_escape=False)
Definition: stats.py:46
def _get_custom_attrs(self)
Definition: stats.py:63
def _update_elapsed(self, test)
Definition: stats.py:87
Stores statistics values for a single suite.
Definition: stats.py:109
def _get_custom_attrs(self)
Definition: stats.py:121
def _update_elapsed(self, test)
Definition: stats.py:124
def __init__(self, suite)
Definition: stats.py:112
def add_stat(self, other)
Definition: stats.py:127
Stores statistic values for a single tag.
Definition: stats.py:134
def __init__(self, name, doc='', links=None, combined=None)
Definition: stats.py:137
def _get_links_as_string(self)
Definition: stats.py:162
def _get_custom_attrs(self)
Definition: stats.py:158
info
Returns additional information of the tag statistics are about.
Definition: stats.py:151
Stores statistic values for a test run.
Definition: stats.py:104
def TagPattern(pattern)
Definition: tags.py:116
def html_escape(text, linkify=True)
Definition: markuputils.py:44
def normalize(string, ignore=(), caseless=True, spaceless=True)
Normalizes given string according to given spec.
Definition: normalizing.py:27
def elapsed_time_to_string(elapsed, include_millis=True)
Converts elapsed time in milliseconds to format 'hh:mm:ss.mil'.
Definition: robottime.py:374