如何使用 pythons unittest 从多个基础测试 类 继承?
How to inherit from multiple base test classes using pythons unittest?
我正在编写一些测试并希望在不同的 TestCase classes 之间共享 setUp 和 tearDown 方法。为此,我认为您可以使用基础测试 class,它仅实现 setUp 和 tearDown 方法并从中继承。但是,我也有想使用来自多个设置的变量的情况。这是一个例子:
class Base(unittest.TestCase):
def setUp(self):
self.shared = 'I am shared between everyone'
def tearDown(self):
del self.shared
class Base2(unittest.TestCase):
def setUp(self):
self.partial_shared = 'I am shared between only some tests'
def tearDown(self):
del self.partial_shared
class Test1(Base):
def test(self):
print self.shared
test_var = 'I only need Base'
print test_var
class Test2(Base2):
def test(self):
print self.partial_shared
test_var = 'I only need Base2'
class Test3(Base, Base2):
def test(self):
test_var = 'I need both Base and Base2'
print self.shared
print self.partial_shared
if __name__=='__main__':
unittest.main()
这是输出:
..EI am shared between everyone
I only need Base
I am shared between only some tests
I am shared between everyone
======================================================================
ERROR: test (__main__.Test3)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/b3053674/Documents/PyCoTools/PyCoTools/Tests/base_tests.py", line 134, in test
print self.partial_shared
AttributeError: 'Test3' object has no attribute 'partial_shared'
----------------------------------------------------------------------
Ran 3 tests in 0.004s
FAILED (errors=1)
是否有可能实施这样的 class 继承制?
Python支持链式继承。
您可以 class Base2()
从 Base 继承,然后添加您想要的东西。
像这样:
class Base2(Base):
def setUp(self):
super(Base2, self).setUp()
self.partial_shared = 'I am shared between only some tests'
然后从中继承:
class Test3(Base2):
def test(self):
test_var = 'I need both Base and Base2'
print self.shared
print self.partial_shared
我正在编写一些测试并希望在不同的 TestCase classes 之间共享 setUp 和 tearDown 方法。为此,我认为您可以使用基础测试 class,它仅实现 setUp 和 tearDown 方法并从中继承。但是,我也有想使用来自多个设置的变量的情况。这是一个例子:
class Base(unittest.TestCase):
def setUp(self):
self.shared = 'I am shared between everyone'
def tearDown(self):
del self.shared
class Base2(unittest.TestCase):
def setUp(self):
self.partial_shared = 'I am shared between only some tests'
def tearDown(self):
del self.partial_shared
class Test1(Base):
def test(self):
print self.shared
test_var = 'I only need Base'
print test_var
class Test2(Base2):
def test(self):
print self.partial_shared
test_var = 'I only need Base2'
class Test3(Base, Base2):
def test(self):
test_var = 'I need both Base and Base2'
print self.shared
print self.partial_shared
if __name__=='__main__':
unittest.main()
这是输出:
..EI am shared between everyone
I only need Base
I am shared between only some tests
I am shared between everyone
======================================================================
ERROR: test (__main__.Test3)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/b3053674/Documents/PyCoTools/PyCoTools/Tests/base_tests.py", line 134, in test
print self.partial_shared
AttributeError: 'Test3' object has no attribute 'partial_shared'
----------------------------------------------------------------------
Ran 3 tests in 0.004s
FAILED (errors=1)
是否有可能实施这样的 class 继承制?
Python支持链式继承。
您可以 class Base2()
从 Base 继承,然后添加您想要的东西。
像这样:
class Base2(Base):
def setUp(self):
super(Base2, self).setUp()
self.partial_shared = 'I am shared between only some tests'
然后从中继承:
class Test3(Base2):
def test(self):
test_var = 'I need both Base and Base2'
print self.shared
print self.partial_shared