Python multiple inheritance unittest -
我试图理解使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | import unittest class MyClassTest1(object): def setUp(self): print 'Setting up', self.__class__ def test_a1(self): print"Running test_a1 for class", self.__class__ def test_a2(self): print"Running test_a2 for class", self.__class__ def tearDown(self): print 'Tearing down', self.__class__ class MyClassTest2(object): def setUp(self): print 'Setting up', self.__class__ def test_b1(self): print"Running test_b1 for class", self.__class__ def test_b2(self): print"Running test_b2 for class", self.__class__ def tearDown(self): print 'Tearing down', self.__class__ class MyTest_DoesNotWork(unittest.TestCase, MyClassTest1, MyClassTest2): """ Output: Running test_a1 for class <class '__main__.MyTest_DoesNotWork'> .Running test_a2 for class <class '__main__.MyTest_DoesNotWork'> .Running test_b1 for class <class '__main__.MyTest_DoesNotWork'> .Running test_b2 for class <class '__main__.MyTest_DoesNotWork'> """ pass class MyTest_DoesWork(MyClassTest1, MyClassTest2, unittest.TestCase): """ Output: Setting up <class '__main__.MyTest_DoesWork'> Running test_a1 for class <class '__main__.MyTest_DoesWork'> Tearing down <class '__main__.MyTest_DoesWork'> .Setting up <class '__main__.MyTest_DoesWork'> Running test_a2 for class <class '__main__.MyTest_DoesWork'> Tearing down <class '__main__.MyTest_DoesWork'> .Setting up <class '__main__.MyTest_DoesWork'> Running test_b1 for class <class '__main__.MyTest_DoesWork'> Tearing down <class '__main__.MyTest_DoesWork'> .Setting up <class '__main__.MyTest_DoesWork'> Running test_b2 for class <class '__main__.MyTest_DoesWork'> Tearing down <class '__main__.MyTest_DoesWork'> """ pass if __name__ =="__main__": unittest.main() |
python的方法解析顺序导致了这一点。对于继承结构,它按照您声明父类的顺序从左到右进行解析。
因此,使用
使用