zhu*_*ala 11 python unit-testing
unittest.skip*自python2.7以来我添加了以下的装饰器和方法(参见此处了解更多详细信息),我发现它们非常有用.
unittest.skip(reason)
unittest.skipIf(condition, reason)
unittest.skipUnless(condition, reason)
Run Code Online (Sandbox Code Playgroud)
但是,我的问题是如果使用python2.6我们应该如何做类似的事情?
使用unittest2.
以下代码unittest以对代码其余部分透明的方式导入权限:
import sys
if sys.version_info < (2, 7):
import unittest2 as unittest
else:
import unittest
Run Code Online (Sandbox Code Playgroud)
如果您不能使用unittest2并且不介意在Python 2.6中进行不同数量的测试,则可以编写简单的装饰器以使测试消失:
try:
from unittest import skip, skipUnless
except ImportError:
def skip(f):
return lambda self: None
def skipUnless(condition, reason):
if condition:
return lambda x: x
else:
return lambda x: None
Run Code Online (Sandbox Code Playgroud)