我有什么:
我有一个TestBase.py
我正在使用它作为抽象测试类。
from abc import abstractmethod
class TestBase:
expectData = None
def test_check_expectData(self):
if self.expectData is None:
raise NotImplementedError('you must have expectData')
@abstractmethod
def test_command_OK(self):
raise NotImplementedError('subclasses must override test_command()!')
我希望其他人继承TestBase
。例如TestDemo.py
import unittest
from TestBase import TestBase
class TestDemo(unittest.TestCase, TestBase):
expectData = ["someData"]
def test_command_OK(self):
self.assertTrue(True)
if __name__ =='__main__':
unittest.main()
如果我运行python TestDemo.py
,一切都按预期工作。如果我没有预期日期,我将获得NotImplementedError
,如果我没有test_command_OK
方法,我会获得NotImplementedError: subclasses must override test_command()!
问题:
当我从setup.py python setup.py test
运行测试套件时,事情就中断了。我认为setup.py自己和TestBase
类TestBase
中运行expectedData
类的原因是None
,因此无法通过测试。
====================================================================== ERROR: myProject.tests.TestDemo.TestBase.test_check_expectData
---------------------------------------------------------------------- Traceback (most recent call last): File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/nose/case.py", line 197, in runTest
self.test(*self.arg) File "/Users/z002x6m/Desktop/myProject/tests/TestBase.py", line 49, in test_check_expectData
if self.expectData is None: AttributeError: TestBase instance has no attribute 'expectData'
====================================================================== ERROR: myProject.tests.TestDemo.TestBase.test_command_OK
---------------------------------------------------------------------- Traceback (most recent call last): File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/nose/case.py", line 197, in runTest
self.test(*self.arg) File "/Users/z002x6m/Desktop/myProject/tests/TestBase.py", line 57, in test_command_OK
raise NotImplementedError('subclasses must override test_command()!') NotImplementedError: subclasses must override test_command()!
如果我在print(self.__class__)
方法中打印出test_check_expectData(self)
,当我直接运行TestDemo时,我得到<class '__main__.TestDemo'>
,这时候一切正常。当我运行myProject.tests.TestDemo.TestBase
时,我得到python setup.py test
。
有没有办法告诉setup.py
不要运行TestBase
课程?也许@unittest.skipis , @unittest.skipif, @unittest.skipunless
?但我不希望子类跳过这些需要测试,因为我希望所有子类都有expectData。 TestBase用作模板,force / check子测试类具有expectData
等所有要求。但它不应该自己运行。我该怎么办才能通过这个问题?即使意味着要重新构建我当前的测试框架,我也不介意建议
答案 0 :(得分:0)
显然,如果子类super()
TestBase
类
from unittest.case import SkipTest
def test_check_expectData(self):
if self.expectData is None:
raise SkipTest