How to use test_subTest_pass method in unittest-xml-reporting

Best Python code snippet using unittest-xml-reporting_python

testsuite.py

Source:testsuite.py Github

copy

Full Screen

...128 def test_non_ascii_runner_buffer_output_fail(self):129 print(u'Where is the café ?')130 self.fail(u'The café could not be found')131 class DummySubTest(unittest.TestCase):132 def test_subTest_pass(self):133 for i in range(2):134 with self.subTest(i=i):135 pass136 def test_subTest_fail(self):137 for i in range(2):138 with self.subTest(i=i):139 self.fail('this is a subtest.')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',),473 })474 self.assertNotIn(475 'name="test_subTest_mixed (i=0)"'.encode('utf8'),476 output)477 self.assertIn(478 'name="test_subTest_mixed (i=1)"'.encode('utf8'),479 output)480 @unittest.skipIf(not hasattr(unittest.TestCase, 'subTest'),481 'unittest.TestCase.subTest not present.')482 def test_unittest_subTest_pass(self):483 # Test for issue #85484 suite = unittest.TestSuite()485 suite.addTest(self.DummySubTest('test_subTest_pass'))486 self._test_xmlrunner(suite)487 @unittest.skipIf(not hasattr(unittest.TestCase, 'subTest'),488 'unittest.TestCase.subTest not present.')489 def test_unittest_subTest_with_dots(self):490 # Test for issue #85491 suite = unittest.TestSuite()492 suite.addTest(self.DummySubTest('test_subTest_with_dots'))493 outdir = BytesIO()494 self._test_xmlrunner(suite, outdir=outdir)495 xmlcontent = outdir.getvalue().decode()496 # Method name...

Full Screen

Full Screen

unittest3_backport_test.py

Source:unittest3_backport_test.py Github

copy

Full Screen

1"""Tests for absl.third_party.unittest3_backport."""2from __future__ import absolute_import3from __future__ import division4from __future__ import print_function5import unittest6from absl.testing import absltest7from absl.testing import xml_reporter8import mock9import six10class MockTestResult(xml_reporter._TextAndXMLTestResult):11 def __init__(self):12 super(MockTestResult, self).__init__(six.StringIO(), six.StringIO(),13 'description', False)14 self.subtest_success = []15 self.subtest_failure = []16 def addSubTest(self, test, subtest, err): # pylint: disable=invalid-name17 super(MockTestResult, self).addSubTest(test, subtest, err)18 if six.PY2:19 params = {}20 for param in subtest.params:21 for param_name, param_value in param.items():22 params[param_name] = param_value23 else:24 params = dict(subtest.params)25 if err is not None:26 self.addSubTestFailure(params)27 else:28 self.addSubTestSuccess(params)29 def addSubTestFailure(self, params): # pylint: disable=invalid-name30 self.subtest_failure.append(params)31 def addSubTestSuccess(self, params): # pylint: disable=invalid-name32 self.subtest_success.append(params)33class MockTestResultWithoutSubTest(xml_reporter._TextAndXMLTestResult):34 # hasattr(MockTestResultWithoutSubTest, addSubTest) return False35 def __init__(self):36 super(MockTestResultWithoutSubTest, self).__init__(six.StringIO(),37 six.StringIO(),38 'description',39 False)40 @property41 def addSubTest(self): # pylint: disable=invalid-name42 raise AttributeError43class Unittest3BackportTest(absltest.TestCase):44 def test_subtest_pass(self):45 class Foo(absltest.TestCase):46 def runTest(self):47 for i in [1, 2]:48 with self.subTest(i=i):49 for j in [2, 3]:50 with self.subTest(j=j):51 pass52 result = MockTestResult()53 Foo().run(result)54 expected_success = [{'i': 1, 'j': 2}, {'i': 1, 'j': 3}, {'i': 1},55 {'i': 2, 'j': 2}, {'i': 2, 'j': 3}, {'i': 2}]56 self.assertListEqual(result.subtest_success, expected_success)57 def test_subtest_fail(self):58 class Foo(absltest.TestCase):59 def runTest(self):60 for i in [1, 2]:61 with self.subTest(i=i):62 for j in [2, 3]:63 with self.subTest(j=j):64 if j == 2:65 self.fail('failure')66 result = MockTestResult()67 Foo().run(result)68 # The first layer subtest result is only added to the output when it is a69 # success70 expected_success = [{'i': 1, 'j': 3}, {'i': 2, 'j': 3}]71 expected_failure = [{'i': 1, 'j': 2}, {'i': 2, 'j': 2}]72 self.assertListEqual(expected_success, result.subtest_success)73 self.assertListEqual(expected_failure, result.subtest_failure)74 def test_subtest_expected_failure(self):75 class Foo(absltest.TestCase):76 @unittest.expectedFailure77 def runTest(self):78 for i in [1, 2, 3]:79 with self.subTest(i=i):80 self.assertEqual(i, 2)81 foo = Foo()82 with mock.patch.object(foo, '_addExpectedFailure',83 autospec=True) as mock_subtest_expected_failure:84 result = MockTestResult()85 foo.run(result)86 self.assertEqual(mock_subtest_expected_failure.call_count, 1)87 def test_subtest_unexpected_success(self):88 class Foo(absltest.TestCase):89 @unittest.expectedFailure90 def runTest(self):91 for i in [1, 2, 3]:92 with self.subTest(i=i):93 self.assertEqual(i, i)94 foo = Foo()95 with mock.patch.object(foo, '_addUnexpectedSuccess',96 autospec=True) as mock_subtest_unexpected_success:97 result = MockTestResult()98 foo.run(result)99 self.assertEqual(mock_subtest_unexpected_success.call_count, 1)100 def test_subtest_fail_fast(self):101 # Ensure failfast works with subtest102 class Foo(absltest.TestCase):103 def runTest(self):104 with self.subTest(i=1):105 self.fail('failure')106 with self.subTest(i=2):107 self.fail('failure')108 self.fail('failure')109 result = MockTestResult()110 result.failfast = True111 Foo().run(result)112 expected_failure = [{'i': 1}]113 self.assertListEqual(expected_failure, result.subtest_failure)114 def test_subtest_skip(self):115 # When a test case is skipped, addSubTest should not be called116 class Foo(absltest.TestCase):117 @unittest.skip('no reason')118 def runTest(self):119 for i in [1, 2, 3]:120 with self.subTest(i=i):121 self.assertEqual(i, i)122 foo = Foo()123 result = MockTestResult()124 with mock.patch.object(foo, '_addSkip', autospec=True) as mock_test_skip:125 with mock.patch.object(result, 'addSubTestSuccess',126 autospec=True) as mock_subtest_success:127 foo.run(result)128 self.assertEqual(mock_test_skip.call_count, 1)129 self.assertEqual(mock_subtest_success.call_count, 0)130 @mock.patch.object(MockTestResultWithoutSubTest, 'addFailure', autospec=True)131 def test_subtest_legacy(self, mock_test_fail):132 # When the result object does not have addSubTest method,133 # text execution stops after the first subtest failure.134 class Foo(absltest.TestCase):135 def runTest(self):136 for i in [1, 2, 3]:137 with self.subTest(i=i):138 if i == 1:139 self.fail('failure')140 for j in [2, 3]:141 with self.subTest(j=j):142 if i * j == 6:143 raise RuntimeError('raised by Foo.test')144 result = MockTestResultWithoutSubTest()145 Foo().run(result)146 self.assertEqual(mock_test_fail.call_count, 1)147if __name__ == '__main__':...

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run unittest-xml-reporting automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful