首页 > pyunit测试失败时如何自动重新运行

pyunit测试失败时如何自动重新运行

在stackflow上看到一个帖子:
http://stackoverflow.com/questions/8295100/how-to-re-run-failed-junit-tests-immediately

是采用selenium大量UI测试运行时,因为DOM刷新等原因,一些测试不是很稳定,一次失败不是真的失败,需要设定多次失败后才算真的失败,stackoverflow上给出了junit的例子,但是没有pyunit的,我想实现方法应该是差不多的,试着写了一个:

class RetryTestCase(unittest.TestCase):

    def __init__(self, methodName='runTest'):
        self.retry_count = 2
        self._result = result.TestResult()
        super(RetryTestCase, self).__init__(methodName)

    def run(self, _result=None):
        super(RetryTestCase, self).run(_result)
        while self.retry_count > 0 and (_result.failures<>[] or _result.errors<>[]):
            self.retry_count = self.retry_count - 1
            super(RetryTestCase, self).run(_result)

        if _result.failures or _result.errors:
            _result.testsRun = 1

        if _result.failures:
            del _result.failures[1:]
        if _result.errors:
            del _result.errors[1:]

这样写测试的时候,直接结成RetryTestCase就可以了。 但是这样的代码很hack,对于Failture及Err处理也简单化了,不知有没有更好的办法。

【热门文章】
【热门文章】