Best Python code snippet using SeleniumBase
test.py
Source:test.py
1# Class encapsulating a unit test.2#3# * @author Ben Gardner October 20094# * @author Guy Maurel October 20155# * @author Matthew Woehlke June 20186#7import filecmp8import os9import re10import subprocess11import sys12import errno13from .ansicolor import printc14from .config import (config, test_dir, FAIL_ATTRS, PASS_ATTRS,15 MISMATCH_ATTRS, UNSTABLE_ATTRS)16from .failure import (ExecutionFailure, MismatchFailure, MissingFailure,17 TestDeclarationParseError, UnexpectedlyPassingFailure,18 UnstableFailure)19# =============================================================================20class SourceTest(object):21 # -------------------------------------------------------------------------22 def __init__(self):23 self.test_result_dir = 'results'24 self.diff_text = 'MISMATCH'25 self.diff_attrs = MISMATCH_ATTRS26 self.diff_exception = MismatchFailure27 # -------------------------------------------------------------------------28 def _check_attr(self, name):29 if not hasattr(self, name) or getattr(self, name) is None:30 raise AttributeError(31 'Test is missing required attribute {!r}'.format(name))32 # -------------------------------------------------------------------------33 def _make_abs(self, name, base):34 path = getattr(self, name)35 if not os.path.isabs(path):36 setattr(self, name, os.path.join(test_dir, base, path))37 # -------------------------------------------------------------------------38 def _diff(self, expected, actual):39 sys.stdout.flush()40 cmd = [config.git_exe, 'diff', '--no-index', expected, actual]41 subprocess.call(cmd)42 # -------------------------------------------------------------------------43 def build(self, test_input, test_lang, test_config, test_expected):44 self.test_name = os.path.basename(test_input)45 self.test_lang = test_lang46 self.test_input = test_input47 self.test_config = test_config48 self.test_expected = test_expected49 self.test_xfail = False50 # -------------------------------------------------------------------------51 def _check(self):52 self._check_attr('test_name')53 self._check_attr('test_lang')54 self._check_attr('test_input')55 self._check_attr('test_config')56 self._check_attr('test_expected')57 self._check_attr('test_xfail')58 # -------------------------------------------------------------------------59 def run(self, args):60 self._check()61 _expected = self.test_expected62 _result = os.path.join(args.result_dir, self.test_result_dir,63 os.path.basename(os.path.dirname(_expected)),64 os.path.basename(_expected))65 if args.verbose:66 print(self.test_name)67 print(' Language : {}'.format(self.test_lang))68 print(' Input : {}'.format(self.test_input))69 print(' Config : {}'.format(self.test_config))70 print(' Expected : {}'.format(_expected))71 print(' Result : {}'.format(_result))72 print(' XFail : {}'.format(self.test_xfail))73 if not os.path.exists(os.path.dirname(_result)):74 try:75 os.makedirs(os.path.dirname(_result))76 except OSError as e:77 if e.errno != errno.EEXIST:78 raise79 cmd = [80 config.uncrustify_exe,81 '-q',82 '-l', self.test_lang,83 '-c', self.test_config,84 '-f', self.test_input,85 '-o', _result86 ]87 if args.debug:88 cmd += [89 '-LA',90 '-p', _result + '.unc'91 ]92 else:93 cmd += ['-LA']94 if args.show_commands:95 printc('RUN: ', repr(cmd))96 try:97 output = subprocess.check_output(cmd, stderr=subprocess.STDOUT)98 except subprocess.CalledProcessError as exc:99 output = exc.output100 if not self.test_xfail:101 print(output.rstrip())102 msg = '{} (Uncrustify error code {})'103 msg = msg.format(self.test_name, exc.returncode)104 printc('FAILED: ', msg, **FAIL_ATTRS)105 raise ExecutionFailure(exc)106 elif args.xdiff:107 print(output.rstrip())108 finally:109 if args.debug:110 with open(_result + '.log', 'wt') as f:111 f.write(output)112 try:113 has_diff = not filecmp.cmp(_expected, _result)114 if has_diff and not self.test_xfail:115 if args.diff:116 self._diff(_expected, _result)117 printc('{}: '.format(self.diff_text),118 self.test_name, **self.diff_attrs)119 raise self.diff_exception(_expected, _result)120 if not has_diff and self.test_xfail:121 raise UnexpectedlyPassingFailure(_expected, _result)122 if has_diff and self.test_xfail:123 if args.xdiff:124 self._diff(_expected, _result)125 if not args.show_all:126 printc('XFAILED: ', self.test_name, **PASS_ATTRS)127 except OSError as exc:128 printc('MISSING: ', self.test_name, **self.diff_attrs)129 raise MissingFailure(exc, _expected)130# =============================================================================131class FormatTest(SourceTest):132 pass_config = ['test_config', 'test_rerun_config']133 pass_input = ['test_input', 'test_expected']134 pass_expected = ['test_expected', 'test_rerun_expected']135 re_test_declaration = re.compile(r'^(?P<num>\d+)(?P<mark>[~!]*)\s+'136 r'(?P<config>\S+)\s+(?P<input>\S+)'137 r'(?:\s+(?P<lang>\S+))?$')138 # -------------------------------------------------------------------------139 def _build_pass(self, i):140 p = SourceTest()141 p.test_name = self.test_name142 p.test_lang = self.test_lang143 p.test_config = getattr(self, self.pass_config[i])144 p.test_input = getattr(self, self.pass_input[i])145 p.test_expected = getattr(self, self.pass_expected[i])146 p.test_xfail = self.test_xfail147 if i == 1 and not os.path.exists(p.test_expected):148 p.test_expected = getattr(self, self.pass_expected[0])149 return p150 # -------------------------------------------------------------------------151 def _build_passes(self):152 self._check()153 self._check_attr('test_rerun_config')154 self._check_attr('test_rerun_expected')155 self._make_abs('test_input', 'input')156 self._make_abs('test_config', 'config')157 self._make_abs('test_expected', 'expected')158 self._make_abs('test_rerun_config', 'config')159 self._make_abs('test_rerun_expected', 'expected')160 self.test_passes = [161 self._build_pass(0),162 self._build_pass(1)]163 self.test_passes[1].test_name = self.test_name + ' (re-run)'164 self.test_passes[1].test_result_dir = 'results_2'165 self.test_passes[1].diff_text = 'UNSTABLE'166 self.test_passes[1].diff_attrs = UNSTABLE_ATTRS167 self.test_passes[1].diff_exception = UnstableFailure168 # -------------------------------------------------------------------------169 def build_from_declaration(self, decl, group, line_number):170 match = self.re_test_declaration.match(decl)171 if not match:172 raise TestDeclarationParseError(group, line_number)173 num = match.group('num')174 is_rerun = ('!' in match.group('mark'))175 is_xfail = ('~' in match.group('mark'))176 self.test_xfail = is_xfail177 self.test_config = match.group('config')178 self.test_input = match.group('input')179 test_dir = os.path.dirname(self.test_input)180 test_filename = os.path.basename(self.test_input)181 if match.group('lang'):182 self.test_lang = match.group('lang')183 else:184 self.test_lang = test_dir185 self.test_expected = os.path.join(186 test_dir, '{}-{}'.format(num, test_filename))187 def rerun_file(name):188 parts = name.split('.')189 return '.'.join(parts[:-1] + ['rerun'] + parts[-1:])190 if is_rerun:191 self.test_rerun_config = rerun_file(self.test_config)192 self.test_rerun_expected = rerun_file(self.test_expected)193 else:194 self.test_rerun_config = self.test_config195 self.test_rerun_expected = self.test_expected196 self.test_name = '{}:{}'.format(group, num)197 self._build_passes()198 # -------------------------------------------------------------------------199 def build_from_args(self, args):200 self.test_name = args.name201 self.test_lang = args.lang202 self.test_input = args.input203 self.test_config = args.config204 self.test_expected = args.expected205 self.test_rerun_config = args.rerun_config or args.config206 self.test_rerun_expected = args.rerun_expected or args.expected207 self.test_xfail = args.xfail208 self._build_passes()209 # -------------------------------------------------------------------------210 def print_as_ctest(self, out_file=sys.stdout):211 self._check()212 def to_cmake_path(obj):213 if type(obj) is dict:214 return {k: to_cmake_path(v) for k, v in obj.items()}215 if type(obj) is str:216 return obj.replace(os.sep, '/')217 return obj218 runner = os.path.join(test_dir, 'run_test.py')219 out_file.write(220 ('add_test({test_name}\n' +221 ' "{python_exe}" -S "{test_runner}" "{test_name}"\n' +222 ' --executable "{uncrustify_exe}"\n' +223 ' --lang "{test_lang}"\n' +224 ' --input "{test_input}"\n' +225 ' --config "{test_config}"\n' +226 ' --expected "{test_expected}"\n' +227 ' --rerun-config "{test_rerun_config}"\n' +228 ' --rerun-expected "{test_rerun_expected}"\n' +229 ' -d --git "{git_exe}"\n' +230 '{xfail}' +231 ')\n').format(232 test_runner=to_cmake_path(runner),233 python_exe=to_cmake_path(config.python_exe),234 uncrustify_exe=to_cmake_path(config.uncrustify_exe),235 git_exe=to_cmake_path(config.git_exe),236 xfail=(' --xfail\n' if self.test_xfail else ''),237 **to_cmake_path(self.__dict__)))238 out_file.write(239 ('set_tests_properties({}\n' +240 ' PROPERTIES LABELS "{}"\n)\n').format(241 self.test_name, self.test_name.split(':')[0]))242 # -------------------------------------------------------------------------243 def run(self, args):244 for p in self.test_passes:...
test_xfail.py
Source:test_xfail.py
1import pytest2def test_strjoin(): # PASSED3 s1 = "Python,Pytest and Automation"4 l1 = ["Python,Pytest", "and", "Automation"]5 l2 = ["Python", "Pytest and Automation"]6 assert " ".join(l1) == s17@pytest.mark.xfail(raises=IndexError) # if wrong error expected - test will be failed, if correct - XFAIL8def test_str01(): # XFAIL9 letters = "abc"10 assert letters[100]11def old_func():12 return False13@pytest.mark.xfail(old_func() == False, reason="Known Issue, function does not working fine (should return True)")14def test_str02(): # XFAIL15 assert old_func() == True16@pytest.mark.xfail(old_func() == False, reason="Known Issue, function does not working fine (should return True)")17def test_str03(): # XPASS (supposed to be failed, but passed)18 assert old_func() == False19# Pytest_topics/test_xfail.py::test_strjoin PASSED [ 68%]20# Pytest_topics/test_xfail.py::test_str01 XFAIL [ 72%]21# Pytest_topics/test_xfail.py::test_str02 XFAIL (Known Issue, function does not working fine (should return True)) [ 76%]...
xfail_demo.py
Source:xfail_demo.py
...4Time : 2022/6/13 22:195DESC: 6"""7import pytest8def test_xfail():9 print("å¼å§æµè¯")10 pytest.xfail(reason="该åè½æªå®æ")11 print("æµè¯æ¥éª¤")12 assert True13@pytest.mark.xfail14def test_a():15 print("test_xfail")16 assert 1 == 217xfail = pytest.mark.xfail18@xfail(reason="bug 001")19def test_b():...
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!!