Getting Python's unittest results in a tearDown() method

前端 未结 13 1341
野的像风
野的像风 2020-11-28 22:40

Is it possible to get the results of a test (i.e. whether all assertions have passed) in a tearDown() method? I\'m running Selenium scripts, and I\'d like to do some reporti

13条回答
  •  执笔经年
    2020-11-28 22:54

    If you are using Python2 you can use the method _resultForDoCleanups. This method return a TextTestResult object:

    You can use this object to check the result of your tests:

    def tearDown(self):
        if self._resultForDoCleanups.failures:
            ...
        elif self._resultForDoCleanups.errors:
            ...
        else:
            #Success
    

    If you are using Python3 you can use _outcomeForDoCleanups:

    def tearDown(self):
        if not self._outcomeForDoCleanups.success:
            ...
    

提交回复
热议问题