Is there a way to skip whole TestCase
based on custom condition using nosetests? I mean something in unittest.skip*
style.
I tried
import unittest
@unittest.skip("No reason")
class TestFoo(object):
def test_foo(self):
assert False
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?
Notes:
unittest.TestCase
it works, but that is not what I need.setUpClass
raises SkipTest
, but it looks clumsy.nottest
decorator, but it does not mark test as skipped.Summary
SkipTest
in setUpClass
is not a good idea in some cases since nosetests doesn't teardownContext
in those cases. This may have adverse effects if plugins are involved.I have observed this same behavior, that unittest.skip, unittest.skipIf, etc. decorators are not respected when using nose to run my tests.
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.
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
Yes.
from nose.plugins.skip import SkipTest
@SkipTest
def execute_main_test():
model_small = os.path.join(utils.get_project_root(),
"models",
"small-baseline")
view.main(True, model_small, False, 31, False, 'mysql_online')
view.main(False, model_small, False, 31, False, 'mysql_online')
view.main(False, model_small, True, 31, False, 'mysql_online')
view.main(False, model_small, False, 31, True, 'mysql_online')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With