Back to index

python3.2  3.2.2
Public Member Functions
unittest.test.test_setups.TestSetups Class Reference

List of all members.

Public Member Functions

def getRunner
def runTests
def test_setup_class
def test_teardown_class
def test_teardown_class_two_classes
def test_error_in_setupclass
def test_error_in_teardown_class
def test_class_not_torndown_when_setup_fails
def test_class_not_setup_or_torndown_when_skipped
def test_setup_teardown_order_with_pathological_suite
def test_setup_module
def test_error_in_setup_module
def test_testcase_with_missing_module
def test_teardown_module
def test_error_in_teardown_module
def test_skiptest_in_setupclass
def test_skiptest_in_setupmodule
def test_suite_debug_executes_setups_and_teardowns
def test_suite_debug_propagates_exceptions

Detailed Description

Definition at line 11 of file test_setups.py.


Member Function Documentation

Definition at line 13 of file test_setups.py.

00013 
00014     def getRunner(self):
00015         return unittest.TextTestRunner(resultclass=resultFactory,
                                          stream=io.StringIO())

Here is the caller graph for this function:

def unittest.test.test_setups.TestSetups.runTests (   self,
  cases 
)

Definition at line 16 of file test_setups.py.

00016 
00017     def runTests(self, *cases):
00018         suite = unittest.TestSuite()
00019         for case in cases:
00020             tests = unittest.defaultTestLoader.loadTestsFromTestCase(case)
00021             suite.addTests(tests)
00022 
00023         runner = self.getRunner()
00024 
00025         # creating a nested suite exposes some potential bugs
00026         realSuite = unittest.TestSuite()
00027         realSuite.addTest(suite)
00028         # adding empty suites to the end exposes potential bugs
00029         suite.addTest(unittest.TestSuite())
00030         realSuite.addTest(unittest.TestSuite())
00031         return runner.run(realSuite)

Here is the call graph for this function:

Definition at line 165 of file test_setups.py.

00165 
00166     def test_class_not_setup_or_torndown_when_skipped(self):
00167         class Test(unittest.TestCase):
00168             classSetUp = False
00169             tornDown = False
00170             @classmethod
00171             def setUpClass(cls):
00172                 Test.classSetUp = True
00173             @classmethod
00174             def tearDownClass(cls):
00175                 Test.tornDown = True
00176             def test_one(self):
00177                 pass
00178 
00179         Test = unittest.skip("hop")(Test)
00180         self.runTests(Test)
00181         self.assertFalse(Test.classSetUp)
00182         self.assertFalse(Test.tornDown)

Here is the call graph for this function:

Definition at line 149 of file test_setups.py.

00149 
00150     def test_class_not_torndown_when_setup_fails(self):
00151         class Test(unittest.TestCase):
00152             tornDown = False
00153             @classmethod
00154             def setUpClass(cls):
00155                 raise TypeError
00156             @classmethod
00157             def tearDownClass(cls):
00158                 Test.tornDown = True
00159                 raise TypeError('foo')
00160             def test_one(self):
00161                 pass
00162 
00163         self.runTests(Test)
00164         self.assertFalse(Test.tornDown)

Here is the call graph for this function:

Definition at line 285 of file test_setups.py.

00285 
00286     def test_error_in_setup_module(self):
00287         class Module(object):
00288             moduleSetup = 0
00289             moduleTornDown = 0
00290             @staticmethod
00291             def setUpModule():
00292                 Module.moduleSetup += 1
00293                 raise TypeError('foo')
00294             @staticmethod
00295             def tearDownModule():
00296                 Module.moduleTornDown += 1
00297 
00298         class Test(unittest.TestCase):
00299             classSetUp = False
00300             classTornDown = False
00301             @classmethod
00302             def setUpClass(cls):
00303                 Test.classSetUp = True
00304             @classmethod
00305             def tearDownClass(cls):
00306                 Test.classTornDown = True
00307             def test_one(self):
00308                 pass
00309             def test_two(self):
00310                 pass
00311 
00312         class Test2(unittest.TestCase):
00313             def test_one(self):
00314                 pass
00315             def test_two(self):
00316                 pass
00317         Test.__module__ = 'Module'
00318         Test2.__module__ = 'Module'
00319         sys.modules['Module'] = Module
00320 
00321         result = self.runTests(Test, Test2)
00322         self.assertEqual(Module.moduleSetup, 1)
00323         self.assertEqual(Module.moduleTornDown, 0)
00324         self.assertEqual(result.testsRun, 0)
00325         self.assertFalse(Test.classSetUp)
00326         self.assertFalse(Test.classTornDown)
00327         self.assertEqual(len(result.errors), 1)
00328         error, _ = result.errors[0]
00329         self.assertEqual(str(error), 'setUpModule (Module)')

