Best Python code snippet using stestr_python
test_selection.py
Source:test_selection.py
...30 ['fake_regex_with_bad_part[The-BAD-part]'],31 test_list)32 mock_exit.assert_called_once_with(5)33class TestExclusionReader(base.TestCase):34 def test_exclusion_reader(self):35 exclude_list = io.StringIO()36 for i in range(4):37 exclude_list.write('fake_regex_%s\n' % i)38 exclude_list.write('fake_regex_with_note_%s # note\n' % i)39 exclude_list.seek(0)40 with mock.patch('builtins.open',41 return_value=exclude_list):42 result = selection.exclusion_reader('fake_path')43 self.assertEqual(2 * 4, len(result))44 note_cnt = 045 # not assuming ordering, mainly just testing the type46 for r in result:47 self.assertEqual(r[2], [])48 if r[1] == 'note':49 note_cnt += 150 self.assertIn('search', dir(r[0])) # like a compiled regexp51 self.assertEqual(note_cnt, 4)52 def test_invalid_regex(self):53 exclude_list = io.StringIO()54 exclude_list.write("fake_regex_with_bad_part[The-BAD-part]")55 exclude_list.seek(0)56 with mock.patch('builtins.open',57 return_value=exclude_list):58 with mock.patch('sys.exit') as mock_exit:59 selection.exclusion_reader('fake_path')60 mock_exit.assert_called_once_with(5)61class TestConstructList(base.TestCase):62 def test_simple_re(self):63 test_lists = ['fake_test(scen)[tag,bar])', 'fake_test(scen)[egg,foo])']64 result = selection.construct_list(test_lists, regexes=['foo'])65 self.assertEqual(list(result), ['fake_test(scen)[egg,foo])'])66 def test_simple_exclusion_re(self):67 test_lists = ['fake_test(scen)[tag,bar])', 'fake_test(scen)[egg,foo])']68 result = selection.construct_list(test_lists, exclude_regex='foo')69 self.assertEqual(list(result), ['fake_test(scen)[tag,bar])'])70 def test_invalid_exclusion_re(self):71 test_lists = ['fake_test(scen)[tag,bar])', 'fake_test(scen)[egg,foo])']72 invalid_regex = "fake_regex_with_bad_part[The-BAD-part]"73 with mock.patch('sys.exit', side_effect=ImportError) as exit_mock:...
selection.py
Source:selection.py
...38 for pred in _filters:39 if pred.search(test_id):40 return True41 return list(filter(include, test_ids))42def exclusion_reader(exclude_list):43 with contextlib.closing(open(exclude_list)) as exclude_file:44 regex_comment_lst = [] # tuple of (regex_compiled, msg, skipped_lst)45 for line in exclude_file:46 raw_line = line.strip()47 split_line = raw_line.split('#')48 # Before the # is the regex49 line_regex = split_line[0].strip()50 if len(split_line) > 1:51 # After the # is a comment52 comment = ''.join(split_line[1:]).strip()53 else:54 comment = 'Skipped because of regex %s:' % line_regex55 if not line_regex:56 continue57 try:58 regex_comment_lst.append((re.compile(line_regex), comment, []))59 except re.error:60 print("Invalid regex: %s in provided exclusion list file" %61 line_regex, file=sys.stderr)62 sys.exit(5)63 return regex_comment_lst64def _get_regex_from_include_list(file_path):65 lines = []66 for line in open(file_path).read().splitlines():67 split_line = line.strip().split('#')68 # Before the # is the regex69 line_regex = split_line[0].strip()70 if line_regex:71 try:72 lines.append(re.compile(line_regex))73 except re.error:74 print("Invalid regex: %s in provided inclusion_list file" %75 line_regex, file=sys.stderr)76 sys.exit(5)77 return lines78def construct_list(test_ids, regexes=None, exclude_list=None,79 include_list=None, exclude_regex=None):80 """Filters the discovered test cases81 :param list test_ids: The set of test_ids to be filtered82 :param list regexes: A list of regex filters to apply to the test_ids. The83 output will contain any test_ids which have a re.search() match for any84 of the regexes in this list. If this is None all test_ids will be85 returned86 :param str exclude_list: The path to an exclusion_list file87 :param str include_list: The path to an inclusion_list file88 :param str exclude_regex: regex pattern to exclude tests89 :return: iterable of strings. The strings are full90 test_ids91 :rtype: set92 """93 if not regexes:94 regexes = None # handle the other false things95 safe_re = None96 if include_list:97 safe_re = _get_regex_from_include_list(include_list)98 if not regexes and safe_re:99 regexes = safe_re100 elif regexes and safe_re:101 regexes += safe_re102 if exclude_list:103 exclude_data = exclusion_reader(exclude_list)104 else:105 exclude_data = None106 if exclude_regex:107 msg = "Skipped because of regexp provided as a command line argument:"108 try:109 record = (re.compile(exclude_regex), msg, [])110 except re.error:111 print("Invalid regex: %s used for exclude_regex" %112 exclude_regex, file=sys.stderr)113 sys.exit(5)114 if exclude_data:115 exclude_data.append(record)116 else:117 exclude_data = [record]...
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!!