Best Python code snippet using avocado_python
test_tags.py
Source:test_tags.py
...248 self.assertEqual(test_suite,249 tags.filter_test_tags(test_suite, [], True, False))250class ParseFilterByTags(unittest.TestCase):251 def test_must(self):252 self.assertEqual(tags._parse_filter_by_tags(['foo,bar,baz']),253 [(set(['foo', 'bar', 'baz']), set([]))])254 def test_must_must_not(self):255 self.assertEqual(tags._parse_filter_by_tags(['foo,-bar,baz']),256 [(set(['foo', 'baz']), set(['bar']))])257 def test_musts_must_nots(self):258 self.assertEqual(tags._parse_filter_by_tags(['foo,bar,baz',259 '-FOO,-BAR,-BAZ']),260 [(set(['foo', 'bar', 'baz']), set([])),261 (set([]), set(['FOO', 'BAR', 'BAZ']))])262class FilterRunnable(unittest.TestCase):263 def test_no_tags(self):264 runnable = Runnable('noop', None)...
tags.py
Source:tags.py
...12# Copyright: Red Hat Inc. 2014-201913"""14Test tags utilities module15"""16def _parse_filter_by_tags(filter_by_tags):17 """18 Parses the various filter by tags in "command line" format19 The filtering of tests usually happens my means of "--filter-by-tags"20 command line options, and many can be given. This parses the contents21 of those into a list of must/must_not pairs, which can be used directly22 for comparisons when filtering.23 :param filter_by_tags: params in the format given to "-t/--filter-by-tags"24 :type filter_by_tags: list of str25 :returns: list of tuples with (set, set)26 """27 result = []28 for raw_tags in filter_by_tags:29 required_tags = raw_tags.split(',')30 must = set()31 must_not = set()32 for tag in required_tags:33 if tag.startswith('-'):34 must_not.add(tag[1:])35 else:36 must.add(tag)37 result.append((must, must_not))38 return result39def _must_split_flat_key_val(must):40 """41 Splits the flat and key:val tags apart42 :returns: the flat set tags and the key:val tags43 :rtype: tuple(set, dict)44 """45 key_val = {}46 flat = set()47 for i in must:48 if ':' in i:49 k, v = i.split(':', 1)50 key_val[k] = v51 else:52 flat.add(i)53 return flat, key_val54def _must_key_val_matches(must_key_vals, test_tags, include_empty_key):55 """56 Checks if the required key:vals are fulfilled by the test_tags57 :rtype: bool58 """59 key_val_test_tags = {}60 for k, v in test_tags.items():61 if v is None:62 continue63 key_val_test_tags[k] = v64 for k, v in must_key_vals.items():65 if k not in test_tags:66 if include_empty_key:67 continue68 else:69 return False70 if v not in key_val_test_tags.get(k, set()):71 return False72 return True73def filter_test_tags(test_suite, filter_by_tags, include_empty=False,74 include_empty_key=False):75 """76 Filter the existing (unfiltered) test suite based on tags77 The filtering mechanism is agnostic to test type. It means that78 if users request filtering by tag and the specific test type does79 not populate the test tags, it will be considered to have empty80 tags.81 :param test_suite: the unfiltered test suite82 :type test_suite: dict83 :param filter_by_tags: the list of tag sets to use as filters84 :type filter_by_tags: list of comma separated tags (['foo,bar', 'fast'])85 :param include_empty: if true tests without tags will not be filtered out86 :type include_empty: bool87 :param include_empty_key: if true tests "keys" on key:val tags will be88 included in the filtered results89 :type include_empty_key: bool90 """91 filtered = []92 must_must_nots = _parse_filter_by_tags(filter_by_tags)93 for klass, info in test_suite:94 test_tags = info.get('tags', {})95 if not test_tags:96 if include_empty:97 filtered.append((klass, info))98 continue99 for must, must_not in must_must_nots:100 if must_not.intersection(test_tags):101 continue102 must_flat, must_key_val = _must_split_flat_key_val(must)103 if must_key_val:104 if not _must_key_val_matches(must_key_val,105 test_tags,106 include_empty_key):107 continue108 if must_flat:109 if not must_flat.issubset(test_tags):110 continue111 filtered.append((klass, info))112 break113 return filtered114def filter_test_tags_runnable(runnable, filter_by_tags, include_empty=False,115 include_empty_key=False):116 """117 Filter the existing (unfiltered) test suite based on tags118 The filtering mechanism is agnostic to test type. It means that119 if users request filtering by tag and the specific test type does120 not populate the test tags, it will be considered to have empty121 tags.122 :param test_suite: the unfiltered test suite123 :type test_suite: dict124 :param filter_by_tags: the list of tag sets to use as filters125 :type filter_by_tags: list of comma separated tags (['foo,bar', 'fast'])126 :param include_empty: if true tests without tags will not be filtered out127 :type include_empty: bool128 :param include_empty_key: if true tests "keys" on key:val tags will be129 included in the filtered results130 :type include_empty_key: bool131 """132 must_must_nots = _parse_filter_by_tags(filter_by_tags)133 runnable_tags = runnable.tags or {}134 if not runnable_tags:135 if include_empty:136 return True137 for must, must_not in must_must_nots:138 if must_not.intersection(runnable_tags):139 continue140 must_flat, must_key_val = _must_split_flat_key_val(must)141 if must_key_val:142 if not _must_key_val_matches(must_key_val,143 runnable_tags,144 include_empty_key):145 continue146 if must_flat:...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!