How to use tearDownClass method in freezegun

Best Python code snippet using freezegun

test_setups.py

Source:test_setups.py Github

copy

Full Screen

...42 self.assertEqual(len(result.errors), 0)43 def test_teardown_class(self):44 class Test(unittest2.TestCase):45 tearDownCalled = 046 def tearDownClass(cls):47 Test.tearDownCalled += 148 unittest2.TestCase.tearDownClass()49 tearDownClass = classmethod(tearDownClass)50 def test_one(self):51 pass52 def test_two(self):53 pass54 55 result = self.runTests(Test)56 57 self.assertEqual(Test.tearDownCalled, 1)58 self.assertEqual(result.testsRun, 2)59 self.assertEqual(len(result.errors), 0)60 61 def test_teardown_class_two_classes(self):62 class Test(unittest2.TestCase):63 tearDownCalled = 064 def tearDownClass(cls):65 Test.tearDownCalled += 166 unittest2.TestCase.tearDownClass()67 tearDownClass = classmethod(tearDownClass)68 def test_one(self):69 pass70 def test_two(self):71 pass72 73 class Test2(unittest2.TestCase):74 tearDownCalled = 075 def tearDownClass(cls):76 Test2.tearDownCalled += 177 unittest2.TestCase.tearDownClass()78 tearDownClass = classmethod(tearDownClass)79 def test_one(self):80 pass81 def test_two(self):82 pass83 84 result = self.runTests(Test, Test2)85 86 self.assertEqual(Test.tearDownCalled, 1)87 self.assertEqual(Test2.tearDownCalled, 1)88 self.assertEqual(result.testsRun, 4)89 self.assertEqual(len(result.errors), 0)90 def test_error_in_setupclass(self):91 class BrokenTest(unittest2.TestCase):92 def setUpClass(cls):93 raise TypeError('foo')94 setUpClass = classmethod(setUpClass)95 def test_one(self):96 pass97 def test_two(self):98 pass99 100 result = self.runTests(BrokenTest)101 102 self.assertEqual(result.testsRun, 0)103 self.assertEqual(len(result.errors), 1)104 error, _ = result.errors[0]105 self.assertEqual(str(error), 106 'setUpClass (%s.BrokenTest)' % __name__)107 def test_error_in_teardown_class(self):108 class Test(unittest2.TestCase):109 tornDown = 0110 def tearDownClass(cls):111 Test.tornDown += 1112 raise TypeError('foo')113 tearDownClass = classmethod(tearDownClass)114 def test_one(self):115 pass116 def test_two(self):117 pass118 119 class Test2(unittest2.TestCase):120 tornDown = 0121 def tearDownClass(cls):122 Test2.tornDown += 1123 raise TypeError('foo')124 tearDownClass = classmethod(tearDownClass)125 def test_one(self):126 pass127 def test_two(self):128 pass129 130 result = self.runTests(Test, Test2)131 self.assertEqual(result.testsRun, 4)132 self.assertEqual(len(result.errors), 2)133 self.assertEqual(Test.tornDown, 1)134 self.assertEqual(Test2.tornDown, 1)135 136 error, _ = result.errors[0]137 self.assertEqual(str(error), 138 'tearDownClass (%s.Test)' % __name__)139 def test_class_not_torndown_when_setup_fails(self):140 class Test(unittest2.TestCase):141 tornDown = False142 def setUpClass(cls):143 raise TypeError144 setUpClass = classmethod(setUpClass)145 def tearDownClass(cls):146 Test.tornDown = True147 raise TypeError('foo')148 tearDownClass = classmethod(tearDownClass)149 def test_one(self):150 pass151 self.runTests(Test)152 self.assertFalse(Test.tornDown)153 154 def test_class_not_setup_or_torndown_when_skipped(self):155 class Test(unittest2.TestCase):156 classSetUp = False157 tornDown = False158 def setUpClass(cls):159 Test.classSetUp = True160 setUpClass = classmethod(setUpClass)161 def tearDownClass(cls):162 Test.tornDown = True163 tearDownClass = classmethod(tearDownClass)164 def test_one(self):165 pass166 Test = unittest2.skip("hop")(Test)167 self.runTests(Test)168 self.assertFalse(Test.classSetUp)169 self.assertFalse(Test.tornDown)170 def test_setup_teardown_order_with_pathological_suite(self):171 results = []172 173 class Module1(object):174 def setUpModule():175 results.append('Module1.setUpModule')176 setUpModule = staticmethod(setUpModule)177 def tearDownModule():178 results.append('Module1.tearDownModule')179 tearDownModule = staticmethod(tearDownModule)180 181 class Module2(object):182 def setUpModule():183 results.append('Module2.setUpModule')184 setUpModule = staticmethod(setUpModule)185 def tearDownModule():186 results.append('Module2.tearDownModule')187 tearDownModule = staticmethod(tearDownModule)188 189 class Test1(unittest2.TestCase):190 def setUpClass(cls):191 results.append('setup 1')192 setUpClass = classmethod(setUpClass)193 def tearDownClass(cls):194 results.append('teardown 1')195 tearDownClass = classmethod(tearDownClass)196 def testOne(self):197 results.append('Test1.testOne')198 def testTwo(self):199 results.append('Test1.testTwo')200 201 class Test2(unittest2.TestCase):202 def setUpClass(cls):203 results.append('setup 2')204 setUpClass = classmethod(setUpClass)205 def tearDownClass(cls):206 results.append('teardown 2')207 tearDownClass = classmethod(tearDownClass)208 def testOne(self):209 results.append('Test2.testOne')210 def testTwo(self):211 results.append('Test2.testTwo')212 213 class Test3(unittest2.TestCase):214 def setUpClass(cls):215 results.append('setup 3')216 setUpClass = classmethod(setUpClass)217 def tearDownClass(cls):218 results.append('teardown 3')219 tearDownClass = classmethod(tearDownClass)220 def testOne(self):221 results.append('Test3.testOne')222 def testTwo(self):223 results.append('Test3.testTwo')224 225 Test1.__module__ = Test2.__module__ = 'Module'226 Test3.__module__ = 'Module2'227 sys.modules['Module'] = Module1228 sys.modules['Module2'] = Module2229 230 first = unittest2.TestSuite((Test1('testOne'),))231 second = unittest2.TestSuite((Test1('testTwo'),))232 third = unittest2.TestSuite((Test2('testOne'),))233 fourth = unittest2.TestSuite((Test2('testTwo'),))234 fifth = unittest2.TestSuite((Test3('testOne'),))235 sixth = unittest2.TestSuite((Test3('testTwo'),))236 suite = unittest2.TestSuite((first, second, third, fourth, fifth, sixth))237 238 runner = self.getRunner()239 result = runner.run(suite)240 self.assertEqual(result.testsRun, 6)241 self.assertEqual(len(result.errors), 0)242 self.assertEqual(results,243 ['Module1.setUpModule', 'setup 1', 244 'Test1.testOne', 'Test1.testTwo', 'teardown 1',245 'setup 2', 'Test2.testOne', 'Test2.testTwo', 246 'teardown 2', 'Module1.tearDownModule',247 'Module2.setUpModule', 'setup 3',248 'Test3.testOne', 'Test3.testTwo', 249 'teardown 3', 'Module2.tearDownModule'])250 251 def test_setup_module(self):252 class Module(object):253 moduleSetup = 0254 def setUpModule():255 Module.moduleSetup += 1256 setUpModule = staticmethod(setUpModule)257 258 class Test(unittest2.TestCase):259 def test_one(self):260 pass261 def test_two(self):262 pass263 Test.__module__ = 'Module'264 sys.modules['Module'] = Module265 266 result = self.runTests(Test)267 self.assertEqual(Module.moduleSetup, 1)268 self.assertEqual(result.testsRun, 2)269 self.assertEqual(len(result.errors), 0)270 271 def test_error_in_setup_module(self):272 class Module(object):273 moduleSetup = 0274 moduleTornDown = 0275 def setUpModule():276 Module.moduleSetup += 1277 raise TypeError('foo')278 setUpModule = staticmethod(setUpModule)279 def tearDownModule():280 Module.moduleTornDown += 1281 tearDownModule = staticmethod(tearDownModule)282 283 class Test(unittest2.TestCase):284 classSetUp = False285 classTornDown = False286 def setUpClass(cls):287 Test.classSetUp = True288 setUpClass = classmethod(setUpClass)289 def tearDownClass(cls):290 Test.classTornDown = True291 tearDownClass = classmethod(tearDownClass)292 def test_one(self):293 pass294 def test_two(self):295 pass296 297 class Test2(unittest2.TestCase):298 def test_one(self):299 pass300 def test_two(self):301 pass302 Test.__module__ = 'Module'303 Test2.__module__ = 'Module'304 sys.modules['Module'] = Module305 306 result = self.runTests(Test, Test2)307 self.assertEqual(Module.moduleSetup, 1)308 self.assertEqual(Module.moduleTornDown, 0)309 self.assertEqual(result.testsRun, 0)310 self.assertFalse(Test.classSetUp)311 self.assertFalse(Test.classTornDown)312 self.assertEqual(len(result.errors), 1)313 error, _ = result.errors[0]314 self.assertEqual(str(error), 'setUpModule (Module)')315 316 def test_testcase_with_missing_module(self):317 class Test(unittest2.TestCase):318 def test_one(self):319 pass320 def test_two(self):321 pass322 Test.__module__ = 'Module'323 sys.modules.pop('Module', None)324 325 result = self.runTests(Test)326 self.assertEqual(result.testsRun, 2)327 def test_teardown_module(self):328 class Module(object):329 moduleTornDown = 0330 def tearDownModule():331 Module.moduleTornDown += 1332 tearDownModule = staticmethod(tearDownModule)333 334 class Test(unittest2.TestCase):335 def test_one(self):336 pass337 def test_two(self):338 pass339 Test.__module__ = 'Module'340 sys.modules['Module'] = Module341 342 result = self.runTests(Test)343 self.assertEqual(Module.moduleTornDown, 1)344 self.assertEqual(result.testsRun, 2)345 self.assertEqual(len(result.errors), 0)346 def test_error_in_teardown_module(self):347 class Module(object):348 moduleTornDown = 0349 def tearDownModule():350 Module.moduleTornDown += 1351 raise TypeError('foo')352 tearDownModule = staticmethod(tearDownModule)353 354 class Test(unittest2.TestCase):355 classSetUp = False356 classTornDown = False357 def setUpClass(cls):358 Test.classSetUp = True359 setUpClass = classmethod(setUpClass)360 def tearDownClass(cls):361 Test.classTornDown = True362 tearDownClass = classmethod(tearDownClass)363 def test_one(self):364 pass365 def test_two(self):366 pass367 368 class Test2(unittest2.TestCase):369 def test_one(self):370 pass371 def test_two(self):372 pass373 Test.__module__ = 'Module'374 Test2.__module__ = 'Module'375 sys.modules['Module'] = Module376 377 result = self.runTests(Test, Test2)378 self.assertEqual(Module.moduleTornDown, 1)379 self.assertEqual(result.testsRun, 4)380 self.assertTrue(Test.classSetUp)381 self.assertTrue(Test.classTornDown)382 self.assertEqual(len(result.errors), 1)383 error, _ = result.errors[0]384 self.assertEqual(str(error), 'tearDownModule (Module)')385 def test_skiptest_in_setupclass(self):386 class Test(unittest2.TestCase):387 def setUpClass(cls):388 raise unittest2.SkipTest('foo')389 setUpClass = classmethod(setUpClass)390 def test_one(self):391 pass392 def test_two(self):393 pass394 result = self.runTests(Test)395 self.assertEqual(result.testsRun, 0)396 self.assertEqual(len(result.errors), 0)397 self.assertEqual(len(result.skipped), 1)398 skipped = result.skipped[0][0]399 self.assertEqual(str(skipped), 'setUpClass (%s.Test)' % __name__)400 def test_skiptest_in_setupmodule(self):401 class Test(unittest2.TestCase):402 def test_one(self):403 pass404 def test_two(self):405 pass406 class Module(object):407 def setUpModule():408 raise unittest2.SkipTest('foo')409 setUpModule = staticmethod(setUpModule)410 Test.__module__ = 'Module'411 sys.modules['Module'] = Module412 result = self.runTests(Test)413 self.assertEqual(result.testsRun, 0)414 self.assertEqual(len(result.errors), 0)415 self.assertEqual(len(result.skipped), 1)416 skipped = result.skipped[0][0]417 self.assertEqual(str(skipped), 'setUpModule (Module)')418 def test_suite_debug_executes_setups_and_teardowns(self):419 ordering = []420 class Module(object):421 def setUpModule():422 ordering.append('setUpModule')423 setUpModule = staticmethod(setUpModule)424 def tearDownModule():425 ordering.append('tearDownModule')426 tearDownModule = staticmethod(tearDownModule)427 class Test(unittest2.TestCase):428 def setUpClass(cls):429 ordering.append('setUpClass')430 setUpClass = classmethod(setUpClass)431 def tearDownClass(cls):432 ordering.append('tearDownClass')433 tearDownClass = classmethod(tearDownClass)434 def test_something(self):435 ordering.append('test_something')436 Test.__module__ = 'Module'437 sys.modules['Module'] = Module438 suite = unittest2.defaultTestLoader.loadTestsFromTestCase(Test)439 suite.debug()440 expectedOrder = ['setUpModule', 'setUpClass', 'test_something', 'tearDownClass', 'tearDownModule']441 self.assertEqual(ordering, expectedOrder)442 def test_suite_debug_propagates_exceptions(self):443 class Module(object):444 def setUpModule():445 if phase == 0:446 raise Exception('setUpModule')447 setUpModule = staticmethod(setUpModule)448 def tearDownModule():449 if phase == 1:450 raise Exception('tearDownModule')451 tearDownModule = staticmethod(tearDownModule)452 class Test(unittest2.TestCase):453 def setUpClass(cls):454 if phase == 2:455 raise Exception('setUpClass')456 setUpClass = classmethod(setUpClass)457 def tearDownClass(cls):458 if phase == 3:459 raise Exception('tearDownClass')460 tearDownClass = classmethod(tearDownClass)461 def test_something(self):462 if phase == 4:463 raise Exception('test_something')464 Test.__module__ = 'Module'465 sys.modules['Module'] = Module466 _suite = unittest2.defaultTestLoader.loadTestsFromTestCase(Test)467 suite = unittest2.TestSuite()468 469 # nesting a suite again exposes a bug in the initial implementation470 suite.addTest(_suite)471 messages = ('setUpModule', 'tearDownModule', 'setUpClass', 'tearDownClass', 'test_something')...

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