ziima
ziima

Reputation: 816

Conditional skip TestCase decorator in nosetests

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:

Summary

Upvotes: 13

Views: 5583

Answers (2)

Darshan Ahluwalia
Darshan Ahluwalia

Reputation: 116

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

Upvotes: 10

Martin Thoma
Martin Thoma

Reputation: 136665

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')

Upvotes: 6

Related Questions