1

I based my code around this : How to execute code only on test failures with python unittest2?

But instead of a screenshot I want to call an API to say the same thing as the assert when a test fail. I can use the stacktrace as a message it's fine (if possible). I tried with traceback module but the stacktrace was empty.

@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

if I use the args, I get : True is not False, for assertFalse. Which is not helpful.

Community
  • 1
  • 1
Lorac
  • 395
  • 1
  • 4
  • 14

1 Answers1

0

The primary problem is you should pass in a meaningful error message:

self.assertFalse(True)  # message == 'True is not False'
self.assertFalse(True, 'We expected False for variable X, but got True')
Hai Vu
  • 37,849
  • 11
  • 66
  • 93