Best Python code snippet using unittest-xml-reporting_python
testsuite.py
Source:testsuite.py
...547 minidom.parseString(output)548 except Exception as e: # pragma: no cover549 # note: we could remove the try/except, but it's more crude.550 self.fail(e)551 def test_xmlrunner_outsuffix(self):552 self.runner_kwargs['outsuffix'] = '.somesuffix'553 suite = unittest.TestSuite()554 suite.addTest(self.DummyTest('test_pass'))555 self._test_xmlrunner(suite)556 xmlfile = glob(os.path.join(self.outdir, '*xml'))[0]557 assert xmlfile.endswith('.somesuffix.xml')558 def test_xmlrunner_nosuffix(self):559 self.runner_kwargs['outsuffix'] = ''560 suite = unittest.TestSuite()561 suite.addTest(self.DummyTest('test_pass'))562 self._test_xmlrunner(suite)563 xmlfile = glob(os.path.join(self.outdir, '*xml'))[0]564 xmlfile = os.path.basename(xmlfile)565 assert xmlfile.endswith('DummyTest.xml')566 def test_junitxml_properties(self):567 suite = unittest.TestSuite()568 suite.addTest(self.DummyTest('test_pass'))569 suite.properties = dict(key='value')570 self._test_xmlrunner(suite)571 def test_junitxml_xsd_validation_order(self):572 suite = unittest.TestSuite()573 suite.addTest(self.DummyTest('test_fail'))574 suite.addTest(self.DummyTest('test_pass'))575 suite.addTest(self.DummyTest('test_output_stdout_and_stderr'))576 suite.properties = dict(key='value')577 outdir = BytesIO()578 runner = xmlrunner.XMLTestRunner(579 stream=self.stream, output=outdir, verbosity=self.verbosity,580 **self.runner_kwargs)581 runner.run(suite)582 outdir.seek(0)583 output = outdir.read()584 # poor man's schema validation; see issue #90585 i_properties = output.index('<properties>'.encode('utf8'))586 i_system_out = output.index('<system-out>'.encode('utf8'))587 i_system_err = output.index('<system-err>'.encode('utf8'))588 i_testcase = output.index('<testcase'.encode('utf8'))589 self.assertTrue(i_properties < i_testcase <590 i_system_out < i_system_err)591 # XSD validation - for good measure.592 validate_junit_report('14c6e39c38408b9ed6280361484a13c6f5becca7', output)593 def test_junitxml_xsd_validation_empty_properties(self):594 suite = unittest.TestSuite()595 suite.addTest(self.DummyTest('test_fail'))596 suite.addTest(self.DummyTest('test_pass'))597 suite.properties = None598 outdir = BytesIO()599 runner = xmlrunner.XMLTestRunner(600 stream=self.stream, output=outdir, verbosity=self.verbosity,601 **self.runner_kwargs)602 runner.run(suite)603 outdir.seek(0)604 output = outdir.read()605 self.assertNotIn('<properties>'.encode('utf8'), output)606 validate_junit_report('14c6e39c38408b9ed6280361484a13c6f5becca7', output)607 @unittest.skipIf(hasattr(sys, 'pypy_version_info'),608 'skip - PyPy + lxml seems to be hanging')609 def test_xunit_plugin_transform(self):610 suite = unittest.TestSuite()611 suite.addTest(self.DummyTest('test_fail'))612 suite.addTest(self.DummyTest('test_pass'))613 suite.properties = None614 outdir = BytesIO()615 runner = xmlrunner.XMLTestRunner(616 stream=self.stream, output=outdir, verbosity=self.verbosity,617 **self.runner_kwargs)618 runner.run(suite)619 outdir.seek(0)620 output = outdir.read()621 validate_junit_report('14c6e39c38408b9ed6280361484a13c6f5becca7', output)622 with self.assertRaises(etree.DocumentInvalid):623 validate_junit_report('ae25da5089d4f94ac6c4669bf736e4d416cc4665', output)624 from xmlrunner.extra.xunit_plugin import transform625 transformed = transform(output)626 validate_junit_report('14c6e39c38408b9ed6280361484a13c6f5becca7', transformed)627 validate_junit_report('ae25da5089d4f94ac6c4669bf736e4d416cc4665', transformed)628 self.assertIn('test_pass'.encode('utf8'), transformed)629 self.assertIn('test_fail'.encode('utf8'), transformed)630 def test_xmlrunner_elapsed_times(self):631 self.runner_kwargs['elapsed_times'] = False632 suite = unittest.TestSuite()633 suite.addTest(self.DummyTest('test_pass'))634 self._test_xmlrunner(suite)635 def test_xmlrunner_resultclass(self):636 class Result(_XMLTestResult):637 pass638 suite = unittest.TestSuite()639 suite.addTest(self.DummyTest('test_pass'))640 self.runner_kwargs['resultclass'] = Result641 self._test_xmlrunner(suite)642 def test_xmlrunner_stream(self):643 stream = self.stream644 output = BytesIO()645 runner = xmlrunner.XMLTestRunner(646 stream=stream, output=output, verbosity=self.verbosity,647 **self.runner_kwargs)648 suite = unittest.TestSuite()649 suite.addTest(self.DummyTest('test_pass'))650 runner.run(suite)651 def test_xmlrunner_stream_empty_testsuite(self):652 stream = self.stream653 output = BytesIO()654 runner = xmlrunner.XMLTestRunner(655 stream=stream, output=output, verbosity=self.verbosity,656 **self.runner_kwargs)657 suite = unittest.TestSuite()658 runner.run(suite)659 def test_xmlrunner_output_subdir(self):660 stream = self.stream661 output = os.path.join(self.outdir, 'subdir')662 runner = xmlrunner.XMLTestRunner(663 stream=stream, output=output, verbosity=self.verbosity,664 **self.runner_kwargs)665 suite = unittest.TestSuite()666 suite.addTest(self.DummyTest('test_pass'))667 runner.run(suite)668 def test_xmlrunner_patched_stdout(self):669 old_stdout, old_stderr = sys.stdout, sys.stderr670 try:671 sys.stdout, sys.stderr = StringIO(), StringIO()672 suite = unittest.TestSuite()673 suite.addTest(self.DummyTest('test_pass'))674 suite.properties = dict(key='value')675 self._test_xmlrunner(suite)676 finally:677 sys.stdout, sys.stderr = old_stdout, old_stderr678 def test_opaque_decorator(self):679 suite = unittest.TestSuite()680 suite.addTest(self.DecoratedUnitTest('test_pass'))681 self._test_xmlrunner(suite)682 testsuite_output = self.stream.getvalue()683 self.assertNotIn('IOError:', testsuite_output)684 def test_xmlrunner_error_in_call(self):685 suite = unittest.TestSuite()686 suite.addTest(self.DummyErrorInCallTest('test_pass'))687 self._test_xmlrunner(suite)688 testsuite_output = self.stream.getvalue()689 self.assertIn('Exception: Massive fail', testsuite_output)690 @unittest.skipIf(not hasattr(sys, 'getrefcount'),691 'skip - PyPy does not have sys.getrefcount.')692 @unittest.skipIf((3, 0) <= sys.version_info < (3, 4),693 'skip - test not garbage collected. '694 'https://bugs.python.org/issue11798.')695 def test_xmlrunner_hold_traceback(self):696 suite = unittest.TestSuite()697 suite.addTest(self.DummyRefCountTest('test_fail'))698 countBeforeTest = sys.getrefcount(self.DummyRefCountTest.dummy)699 runner = self._test_xmlrunner(suite)700 countAfterTest = sys.getrefcount(self.DummyRefCountTest.dummy)701 self.assertEqual(countBeforeTest, countAfterTest)702 class StderrXMLTestRunner(xmlrunner.XMLTestRunner):703 """704 XMLTestRunner that outputs to sys.stderr that might be replaced705 Though XMLTestRunner defaults to use sys.stderr as stream,706 it cannot be replaced e.g. by replaced by capture_stdout_stderr(),707 as it's already resolved.708 This class resolved sys.stderr lazily and outputs to replaced sys.stderr.709 """710 def __init__(self, **kwargs):711 super(XMLTestRunnerTestCase.StderrXMLTestRunner, self).__init__(712 stream=sys.stderr,713 **kwargs714 )715 def test_test_program_succeed_with_buffer(self):716 with capture_stdout_stderr() as r:717 unittest.TestProgram(718 module=self.__class__.__module__,719 testRunner=self.StderrXMLTestRunner,720 argv=[721 sys.argv[0],722 '-b',723 'XMLTestRunnerTestCase.DummyTest.test_runner_buffer_output_pass',724 ],725 exit=False,726 )727 self.assertNotIn('should not be printed', r[0].getvalue())728 self.assertNotIn('should not be printed', r[1].getvalue())729 def test_test_program_succeed_wo_buffer(self):730 with capture_stdout_stderr() as r:731 unittest.TestProgram(732 module=self.__class__.__module__,733 testRunner=self.StderrXMLTestRunner,734 argv=[735 sys.argv[0],736 'XMLTestRunnerTestCase.DummyTest.test_runner_buffer_output_pass',737 ],738 exit=False,739 )740 self.assertIn('should not be printed', r[0].getvalue())741 self.assertNotIn('should not be printed', r[1].getvalue())742 def test_test_program_fail_with_buffer(self):743 with capture_stdout_stderr() as r:744 unittest.TestProgram(745 module=self.__class__.__module__,746 testRunner=self.StderrXMLTestRunner,747 argv=[748 sys.argv[0],749 '-b',750 'XMLTestRunnerTestCase.DummyTest.test_runner_buffer_output_fail',751 ],752 exit=False,753 )754 self.assertNotIn('should be printed', r[0].getvalue())755 self.assertIn('should be printed', r[1].getvalue())756 def test_test_program_fail_wo_buffer(self):757 with capture_stdout_stderr() as r:758 unittest.TestProgram(759 module=self.__class__.__module__,760 testRunner=self.StderrXMLTestRunner,761 argv=[762 sys.argv[0],763 'XMLTestRunnerTestCase.DummyTest.test_runner_buffer_output_fail',764 ],765 exit=False,766 )767 self.assertIn('should be printed', r[0].getvalue())768 self.assertNotIn('should be printed', r[1].getvalue())769 def test_partialmethod(self):770 from functools import partialmethod771 def test_partialmethod(test):772 pass773 class TestWithPartialmethod(unittest.TestCase):774 pass775 setattr(776 TestWithPartialmethod,777 'test_partialmethod',778 partialmethod(test_partialmethod),779 )780 suite = unittest.TestSuite()781 suite.addTest(TestWithPartialmethod('test_partialmethod'))782 self._test_xmlrunner(suite)783class DuplicateWriterTestCase(unittest.TestCase):784 def setUp(self):785 fd, self.file = mkstemp()786 self.fh = os.fdopen(fd, 'w')787 self.buffer = StringIO()788 self.writer = _DuplicateWriter(self.fh, self.buffer)789 def tearDown(self):790 self.buffer.close()791 self.fh.close()792 os.unlink(self.file)793 def getFirstContent(self):794 with open(self.file, 'r') as f:795 return f.read()796 def getSecondContent(self):797 return self.buffer.getvalue()798 def test_flush(self):799 self.writer.write('foobarbaz')800 self.writer.flush()801 self.assertEqual(self.getFirstContent(), self.getSecondContent())802 def test_writable(self):803 self.assertTrue(self.writer.writable())804 def test_writelines(self):805 self.writer.writelines([806 'foo\n',807 'bar\n',808 'baz\n',809 ])810 self.writer.flush()811 self.assertEqual(self.getFirstContent(), self.getSecondContent())812 def test_write(self):813 # try long buffer (1M)814 buffer = 'x' * (1024 * 1024)815 wrote = self.writer.write(buffer)816 self.writer.flush()817 self.assertEqual(self.getFirstContent(), self.getSecondContent())818 self.assertEqual(wrote, len(self.getSecondContent()))819class XMLProgramTestCase(unittest.TestCase):820 @mock.patch('sys.argv', ['xmlrunner', '-o', 'flaf'])821 @mock.patch('xmlrunner.runner.XMLTestRunner')822 @mock.patch('sys.exit')823 def test_xmlrunner_output(self, exiter, testrunner):824 xmlrunner.runner.XMLTestProgram()825 kwargs = dict(826 buffer=mock.ANY,827 failfast=mock.ANY,828 verbosity=mock.ANY,829 warnings=mock.ANY,830 output='flaf',831 )832 if sys.version_info[:2] > (3, 4):833 kwargs.update(tb_locals=mock.ANY)834 testrunner.assert_called_once_with(**kwargs)835 exiter.assert_called_once_with(False)836 @mock.patch('sys.argv', ['xmlrunner', '--output-file', 'test.xml'])837 @mock.patch('xmlrunner.runner.open')838 @mock.patch('xmlrunner.runner.XMLTestRunner')839 @mock.patch('sys.exit')840 def test_xmlrunner_output_file(self, exiter, testrunner, opener):841 xmlrunner.runner.XMLTestProgram()842 opener.assert_called_once_with('test.xml', 'wb')843 open_file = opener()844 open_file.close.assert_called_with()845 kwargs = dict(846 buffer=mock.ANY,847 failfast=mock.ANY,848 verbosity=mock.ANY,849 warnings=mock.ANY,850 output=open_file,851 )852 if sys.version_info[:2] > (3, 4):853 kwargs.update(tb_locals=mock.ANY)854 testrunner.assert_called_once_with(**kwargs)855 exiter.assert_called_once_with(False)856 @mock.patch('sys.argv', ['xmlrunner', '--outsuffix', ''])857 @mock.patch('xmlrunner.runner.open')858 @mock.patch('xmlrunner.runner.XMLTestRunner')859 @mock.patch('sys.exit')860 def test_xmlrunner_outsuffix(self, exiter, testrunner, opener):861 xmlrunner.runner.XMLTestProgram()862 kwargs = dict(863 buffer=mock.ANY,864 failfast=mock.ANY,865 verbosity=mock.ANY,866 warnings=mock.ANY,867 outsuffix='',868 )869 if sys.version_info[:2] > (3, 4):870 kwargs.update(tb_locals=mock.ANY)871 testrunner.assert_called_once_with(**kwargs)872 exiter.assert_called_once_with(False)873class ResolveFilenameTestCase(unittest.TestCase):874 @mock.patch('os.path.relpath')...
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!!