鼻子测试中的条件跳过TestCase装饰器 [英] Conditional skip TestCase decorator in nosetests
问题描述
是否有一种方法可以使用鼻子测试根据自定义条件跳过整个TestCase
?我的意思是unittest.skip*
风格.
Is there a way to skip whole TestCase
based on custom condition using nosetests? I mean something in unittest.skip*
style.
我尝试过
import unittest
@unittest.skip("No reason")
class TestFoo(object):
def test_foo(self):
assert False
我发现使用python< = 2.7.3(显然是偶然的),但在python 2.7.6中不是这样.
I found out this works using python <= 2.7.3 (apparently by accident), but in python 2.7.6 not.
是否可以通过鼻子测试,还是必须创建自己的装饰器?
Is there a nosetests way to do this, or I have to create my own decorator?
注意:
- 我们尝试了python 2.7.3、2.7.6和鼻子测试1.1.2、1.3.0的所有组合.
- 如果该类是从
unittest.TestCase
继承的,那么它可以工作,但这不是我所需要的. - 当
setUpClass
升起SkipTest
时似乎可以工作,但看起来笨拙. - 我找到了
nottest
装饰器,但未将测试标记为已跳过.
- We tried all combinations of python 2.7.3, 2.7.6 and nosetests 1.1.2, 1.3.0.
- If the class is inherited from
unittest.TestCase
it works, but that is not what I need. - It seems to work when
setUpClass
raisesSkipTest
, but it looks clumsy. - I found
nottest
decorator, but it does not mark test as skipped.
摘要
- 更新20. 5. 2014:到目前为止,我还没有找到解决此问题的任何方法,因此看来唯一的选择就是编写自定义装饰器.
- 更新12.6. 2014:我发现在某些情况下提高
setUpClass
中的SkipTest
不是一个好主意,因为在这些情况下鼻子测试不会teardownContext
.如果涉及插件,这可能会产生不利影响.
- Update 20. 5. 2014: To this date I haven't found any solution to this problem, so it seems the only option is to write custom decorator.
- Update 12. 6. 2014: I have found that raising
SkipTest
insetUpClass
is not a good idea in some cases since nosetests doesn'tteardownContext
in those cases. This may have adverse effects if plugins are involved.
推荐答案
我观察到了同样的行为,即 unittest.skip , unittest .skipIf 等装饰器在使用鼻子进行测试时不受尊重.
I have observed this same behavior, that unittest.skip, unittest.skipIf, etc. decorators are not respected when using nose to run my tests.
Bakuriu建议编写一个装饰器,该装饰器会在setUpClass方法中引发SkipTest异常,从而解决了该问题:现在可以正确地跳过测试,无论是从unittest.main还是从鼻子运行.
Bakuriu's suggestion to write a decorator which raises a SkipTest exception in the setUpClass method fixes the problem: tests are now properly skipped whether running from unittest.main or from nose.
这里是代码,很大程度上基于unittest装饰器源代码.关键行是在TestCase类上使用装饰器时的行:
Here is code, heavily based on the unittest decorator source code. The key lines are the ones for when the decorator is used on a TestCase class:
from unittest import SkipTest, TestCase
import functools
import types
def _id(obj):
return obj
def skip(reason):
"""Unconditionally skip a test."""
def decorator(test_item):
if not isinstance(test_item, (type, types.ClassType)):
@functools.wraps(test_item)
def skip_wrapper(*args, **kwargs):
raise SkipTest(reason)
test_item = skip_wrapper
elif issubclass(test_item, TestCase):
@classmethod
@functools.wraps(test_item.setUpClass)
def skip_wrapper(*args, **kwargs):
raise SkipTest(reason)
test_item.setUpClass = skip_wrapper
test_item.__unittest_skip__ = True
test_item.__unittest_skip_why__ = reason
return test_item
return decorator
def skipIf(condition, reason):
"""Skip a test if the condition is true."""
if condition:
return skip(reason)
return _id
这篇关于鼻子测试中的条件跳过TestCase装饰器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!