Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I am trying to use python's unittest library to 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 am trying to use assertCountEqual to do this.

However, this seems to be failing, despite the individual objects being equal (==) to each other. Here is the 'diff' output from the assertion failure:

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)

Verifying that 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 guess is that the assertCountEqual function is checking if the two have the same identity (e.g. i is j), rather than equality.

  • Is there a unittest function that will provide the same diff capabilities, but use equality comparison, rather than identity?
  • Alternatively, is there some way I can write a function that performs similarly to assertCountEqual?

EDIT: I am running python 3.2.2.

like image 998
Casey Kuball Avatar asked Aug 16 '26 06:08

Casey Kuball


1 Answers

you can look for yourself how the comparison is done:

  • generate a list from each iterable
  • use a collections.Counter to count the objects - works only for hashable elements
  • if the elements are not hashable, compare them directly

as your Intersections are objects, they are hashable per default, but if you don't provide a suitable hash function (which you should do if you provide comparison methods) they will be considered different.

so, does your Intersection class fullfill the hash contract?

like image 144
mata Avatar answered Aug 18 '26 20:08

mata



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!