Best Python code snippet using robotframework
test_run_and_rebot.py
Source:test_run_and_rebot.py
...20OUTPUT_PATH = join(TEMP, 'output.xml')21REPORT_PATH = join(TEMP, 'report.html')22LOG_PATH = join(TEMP, 'log.html')23LOG = 'Log: %s' % LOG_PATH24def run_without_outputs(*args, **kwargs):25 kwargs.update(output='NONE', log='NoNe', report=None)26 return run(*args, **kwargs)27class StreamWithOnlyWriteAndFlush(object):28 def __init__(self):29 self._buffer = []30 def write(self, msg):31 self._buffer.append(msg)32 def flush(self):33 pass34 def getvalue(self):35 return ''.join(self._buffer)36class TestRun(RunningTestCase):37 data = join(ROOT, 'atest', 'testdata', 'misc', 'pass_and_fail.robot')38 warn = join(ROOT, 'atest', 'testdata', 'misc', 'warnings_and_errors.robot')39 nonex = join(TEMP, 'non-existing-file-this-is.robot')40 remove_files = [LOG_PATH, REPORT_PATH, OUTPUT_PATH]41 def test_run_once(self):42 assert_equal(run(self.data, outputdir=TEMP, report='none'), 1)43 self._assert_outputs([('Pass And Fail', 2), (LOG, 1), ('Report:', 0)])44 assert exists(LOG_PATH)45 def test_run_multiple_times(self):46 assert_equal(run_without_outputs(self.data, critical='nomatch'), 0)47 assert_equal(run_without_outputs(self.data, name='New Name'), 1)48 self._assert_outputs([('Pass And Fail', 2), ('New Name', 2), (LOG, 0)])49 def test_run_fail(self):50 assert_equal(run(self.data, outputdir=TEMP), 1)51 self._assert_outputs(stdout=[('Pass And Fail', 2), (LOG, 1)])52 def test_run_error(self):53 assert_equal(run(self.nonex), 252)54 self._assert_outputs(stderr=[('[ ERROR ]', 1), (self.nonex, 1),55 ('--help', 1)])56 def test_custom_stdout(self):57 stdout = StringIO()58 assert_equal(run_without_outputs(self.data, stdout=stdout), 1)59 self._assert_output(stdout, [('Pass And Fail', 2), ('Output:', 1),60 ('Log:', 0), ('Report:', 0)])61 self._assert_outputs()62 def test_custom_stderr(self):63 stderr = StringIO()64 assert_equal(run_without_outputs(self.warn, stderr=stderr), 0)65 self._assert_output(stderr, [('[ WARN ]', 4), ('[ ERROR ]', 2)])66 self._assert_outputs([('Warnings And Errors', 2), ('Output:', 1),67 ('Log:', 0), ('Report:', 0)])68 def test_custom_stdout_and_stderr_with_minimal_implementation(self):69 output = StreamWithOnlyWriteAndFlush()70 assert_equal(run_without_outputs(self.warn, stdout=output, stderr=output), 0)71 self._assert_output(output, [('[ WARN ]', 4), ('[ ERROR ]', 2),72 ('Warnings And Errors', 3), ('Output:', 1),73 ('Log:', 0), ('Report:', 0)])74 self._assert_outputs()75 def test_multi_options_as_single_string(self):76 assert_equal(run_without_outputs(self.data, exclude='fail'), 0)77 self._assert_outputs([('FAIL', 0)])78 def test_listener_gets_notification_about_log_report_and_output(self):79 listener = join(ROOT, 'utest', 'resources', 'Listener.py')80 assert_equal(run(self.data, output=OUTPUT_PATH, report=REPORT_PATH,81 log=LOG_PATH, listener=listener), 1)82 self._assert_outputs(stdout=[('[output {0}]'.format(OUTPUT_PATH), 1),83 ('[report {0}]'.format(REPORT_PATH), 1),84 ('[log {0}]'.format(LOG_PATH), 1),85 ('[listener close]', 1)])86 def test_pass_listener_as_instance(self):87 assert_equal(run_without_outputs(self.data, listener=Listener(1)), 1)88 self._assert_outputs([("[from listener 1]", 1)])89 def test_pass_listener_as_string(self):90 module_file = join(ROOT, 'utest', 'resources', 'Listener.py')91 assert_equal(run_without_outputs(self.data, listener=module_file+":1"), 1)92 self._assert_outputs([("[from listener 1]", 1)])93 def test_pass_listener_as_list(self):94 module_file = join(ROOT, 'utest', 'resources', 'Listener.py')95 assert_equal(run_without_outputs(self.data, listener=[module_file+":1", Listener(2)]), 1)96 self._assert_outputs([("[from listener 1]", 1), ("[from listener 2]", 1)])97 def test_pre_run_modifier_as_instance(self):98 class Modifier(SuiteVisitor):99 def start_suite(self, suite):100 suite.tests = [t for t in suite.tests if t.tags.match('pass')]101 assert_equal(run_without_outputs(self.data, prerunmodifier=Modifier()), 0)102 self._assert_outputs([('Pass ', 1), ('Fail :: FAIL', 0)])103 def test_pre_rebot_modifier_as_instance(self):104 class Modifier(SuiteVisitor):105 def __init__(self):106 self.tests = []107 def visit_test(self, test):108 self.tests.append(test.name)109 modifier = Modifier()110 assert_equal(run(self.data, outputdir=TEMP, log=LOG_PATH, prerebotmodifier=modifier), 1)111 assert_equal(modifier.tests, ['Pass', 'Fail'])112 self._assert_outputs([('Pass ', 1), ('Fail :: FAIL', 1)])113 def test_invalid_modifier(self):114 assert_equal(run_without_outputs(self.data, prerunmodifier=42), 1)115 self._assert_outputs([('Pass ', 1), ('Fail :: FAIL', 1)],116 [("[ ERROR ] Executing model modifier 'integer' "117 "failed: AttributeError: ", 1)])118class TestRebot(RunningTestCase):119 data = join(ROOT, 'atest', 'testdata', 'rebot', 'created_normal.xml')120 nonex = join(TEMP, 'non-existing-file-this-is.xml')121 remove_files = [LOG_PATH, REPORT_PATH]122 def test_run_once(self):123 assert_equal(rebot(self.data, outputdir=TEMP, report='NONE'), 1)124 self._assert_outputs([(LOG, 1), ('Report:', 0)])125 assert exists(LOG_PATH)126 def test_run_multiple_times(self):127 assert_equal(rebot(self.data, outputdir=TEMP, critical='nomatch'), 0)128 assert_equal(rebot(self.data, outputdir=TEMP, name='New Name'), 1)129 self._assert_outputs([(LOG, 2)])130 def test_run_fails(self):131 assert_equal(rebot(self.nonex), 252)132 assert_equal(rebot(self.data, outputdir=TEMP), 1)133 self._assert_outputs(stdout=[(LOG, 1)],134 stderr=[('[ ERROR ]', 1), (self.nonex, (1, 2)),135 ('--help', 1)])136 def test_custom_stdout(self):137 stdout = StringIO()138 assert_equal(rebot(self.data, report='None', stdout=stdout,139 outputdir=TEMP), 1)140 self._assert_output(stdout, [('Log:', 1), ('Report:', 0)])141 self._assert_outputs()142 def test_custom_stdout_and_stderr_with_minimal_implementation(self):143 output = StreamWithOnlyWriteAndFlush()144 assert_equal(rebot(self.data, log='NONE', report='NONE', stdout=output,145 stderr=output), 252)146 assert_equal(rebot(self.data, report='NONE', stdout=output,147 stderr=output, outputdir=TEMP), 1)148 self._assert_output(output, [('[ ERROR ] No outputs created', 1),149 ('--help', 1), ('Log:', 1), ('Report:', 0)])150 self._assert_outputs()151 def test_pre_rebot_modifier_as_instance(self):152 class Modifier(SuiteVisitor):153 def __init__(self):154 self.tests = []155 def visit_test(self, test):156 self.tests.append(test.name)157 test.status = 'FAIL'158 modifier = Modifier()159 assert_equal(rebot(self.data, outputdir=TEMP,160 prerebotmodifier=modifier), 3)161 assert_equal(modifier.tests, ['Test 1.1', 'Test 1.2', 'Test 2.1'])162class TestStateBetweenTestRuns(RunningTestCase):163 data = join(ROOT, 'atest', 'testdata', 'misc', 'normal.robot')164 def test_importer_caches_are_cleared_between_runs(self):165 self._run(self.data)166 lib = self._import_library()167 res = self._import_resource()168 self._run(self.data)169 assert_true(lib is not self._import_library())170 assert_true(res is not self._import_resource())171 def _run(self, data, **config):172 return run_without_outputs(data, outputdir=TEMP, **config)173 def _import_library(self):174 return namespace.IMPORTER.import_library('BuiltIn', None, None, None)175 def _import_resource(self):176 resource = join(ROOT, 'atest', 'testdata', 'core', 'resources.robot')177 return namespace.IMPORTER.import_resource(resource)178 def test_clear_namespace_between_runs(self):179 data = join(ROOT, 'atest', 'testdata', 'variables', 'commandline_variables.robot')180 rc = self._run(data, test=['NormalText'], variable=['NormalText:Hello'])181 assert_equal(rc, 0)182 rc = self._run(data, test=['NormalText'])183 assert_equal(rc, 1)184 def test_reset_logging_conf(self):185 assert_equal(logging.getLogger().handlers, [])186 assert_equal(logging.raiseExceptions, 1)187 self._run(join(ROOT, 'atest', 'testdata', 'misc', 'normal.robot'))188 assert_equal(logging.getLogger().handlers, [])189 assert_equal(logging.raiseExceptions, 1)190 def test_listener_unregistration(self):191 listener = join(ROOT, 'utest', 'resources', 'Listener.py')192 assert_equal(run_without_outputs(self.data, listener=listener+':1'), 0)193 self._assert_outputs([("[from listener 1]", 1), ("[listener close]", 1)])194 self._clear_outputs()195 assert_equal(run_without_outputs(self.data), 0)196 self._assert_outputs([("[from listener 1]", 0), ("[listener close]", 0)])197class TestTimestampOutputs(RunningTestCase):198 output = join(TEMP, 'output-ts-*.xml')199 report = join(TEMP, 'report-ts-*.html')200 log = join(TEMP, 'log-ts-*.html')201 remove_files = [output, report, log]202 def test_different_timestamps_when_run_multiple_times(self):203 self.run_tests()204 output1, = self.find_results(self.output, 1)205 report1, = self.find_results(self.report, 1)206 log1, = self.find_results(self.log, 1)207 self.wait_until_next_second()208 self.run_tests()209 output21, output22 = self.find_results(self.output, 2)210 report21, report22 = self.find_results(self.report, 2)211 log21, log22 = self.find_results(self.log, 2)212 assert_equal(output1, output21)213 assert_equal(report1, report21)214 assert_equal(log1, log21)215 def run_tests(self):216 data = join(ROOT, 'atest', 'testdata', 'misc', 'pass_and_fail.robot')217 assert_equal(run(data, timestampoutputs=True, outputdir=TEMP,218 output='output-ts.xml', report='report-ts.html',219 log='log-ts'), 1)220 def find_results(self, pattern, expected):221 matches = glob.glob(pattern)222 assert_equal(len(matches), expected)223 return sorted(matches)224 def wait_until_next_second(self):225 start = time.localtime()[5]226 while time.localtime()[5] == start:227 time.sleep(0.01)228class TestSignalHandlers(unittest.TestCase):229 data = join(ROOT, 'atest', 'testdata', 'misc', 'pass_and_fail.robot')230 def test_original_signal_handlers_are_restored(self):231 orig_sigint = signal.getsignal(signal.SIGINT)232 orig_sigterm = signal.getsignal(signal.SIGTERM)233 my_sigterm = lambda signum, frame: None234 signal.signal(signal.SIGTERM, my_sigterm)235 try:236 run_without_outputs(self.data, stdout=StringIO())237 assert_equal(signal.getsignal(signal.SIGINT), orig_sigint)238 assert_equal(signal.getsignal(signal.SIGTERM), my_sigterm)239 finally:240 signal.signal(signal.SIGINT, orig_sigint)241 signal.signal(signal.SIGTERM, orig_sigterm)242 def test_dont_register_signal_handlers_then_run_on_thread(self):243 stream = StringIO()244 thread = threading.Thread(target=run_without_outputs, args=(self.data,),245 kwargs=dict(stdout=stream, stderr=stream))246 thread.start()247 thread.join()248 output = stream.getvalue()249 assert_true('ERROR' not in output.upper(), 'Errors:\n%s' % output)250class TestRelativeImportsFromPythonpath(RunningTestCase):...
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!!