Best Python code snippet using green
test_junitxml.py
Source:test_junitxml.py
...46 # replacing _test_start if needed.47 self.result.startTestRun() # the time is now.48 # Lose an hour (peeks inside, a little naughty but not very).49 self.result.time(self.result._run_start - datetime.timedelta(0, 3600))50 self.result.stopTestRun()51 self.assertEqual("""<testsuite errors="0" failures="0" name="" tests="0" time="0.000">52</testsuite>53""", self.get_output())54 def test_startTestRun_no_output(self):55 # startTestRun doesn't output anything, because JUnit wants an up-front56 # summary.57 self.result.startTestRun()58 self.assertEqual('', self.get_output())59 def test_stopTestRun_outputs(self):60 # When stopTestRun is called, everything is output.61 self.result.startTestRun()62 self.result.stopTestRun()63 self.assertEqual("""<testsuite errors="0" failures="0" name="" tests="0" time="0.000">64</testsuite>65""", self.get_output())66 def test_test_count(self):67 class Passes(unittest.TestCase):68 def test_me(self):69 pass70 self.result.startTestRun()71 Passes("test_me").run(self.result)72 Passes("test_me").run(self.result)73 self.result.stopTestRun()74 # When tests are run, the number of tests is counted.75 output = self.get_output()76 self.assertTrue('tests="2"' in output)77 def test_test_id_with_parameter(self):78 class Passes(unittest.TestCase):79 def id(self):80 return unittest.TestCase.id(self) + '(version_1.6)'81 def test_me(self):82 pass83 self.result.startTestRun()84 Passes("test_me").run(self.result)85 self.result.stopTestRun()86 output = self.get_output()87 self.assertTrue('Passes" name="test_me(version_1.6)"' in output)88 def test_erroring_test(self):89 class Errors(unittest.TestCase):90 def test_me(self):91 1/092 self.result.startTestRun()93 Errors("test_me").run(self.result)94 self.result.stopTestRun()95 self.assertEqual("""<testsuite errors="1" failures="0" name="" tests="1" time="0.000">96<testcase classname="junitxml.tests.test_junitxml.Errors" name="test_me" time="0.000">97<error type="ZeroDivisionError">error</error>98</testcase>99</testsuite>100""", self.get_output())101 def test_failing_test(self):102 class Fails(unittest.TestCase):103 def test_me(self):104 self.fail()105 self.result.startTestRun()106 Fails("test_me").run(self.result)107 self.result.stopTestRun()108 self.assertEqual("""<testsuite errors="0" failures="1" name="" tests="1" time="0.000">109<testcase classname="junitxml.tests.test_junitxml.Fails" name="test_me" time="0.000">110<failure type="AssertionError">failure</failure>111</testcase>112</testsuite>113""", self.get_output())114 def test_successful_test(self):115 class Passes(unittest.TestCase):116 def test_me(self):117 pass118 self.result.startTestRun()119 Passes("test_me").run(self.result)120 self.result.stopTestRun()121 self.assertEqual("""<testsuite errors="0" failures="0" name="" tests="1" time="0.000">122<testcase classname="junitxml.tests.test_junitxml.Passes" name="test_me" time="0.000"/>123</testsuite>124""", self.get_output())125 def test_skip_test(self):126 class Skips(unittest.TestCase):127 def test_me(self):128 self.skipTest("yo")129 self.result.startTestRun()130 test = Skips("test_me")131 self.run_test_or_simulate(test, 'skipTest', self.result.addSkip, 'yo')132 self.result.stopTestRun()133 output = self.get_output()134 expected = """<testsuite errors="0" failures="0" name="" tests="1" time="0.000">135<testcase classname="junitxml.tests.test_junitxml.Skips" name="test_me" time="0.000">136<skip>yo</skip>137</testcase>138</testsuite>139"""140 self.assertEqual(expected, output)141 def test_unexpected_success_test(self):142 class Succeeds(unittest.TestCase):143 def test_me(self):144 pass145 try:146 test_me = unittest.expectedFailure(test_me)147 except AttributeError:148 pass # Older python - just let the test pass149 self.result.startTestRun()150 Succeeds("test_me").run(self.result)151 self.result.stopTestRun()152 output = self.get_output()153 expected = """<testsuite errors="0" failures="1" name="" tests="1" time="0.000">154<testcase classname="junitxml.tests.test_junitxml.Succeeds" name="test_me" time="0.000">155<failure type="unittest.case._UnexpectedSuccess"/>156</testcase>157</testsuite>158"""159 expected_old = """<testsuite errors="0" failures="0" name="" tests="1" time="0.000">160<testcase classname="junitxml.tests.test_junitxml.Succeeds" name="test_me" time="0.000"/>161</testsuite>162"""163 if output != expected_old:164 self.assertEqual(expected, output)165 def test_expected_failure_test(self):166 expected_failure_support = [True]167 class ExpectedFail(unittest.TestCase):168 def test_me(self):169 self.fail("fail")170 try:171 test_me = unittest.expectedFailure(test_me)172 except AttributeError:173 # Older python - just let the test fail174 expected_failure_support[0] = False175 self.result.startTestRun()176 ExpectedFail("test_me").run(self.result)177 self.result.stopTestRun()178 output = self.get_output()179 expected = """<testsuite errors="0" failures="0" name="" tests="1" time="0.000">180<testcase classname="junitxml.tests.test_junitxml.ExpectedFail" name="test_me" time="0.000"/>181</testsuite>182"""183 expected_old = """<testsuite errors="0" failures="1" name="" tests="1" time="0.000">184<testcase classname="junitxml.tests.test_junitxml.ExpectedFail" name="test_me" time="0.000">185<failure type="AssertionError">failure</failure>186</testcase>187</testsuite>188"""189 if expected_failure_support[0]:190 self.assertEqual(expected, output)191 else:192 self.assertEqual(expected_old, output)193class TestWellFormedXml(unittest.TestCase):194 """XML created should always be well formed even with odd test cases"""195 def _run_and_parse_test(self, case):196 output = StringIO()197 result = junitxml.JUnitXmlResult(output)198 result.startTestRun()199 case.run(result)200 result.stopTestRun()201 return xml.dom.minidom.parseString(output.getvalue())202 def test_failure_with_amp(self):203 """Check the failure element content is escaped"""204 class FailWithAmp(unittest.TestCase):205 def runTest(self):206 self.fail("& should be escaped as &")207 doc = self._run_and_parse_test(FailWithAmp())208 self.assertTrue(209 doc.getElementsByTagName("failure")[0].firstChild.nodeValue210 .endswith("AssertionError: & should be escaped as &\n"))211 def test_quotes_in_test_case_id(self):212 """Check that quotes in an attribute are escaped"""213 class QuoteId(unittest.TestCase):214 def id(self):...
runner.py
Source:runner.py
...47 _handle_error(e)48 finally:49 stopTestRun = getattr(result, 'stopTestRun', None)50 if stopTestRun is not None:51 stopTestRun()52 def _continue_testing(deferred, send_value=None, throw_value=None):53 try:54 if throw_value:55 condition = deferred.throw(throw_value)56 else:57 condition = deferred.send(send_value)58 if callable(condition):59 defer(0, _wait_condition, deferred, condition)60 elif isinstance(condition, dict) and "condition" in condition and \61 callable(condition["condition"]):62 period = condition.get("period", DEFAULT_CONDITION_POLL_TIME)63 defer(period, _wait_condition, deferred, **condition)64 elif isinstance(condition, int):65 defer(condition, _continue_testing, deferred)66 elif condition == AWAIT_WORKER:67 run_on_worker(68 partial(defer, 0, _continue_testing, deferred)69 )70 else:71 defer(0, _continue_testing, deferred)72 except StopIteration:73 _stop_testing()74 self.finished = True75 except Exception as e:76 _handle_error(e)77 def _wait_condition(78 deferred, condition,79 period=DEFAULT_CONDITION_POLL_TIME,80 timeout=DEFAULT_CONDITION_TIMEOUT,81 start_time=None82 ):83 if start_time is None:84 start_time = time.time()85 try:86 send_value = condition()87 except Exception as e:88 _continue_testing(deferred, throw_value=e)89 return90 if send_value:91 _continue_testing(deferred, send_value=send_value)92 elif (time.time() - start_time) * 1000 >= timeout:93 error = TimeoutError(94 'Condition not fulfilled within {:.2f} seconds'95 .format(timeout / 1000)96 )97 _continue_testing(deferred, throw_value=error)98 else:99 defer(period, _wait_condition, deferred, condition, period, timeout, start_time)100 def _handle_error(e):101 stopTestRun = getattr(result, 'stopTestRun', None)102 if stopTestRun is not None:103 stopTestRun()104 self.finished = True105 raise e106 def _stop_testing():107 with warnings.catch_warnings():108 stopTestRun = getattr(result, 'stopTestRun', None)109 if stopTestRun is not None:110 stopTestRun()111 stopTime = time.perf_counter()112 timeTaken = stopTime - startTime113 result.printErrors()114 if hasattr(result, 'separator2'):115 self.stream.writeln(result.separator2)116 run = result.testsRun117 self.stream.writeln("Ran %d test%s in %.3fs" %118 (run, run != 1 and "s" or "", timeTaken))119 self.stream.writeln()120 expectedFails = unexpectedSuccesses = skipped = 0121 try:122 results = map(len, (result.expectedFailures,123 result.unexpectedSuccesses,124 result.skipped))...
support.py
Source:support.py
...40 super().startTestRun()41 def stopTest(self, test):42 self._events.append('stopTest')43 super().stopTest(test)44 def stopTestRun(self):45 self._events.append('stopTestRun')46 super().stopTestRun()47 def addFailure(self, *args):48 self._events.append('addFailure')49 super().addFailure(*args)50 def addSuccess(self, *args):51 self._events.append('addSuccess')52 super().addSuccess(*args)53 def addError(self, *args):54 self._events.append('addError')55 super().addError(*args)56 def addSkip(self, *args):57 self._events.append('addSkip')58 super().addSkip(*args)59 def addExpectedFailure(self, *args):60 self._events.append('addExpectedFailure')...
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!!