1

I have the following test method

def test_fingerprintBadFormat(self):
    """
    A C{BadFingerPrintFormat} error is raised when unsupported
    formats are requested.
    """
    with self.assertRaises(keys.BadFingerPrintFormat) as em:
        keys.Key(self.rsaObj).fingerprint('sha256-base')
    self.assertEqual('Unsupported fingerprint format: sha256-base',
        em.exception.message)

Here is the exception class.

class BadFingerPrintFormat(Exception):
    """
    Raises when unsupported fingerprint formats are presented to fingerprint.
    """

this test method works perfectly fine in Python2 but fails in python 3 with the following message

builtins.AttributeError: 'BadFingerPrintFormat' object has no attribute 'message'

How can I test the error message in Python3. I don't like the idea of using asserRaisesRegex as it tests the regex rather than the exception message.

2
  • Can you shed some light on BadFingerPrintFormat? Commented Aug 16, 2016 at 13:04
  • @LajosArpad I edited the question. Commented Aug 16, 2016 at 13:12

1 Answer 1

3

The .message attribute was removed from exceptions in Python 3. Use .args[0] instead:

self.assertEqual('Unsupported fingerprint format: sha256-base',
    em.exception.args[0])

or use str(em.exception) to get the same value:

self.assertEqual('Unsupported fingerprint format: sha256-base',
    str(em.exception))

This will work both on Python 2 and 3:

>>> class BadFingerPrintFormat(Exception):
...     """
...     Raises when unsupported fingerprint formats are presented to fingerprint.
...     """
...
>>> exception = BadFingerPrintFormat('Unsupported fingerprint format: sha256-base')
>>> exception.args
('Unsupported fingerprint format: sha256-base',)
>>> exception.args[0]
'Unsupported fingerprint format: sha256-base'
>>> str(exception)
'Unsupported fingerprint format: sha256-base'
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.