比较两个容器以确定其内容物的身份

发布于 2024-10-06 23:41:10 字数 852 浏览 0 评论 0原文

我有一个返回一组对象的方法,并且我正在为此方法编写单元测试。是否有一种通用的、整洁的和惯用的方法来比较它们的同一性(而不是平等)?还是我需要自己写一个合适的实现?

一个例子(为了简单起见):

class Foo(object):
    def has_some_property(self):
        ...

class Container(object):
    def __init__(self):
        self.foo_set = set()

    def add_foo(self, foo):
        self.foo_set.add(foo)

    def foo_objects_that_have_property(self):
        return set([foo for foo in self.foo_set if foo.has_some_property()])

import unittest

class TestCase(unittest.TestCase):
    def testFoo(self):
        c = Container()
        x, y, z = Foo(), Foo(), Foo()
        ...
        self.assertContentIdentity(c.foo_objects_that_have_property(), set([x, y]))

重要的是,在这里测试相等性是行不通的,因为改变 foo_objects_that_have_property() 返回的对象可能会导致不一致的结果,具体取决于这些对象的使用方式即使它们在测试时“相等”,在 Container 中也会有所不同。

I have a method that returns a set of objects, and I'm writing a unit test for this method. Is there a generic, tidy and idiomatic way of comparing these for identity (rather than equality)? Or do I need to write a suitable implementation myself?

An example (somewhat contrived to keep it simple):

class Foo(object):
    def has_some_property(self):
        ...

class Container(object):
    def __init__(self):
        self.foo_set = set()

    def add_foo(self, foo):
        self.foo_set.add(foo)

    def foo_objects_that_have_property(self):
        return set([foo for foo in self.foo_set if foo.has_some_property()])

import unittest

class TestCase(unittest.TestCase):
    def testFoo(self):
        c = Container()
        x, y, z = Foo(), Foo(), Foo()
        ...
        self.assertContentIdentity(c.foo_objects_that_have_property(), set([x, y]))

Importantly, testing here for equality won't do, since mutating the objects returned by foo_objects_that_have_property() may lead to inconsistent results depending on how those objects are used differently in Container even if they are "equal" at the time of the test.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

清风挽心 2024-10-13 23:41:10

我能想到的最好的办法是:

@staticmethod
def set_id(c):
    return set([id(e) for e in c])

def assertContentIdentity(self, a, b):
    self.assertEqual(set_id(a), set_id(b))

然而,这是专门用于集合的,不能处理嵌套容器。

The best I can come up with is:

@staticmethod
def set_id(c):
    return set([id(e) for e in c])

def assertContentIdentity(self, a, b):
    self.assertEqual(set_id(a), set_id(b))

However, this is specialised for sets and can't deal with nested containers.

素衣风尘叹 2024-10-13 23:41:10

一个简单但不是最有效的方法:

def assertContentIdentity(set1, set2):
    set1 = set([id(a) for a in set1])
    set2 = set([id(a) for a in set2])
    assert set1 == set2

A simple, albeit not the most efficient, way to do it:

def assertContentIdentity(set1, set2):
    set1 = set([id(a) for a in set1])
    set2 = set([id(a) for a in set2])
    assert set1 == set2
后知后觉 2024-10-13 23:41:10

x is y 在这里不起作用
会告诉我这些集合是
不同,我已经知道了。我
想知道他们是否有对象
包含相同的对象或
不同的对象。

然后你需要编写自己的函数,例如

set([id(x) for x in X]) == set([id(y) for y in Y])

x is y won't work here since that
would tell me that the sets are
different, which I know already. I
want to know if the objects that they
contain are the same objects or
different objects.

Then you need to write your own function, like

set([id(x) for x in X]) == set([id(y) for y in Y])
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文