對於unittest
模塊,我喜歡feature to skip tests,但它只適用於Python 2.7+。在舊版本的Python中使用`@ unittest.skipIf`
例如,考慮test.py
:
import unittest
try:
import proprietary_module
except ImportError:
proprietary_module = None
class TestProprietary(unittest.TestCase):
@unittest.skipIf(proprietary_module is None, "requries proprietary module")
def test_something_proprietary(self):
self.assertTrue(proprietary_module is not None)
if __name__ == '__main__':
unittest.main()
如果我試圖與Python的早期版本運行測試,我得到一個錯誤:
Traceback (most recent call last):
File "test.py", line 7, in <module>
class TestProprietary(unittest.TestCase):
File "test.py", line 8, in TestProprietary
@unittest.skipIf(proprietary_module is None, "requries proprietary module")
AttributeError: 'module' object has no attribute 'skipIf'
是有辦法「帽子戲法「舊版本的Python忽略unittest裝飾器,並跳過測試?
unittest2在我的結尾並不令人滿意,雖然它工作正常,但它吐出了一個棄用警告,如: 'DeprecationWarning:不使用addSkip方法的TestResult的使用已被棄用 self._addSkip(result,skip_why)' 我無法讓它迅速消失。 –