Robot Framework Integrated Development Environment (RIDE)
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 robotide.lib.robot.utils import (Sortable, elapsed_time_to_string, html_escape,
17  is_string, normalize, py2to3, unicode)
18 
19 from .tags import TagPattern
20 
21 
22 @py2to3
23 
24 class Stat(Sortable):
25 
26  def __init__(self, name):
27  #: Human readable identifier of the object these statistics
28  #: belong to. Either `All Tests` or `Critical Tests` for
29  #: :class:`~robot.model.totalstatistics.TotalStatistics`,
30  #: long name of the suite for
31  #: :class:`~robot.model.suitestatistics.SuiteStatistics`
32  #: or name of the tag for
33  #: :class:`~robot.model.tagstatistics.TagStatistics`
34  self.namename = name
35  #: Number of passed tests.
36  self.passedpassed = 0
37  #: Number of failed tests.
38  self.failedfailed = 0
39  #: Number of milliseconds it took to execute.
40  self.elapsedelapsed = 0
41  self._norm_name_norm_name = normalize(name, ignore='_')
42 
43  def get_attributes(self, include_label=False, include_elapsed=False,
44  exclude_empty=True, values_as_strings=False,
45  html_escape=False):
46  attrs = {'pass': self.passedpassed, 'fail': self.failedfailed}
47  attrs.update(self._get_custom_attrs_get_custom_attrs())
48  if include_label:
49  attrs['label'] = self.namename
50  if include_elapsed:
51  attrs['elapsed'] = elapsed_time_to_string(self.elapsedelapsed,
52  include_millis=False)
53  if exclude_empty:
54  attrs = dict((k, v) for k, v in attrs.items() if v not in ('', None))
55  if values_as_strings:
56  attrs = dict((k, unicode(v if v is not None else ''))
57  for k, v in attrs.items())
58  if html_escape:
59  attrs = dict((k, self._html_escape_html_escape(v)) for k, v in attrs.items())
60  return attrs
61 
62  def _get_custom_attrs(self):
63  return {}
64 
65  def _html_escape(self, item):
66  return html_escape(item) if is_string(item) else item
67 
68  @property
69  total = property
70 
71  def total(self):
72  return self.passedpassed + self.failedfailed
73 
74  def add_test(self, test):
75  self._update_stats_update_stats(test)
76  self._update_elapsed_update_elapsed(test)
77 
78  def _update_stats(self, test):
79  if test.passed:
80  self.passedpassed += 1
81  else:
82  self.failedfailed += 1
83 
84  def _update_elapsed(self, test):
85  self.elapsedelapsed += test.elapsedtime
86 
87  @property
88  _sort_key = property
89 
90  def _sort_key(self):
91  return self._norm_name_norm_name
92 
93  def __nonzero__(self):
94  return not self.failedfailed
95 
96  def visit(self, visitor):
97  visitor.visit_stat(self)
98 
99 
100 
102  type = 'total'
103 
104 
105 
107  type = 'suite'
108 
109  def __init__(self, suite):
110  Stat.__init__(self, suite.longname)
111  #: Identifier of the suite, e.g. `s1-s2`.
112  self.idid = suite.id
113  #: Number of milliseconds it took to execute this suite,
114  #: including sub-suites.
115  self.elapsedelapsedelapsed = suite.elapsedtime
116  self._name_name = suite.name
117 
118  def _get_custom_attrs(self):
119  return {'id': self.idid, 'name': self._name_name}
120 
121  def _update_elapsed(self, test):
122  pass
123 
124  def add_stat(self, other):
125  self.passed += other.passed
126  self.failed += other.failed
127 
128 
129 
130 class TagStat(Stat):
131  type = 'tag'
132 
133  def __init__(self, name, doc='', links=None, critical=False,
134  non_critical=False, combined=None):
135  Stat.__init__(self, name)
136  #: Documentation of tag as a string.
137  self.docdoc = doc
138  #: List of tuples in which the first value is the link URL and
139  #: the second is the link title. An empty list by default.
140  self.linkslinks = links or []
141  #: ``True`` if tag is considered critical, ``False`` otherwise.
142  self.criticalcritical = critical
143  #: ``True`` if tag is considered non-critical, ``False`` otherwise.
144  self.non_criticalnon_critical = non_critical
145  #: Pattern as a string if the tag is combined, ``None`` otherwise.
146  self.combinedcombined = combined
147 
148  @property
149 
153  info = property
154 
155  def info(self):
156  if self.criticalcritical:
157  return 'critical'
158  if self.non_criticalnon_critical:
159  return 'non-critical'
160  if self.combinedcombined:
161  return 'combined'
162  return ''
163 
164  def _get_custom_attrs(self):
165  return {'doc': self.docdoc, 'links': self._get_links_as_string_get_links_as_string(),
166  'info': self.infoinfoinfo, 'combined': self.combinedcombined}
167 
169  return ':::'.join('%s:%s' % (title, url) for url, title in self.linkslinks)
170 
171  @property
172  _sort_key = property
173 
174  def _sort_key(self):
175  return (not self.criticalcritical,
176  not self.non_criticalnon_critical,
177  not self.combinedcombined,
178  self._norm_name_norm_name)
179 
180 
182 
183  def __init__(self, pattern, name=None, doc='', links=None):
184  TagStat.__init__(self, name or pattern, doc, links, combined=pattern)
185  self.patternpattern = TagPattern(pattern)
186 
187  def match(self, tags):
188  return self.patternpattern.match(tags)
189 
190 
192 
193  def __init__(self, tag_pattern, name=None, critical=True, doc='',
194  links=None):
195  TagStat.__init__(self, name or unicode(tag_pattern), doc, links,
196  critical=critical, non_critical=not critical)
197  self.patternpattern = tag_pattern
198 
199  def match(self, tags):
200  return self.patternpattern.match(tags)
def __init__(self, pattern, name=None, doc='', links=None)
Definition: stats.py:183
def __init__(self, tag_pattern, name=None, critical=True, doc='', links=None)
Definition: stats.py:194
Generic statistic object used for storing all the statistic values.
Definition: stats.py:24
def _update_elapsed(self, test)
Definition: stats.py:84
def visit(self, visitor)
Definition: stats.py:96
def _html_escape(self, item)
Definition: stats.py:65
def _update_stats(self, test)
Definition: stats.py:78
def get_attributes(self, include_label=False, include_elapsed=False, exclude_empty=True, values_as_strings=False, html_escape=False)
Definition: stats.py:45
Stores statistics values for a single suite.
Definition: stats.py:106
Stores statistic values for a single tag.
Definition: stats.py:130
info
Returns additional information of the tag statistics are about.
Definition: stats.py:153
def __init__(self, name, doc='', links=None, critical=False, non_critical=False, combined=None)
Definition: stats.py:134
Stores statistic values for a test run.
Definition: stats.py:101
unicode
Exceptions and return codes used internally.
Definition: errors.py:24
def TagPattern(pattern)
Definition: tags.py:100
def html_escape(text, linkify=True)
Definition: markuputils.py:40
def normalize(string, ignore=(), caseless=True, spaceless=True)
Normalizes given string according to given spec.
Definition: normalizing.py:30
def elapsed_time_to_string(elapsed, include_millis=True)
Converts elapsed time in milliseconds to format 'hh:mm:ss.mil'.
Definition: robottime.py:335