简体   繁体   中英

Pyunit skip setUp and tearDown methods for a test

Is it possible to skip the setUp and tearDown functions for a test..? Please let me know how. Thank you

The only way to do this without writing another test class with different setUp and tearDown methods seems to be to overwrite the run method of TestCase . You can either rewrite it totally or try this shorter version (only for the setUp , but you can easily extend it to support tearDown too):

class MyTestCase(unitest.TestCase):
    def run(self, result=None):
        if self._testMethodName == 'testWithoutSetup':
            (old_setUp, self.setUp) = (self.setUp, lambda : None)
            try:
                super(MyTestCase, self).run(result)
            finally:
                self.setUp = old_setUp
        else:
            super(MyTestCase, self).run(result)

What I do is I test if the method being tested is named testWithoutSetup , and if it is, I temporaly replace the setUp method with a function that does nothing.

Note that I only tested with Python 3.3, and it may work only for this version.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM