Is it possible to skip setUp() for a specific test in python's unittest?

From the docs (italics mine):

unittest.TestCase.setUp()

Method called to prepare the test fixture. This is called immediately before calling the test method; any exception raised by this method will be considered an error rather than a test failure. The default implementation does nothing.

So if you don't need any set up then don't override unittest.TestCase.setUp.

However, if one of your test_* methods doesn't need the set up and the others do, I would recommend putting that in a separate class.


You can use Django's @tag decorator as a criteria for skipping setUp

# import tag decorator
from django.test.util import tag

# The test which you want to skip setUp
@tag('skip_setup')
def test_mytest(self):
    assert True

def setUp(self):
    method = getattr(self,self._testMethodName)
    tags = getattr(method,'tags', {})
    if 'skip_setup' in tags:
        return #setUp skipped
    #do_stuff if not skipped

Besides skipping you can also use tags to do different setups.

P.S. If you are not using Django, the source code for that decorator is really simple:

def tag(*tags):
    """
    Decorator to add tags to a test class or method.
    """
    def decorator(obj):
        setattr(obj, 'tags', set(tags))
        return obj
    return decorator

In setUp(), self._testMethodName contains the name of the test that will be executed. It's likely better to put the test into a different class or something, of course, but it's in there.