欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Python的多重继承问题

程序员文章站 2022-05-21 11:21:47
...

因为要写个测试用例,测试4层LB,7层LB,这种测试只有一个参数不一样,实在不愿意把代码复制一份,想到了先写一个测试类,在分别继承这个测试类,写俩L4,L7子类
代码如下

class LbTest():
  ##初始化工作
  def setUp(self):
    print "in class %s, func %s" % (self.__class__.__name__, sys._getframe().f_code.co_name)

  #退出清理工作
  def tearDown(self):
    print "in class %s, func %s" % (self.__class__.__name__, sys._getframe().f_code.co_name)

  #具体的测试用例,一定要以test开头
  def testsum(self):
    print "in class %s, func %s" % (self.__class__.__name__, sys._getframe().f_code.co_name)

  def testsub(self):
    print "in class %s, func %s" % (self.__class__.__name__, sys._getframe().f_code.co_name)


class L4LbTestCase(unittest.TestCase, LbTest):
  def __init__(self, methodName='runTest'):
    unittest.TestCase.__init__(self, methodName)
    print ">>> Generate class %s, func %s\n" % (self.__class__.__name__, sys._getframe().f_code.co_name)

  def setUp(self):
    print ">in class %s, func %s" % (self.__class__.__name__, sys._getframe().f_code.co_name)
    LbTest.setUp(self)

  def tearDown(self):
    LbTest.tearDown(self)
    print "<in class %s, func %s" % (self.__class__.__name__, sys._getframe().f_code.co_name)

class L7LbTestCase(unittest.TestCase, LbTest):
  def __init__(self, methodName='runTest'):
    unittest.TestCase.__init__(self, methodName)
    print ">>> Generate class %s, func %s\n" % (self.__class__.__name__, sys._getframe().f_code.co_name)

  def setUp(self):
    print ">in class %s, func %s" % (self.__class__.__name__, sys._getframe().f_code.co_name)
    LbTest.setUp(self)

  def tearDown(self):
    LbTest.tearDown(self)
    print "<in class %s, func %s" % (self.__class__.__name__, sys._getframe().f_code.co_name)

但是执行结果很奇怪

Launching unittests with arguments python -m unittest discover -s D:/code/ -p test_lb.py -t D:\code in D:\code

>>> Generate class L4LbTestCase, func __init__

>>> Generate class L4LbTestCase, func __init__

>>> Generate class L7LbTestCase, func __init__   // 这一块调用不正常,为什么会调用两次?

>>> Generate class L7LbTestCase, func __init__   // 这一块调用不正常
>in class L4LbTestCase, func setUp
in class L4LbTestCase, func setUp
in class L4LbTestCase, func testsub
in class L4LbTestCase, func tearDown
<in class L4LbTestCase, func tearDown
>in class L4LbTestCase, func setUp
in class L4LbTestCase, func setUp
in class L4LbTestCase, func testsum
in class L4LbTestCase, func tearDown
<in class L4LbTestCase, func tearDown
>in class L7LbTestCase, func setUp
in class L7LbTestCase, func setUp
in class L7LbTestCase, func testsub
in class L7LbTestCase, func tearDown
<in class L7LbTestCase, func tearDown
>in class L7LbTestCase, func setUp
in class L7LbTestCase, func setUp
in class L7LbTestCase, func testsum
in class L7LbTestCase, func tearDown
<in class L7LbTestCase, func tearDown


Ran 4 tests in 0.004s

OK

Process finished with exit code 0

另外一段代码

class A(object):
    def __init__(self):
        print 'Running A.__init__'
        super(A, self).__init__()
        print 'Leaving A.__init__'


class B(A):
    def __init__(self):
        print 'Running B.__init__ '
        super(B,self).__init__()
        #A.__init__(self)
        print 'Leaving B.__init__ '


class C(A):
    def __init__(self):
        print 'Running C.__init__ '
        super(C, self).__init__()
        print 'Leaving C__init__ '

class D(A):
    def __init__(self):
        print 'Running D.__init__ '
        super(D, self).__init__()
        print 'Leaving D.__init__ '

class E(B, C,D):
    def __init__(self):
        print 'Running E.__init__ '
        super(E, self).__init__()
        print 'Leaving E.__init__ '


foo = E()

执行结果

Leaving A.__init__
Leaving B.__init__ 
Leaving C__init__ 
Leaving D.__init__ 
Leaving E.__init__ 
Running E.__init__ 
Running B.__init__ 
Running C.__init__ 
Running D.__init__ 
Running A.__init__