Best Python code snippet using pytest
test_setups.py
Source:test_setups.py
...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:...
Looking for an in-depth tutorial around pytest? LambdaTest covers the detailed pytest tutorial that has everything related to the pytest, from setting up the pytest framework to automation testing. Delve deeper into pytest testing by exploring advanced use cases like parallel testing, pytest fixtures, parameterization, executing multiple test cases from a single file, and more.
Skim our below pytest tutorial playlist to get started with automation testing using the pytest framework.
https://www.youtube.com/playlist?list=PLZMWkkQEwOPlcGgDmHl8KkXKeLF83XlrP
Get 100 minutes of automation test minutes FREE!!