Best Python code snippet using nose2
test_setups.py
Source:test_setups.py
...156 @staticmethod157 def setUpModule():158 results.append('Module1.setUpModule')159 @staticmethod160 def tearDownModule():161 results.append('Module1.tearDownModule')162 class Module2(object):163 @staticmethod164 def setUpModule():165 results.append('Module2.setUpModule')166 @staticmethod167 def tearDownModule():168 results.append('Module2.tearDownModule')169 class Test1(unittest.TestCase):170 @classmethod171 def setUpClass(cls):172 results.append('setup 1')173 @classmethod174 def tearDownClass(cls):175 results.append('teardown 1')176 def testOne(self):177 results.append('Test1.testOne')178 def testTwo(self):179 results.append('Test1.testTwo')180 class Test2(unittest.TestCase):181 @classmethod182 def setUpClass(cls):183 results.append('setup 2')184 @classmethod185 def tearDownClass(cls):186 results.append('teardown 2')187 def testOne(self):188 results.append('Test2.testOne')189 def testTwo(self):190 results.append('Test2.testTwo')191 class Test3(unittest.TestCase):192 @classmethod193 def setUpClass(cls):194 results.append('setup 3')195 @classmethod196 def tearDownClass(cls):197 results.append('teardown 3')198 def testOne(self):199 results.append('Test3.testOne')200 def testTwo(self):201 results.append('Test3.testTwo')202 Test1.__module__ = Test2.__module__ = 'Module'203 Test3.__module__ = 'Module2'204 sys.modules['Module'] = Module1205 sys.modules['Module2'] = Module2206 first = unittest.TestSuite((Test1('testOne'),))207 second = unittest.TestSuite((Test1('testTwo'),))208 third = unittest.TestSuite((Test2('testOne'),))209 fourth = unittest.TestSuite((Test2('testTwo'),))210 fifth = unittest.TestSuite((Test3('testOne'),))211 sixth = unittest.TestSuite((Test3('testTwo'),))212 suite = unittest.TestSuite((first, second, third, fourth, fifth, sixth))213 runner = self.getRunner()214 result = runner.run(suite)215 self.assertEqual(result.testsRun, 6)216 self.assertEqual(len(result.errors), 0)217 self.assertEqual(results,218 ['Module1.setUpModule', 'setup 1',219 'Test1.testOne', 'Test1.testTwo', 'teardown 1',220 'setup 2', 'Test2.testOne', 'Test2.testTwo',221 'teardown 2', 'Module1.tearDownModule',222 'Module2.setUpModule', 'setup 3',223 'Test3.testOne', 'Test3.testTwo',224 'teardown 3', 'Module2.tearDownModule'])225 def test_setup_module(self):226 class Module(object):227 moduleSetup = 0228 @staticmethod229 def setUpModule():230 Module.moduleSetup += 1231 class Test(unittest.TestCase):232 def test_one(self):233 pass234 def test_two(self):235 pass236 Test.__module__ = 'Module'237 sys.modules['Module'] = Module238 result = self.runTests(Test)239 self.assertEqual(Module.moduleSetup, 1)240 self.assertEqual(result.testsRun, 2)241 self.assertEqual(len(result.errors), 0)242 def test_error_in_setup_module(self):243 class Module(object):244 moduleSetup = 0245 moduleTornDown = 0246 @staticmethod247 def setUpModule():248 Module.moduleSetup += 1249 raise TypeError('foo')250 @staticmethod251 def tearDownModule():252 Module.moduleTornDown += 1253 class Test(unittest.TestCase):254 classSetUp = False255 classTornDown = False256 @classmethod257 def setUpClass(cls):258 Test.classSetUp = True259 @classmethod260 def tearDownClass(cls):261 Test.classTornDown = True262 def test_one(self):263 pass264 def test_two(self):265 pass266 class Test2(unittest.TestCase):267 def test_one(self):268 pass269 def test_two(self):270 pass271 Test.__module__ = 'Module'272 Test2.__module__ = 'Module'273 sys.modules['Module'] = Module274 result = self.runTests(Test, Test2)275 self.assertEqual(Module.moduleSetup, 1)276 self.assertEqual(Module.moduleTornDown, 0)277 self.assertEqual(result.testsRun, 0)278 self.assertFalse(Test.classSetUp)279 self.assertFalse(Test.classTornDown)280 self.assertEqual(len(result.errors), 1)281 error, _ = result.errors[0]282 self.assertEqual(str(error), 'setUpModule (Module)')283 def test_testcase_with_missing_module(self):284 class Test(unittest.TestCase):285 def test_one(self):286 pass287 def test_two(self):288 pass289 Test.__module__ = 'Module'290 sys.modules.pop('Module', None)291 result = self.runTests(Test)292 self.assertEqual(result.testsRun, 2)293 def test_teardown_module(self):294 class Module(object):295 moduleTornDown = 0296 @staticmethod297 def tearDownModule():298 Module.moduleTornDown += 1299 class Test(unittest.TestCase):300 def test_one(self):301 pass302 def test_two(self):303 pass304 Test.__module__ = 'Module'305 sys.modules['Module'] = Module306 result = self.runTests(Test)307 self.assertEqual(Module.moduleTornDown, 1)308 self.assertEqual(result.testsRun, 2)309 self.assertEqual(len(result.errors), 0)310 def test_error_in_teardown_module(self):311 class Module(object):312 moduleTornDown = 0313 @staticmethod314 def tearDownModule():315 Module.moduleTornDown += 1316 raise TypeError('foo')317 class Test(unittest.TestCase):318 classSetUp = False319 classTornDown = False320 @classmethod321 def setUpClass(cls):322 Test.classSetUp = True323 @classmethod324 def tearDownClass(cls):325 Test.classTornDown = True326 def test_one(self):327 pass328 def test_two(self):329 pass330 class Test2(unittest.TestCase):331 def test_one(self):332 pass333 def test_two(self):334 pass335 Test.__module__ = 'Module'336 Test2.__module__ = 'Module'337 sys.modules['Module'] = Module338 result = self.runTests(Test, Test2)339 self.assertEqual(Module.moduleTornDown, 1)340 self.assertEqual(result.testsRun, 4)341 self.assertTrue(Test.classSetUp)342 self.assertTrue(Test.classTornDown)343 self.assertEqual(len(result.errors), 1)344 error, _ = result.errors[0]345 self.assertEqual(str(error), 'tearDownModule (Module)')346 def test_skiptest_in_setupclass(self):347 class Test(unittest.TestCase):348 @classmethod349 def setUpClass(cls):350 raise unittest.SkipTest('foo')351 def test_one(self):352 pass353 def test_two(self):354 pass355 result = self.runTests(Test)356 self.assertEqual(result.testsRun, 0)357 self.assertEqual(len(result.errors), 0)358 self.assertEqual(len(result.skipped), 1)359 skipped = result.skipped[0][0]360 self.assertEqual(str(skipped), 'setUpClass (%s.Test)' % __name__)361 def test_skiptest_in_setupmodule(self):362 class Test(unittest.TestCase):363 def test_one(self):364 pass365 def test_two(self):366 pass367 class Module(object):368 @staticmethod369 def setUpModule():370 raise unittest.SkipTest('foo')371 Test.__module__ = 'Module'372 sys.modules['Module'] = Module373 result = self.runTests(Test)374 self.assertEqual(result.testsRun, 0)375 self.assertEqual(len(result.errors), 0)376 self.assertEqual(len(result.skipped), 1)377 skipped = result.skipped[0][0]378 self.assertEqual(str(skipped), 'setUpModule (Module)')379 def test_suite_debug_executes_setups_and_teardowns(self):380 ordering = []381 class Module(object):382 @staticmethod383 def setUpModule():384 ordering.append('setUpModule')385 @staticmethod386 def tearDownModule():387 ordering.append('tearDownModule')388 class Test(unittest.TestCase):389 @classmethod390 def setUpClass(cls):391 ordering.append('setUpClass')392 @classmethod393 def tearDownClass(cls):394 ordering.append('tearDownClass')395 def test_something(self):396 ordering.append('test_something')397 Test.__module__ = 'Module'398 sys.modules['Module'] = Module399 suite = unittest.defaultTestLoader.loadTestsFromTestCase(Test)400 suite.debug()401 expectedOrder = ['setUpModule', 'setUpClass', 'test_something', 'tearDownClass', 'tearDownModule']402 self.assertEqual(ordering, expectedOrder)403 def test_suite_debug_propagates_exceptions(self):404 class Module(object):405 @staticmethod406 def setUpModule():407 if phase == 0:408 raise Exception('setUpModule')409 @staticmethod410 def tearDownModule():411 if phase == 1:412 raise Exception('tearDownModule')413 class Test(unittest.TestCase):414 @classmethod415 def setUpClass(cls):416 if phase == 2:417 raise Exception('setUpClass')418 @classmethod419 def tearDownClass(cls):420 if phase == 3:421 raise Exception('tearDownClass')422 def test_something(self):423 if phase == 4:424 raise Exception('test_something')...
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!!