Python unit test with base and sub class

前端 未结 15 2288
独厮守ぢ
独厮守ぢ 2020-11-28 18:44

I currently have a few unit tests which share a common set of tests. Here\'s an example:

import unittest

class BaseTest(unittest.TestCase):

    def testCo         


        
15条回答
  •  一生所求
    2020-11-28 18:59

    Just rename the testCommon method to something else. Unittest (usually) skips anything that doesn't have 'test' in it.

    Quick and simple

      import unittest
    
      class BaseTest(unittest.TestCase):
    
       def methodCommon(self):
           print 'Calling BaseTest:testCommon'
           value = 5
           self.assertEquals(value, 5)
    
      class SubTest1(BaseTest):
    
          def testSub1(self):
              print 'Calling SubTest1:testSub1'
              sub = 3
              self.assertEquals(sub, 3)
    
    
      class SubTest2(BaseTest):
    
          def testSub2(self):
              print 'Calling SubTest2:testSub2'
              sub = 4
              self.assertEquals(sub, 4)
    
      if __name__ == '__main__':
          unittest.main()`
    

提交回复
热议问题