views:

306

answers:

3

When using nosetests for Python it is possible to disable a unit test by setting the test function's __test__ attribute to false. I have implemented this using the following decorator:

def unit_test_disabled():
    def wrapper(func):
         func.__test__ = False
         return func

    return wrapper

@unit_test_disabled
def test_my_sample_test()
    #code here ...

However, this has the side effect of calling wrapper as the unit test. Wrapper will always pass but it is included in nosetests output. Is there another way of structuring the decorator so that the test will not run and does not appear in nosetests output.

A: 

Perhaps this will work:

def unit_test_disabled(f):
    f.__test__ = False
    return f

@unit_test_disabled
def test_my_sample_test()
    #code here ...
Steef
Unfortunately this won't work. When you use the decorator it will expect the function as a parameter. It is also doesn't meet the definition of a decorator. From the python docs, "the result must be a callable, which is invoked with the function object as the only argument.
Richard Dorman
Sorry, your answer is correct but David Raznick's comments below also need to be applied
Richard Dorman
A: 

I think you will also need to rename your decorator to something that has not got test in. The below only fails on the second test for me and the first does not show up in the test suite.

def unit_disabled(func):
    def wrapper(func):
         func.__test__ = False
         return func

    return wrapper

@unit_disabled
def test_my_sample_test():
    assert 1 <> 1

def test2_my_sample_test():
    assert 1 <> 1
David Raznick
+4  A: 

Nose already has a builtin decorator for this:

from nose.tools import nottest

@nottest
def test_my_sample_test()
    #code here ...

Also check out the other goodies that nose provides: http://somethingaboutorange.com/mrl/projects/nose/0.11.1/testing%5Ftools.html

Christian Oudard