Here is the call graph for this function:

Definition at line 98 of file test_setups.py.

00098 
00099     def test_error_in_setupclass(self):
00100         class BrokenTest(unittest.TestCase):
00101             @classmethod
00102             def setUpClass(cls):
00103                 raise TypeError('foo')
00104             def test_one(self):
00105                 pass
00106             def test_two(self):
00107                 pass
00108 
00109         result = self.runTests(BrokenTest)
00110 
00111         self.assertEqual(result.testsRun, 0)
00112         self.assertEqual(len(result.errors), 1)
00113         error, _ = result.errors[0]
00114         self.assertEqual(str(error),
00115                     'setUpClass (%s.BrokenTest)' % __name__)

Here is the call graph for this function:

Definition at line 116 of file test_setups.py.

00116 
00117     def test_error_in_teardown_class(self):
00118         class Test(unittest.TestCase):
00119             tornDown = 0
00120             @classmethod
00121             def tearDownClass(cls):
00122                 Test.tornDown += 1
00123                 raise TypeError('foo')
00124             def test_one(self):
00125                 pass
00126             def test_two(self):
00127                 pass
00128 
00129         class Test2(unittest.TestCase):
00130             tornDown = 0
00131             @classmethod
00132             def tearDownClass(cls):
00133                 Test2.tornDown += 1
00134                 raise TypeError('foo')
00135             def test_one(self):
00136                 pass
00137             def test_two(self):
00138                 pass
00139 
00140         result = self.runTests(Test, Test2)
00141         self.assertEqual(result.testsRun, 4)
00142         self.assertEqual(len(result.errors), 2)
00143         self.assertEqual(Test.tornDown, 1)
00144         self.assertEqual(Test2.tornDown, 1)
00145 
00146         error, _ = result.errors[0]
00147         self.assertEqual(str(error),
00148                     'tearDownClass (%s.Test)' % __name__)

Here is the call graph for this function:

Definition at line 362 of file test_setups.py.

00362 
00363     def test_error_in_teardown_module(self):
00364         class Module(object):
00365             moduleTornDown = 0
00366             @staticmethod
00367             def tearDownModule():
00368                 Module.moduleTornDown += 1
00369                 raise TypeError('foo')
00370 
00371         class Test(unittest.TestCase):
00372             classSetUp = False
00373             classTornDown = False
00374             @classmethod
00375             def setUpClass(cls):
00376                 Test.classSetUp = True
00377             @classmethod
00378             def tearDownClass(cls):
00379                 Test.classTornDown = True
00380             def test_one(self):
00381                 pass
00382             def test_two(self):
00383                 pass
00384 
00385         class Test2(unittest.TestCase):
00386             def test_one(self):
00387                 pass
00388             def test_two(self):
00389                 pass
00390         Test.__module__ = 'Module'
00391         Test2.__module__ = 'Module'
00392         sys.modules['Module'] = Module
00393 
00394         result = self.runTests(Test, Test2)
00395         self.assertEqual(Module.moduleTornDown, 1)
00396         self.assertEqual(result.testsRun, 4)
00397         self.assertTrue(Test.classSetUp)
00398         self.assertTrue(Test.classTornDown)
00399         self.assertEqual(len(result.errors), 1)
00400         error, _ = result.errors[0]
00401         self.assertEqual(str(error), 'tearDownModule (Module)')

