Robot Framework
filter.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 setter
17 
18 from .tags import TagPatterns
19 from .namepatterns import SuiteNamePatterns, TestNamePatterns
20 from .visitor import SuiteVisitor
21 
22 
24 
25  def __init__(self, preserve_direct_children=False):
26  self.preserve_direct_childrenpreserve_direct_children = preserve_direct_children
27 
28  def end_suite(self, suite):
29  if suite.parent or not self.preserve_direct_childrenpreserve_direct_children:
30  suite.suites = [s for s in suite.suites if s.test_count]
31 
32  def visit_test(self, test):
33  pass
34 
35  def visit_keyword(self, kw):
36  pass
37 
38 
39 class Filter(EmptySuiteRemover):
40 
41  def __init__(self, include_suites=None, include_tests=None,
42  include_tags=None, exclude_tags=None):
43  super().__init__()
44  self.include_suitesinclude_suitesinclude_suites = include_suites
45  self.include_testsinclude_testsinclude_tests = include_tests
46  self.include_tagsinclude_tagsinclude_tags = include_tags
47  self.exclude_tagsexclude_tagsexclude_tags = exclude_tags
48 
49  @setter
50  def include_suites(self, suites):
51  return self._patterns_or_none_patterns_or_none(suites, SuiteNamePatterns)
52 
53  @setter
54  def include_tests(self, tests):
55  return self._patterns_or_none_patterns_or_none(tests, TestNamePatterns)
56 
57  @setter
58  def include_tags(self, tags):
59  return self._patterns_or_none_patterns_or_none(tags, TagPatterns)
60 
61  @setter
62  def exclude_tags(self, tags):
63  return self._patterns_or_none_patterns_or_none(tags, TagPatterns)
64 
65  def _patterns_or_none(self, items, pattern_class):
66  if items is None or isinstance(items, pattern_class):
67  return items
68  return pattern_class(items)
69 
70  def start_suite(self, suite):
71  if not self:
72  return False
73  if hasattr(suite, 'starttime'):
74  suite.starttime = suite.endtime = None
75  if self.include_suitesinclude_suitesinclude_suites is not None:
76  return self._filter_by_suite_name_filter_by_suite_name(suite)
77  if self.include_testsinclude_testsinclude_tests is not None:
78  suite.tests = self._filter_filter(suite, self._included_by_test_name_included_by_test_name)
79  if self.include_tagsinclude_tagsinclude_tags is not None:
80  suite.tests = self._filter_filter(suite, self._included_by_tags_included_by_tags)
81  if self.exclude_tagsexclude_tagsexclude_tags is not None:
82  suite.tests = self._filter_filter(suite, self._not_excluded_by_tags_not_excluded_by_tags)
83  return bool(suite.suites)
84 
85  def _filter_by_suite_name(self, suite):
86  if self.include_suitesinclude_suitesinclude_suites.match(suite.name, suite.longname):
87  suite.visit(Filter(include_tests=self.include_testsinclude_testsinclude_tests,
88  include_tags=self.include_tagsinclude_tagsinclude_tags,
89  exclude_tags=self.exclude_tagsexclude_tagsexclude_tags))
90  return False
91  suite.tests = []
92  return True
93 
94  def _filter(self, suite, filter):
95  return [t for t in suite.tests if filter(t)]
96 
97  def _included_by_test_name(self, test):
98  return self.include_testsinclude_testsinclude_tests.match(test.name, test.longname)
99 
100  def _included_by_tags(self, test):
101  return self.include_tagsinclude_tagsinclude_tags.match(test.tags)
102 
103  def _not_excluded_by_tags(self, test):
104  return not self.exclude_tagsexclude_tagsexclude_tags.match(test.tags)
105 
106  def __bool__(self):
107  return bool(self.include_suitesinclude_suitesinclude_suites is not None or
108  self.include_testsinclude_testsinclude_tests is not None or
109  self.include_tagsinclude_tagsinclude_tags is not None or
110  self.exclude_tagsexclude_tagsexclude_tags is not None)
def visit_test(self, test)
Implements traversing through tests.
Definition: filter.py:32
def visit_keyword(self, kw)
Implements traversing through keywords.
Definition: filter.py:35
def __init__(self, preserve_direct_children=False)
Definition: filter.py:25
def end_suite(self, suite)
Called when a suite ends.
Definition: filter.py:28
def _filter_by_suite_name(self, suite)
Definition: filter.py:85
def _filter(self, suite, filter)
Definition: filter.py:94
def include_suites(self, suites)
Definition: filter.py:50
def start_suite(self, suite)
Called when a suite starts.
Definition: filter.py:70
def __init__(self, include_suites=None, include_tests=None, include_tags=None, exclude_tags=None)
Definition: filter.py:42
def include_tests(self, tests)
Definition: filter.py:54
def include_tags(self, tags)
Definition: filter.py:58
def _included_by_tags(self, test)
Definition: filter.py:100
def exclude_tags(self, tags)
Definition: filter.py:62
def _not_excluded_by_tags(self, test)
Definition: filter.py:103
def _patterns_or_none(self, items, pattern_class)
Definition: filter.py:65
def _included_by_test_name(self, test)
Definition: filter.py:97
Interface to ease traversing through a test suite structure.
Definition: visitor.py:85