Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

  • 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 raises SkipTest, but it looks clumsy.
  • I found nottest decorator, but it does not mark test as skipped.

Summary

  • 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 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.
like image 361
ziima Avatar asked Feb 21 '14 13:02

ziima


2 Answers

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
like image 194
Darshan Ahluwalia Avatar answered Oct 29 '22 04:10

Darshan Ahluwalia


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')
like image 36
Martin Thoma Avatar answered Oct 29 '22 06:10

Martin Thoma