Here is the call graph for this function:

Definition at line 32 of file test_setups.py.

00032 
00033     def test_setup_class(self):
00034         class Test(unittest.TestCase):
00035             setUpCalled = 0
00036             @classmethod
00037             def setUpClass(cls):
00038                 Test.setUpCalled += 1
00039                 unittest.TestCase.setUpClass()
00040             def test_one(self):
00041                 pass
00042             def test_two(self):
00043                 pass
00044 
00045         result = self.runTests(Test)
00046 
00047         self.assertEqual(Test.setUpCalled, 1)
00048         self.assertEqual(result.testsRun, 2)
00049         self.assertEqual(len(result.errors), 0)

Here is the call graph for this function:

Definition at line 265 of file test_setups.py.

00265 
00266     def test_setup_module(self):
00267         class Module(object):
00268             moduleSetup = 0
00269             @staticmethod
00270             def setUpModule():
00271                 Module.moduleSetup += 1
00272 
00273         class Test(unittest.TestCase):
00274             def test_one(self):
00275                 pass
00276             def test_two(self):
00277                 pass
00278         Test.__module__ = 'Module'
00279         sys.modules['Module'] = Module
00280 
00281         result = self.runTests(Test)
00282         self.assertEqual(Module.moduleSetup, 1)
00283         self.assertEqual(result.testsRun, 2)
00284         self.assertEqual(len(result.errors), 0)

Here is the call graph for this function:

Definition at line 183 of file test_setups.py.

00183 
00184     def test_setup_teardown_order_with_pathological_suite(self):
00185         results = []
00186 
00187         class Module1(object):
00188             @staticmethod
00189             def setUpModule():
00190                 results.append('Module1.setUpModule')
00191             @staticmethod
00192             def tearDownModule():
00193                 results.append('Module1.tearDownModule')
00194 
00195         class Module2(object):
00196             @staticmethod
00197             def setUpModule():
00198                 results.append('Module2.setUpModule')
00199             @staticmethod
00200             def tearDownModule():
00201                 results.append('Module2.tearDownModule')
00202 
00203         class Test1(unittest.TestCase):
00204             @classmethod
00205             def setUpClass(cls):
00206                 results.append('setup 1')
00207             @classmethod
00208             def tearDownClass(cls):
00209                 results.append('teardown 1')
00210             def testOne(self):
00211                 results.append('Test1.testOne')
00212             def testTwo(self):
00213                 results.append('Test1.testTwo')
00214 
00215         class Test2(unittest.TestCase):
00216             @classmethod
00217             def setUpClass(cls):
00218                 results.append('setup 2')
00219             @classmethod
00220             def tearDownClass(cls):
00221                 results.append('teardown 2')
00222             def testOne(self):
00223                 results.append('Test2.testOne')
00224             def testTwo(self):
00225                 results.append('Test2.testTwo')
00226 
00227         class Test3(unittest.TestCase):
00228             @classmethod
00229             def setUpClass(cls):
00230                 results.append('setup 3')
00231             @classmethod
00232             def tearDownClass(cls):
00233                 results.append('teardown 3')
00234             def testOne(self):
00235                 results.append('Test3.testOne')
00236             def testTwo(self):
00237                 results.append('Test3.testTwo')
00238 
00239         Test1.__module__ = Test2.__module__ = 'Module'
00240         Test3.__module__ = 'Module2'
00241         sys.modules['Module'] = Module1
00242         sys.modules['Module2'] = Module2
00243 
00244         first = unittest.TestSuite((Test1('testOne'),))
00245         second = unittest.TestSuite((Test1('testTwo'),))
00246         third = unittest.TestSuite((Test2('testOne'),))
00247         fourth = unittest.TestSuite((Test2('testTwo'),))
00248         fifth = unittest.TestSuite((Test3('testOne'),))
00249         sixth = unittest.TestSuite((Test3('testTwo'),))
00250         suite = unittest.TestSuite((first, second, third, fourth, fifth, sixth))
00251 
00252         runner = self.getRunner()
00253         result = runner.run(suite)
00254         self.assertEqual(result.testsRun, 6)
00255         self.assertEqual(len(result.errors), 0)
00256 
00257         self.assertEqual(results,
00258                          ['Module1.setUpModule', 'setup 1',
00259                           'Test1.testOne', 'Test1.testTwo', 'teardown 1',
00260                           'setup 2', 'Test2.testOne', 'Test2.testTwo',
00261                           'teardown 2', 'Module1.tearDownModule',
00262                           'Module2.setUpModule', 'setup 3',
00263                           'Test3.testOne', 'Test3.testTwo',
00264                           'teardown 3', 'Module2.tearDownModule'])

