繁体   English   中英

如何跳过python 2.6中的unittest案例

[英]how to skip a unittest case in python 2.6

自python2.7以来我添加了unittest.skip*装饰器和方法( 见这里有更多细节 ),我发现它们非常有用。

unittest.skip(reason)
unittest.skipIf(condition, reason)
unittest.skipUnless(condition, reason)

但是,我的问题是如果使用python2.6我们应该如何做类似的事情?

使用unittest2

以下代码以对代码其余部分透明的方式导入正确的unittest

import sys
if sys.version_info < (2, 7):
    import unittest2 as unittest
else:
    import unittest

如果你不能使用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

如果您可以自由安装其他软件包,则可以使用unittest2 ,即Python 2.7 unittest向后移植到Python 2.3+。 它确实包含跳过装饰器。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM