我试图了解使用unittest.TestCase
类进行多重继承时会发生什么。
MyTest_DoesWork
输出我所期望的,这既是setUp()
又tearDown()
被触发了。这不会发生在MyTest_DoesNotWork
. 为什么会这样?有什么线索吗?
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()