Here is the call graph for this function:

Definition at line 402 of file test_setups.py.

00402 
00403     def test_skiptest_in_setupclass(self):
00404         class Test(unittest.TestCase):
00405             @classmethod
00406             def setUpClass(cls):
00407                 raise unittest.SkipTest('foo')
00408             def test_one(self):
00409                 pass
00410             def test_two(self):
00411                 pass
00412 
00413         result = self.runTests(Test)
00414         self.assertEqual(result.testsRun, 0)
00415         self.assertEqual(len(result.errors), 0)
00416         self.assertEqual(len(result.skipped), 1)
00417         skipped = result.skipped[0][0]
00418         self.assertEqual(str(skipped), 'setUpClass (%s.Test)' % __name__)

Here is the call graph for this function:

Definition at line 419 of file test_setups.py.

00419 
00420     def test_skiptest_in_setupmodule(self):
00421         class Test(unittest.TestCase):
00422             def test_one(self):
00423                 pass
00424             def test_two(self):
00425                 pass
00426 
00427         class Module(object):
00428             @staticmethod
00429             def setUpModule():
00430                 raise unittest.SkipTest('foo')
00431 
00432         Test.__module__ = 'Module'
00433         sys.modules['Module'] = Module
00434 
00435         result = self.runTests(Test)
00436         self.assertEqual(result.testsRun, 0)
00437         self.assertEqual(len(result.errors), 0)
00438         self.assertEqual(len(result.skipped), 1)
00439         skipped = result.skipped[0][0]
00440         self.assertEqual(str(skipped), 'setUpModule (Module)')

Here is the call graph for this function:

Definition at line 441 of file test_setups.py.

00441 
00442     def test_suite_debug_executes_setups_and_teardowns(self):
00443         ordering = []
00444 
00445         class Module(object):
00446             @staticmethod
00447             def setUpModule():
00448                 ordering.append('setUpModule')
00449             @staticmethod
00450             def tearDownModule():
00451                 ordering.append('tearDownModule')
00452 
00453         class Test(unittest.TestCase):
00454             @classmethod
00455             def setUpClass(cls):
00456                 ordering.append('setUpClass')
00457             @classmethod
00458             def tearDownClass(cls):
00459                 ordering.append('tearDownClass')
00460             def test_something(self):
00461                 ordering.append('test_something')
00462 
00463         Test.__module__ = 'Module'
00464         sys.modules['Module'] = Module
00465 
00466         suite = unittest.defaultTestLoader.loadTestsFromTestCase(Test)
00467         suite.debug()
00468         expectedOrder = ['setUpModule', 'setUpClass', 'test_something', 'tearDownClass', 'tearDownModule']
00469         self.assertEqual(ordering, expectedOrder)

Here is the call graph for this function:

Definition at line 470 of file test_setups.py.

