【发布时间】:2012-06-15 00:18:38
【问题描述】:
使用unittest 模块,我喜欢feature to skip tests,但它仅在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 装饰器并跳过测试?
【问题讨论】:
标签: python unit-testing decorator backport