How to use test_subTest_fail method in unittest-xml-reporting

Best Python code snippet using unittest-xml-reporting_python

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

check_runner.py

Source:check_runner.py Github

copy

Full Screen

1import mpi_test_runner as mtr2import unittest as ut3class Simple(mtr.MPITestCase):4 def test_pass(self):5 pass6 def test_fail(self):7 self.fail()8 def test_fail_0(self):9 from mpi4py import MPI10 if MPI.COMM_WORLD.rank == 0:11 self.fail()12 def test_fail_1(self):13 from mpi4py import MPI14 if MPI.COMM_WORLD.rank == 1:15 self.fail()16 def test_fail_nonzero(self):17 from mpi4py import MPI18 if MPI.COMM_WORLD.rank != 0:19 self.fail()20 def test_error(self):21 raise ValueError22 def test_error_nonzero(self):23 from mpi4py import MPI24 if MPI.COMM_WORLD.rank != 0:25 raise ValueError26 def test_valid_error(self):27 with self.assertRaises(ValueError):28 raise ValueError29 def test_mixed(self):30 from mpi4py import MPI31 if MPI.COMM_WORLD.rank < 2:32 self.fail()33 else:34 raise ValueError35 def test_subtest_fail(self):36 with self.subTest(sub='test'):37 self.fail()38 def test_subtest_double(self):39 with self.subTest(sub='error'):40 raise ValueError41 with self.subTest(sub='fail'):42 self.fail()43 def test_subtest_error(self):44 with self.subTest(sub='test'):45 raise ValueError46 @ut.expectedFailure47 def test_expected_fail(self):48 self.fail()49 @ut.expectedFailure50 def test_unexpected_success(self):51 pass52if __name__ == '__main__':53 import argparse54 parser = argparse.ArgumentParser(description='Check MPI test runner behavior.')55 parser.add_argument('name', nargs='?', default=None,56 help='Glob expression to specify specific test cases')57 parser.add_argument('-f', '--failfast', action='store_true',58 help='Stop the tests on first failure.')59 parser.add_argument('-v', '--verbose', choices=[0, 1, 2], default=1, type=int,60 help='Level of detail to show')61 args = parser.parse_args()...

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