00470 
00471     def test_suite_debug_propagates_exceptions(self):
00472         class Module(object):
00473             @staticmethod
00474             def setUpModule():
00475                 if phase == 0:
00476                     raise Exception('setUpModule')
00477             @staticmethod
00478             def tearDownModule():
00479                 if phase == 1:
00480                     raise Exception('tearDownModule')
00481 
00482         class Test(unittest.TestCase):
00483             @classmethod
00484             def setUpClass(cls):
00485                 if phase == 2:
00486                     raise Exception('setUpClass')
00487             @classmethod
00488             def tearDownClass(cls):
00489                 if phase == 3:
00490                     raise Exception('tearDownClass')
00491             def test_something(self):
00492                 if phase == 4:
00493                     raise Exception('test_something')
00494 
00495         Test.__module__ = 'Module'
00496         sys.modules['Module'] = Module
00497 
00498         _suite = unittest.defaultTestLoader.loadTestsFromTestCase(Test)
00499         suite = unittest.TestSuite()
00500         suite.addTest(_suite)
00501 
00502         messages = ('setUpModule', 'tearDownModule', 'setUpClass', 'tearDownClass', 'test_something')
00503         for phase, msg in enumerate(messages):
00504             with self.assertRaisesRegex(Exception, msg):
00505                 suite.debug()

Here is the call graph for this function:

Definition at line 50 of file test_setups.py.

00050 
00051     def test_teardown_class(self):
00052         class Test(unittest.TestCase):
00053             tearDownCalled = 0
00054             @classmethod
00055             def tearDownClass(cls):
00056                 Test.tearDownCalled += 1
00057                 unittest.TestCase.tearDownClass()
00058             def test_one(self):
00059                 pass
00060             def test_two(self):
00061                 pass
00062 
00063         result = self.runTests(Test)
00064 
00065         self.assertEqual(Test.tearDownCalled, 1)
00066         self.assertEqual(result.testsRun, 2)
00067         self.assertEqual(len(result.errors), 0)

Here is the call graph for this function:

Definition at line 68 of file test_setups.py.

00068 
00069     def test_teardown_class_two_classes(self):
00070         class Test(unittest.TestCase):
00071             tearDownCalled = 0
00072             @classmethod
00073             def tearDownClass(cls):
00074                 Test.tearDownCalled += 1
00075                 unittest.TestCase.tearDownClass()
00076             def test_one(self):
00077                 pass
00078             def test_two(self):
00079                 pass
00080 
00081         class Test2(unittest.TestCase):
00082             tearDownCalled = 0
00083             @classmethod
00084             def tearDownClass(cls):
00085                 Test2.tearDownCalled += 1
00086                 unittest.TestCase.tearDownClass()
00087             def test_one(self):
00088                 pass
00089             def test_two(self):
00090                 pass
00091 
00092         result = self.runTests(Test, Test2)
00093 
00094         self.assertEqual(Test.tearDownCalled, 1)
00095         self.assertEqual(Test2.tearDownCalled, 1)
00096         self.assertEqual(result.testsRun, 4)
00097         self.assertEqual(len(result.errors), 0)

Here is the call graph for this function:

Definition at line 342 of file test_setups.py.

00342 
00343     def test_teardown_module(self):
00344         class Module(object):
00345             moduleTornDown = 0
00346             @staticmethod
00347             def tearDownModule():
00348                 Module.moduleTornDown += 1
00349 
00350         class Test(unittest.TestCase):
00351             def test_one(self):
00352                 pass
00353             def test_two(self):
00354                 pass
00355         Test.__module__ = 'Module'
00356         sys.modules['Module'] = Module
00357 
00358         result = self.runTests(Test)
00359         self.assertEqual(Module.moduleTornDown, 1)
00360         self.assertEqual(result.testsRun, 2)
00361         self.assertEqual(len(result.errors), 0)

Here is the call graph for this function:

Definition at line 330 of file test_setups.py.

00330 
00331     def test_testcase_with_missing_module(self):
00332         class Test(unittest.TestCase):
00333             def test_one(self):
00334                 pass
00335             def test_two(self):
00336                 pass
00337         Test.__module__ = 'Module'
00338         sys.modules.pop('Module', None)
00339 
00340         result = self.runTests(Test)
00341         self.assertEqual(result.testsRun, 2)

Here is the call graph for this function:


The documentation for this class was generated from the following file: