简体   繁体   English

pyunit跳过setUp和tearDown方法进行测试

[英]Pyunit skip setUp and tearDown methods for a test

Is it possible to skip the setUp and tearDown functions for a test..? 是否可以跳过setUp和tearDown函数进行测试? 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 . 这样做而无需编写具有不同setUptearDown方法的另一个测试类的唯一方法似乎是覆盖TestCaserun方法。 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): 您可以完全重写它,也可以尝试使用更短的版本(仅适用于setUp ,但是您也可以轻松地扩展它以支持tearDown ):

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. 我要做的是测试是否将要测试的方法命名为testWithoutSetup ,如果是,我暂时将setUp方法替换为不执行任何操作的函数。

Note that I only tested with Python 3.3, and it may work only for this version. 请注意,我仅使用Python 3.3进行了测试,并且它仅适用于该版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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