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

7hi4g0 picture 7hi4g0 · Jun 7, 2013 · Viewed 7.7k times · Source

When I create a unittest.TestCase, I can define a setUp() function that will run before every test in that test case. Is it possible to skip the setUp() for a single specific test?

It's possible that wanting to skip setUp() for a given test is not a good practice. I'm fairly new to unit testing and any suggestion regarding the subject is welcome.

Answer

Steven Rumbalski picture Steven Rumbalski · Jun 7, 2013

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.