How to execute code only on test failures with python unittest2?

后端 未结 5 2181
既然无缘
既然无缘 2020-12-06 11:01

I have some class-based unit tests running in python\'s unittest2 framework. We\'re using Selenium WebDriver, which has a convenient save_screenshot() method. I

5条回答
  •  借酒劲吻你
    2020-12-06 11:55

    Found a solution - I can override failureException:

    @property
    def failureException(self):
        class MyFailureException(AssertionError):
            def __init__(self_, *args, **kwargs):
                self.b.save_screenshot('%s.png' % self.id())
                return super(MyFailureException, self_).__init__(*args, **kwargs)
        MyFailureException.__name__ = AssertionError.__name__
        return MyFailureException
    

    This seems incredibly hacky but it seems to work so far.

提交回复
热议问题