How to use countTestCases method in autotest

Best Python code snippet using autotest_python

test_suite.py

Source:test_suite.py Github

copy

Full Screen

...34 #35 # The tests iterable should be optional36 def test_init__tests_optional(self):37 suite = unittest.TestSuite()38 self.assertEqual(suite.countTestCases(), 0)39 # countTestCases() still works after tests are run40 suite.run(unittest.TestResult())41 self.assertEqual(suite.countTestCases(), 0)42 # "class TestSuite([tests])"43 # ...44 # "If tests is given, it must be an iterable of individual test cases45 # or other test suites that will be used to build the suite initially"46 #47 # TestSuite should deal with empty tests iterables by allowing the48 # creation of an empty suite49 def test_init__empty_tests(self):50 suite = unittest.TestSuite([])51 self.assertEqual(suite.countTestCases(), 0)52 # countTestCases() still works after tests are run53 suite.run(unittest.TestResult())54 self.assertEqual(suite.countTestCases(), 0)55 # "class TestSuite([tests])"56 # ...57 # "If tests is given, it must be an iterable of individual test cases58 # or other test suites that will be used to build the suite initially"59 #60 # TestSuite should allow any iterable to provide tests61 def test_init__tests_from_any_iterable(self):62 def tests():63 yield unittest.FunctionTestCase(lambda: None)64 yield unittest.FunctionTestCase(lambda: None)65 suite_1 = unittest.TestSuite(tests())66 self.assertEqual(suite_1.countTestCases(), 2)67 suite_2 = unittest.TestSuite(suite_1)68 self.assertEqual(suite_2.countTestCases(), 2)69 suite_3 = unittest.TestSuite(set(suite_1))70 self.assertEqual(suite_3.countTestCases(), 2)71 # countTestCases() still works after tests are run72 suite_1.run(unittest.TestResult())73 self.assertEqual(suite_1.countTestCases(), 2)74 suite_2.run(unittest.TestResult())75 self.assertEqual(suite_2.countTestCases(), 2)76 suite_3.run(unittest.TestResult())77 self.assertEqual(suite_3.countTestCases(), 2)78 # "class TestSuite([tests])"79 # ...80 # "If tests is given, it must be an iterable of individual test cases81 # or other test suites that will be used to build the suite initially"82 #83 # Does TestSuite() also allow other TestSuite() instances to be present84 # in the tests iterable?85 def test_init__TestSuite_instances_in_tests(self):86 def tests():87 ftc = unittest.FunctionTestCase(lambda: None)88 yield unittest.TestSuite([ftc])89 yield unittest.FunctionTestCase(lambda: None)90 suite = unittest.TestSuite(tests())91 self.assertEqual(suite.countTestCases(), 2)92 # countTestCases() still works after tests are run93 suite.run(unittest.TestResult())94 self.assertEqual(suite.countTestCases(), 2)95 ################################################################96 ### /Tests for TestSuite.__init__97 # Container types should support the iter protocol98 def test_iter(self):99 test1 = unittest.FunctionTestCase(lambda: None)100 test2 = unittest.FunctionTestCase(lambda: None)101 suite = unittest.TestSuite((test1, test2))102 self.assertEqual(list(suite), [test1, test2])103 # "Return the number of tests represented by the this test object.104 # ...this method is also implemented by the TestSuite class, which can105 # return larger [greater than 1] values"106 #107 # Presumably an empty TestSuite returns 0?108 def test_countTestCases_zero_simple(self):109 suite = unittest.TestSuite()110 self.assertEqual(suite.countTestCases(), 0)111 # "Return the number of tests represented by the this test object.112 # ...this method is also implemented by the TestSuite class, which can113 # return larger [greater than 1] values"114 #115 # Presumably an empty TestSuite (even if it contains other empty116 # TestSuite instances) returns 0?117 def test_countTestCases_zero_nested(self):118 class Test1(unittest.TestCase):119 def test(self):120 pass121 suite = unittest.TestSuite([unittest.TestSuite()])122 self.assertEqual(suite.countTestCases(), 0)123 # "Return the number of tests represented by the this test object.124 # ...this method is also implemented by the TestSuite class, which can125 # return larger [greater than 1] values"126 def test_countTestCases_simple(self):127 test1 = unittest.FunctionTestCase(lambda: None)128 test2 = unittest.FunctionTestCase(lambda: None)129 suite = unittest.TestSuite((test1, test2))130 self.assertEqual(suite.countTestCases(), 2)131 # countTestCases() still works after tests are run132 suite.run(unittest.TestResult())133 self.assertEqual(suite.countTestCases(), 2)134 # "Return the number of tests represented by the this test object.135 # ...this method is also implemented by the TestSuite class, which can136 # return larger [greater than 1] values"137 #138 # Make sure this holds for nested TestSuite instances, too139 def test_countTestCases_nested(self):140 class Test1(unittest.TestCase):141 def test1(self): pass142 def test2(self): pass143 test2 = unittest.FunctionTestCase(lambda: None)144 test3 = unittest.FunctionTestCase(lambda: None)145 child = unittest.TestSuite((Test1('test2'), test2))146 parent = unittest.TestSuite((test3, child, Test1('test1')))147 self.assertEqual(parent.countTestCases(), 4)148 # countTestCases() still works after tests are run149 parent.run(unittest.TestResult())150 self.assertEqual(parent.countTestCases(), 4)151 self.assertEqual(child.countTestCases(), 2)152 # "Run the tests associated with this suite, collecting the result into153 # the test result object passed as result."154 #155 # And if there are no tests? What then?156 def test_run__empty_suite(self):157 events = []158 result = LoggingResult(events)159 suite = unittest.TestSuite()160 suite.run(result)161 self.assertEqual(events, [])162 # "Note that unlike TestCase.run(), TestSuite.run() requires the163 # "result object to be passed in."164 def test_run__requires_result(self):165 suite = unittest.TestSuite()166 try:167 suite.run()168 except TypeError:169 pass170 else:171 self.fail("Failed to raise TypeError")172 # "Run the tests associated with this suite, collecting the result into173 # the test result object passed as result."174 def test_run(self):175 events = []176 result = LoggingResult(events)177 class LoggingCase(unittest.TestCase):178 def run(self, result):179 events.append('run %s' % self._testMethodName)180 def test1(self): pass181 def test2(self): pass182 tests = [LoggingCase('test1'), LoggingCase('test2')]183 unittest.TestSuite(tests).run(result)184 self.assertEqual(events, ['run test1', 'run test2'])185 # "Add a TestCase ... to the suite"186 def test_addTest__TestCase(self):187 class Foo(unittest.TestCase):188 def test(self): pass189 test = Foo('test')190 suite = unittest.TestSuite()191 suite.addTest(test)192 self.assertEqual(suite.countTestCases(), 1)193 self.assertEqual(list(suite), [test])194 # countTestCases() still works after tests are run195 suite.run(unittest.TestResult())196 self.assertEqual(suite.countTestCases(), 1)197 # "Add a ... TestSuite to the suite"198 def test_addTest__TestSuite(self):199 class Foo(unittest.TestCase):200 def test(self): pass201 suite_2 = unittest.TestSuite([Foo('test')])202 suite = unittest.TestSuite()203 suite.addTest(suite_2)204 self.assertEqual(suite.countTestCases(), 1)205 self.assertEqual(list(suite), [suite_2])206 # countTestCases() still works after tests are run207 suite.run(unittest.TestResult())208 self.assertEqual(suite.countTestCases(), 1)209 # "Add all the tests from an iterable of TestCase and TestSuite210 # instances to this test suite."211 #212 # "This is equivalent to iterating over tests, calling addTest() for213 # each element"214 def test_addTests(self):215 class Foo(unittest.TestCase):216 def test_1(self): pass217 def test_2(self): pass218 test_1 = Foo('test_1')219 test_2 = Foo('test_2')220 inner_suite = unittest.TestSuite([test_2])221 def gen():222 yield test_1223 yield test_2224 yield inner_suite225 suite_1 = unittest.TestSuite()226 suite_1.addTests(gen())227 self.assertEqual(list(suite_1), list(gen()))228 # "This is equivalent to iterating over tests, calling addTest() for229 # each element"230 suite_2 = unittest.TestSuite()231 for t in gen():232 suite_2.addTest(t)233 self.assertEqual(suite_1, suite_2)234 # "Add all the tests from an iterable of TestCase and TestSuite235 # instances to this test suite."236 #237 # What happens if it doesn't get an iterable?238 def test_addTest__noniterable(self):239 suite = unittest.TestSuite()240 try:241 suite.addTests(5)242 except TypeError:243 pass244 else:245 self.fail("Failed to raise TypeError")246 def test_addTest__noncallable(self):247 suite = unittest.TestSuite()248 self.assertRaises(TypeError, suite.addTest, 5)249 def test_addTest__casesuiteclass(self):250 suite = unittest.TestSuite()251 self.assertRaises(TypeError, suite.addTest, Test_TestSuite)252 self.assertRaises(TypeError, suite.addTest, unittest.TestSuite)253 def test_addTests__string(self):254 suite = unittest.TestSuite()255 self.assertRaises(TypeError, suite.addTests, "foo")256 def test_function_in_suite(self):257 def f(_):258 pass259 suite = unittest.TestSuite()260 suite.addTest(f)261 # when the bug is fixed this line will not crash262 suite.run(unittest.TestResult())263 def test_remove_test_at_index(self):264 if not unittest.BaseTestSuite._cleanup:265 raise unittest.SkipTest("Suite cleanup is disabled")266 suite = unittest.TestSuite()267 suite._tests = [1, 2, 3]268 suite._removeTestAtIndex(1)269 self.assertEqual([1, None, 3], suite._tests)270 def test_remove_test_at_index_not_indexable(self):271 if not unittest.BaseTestSuite._cleanup:272 raise unittest.SkipTest("Suite cleanup is disabled")273 suite = unittest.TestSuite()274 suite._tests = None275 # if _removeAtIndex raises for noniterables this next line will break276 suite._removeTestAtIndex(2)277 def assert_garbage_collect_test_after_run(self, TestSuiteClass):278 if not unittest.BaseTestSuite._cleanup:279 raise unittest.SkipTest("Suite cleanup is disabled")280 class Foo(unittest.TestCase):281 def test_nothing(self):282 pass283 test = Foo('test_nothing')284 wref = weakref.ref(test)285 suite = TestSuiteClass([wref()])286 suite.run(unittest.TestResult())287 del test288 # for the benefit of non-reference counting implementations289 gc.collect()290 self.assertEqual(suite._tests, [None])291 self.assertIsNone(wref())292 def test_garbage_collect_test_after_run_BaseTestSuite(self):293 self.assert_garbage_collect_test_after_run(unittest.BaseTestSuite)294 def test_garbage_collect_test_after_run_TestSuite(self):295 self.assert_garbage_collect_test_after_run(unittest.TestSuite)296 def test_basetestsuite(self):297 class Test(unittest.TestCase):298 wasSetUp = False299 wasTornDown = False300 @classmethod301 def setUpClass(cls):302 cls.wasSetUp = True303 @classmethod304 def tearDownClass(cls):305 cls.wasTornDown = True306 def testPass(self):307 pass308 def testFail(self):309 fail310 class Module(object):311 wasSetUp = False312 wasTornDown = False313 @staticmethod314 def setUpModule():315 Module.wasSetUp = True316 @staticmethod317 def tearDownModule():318 Module.wasTornDown = True319 Test.__module__ = 'Module'320 sys.modules['Module'] = Module321 self.addCleanup(sys.modules.pop, 'Module')322 suite = unittest.BaseTestSuite()323 suite.addTests([Test('testPass'), Test('testFail')])324 self.assertEqual(suite.countTestCases(), 2)325 result = unittest.TestResult()326 suite.run(result)327 self.assertFalse(Module.wasSetUp)328 self.assertFalse(Module.wasTornDown)329 self.assertFalse(Test.wasSetUp)330 self.assertFalse(Test.wasTornDown)331 self.assertEqual(len(result.errors), 1)332 self.assertEqual(len(result.failures), 0)333 self.assertEqual(result.testsRun, 2)334 self.assertEqual(suite.countTestCases(), 2)335 def test_overriding_call(self):336 class MySuite(unittest.TestSuite):337 called = False338 def __call__(self, *args, **kw):339 self.called = True340 unittest.TestSuite.__call__(self, *args, **kw)341 suite = MySuite()342 result = unittest.TestResult()343 wrapper = unittest.TestSuite()344 wrapper.addTest(suite)345 wrapper(result)346 self.assertTrue(suite.called)347 # reusing results should be permitted even if abominable348 self.assertFalse(result._testRunEntered)...

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 autotest 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