python unittest assertCountEqual使用'is'而不是'=='?

时间:2012-05-04 20:14:04

标签: python unit-testing python-3.x

我正在尝试使用python的unittest库来编写一些单元测试。我有一个函数返回一个无序的对象列表。我想验证对象是否相同,我正在尝试使用assertCountEqual来执行此操作。

然而,尽管各个对象彼此相等(==),但这似乎失败了。这是断言失败的'diff'输出:

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)

确认它们是平等的:

>>> 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

我的猜测是assertCountEqual函数正在检查两者是否具有相同的身份(例如i is j),而不是相等。

  • 是否有一个unittest函数可以提供相同的差异 能力,但使用平等比较,而不是身份?
  • 或者,有什么方法可以编写一个执行的功能 类似于assertCountEqual

编辑:我正在运行python 3.2.2。

3 个答案:

答案 0 :(得分:4)

你可以找自己how the comparison is done

因为您的Intersection是对象,它们hashabledefault,但是如果您没有提供合适的哈希函数(如果您{{3}则应该这样做})他们将被视为不同。

那么,你的Intersection类是否填满了哈希合约?

答案 1 :(得分:1)

如果您的元素是可清除的,则

assertCountEqual()使用collections.Counter。在Python 3中,如果您的类定义了自己的__eq__,则默认__hash__被抑制。

您拥有自己的__eq__ - 定义__hash__(在__eq__相等的位置必须相等),您应该没问题。

答案 2 :(得分:0)

使用无序列表时,我通常使用此模式(如果可以)

在扩展TestCase

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

我在这种情况下使用set,因为它允许比较无序组但是如果有两个对象相同,比较应该失败但在这种情况下不会你需要对两个列表进行排序然后进行比较。

我尝试远离is,除非将其与None进行比较,因为它依赖于实例like this

这是一个例子

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实例

时,我使用过去类似的东西
相关问题