lundi 1 février 2016

When unit testing in Python, how can I display my custom message together with the default message on failure?

Consider the following example:

import unittest

class MessageExampleTest(unittest.TestCase):
    def test_with_default_message(self):
        self.assertEqual('apples', 'oranges')

    def test_with_custom_message(self):
        self.assertEqual('apples', 'oranges', 'You should not compare apples and oranges.')

Output:

======================================================================
FAIL: test_with_default_message (assert_message.MessageExampleTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  ...
    self.assertEqual('apples', 'oranges')
AssertionError: 'apples' != 'oranges'

======================================================================
FAIL: test_with_custom_message (assert_message.MessageExampleTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  ...
    self.assertEqual('apples', 'oranges', 'You should not compare apples and oranges.')
AssertionError: You should not compare apples and oranges.


----------------------------------------------------------------------

What I'd like to see, in the second case, is something like:

AssertionError: 'apples' != 'oranges'; You should not compare apples and oranges.

Aucun commentaire:

Enregistrer un commentaire