【发布时间】:2021-02-24 11:40:28
【问题描述】:
我在测试中预定义了几个字典。 例如。
TIME_DISCOUNT_OFFER = dict(
time_discount=dict(
discount_days=5,
discount_percent=15
)
)
这个 dict 进入我的模型(JSONfield),然后在我的测试中,我从中获取数据。当我命名 test test_add_the_rest_of_name django 检测预定义的字典时,但是当我命名 test 时,例如。 test_time_sthdjango 给我输出
discount = TIME_DISCOUNT_OFFER.get("time_discount").get("discount_percent")
AttributeError: 'NoneType' object has no attribute 'get'
我注意到,在第二种情况下(test_time_sth),根据 django,我的预定义 dict 是空的,无论我是直接从 dict 还是从模型实例获取数据。如果我以不同的方式命名测试,则测试通过。有人知道为什么会这样吗?
小例子:
型号:
from django.contrib.postgres.fields import JSONField
class Offer(models.Model):
details = JSONField()
测试:
TIME_DISCOUNT_OFFER = dict(
time_discount=dict(
discount_days=10,
discount_percent=25
)
)
class OfferTest(TestCase):
@classmethod
def setUpTestData(cls):
cls.offer = Offer.objects.create(details=TIME_DISCOUNT_OFFER)
def test_special_offer_removed(self):
self.offer.details.pop('time_discount', None)
def test_timeframe_offer(self):
discount = TIME_DISCOUNT_OFFER.get("time_discount").get("discount_percent")
def test_add_offer(self):
discount = TIME_DISCOUNT_OFFER.get("time_discount").get("discount_percent")
追溯:
..E
======================================================================
ERROR: test_timeframe_offer (offers.tests.test_offer.OfferTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/home/admin1/Dokumenty/project/offers/tests/test_offer.py", line 142, in test_timeframe_offer
discount = TIME_DISCOUNT_OFFER.get("time_discount").get("discount_percent")
AttributeError: 'NoneType' object has no attribute 'get'
----------------------------------------------------------------------
Ran 3 tests in 0.006s
FAILED (errors=1)
【问题讨论】:
-
请创建一个可重现的最小示例!
-
添加了最小的可重现示例
标签: python django unit-testing