Django 单元测试约束

Posted

技术标签:

【中文标题】Django 单元测试约束【英文标题】:Django unit test constraints 【发布时间】:2021-04-08 07:05:11 【问题描述】:

所以我在为 Check 和 Unique 约束编写单元测试时遇到了问题。以下是它们的定义:

# assert that a partner can have only one headquarter
constraints = [
   models.UniqueConstraint(
       fields=['partner', 'active'],
       condition=models.Q(headquarter=True),
       name='HQ already set.'
     )
]


# assert every partner contant has at least one of email/phone number pair defined
constraints = [            
   models.CheckConstraint(
       check= (
                 models.Q(email__isnull=False) &
                 models.Q(phone_number__isnull=False)
              ),
       name='E-mail or phone number should be set.'
   )
]

单元测试断言哪个失败:

from django.db.utils import IntegrityError
....
# HQ already defined so it should work
with self.assertRaises(Exception) as raised:
    partnerHQCopy.save()

self.assertEqual(IntegrityError, type(raised.exception))

.....
# The contact added has both email and phone number None
with self.assertRaises(Exception) as raised:
    partnerContact.save()

self.assertEqual(IntegrityError, type(raised.exception))

还有回溯:

======================================================================
ERROR: test_unique_hq_for_partner (partners.tests.PartnerModelsTestCases)
----------------------------------------------------------------------

.....

sqlite3.IntegrityError: UNIQUE constraint failed: partners_partnerbranch.partner_id, partners_partnerbranch.active

.....

django.db.utils.IntegrityError: UNIQUE constraint failed: partners_partnerbranch.partner_id, partners_partnerbranch.active

======================================================================
ERROR: test_partner_contact_no_email_no_phone (partners.tests.PartnerModelsTestCases)
----------------------------------------------------------------------
.....

sqlite3.IntegrityError: CHECK constraint failed: E-mail or phone number should be set.

.....

django.db.utils.IntegrityError: CHECK constraint failed: E-mail or phone number should be set.

【问题讨论】:

【参考方案1】:

这些测试失败的原因是它引发了sqlite3.IntegrityError,而不是django.db.utils.IntegrityError,因此您应该断言正确的错误类型:

from sqlite3 import IntegrityError

# …

# HQ already defined so it should work
with self.assertRaises(IntegrityError):
    partnerHQCopy.save()

# …

# The contact added has both email and phone number None
with self.assertRaises(IntegrityError):
    partnerContact.save()

【讨论】:

以上是关于Django 单元测试约束的主要内容,如果未能解决你的问题,请参考以下文章

Django单元测试测试视图问题,怎么解决

以编程方式测试视图约束(在单元测试中)

Sqlite 在单元测试中忽略数据库约束

对自定义 symfony 约束进行单元测试

Django 单元测试 查看覆盖率

Django中的单元测试以及Python单元测试