[issue17459] unittest.assertItemsEqual reports wrong order

Matthijs van der Vleuten report at bugs.python.org
Mon Mar 18 14:48:28 CET 2013


New submission from Matthijs van der Vleuten:

When calling assertItemsEqual(first,second), if the items in a and b differ, the AssertionError treats second as the first sequence and first as the second sequence.

Repro code:

>>> from unittest.case import TestCase
>>> class Foo(TestCase):
...     def runTest(self):
...             self.assertItemsEqual([1],[])
... 
>>> Foo().runTest()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 3, in runTest
  File "/usr/lib/python2.7/unittest/case.py", line 899, in assertItemsEqual
    self.fail(msg)
  File "/usr/lib/python2.7/unittest/case.py", line 408, in fail
    raise self.failureException(msg)
AssertionError: Element counts were not equal:
First has 0, Second has 1:  1

This happens because of this code in unittest/case.py:

def assertItemsEqual(self, expected_seq, actual_seq, msg=None):
    # (skip docstring)
    first_seq, second_seq = list(actual_seq), list(expected_seq)

list(actual_seq) is assigned to first_seq, even though it's actually the second argument.

This would be fixed either by swapping expected_seq and actual_seq in the function's arguments, or swapping the assignment of first_seq and second_seq.

----------
components: Library (Lib)
messages: 184452
nosy: Zr40
priority: normal
severity: normal
status: open
title: unittest.assertItemsEqual reports wrong order
type: behavior
versions: Python 2.7

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue17459>
_______________________________________


More information about the Python-bugs-list mailing list