我有以下构造,我从中派生一个类unittest.TestCase,其中调用了在实际测试类中定义的方法MyTests。
import unittest
class TestCase1(unittest.TestCase):
@classmethod
def setUpClass(cls):
print cls.extra()
class TestCase2(TestCase1):
@classmethod
def setUpClass(cls):
TestCase1.setUpClass()
class MyTests(TestCase1):
@classmethod
def extra(cls):
return 42
def test1(self):
pass
if __name__ == '__main__':
unittest.main()
就目前而言,上面的代码按预期工作。当我执行测试时,setUpClassofTestCase1正在运行,它extra()在MyTests.
当我想改用时,问题就出现了TestCase2,TestCase1即当我在以下行中将 '1' 更改为 '2' 时:
class MyTests(TestCase2): # using TestCase2 instead
这个想法是setUpClass被TestCase2调用的,它本身setUpClass从基本类调用,即TestCase1执行方法extra。但是,我收到一个属性错误:
AttributeError: type object 'TestCase1' has no attribute 'extra'
我似乎误解了这里的一些概念。我本来希望我的代码在这两种情况下都能工作(可能有一些错误),或者在这两种情况下都失败。非常感谢这里可能有什么问题或我如何解决问题的任何想法。