Search code examples
pythonnosetests

Disabling Python nosetests


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.


Solution

  • 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