python unittest阻止抽象testBase类测试本身

问题描述 投票:0回答:1

是)我有的:

我有一个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()!')

我希望其他人继承qazxsw poi。例如qazxsw poi

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

问题:

当我从setup.py test_command_OK运行测试套件时,事情就破了。我认为setup.py自己和NotImplementedError: subclasses must override test_command()!python setup.py test运行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()! ,当我直接运行TestDemo时,我会得到print(self.__class__),这时候一切正常。当我运行test_check_expectData(self)时,我得到<class '__main__.TestDemo'>

有没有办法让我告诉myProject.tests.TestDemo.TestBase不要运行python setup.py test课程?也许setup.py?但我不希望子类跳过这些需要测试,因为我希望所有子类都有expectData。 TestBase旨在用作模板,强制/检查子测试类具有所有要求,如TestBase。但它不应该自己运行。我该怎么办才能通过这个问题?我不介意建议,即使它意味着重新构建我当前的测试框架

python python-2.7 python-unittest
1个回答
0
投票

显然,使用SkipTest会跳过测试,如果子类@unittest.skipis , @unittest.skipif, @unittest.skipunless expectData

super()
© www.soinside.com 2019 - 2024. All rights reserved.