有了这个unittest
模块,我喜欢跳过测试的特性,但它只在 Python 2.7+ 中可用。
例如,考虑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 装饰器并跳过测试?