Skip to content

Instantly share code, notes, and snippets.

@jamescooke
Last active September 27, 2015 11:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamescooke/b54b18eb74eb88fbcf1c to your computer and use it in GitHub Desktop.
Save jamescooke/b54b18eb74eb88fbcf1c to your computer and use it in GitHub Desktop.
PyConUK sprint on improving output from unittest when `assertEqual` finds non-matching values.

Unittest documentation improvements

http://www.pyconuk.org/sprints/unit-test-documentation-improvements/

We explored where the "Expected: 1, actual: 2" messages (used to) come from in unittest. We found that this code has already been improved back in 2011. Why were we still getting old output for so long?

Below are links to our findings, plus you can see the test cases we built and tested on various versions of Python.

Key BDFL 'ruling' ----------------

https://mail.python.org/pipermail/python-dev/2010-December/106954.html

... the least bad thing would be to drop any remnants of expected/actual terminology, keep the diffs in the first-second order, and let developers choose whether they put the expected value first or second.

Main issue

F
======================================================================
FAIL: test_eq_vars (__main__.TestExample)
----------------------------------------------------------------------
Traceback (most recent call last):
File "test_example.py", line 13, in test_eq_vars
self.assertEqual(apple, pear)
AssertionError: 1 != {}
----------------------------------------------------------------------
Ran 1 test in 0.001s
FAILED (failures=1)
F
======================================================================
FAIL: test_eq_vars (__main__.TestExample)
----------------------------------------------------------------------
Traceback (most recent call last):
File "test_example.py", line 13, in test_eq_vars
self.assertEqual(apple, pear)
AssertionError: 1 != {}
----------------------------------------------------------------------
Ran 1 test in 0.000s
FAILED (failures=1)
F
======================================================================
FAIL: test_eq_vars (__main__.TestExample)
----------------------------------------------------------------------
Traceback (most recent call last):
File "test_example.py", line 13, in test_eq_vars
self.assertEqual(apple, pear)
AssertionError: 1 != {}
----------------------------------------------------------------------
Ran 1 test in 0.000s
FAILED (failures=1)
try:
import unittest2 as unittest
except ImportError:
import unittest
class TestExample(unittest.TestCase):
def test_eq_vars(self):
apple = 1
pear = {}
self.assertEqual(apple, pear)
if __name__ == '__main__':
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment