使用有意义的消息在测试失败时调用 API

Call an API on test failure with meaningful message

我的代码基于此:How to execute code only on test failures with python unittest2?

但是我想调用一个 API 来代替屏幕截图,以在测试失败时说出与断言相同的内容。我可以使用堆栈跟踪作为消息,这很好(如果可能)。我尝试使用回溯模块,但堆栈跟踪为空。

@property
def failureException(self):
    class MyFailureException(AssertionError):
        def __init__(self_, *args, **kwargs):
            test_id = os.path.basename(__file__).split('_')[1] # file has the id for the API
            client = getClient()                

            client.test_failed(test_id, comment=str('Failed'))
            return super(MyFailureException, self_).__init__(*args, **kwargs)

    MyFailureException.__name__ = AssertionError.__name__
    return MyFailureException

如果我使用 args,我得到:True is not False, for assertFalse。这没有帮助。

主要问题是您应该传递有意义的错误消息:

self.assertFalse(True)  # message == 'True is not False'
self.assertFalse(True, 'We expected False for variable X, but got True')