Python unittest assertCountEqual uses 'is' instead of '=='?

I am trying to use the python library unittestto write some unit tests. I have a function that returns an unordered list of objects. I want to verify that the objects are the same, and I'm trying to use assertCountEqual for this.

However, this seems to fail, even though the individual objects are equal ( ==) to each other. Here is the "diff" output from the failure of the statement:

First has 1, Second has 0:  Intersection(time=8.033252939677466e-08, del_time=8.033252939677466e-08, pos=Vector(10.00, 0.00), line=Line(Vector(500.00, 0.00), Vector(0.00, 0.00)), ent=None, oth=None, invalid=False)
First has 1, Second has 0:  Intersection(time=-9.918729244820295e-16, del_time=-9.918729244820295e-16, pos=Vector(10.00, 0.00), line=Line(Vector(500.00, 0.00), Vector(0.00, 0.00)), ent=None, oth=None, invalid=False)
First has 0, Second has 1:  Intersection(time=8.033252939677466e-08, del_time=8.033252939677466e-08, pos=Vector(10.00, 0.00), line=Line(Vector(500.00, 0.00), Vector(0.00, 0.00)), ent=None, oth=None, invalid=False)
First has 0, Second has 1:  Intersection(time=-9.918729244820295e-16, del_time=-9.918729244820295e-16, pos=Vector(10.00, 0.00), line=Line(Vector(500.00, 0.00), Vector(0.00, 0.00)), ent=None, oth=None, invalid=False)

Make sure they are equal:

>>> i = Intersection(time=8.033252939677466e-08, del_time=8.033252939677466e-08, pos=Vector(10.00, 0.00), line=Line(Vector(500.00, 0.00), Vector(0.00, 0.00)), ent=None, oth=None, invalid=False)
>>> j = Intersection(time=8.033252939677466e-08, del_time=8.033252939677466e-08, pos=Vector(10.00, 0.00), line=Line(Vector(500.00, 0.00), Vector(0.00, 0.00)), ent=None, oth=None, invalid=False)
>>> i == j
True
>>> i = Intersection(time=-9.918729244820295e-16, del_time=-9.918729244820295e-16, pos=Vector(10.00, 0.00), line=Line(Vector(500.00, 0.00), Vector(0.00, 0.00)), ent=None, oth=None, invalid=False)
>>> j = Intersection(time=-9.918729244820295e-16, del_time=-9.918729244820295e-16, pos=Vector(10.00, 0.00), line=Line(Vector(500.00, 0.00), Vector(0.00, 0.00)), ent=None, oth=None, invalid=False)
>>> i == j
True

My hunch is that the function assertCountEqualchecks if they have two identical identifiers (e.g. i is j), and not equality.

  • unittest, diff , , ?
  • - , assertCountEqual?

EDIT: python 3.2.2.

+3
3

:

Intersection , hashable per default, - ( , ), .

, Intersection -?

+4

assertCountEqual() collections.Counter, . Python 3, __eq__, __hash__ .

__eq__ - a __hash__ ( , __eq__ ), .

+1

( )

, TestCase

self.assertTrue(set(a) == set(b), 'The lists are not equal.')

set , . a , , , .

is, None,

In [2]: a = [0,1,2]

In [3]: b = [0,2,1,0]

In [4]: set(a) == set(b)
Out[4]: True

In [5]: c = [2,0,1]

In [6]: a.sort() == c.sort()
Out[6]: True

-

self.assertTrue(a==b)

def compare_complex(*args): 
  for attr in ...
    if getattr(args[0],attr) != getattr(args[1],attr): return False
  return True

- , Numpy

0

All Articles