Best Python code snippet using unittest-xml-reporting_python
testsuite.py
Source:testsuite.py
...140 def test_subTest_error(self):141 for i in range(2):142 with self.subTest(i=i):143 raise Exception('this is a subtest')144 def test_subTest_mixed(self):145 for i in range(2):146 with self.subTest(i=i):147 self.assertLess(i, 1, msg='this is a subtest.')148 def test_subTest_with_dots(self):149 for i in range(2):150 with self.subTest(module='hello.world.subTest{}'.format(i)):151 self.fail('this is a subtest.')152 class DecoratedUnitTest(unittest.TestCase):153 @some_decorator154 def test_pass(self):155 pass156 class DummyErrorInCallTest(unittest.TestCase):157 def __call__(self, result):158 try:159 raise Exception('Massive fail')160 except Exception:161 result.addError(self, sys.exc_info())162 return163 def test_pass(self):164 # it is expected not to be called.165 pass # pragma: no cover166 class DummyRefCountTest(unittest.TestCase):167 class dummy(object):168 pass169 def test_fail(self):170 inst = self.dummy()171 self.assertTrue(False)172 def setUp(self):173 self.stream = StringIO()174 self.outdir = mkdtemp()175 self.verbosity = 0176 self.runner_kwargs = {}177 self.addCleanup(rmtree, self.outdir)178 def _test_xmlrunner(self, suite, runner=None, outdir=None):179 if outdir is None:180 outdir = self.outdir181 stream = self.stream182 verbosity = self.verbosity183 runner_kwargs = self.runner_kwargs184 if runner is None:185 runner = xmlrunner.XMLTestRunner(186 stream=stream, output=outdir, verbosity=verbosity,187 **runner_kwargs)188 if isinstance(outdir, BytesIO):189 self.assertFalse(outdir.getvalue())190 else:191 self.assertEqual(0, len(glob(os.path.join(outdir, '*xml'))))192 runner.run(suite)193 if isinstance(outdir, BytesIO):194 self.assertTrue(outdir.getvalue())195 else:196 self.assertEqual(1, len(glob(os.path.join(outdir, '*xml'))))197 return runner198 def test_basic_unittest_constructs(self):199 suite = unittest.TestSuite()200 suite.addTest(self.DummyTest('test_pass'))201 suite.addTest(self.DummyTest('test_skip'))202 suite.addTest(self.DummyTest('test_fail'))203 suite.addTest(self.DummyTest('test_expected_failure'))204 suite.addTest(self.DummyTest('test_unexpected_success'))205 suite.addTest(self.DummyTest('test_error'))206 self._test_xmlrunner(suite)207 def test_classnames(self):208 suite = unittest.TestSuite()209 suite.addTest(self.DummyTest('test_pass'))210 suite.addTest(self.DummySubTest('test_subTest_pass'))211 outdir = BytesIO()212 stream = StringIO()213 runner = xmlrunner.XMLTestRunner(214 stream=stream, output=outdir, verbosity=0)215 runner.run(suite)216 outdir.seek(0)217 output = outdir.read()218 output = _strip_xml(output, {219 '//testsuite': (),220 '//testcase': ('classname', 'name'),221 '//failure': ('message',),222 })223 self.assertRegexpMatches(224 output,225 r'classname="tests\.testsuite\.(XMLTestRunnerTestCase\.)?'226 r'DummyTest" name="test_pass"'.encode('utf8'),227 )228 self.assertRegexpMatches(229 output,230 r'classname="tests\.testsuite\.(XMLTestRunnerTestCase\.)?'231 r'DummySubTest" name="test_subTest_pass"'.encode('utf8'),232 )233 def test_expected_failure(self):234 suite = unittest.TestSuite()235 suite.addTest(self.DummyTest('test_expected_failure'))236 outdir = BytesIO()237 self._test_xmlrunner(suite, outdir=outdir)238 self.assertNotIn(b'<failure', outdir.getvalue())239 self.assertNotIn(b'<error', outdir.getvalue())240 self.assertIn(b'<skip', outdir.getvalue())241 def test_unexpected_success(self):242 suite = unittest.TestSuite()243 suite.addTest(self.DummyTest('test_unexpected_success'))244 outdir = BytesIO()245 self._test_xmlrunner(suite, outdir=outdir)246 self.assertNotIn(b'<failure', outdir.getvalue())247 self.assertIn(b'<error', outdir.getvalue())248 self.assertNotIn(b'<skip', outdir.getvalue())249 def test_xmlrunner_non_ascii(self):250 suite = unittest.TestSuite()251 suite.addTest(self.DummyTest('test_non_ascii_skip'))252 suite.addTest(self.DummyTest('test_non_ascii_error'))253 outdir = BytesIO()254 runner = xmlrunner.XMLTestRunner(255 stream=self.stream, output=outdir, verbosity=self.verbosity,256 **self.runner_kwargs)257 runner.run(suite)258 outdir.seek(0)259 output = outdir.read()260 self.assertIn(261 u'message="demonstrating non-ascii skipping: éçà "'.encode('utf8'),262 output)263 def test_xmlrunner_safe_xml_encoding_name(self):264 suite = unittest.TestSuite()265 suite.addTest(self.DummyTest('test_pass'))266 outdir = BytesIO()267 runner = xmlrunner.XMLTestRunner(268 stream=self.stream, output=outdir, verbosity=self.verbosity,269 **self.runner_kwargs)270 runner.run(suite)271 outdir.seek(0)272 output = outdir.read()273 firstline = output.splitlines()[0]274 # test for issue #74275 self.assertIn('encoding="UTF-8"'.encode('utf8'), firstline)276 def test_xmlrunner_check_for_valid_xml_streamout(self):277 """278 This test checks if the xml document is valid if there are more than279 one testsuite and the output of the report is a single stream.280 """281 class DummyTestA(unittest.TestCase):282 def test_pass(self):283 pass284 class DummyTestB(unittest.TestCase):285 def test_pass(self):286 pass287 suite = unittest.TestSuite()288 suite.addTest(unittest.TestLoader().loadTestsFromTestCase(DummyTestA))289 suite.addTest(unittest.TestLoader().loadTestsFromTestCase(DummyTestB))290 outdir = BytesIO()291 runner = xmlrunner.XMLTestRunner(292 stream=self.stream, output=outdir, verbosity=self.verbosity,293 **self.runner_kwargs)294 runner.run(suite)295 outdir.seek(0)296 output = outdir.read()297 # Finally check if we have a valid XML document or not.298 try:299 minidom.parseString(output)300 except Exception as e: # pragma: no cover301 # note: we could remove the try/except, but it's more crude.302 self.fail(e)303 def test_xmlrunner_unsafe_unicode(self):304 suite = unittest.TestSuite()305 suite.addTest(self.DummyTest('test_unsafe_unicode'))306 outdir = BytesIO()307 runner = xmlrunner.XMLTestRunner(308 stream=self.stream, output=outdir, verbosity=self.verbosity,309 **self.runner_kwargs)310 runner.run(suite)311 outdir.seek(0)312 output = outdir.read()313 self.assertIn(u"<![CDATA[ABCD\n]]>".encode('utf8'),314 output)315 def test_xmlrunner_non_ascii_failures(self):316 self._xmlrunner_non_ascii_failures()317 def test_xmlrunner_non_ascii_failures_buffered_output(self):318 self._xmlrunner_non_ascii_failures(buffer=True)319 def _xmlrunner_non_ascii_failures(self, buffer=False):320 suite = unittest.TestSuite()321 suite.addTest(self.DummyTest(322 'test_non_ascii_runner_buffer_output_fail'))323 outdir = BytesIO()324 runner = xmlrunner.XMLTestRunner(325 stream=self.stream, output=outdir, verbosity=self.verbosity,326 buffer=buffer, **self.runner_kwargs)327 # allow output non-ascii letters to stdout328 orig_stdout = sys.stdout329 sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')330 try:331 runner.run(suite)332 finally:333 # Not to be closed when TextIOWrapper is disposed.334 sys.stdout.detach()335 sys.stdout = orig_stdout336 outdir.seek(0)337 output = outdir.read()338 self.assertIn(339 u'Where is the café ?'.encode('utf8'),340 output)341 self.assertIn(342 u'The café could not be found'.encode('utf8'),343 output)344 @unittest.expectedFailure345 def test_xmlrunner_buffer_output_pass(self):346 suite = unittest.TestSuite()347 suite.addTest(self.DummyTest('test_runner_buffer_output_pass'))348 self._test_xmlrunner(suite)349 testsuite_output = self.stream.getvalue()350 # Since we are always buffering stdout/stderr351 # it is currently troublesome to print anything at all352 # and be consistent with --buffer option (issue #59)353 self.assertIn('should not be printed', testsuite_output)354 # this will be fixed when using the composite approach355 # that was under development in the rewrite branch.356 def test_xmlrunner_buffer_output_fail(self):357 suite = unittest.TestSuite()358 suite.addTest(self.DummyTest('test_runner_buffer_output_fail'))359 # --buffer option360 self.runner_kwargs['buffer'] = True361 self._test_xmlrunner(suite)362 testsuite_output = self.stream.getvalue()363 self.assertIn('should be printed', testsuite_output)364 def test_xmlrunner_output_without_buffer(self):365 suite = unittest.TestSuite()366 suite.addTest(self.DummyTest('test_output'))367 with capture_stdout_stderr() as r:368 self._test_xmlrunner(suite)369 output_from_test = r[0].getvalue()370 self.assertIn('test message', output_from_test)371 def test_xmlrunner_output_with_buffer(self):372 suite = unittest.TestSuite()373 suite.addTest(self.DummyTest('test_output'))374 # --buffer option375 self.runner_kwargs['buffer'] = True376 with capture_stdout_stderr() as r:377 self._test_xmlrunner(suite)378 output_from_test = r[0].getvalue()379 self.assertNotIn('test message', output_from_test)380 def test_xmlrunner_stdout_stderr_recovered_without_buffer(self):381 orig_stdout = sys.stdout382 orig_stderr = sys.stderr383 suite = unittest.TestSuite()384 suite.addTest(self.DummyTest('test_pass'))385 self._test_xmlrunner(suite)386 self.assertIs(orig_stdout, sys.stdout)387 self.assertIs(orig_stderr, sys.stderr)388 def test_xmlrunner_stdout_stderr_recovered_with_buffer(self):389 orig_stdout = sys.stdout390 orig_stderr = sys.stderr391 suite = unittest.TestSuite()392 suite.addTest(self.DummyTest('test_pass'))393 # --buffer option394 self.runner_kwargs['buffer'] = True395 self._test_xmlrunner(suite)396 self.assertIs(orig_stdout, sys.stdout)397 self.assertIs(orig_stderr, sys.stderr)398 suite = unittest.TestSuite()399 suite.addTest(self.DummyTest('test_pass'))400 @unittest.skipIf(not hasattr(unittest.TestCase, 'subTest'),401 'unittest.TestCase.subTest not present.')402 def test_unittest_subTest_fail(self):403 # test for issue #77404 outdir = BytesIO()405 runner = xmlrunner.XMLTestRunner(406 stream=self.stream, output=outdir, verbosity=self.verbosity,407 **self.runner_kwargs)408 suite = unittest.TestSuite()409 suite.addTest(self.DummySubTest('test_subTest_fail'))410 runner.run(suite)411 outdir.seek(0)412 output = outdir.read()413 output = _strip_xml(output, {414 '//testsuite': (),415 '//testcase': ('classname', 'name'),416 '//failure': ('message',),417 })418 self.assertRegexpMatches(419 output,420 br'<testcase classname="tests\.testsuite\.'421 br'(XMLTestRunnerTestCase\.)?DummySubTest" '422 br'name="test_subTest_fail \(i=0\)"')423 self.assertRegexpMatches(424 output,425 br'<testcase classname="tests\.testsuite\.'426 br'(XMLTestRunnerTestCase\.)?DummySubTest" '427 br'name="test_subTest_fail \(i=1\)"')428 @unittest.skipIf(not hasattr(unittest.TestCase, 'subTest'),429 'unittest.TestCase.subTest not present.')430 def test_unittest_subTest_error(self):431 # test for issue #155432 outdir = BytesIO()433 runner = xmlrunner.XMLTestRunner(434 stream=self.stream, output=outdir, verbosity=self.verbosity,435 **self.runner_kwargs)436 suite = unittest.TestSuite()437 suite.addTest(self.DummySubTest('test_subTest_error'))438 runner.run(suite)439 outdir.seek(0)440 output = outdir.read()441 output = _strip_xml(output, {442 '//testsuite': (),443 '//testcase': ('classname', 'name'),444 '//failure': ('message',),445 })446 self.assertRegexpMatches(447 output,448 br'<testcase classname="tests\.testsuite\.'449 br'(XMLTestRunnerTestCase\.)?DummySubTest" '450 br'name="test_subTest_error \(i=0\)"')451 self.assertRegexpMatches(452 output,453 br'<testcase classname="tests\.testsuite\.'454 br'(XMLTestRunnerTestCase\.)?DummySubTest" '455 br'name="test_subTest_error \(i=1\)"')456 @unittest.skipIf(not hasattr(unittest.TestCase, 'subTest'),457 'unittest.TestCase.subTest not present.')458 def test_unittest_subTest_mixed(self):459 # test for issue #155460 outdir = BytesIO()461 runner = xmlrunner.XMLTestRunner(462 stream=self.stream, output=outdir, verbosity=self.verbosity,463 **self.runner_kwargs)464 suite = unittest.TestSuite()465 suite.addTest(self.DummySubTest('test_subTest_mixed'))466 runner.run(suite)467 outdir.seek(0)468 output = outdir.read()469 output = _strip_xml(output, {470 '//testsuite': (),471 '//testcase': ('classname', 'name'),472 '//failure': ('message',),...
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!!