python unittest assertCountEqual 使用“is"而不是“=="? [英] python unittest assertCountEqual uses 'is' instead of '=='?
问题描述
我正在尝试使用 python 的 unittest
库来编写一些单元测试.我有一个函数返回一个无序列表的对象.我想验证对象是否相同,并且我正在尝试使用 assertCountEqual 来做到这一点.
然而,尽管各个对象彼此相等 (==
),但这似乎失败了.这是断言失败的差异"输出:
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),5000.向量(0.00, 0.00)), ent=None, oth=None, invalid=False)第一个有 1,第二个有 0: Intersection(time=-9.918729244820295e-16, del_time=-9.918729244820295e-16, pos=Vector(10.00, 0.00), line=Line(Vector, Vector, 0.000)0.00)), ent=None, oth=None, invalid=False)第一个有 0,第二个有 1: Intersection(time=8.033252939677466e-08, del_time=8.033252939677466e-08, pos=Vector(10.00, 0.00), line=Line(Vector(500.000),(500.00), 0 Vector), ent=None, oth=None, invalid=False)第一个有 0,第二个有 1: Intersection(time=-9.918729244820295e-16, del_time=-9.918729244820295e-16, pos=Vector(10.00, 0.00), line=Line(Vector, Vector(500.000).000)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), N), Vector(0.00), Vector(0.00),其他=无,无效=假)>>>j = Intersection(time=8.033252939677466e-08, del_time=8.033252939677466e-08, pos=Vector(10.00, 0.00), line=Line(Vector(500.00, 0.00), N), Vector(0.00), Vector(0.00),其他=无,无效=假)>>>我==j真的>>>i = Intersection(time=-9.918729244820295e-16, del_time=-9.918729244820295e-16, pos=Vector(10.00, 0.00), line=Line(Vector(500.00), 0.0.0), Vector无,其他=无,无效=假)>>>j = Intersection(time=-9.918729244820295e-16, del_time=-9.918729244820295e-16, pos=Vector(10.00, 0.00), line=Line(Vector(500.00), 0.0.0), Vector无,其他=无,无效=假)>>>我==j真的我的猜测是 assertCountEqual
函数正在检查两者是否具有相同的身份(例如 i is j
),而不是相等.
- 是否有提供相同差异的单元测试功能能力,但使用相等比较而不是身份?
- 或者,有什么方法可以编写一个函数来执行类似于
assertCountEqual
?
编辑:我正在运行 python 3.2.2.
你可以自己找比较是如何完成的:
- 从每个可迭代对象生成一个列表
- 使用 collections.Counter 来计算对象 - 有效仅适用于可散列元素
- 如果元素不可散列,比较它们直接
因为您的 Intersection
是对象,所以它们是 hashable 每个 default,但如果你不提供合适的哈希函数(如果你提供比较方法,你应该这样做) 他们将被视为不同.
那么,你的 Intersection
类是否满足了哈希合约?
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.
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 Intersection
s 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?
这篇关于python unittest assertCountEqual 使用“is"而不是“=="?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!