断言django测试中两个对象列表相同

时间:2022-12-10 11:34:47

Is there a way to check that two lists of objects are equal in django tests.

有没有办法在django测试中检查两个对象列表是否相等。

lets say I have some model:

让我说我有一些模型:

class Tag(models.Model):
    slug = models.SlugField(max_length=50, unique=True)
    def __unicode__(self):
        return self.slug

and I run this simple test:

我运行这个简单的测试:

def test_equal_list_fail(self):
    tag_list = []
    for tag in ['a', 'b', 'c']:
        tag_list.append(Tag.objects.create(slug=tag))

    tags = Tag.objects.all()

    self.assertEqual(tag_list, tags)

this fails with:

这失败了:

======================================================================
FAIL: test_equal_list_fail (userAccount.tests.ProfileTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/tests.py", line 155, in test_equal_list_fail
    self.assertEqual(tag_list, tags)
AssertionError: [<Tag: a>, <Tag: b>, <Tag: c>] != [<Tag: a>, <Tag: b>, <Tag: c>]

----------------------------------------------------------------------

this will work:

这将工作:

def test_equal_list_passes(self):
    tag_list = []
    for tag in ['a', 'b', 'c']:
        tag_list.append(Tag.objects.create(slug=tag))

    tags = Tag.objects.all()

    for tag_set in zip(tags, tag_list):
        self.assertEqual(*tag_set)

However, This fails:

但是,这失败了:

def test_equal_list_fail(self):
    tag_list = []
    for tag in ['a', 'b', 'c']:
        tag_list.append(Tag.objects.create(slug=tag))

    tags = Tag.objects.all()

    for tag_set in zip(tags, tag_list):
        print "\n"
        print tag_set[0].slug + "'s pk is %s" % tag_set[0].pk
        print tag_set[1].slug + "'s pk is %s" % tag_set[1].pk
        print "\n"
        self.assertIs(*tag_set)

with:

有:

Creating test database for alias 'default'...
.......

a's pk is 1
a's pk is 1

F.
======================================================================
FAIL: test_equal_list_fail (userAccount.tests.ProfileTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "path/to/tests.py", line 160, in test_equal_list_fail
    self.assertIs(*tag_set)
AssertionError: <Tag: a> is not <Tag: a>

Is this expected behavior?

这是预期的行为吗?

Edit in response to comment
This type of comparison works:

编辑以回应评论此类比较有效:

class Obj:
    def __init__(self, x):
        self.x = x

>>> one = Obj(1)
>>> two = Obj(2)
>>> a = [one, two]
>>> b = [one, two]
>>> a == b
True

Why is the test failing for the other arrays?

为什么其他阵列的测试失败?

2 个解决方案

#1


9  

To test two lists

use: assertSequenceEqual

use:assertSequenceEqual

Because, in this case, tags = Tag.objects.all() generates a django.db.models.query.QuerySet where as tag_list.append(...) creates a list.

因为,在这种情况下,tags = Tag.objects.all()生成一个django.db.models.query.QuerySet,其中tag_list.append(...)创建一个列表。

Other options in different situations are:

不同情况下的其他选择是:

  • assertListEqual(a, b)
  • assertListEqual(a,b)
  • assertTupleEqual(a, b)
  • assertTupleEqual(a,b)
  • assertSetEqual(a, b)
  • assertSetEqual(a,b)
  • assertDictEqual(a, b)
  • assertDictEqual(a,b)

Why <Tag: a> is not <Tag: a>

The tags are the same model, but they've been loaded into different places in memory

标签是相同的模型,但它们已被加载到内存中的不同位置

for tag_set in zip(tags, tag_list):
    print "\n"
    print tag_set[0].slug + "'s pk is %s" % tag_set[0].pk + ' id is: ' + id(tag_set[0])
    print tag_set[1].slug + "'s pk is %s" % tag_set[1].pk + ' id is: ' + id(tag_set[1])
    print "\n"
    self.assertIs(*tag_set)

returns

回报

.......

a's pk is 1 id is: 4522000208
a's pk is 1 id is: 4522228112

F.

Therefore, is will retrun False

因此,将是后退假

#2


2  

I think what you want to test is if the tags created have the same slugs as those in your test list.

我想你要测试的是,如果创建的标签与测试列表中的标签具有相同的标记。

For that, fetch only the slug as a list with values_list, and then compare that:

为此,只将slug作为带有values_list的列表获取,然后比较:

assertEqual(Tag.objects.values_list('slug', flat=True), ['a','b','c'])

I have to say, this isn't quite a useful test because you are checking django orm functionality, which has already been tested quite well.

我不得不说,这不是一个非常有用的测试,因为你正在检查django orm功能,它已经经过了相当好的测试。

Your tests should check for specifics of your own application.

您的测试应检查您自己的应用程序的细节。

#1


9  

To test two lists

use: assertSequenceEqual

use:assertSequenceEqual

Because, in this case, tags = Tag.objects.all() generates a django.db.models.query.QuerySet where as tag_list.append(...) creates a list.

因为,在这种情况下,tags = Tag.objects.all()生成一个django.db.models.query.QuerySet,其中tag_list.append(...)创建一个列表。

Other options in different situations are:

不同情况下的其他选择是:

  • assertListEqual(a, b)
  • assertListEqual(a,b)
  • assertTupleEqual(a, b)
  • assertTupleEqual(a,b)
  • assertSetEqual(a, b)
  • assertSetEqual(a,b)
  • assertDictEqual(a, b)
  • assertDictEqual(a,b)

Why <Tag: a> is not <Tag: a>

The tags are the same model, but they've been loaded into different places in memory

标签是相同的模型,但它们已被加载到内存中的不同位置

for tag_set in zip(tags, tag_list):
    print "\n"
    print tag_set[0].slug + "'s pk is %s" % tag_set[0].pk + ' id is: ' + id(tag_set[0])
    print tag_set[1].slug + "'s pk is %s" % tag_set[1].pk + ' id is: ' + id(tag_set[1])
    print "\n"
    self.assertIs(*tag_set)

returns

回报

.......

a's pk is 1 id is: 4522000208
a's pk is 1 id is: 4522228112

F.

Therefore, is will retrun False

因此,将是后退假

#2


2  

I think what you want to test is if the tags created have the same slugs as those in your test list.

我想你要测试的是,如果创建的标签与测试列表中的标签具有相同的标记。

For that, fetch only the slug as a list with values_list, and then compare that:

为此,只将slug作为带有values_list的列表获取,然后比较:

assertEqual(Tag.objects.values_list('slug', flat=True), ['a','b','c'])

I have to say, this isn't quite a useful test because you are checking django orm functionality, which has already been tested quite well.

我不得不说,这不是一个非常有用的测试,因为你正在检查django orm功能,它已经经过了相当好的测试。

Your tests should check for specifics of your own application.

您的测试应检查您自己的应用程序的细节。