【发布时间】:2021-06-28 11:58:11
【问题描述】:
如您所知,django 在测试中为您提供了清晰的数据库,但我有一个 ready() 方法为我创建了一些数据,我需要在测试中查询这些数据。
class YourAppConfig(AppConfig):
default_auto_field = 'django.db.models.AutoField'
name = 'Functions.MyAppsConfig'
def ready(self):
from django.contrib.auth.models import Permission
from django import apps
from django.contrib.contenttypes.models import ContentType
try:
Permission.objects.get_or_create(....)
MyOtherModel.objects.get_or_create(....)
except:
pass
class TestRules(APITestCase):
def test_my_model(self):
....
x = MyOtherModel.objects.filter(....).first()
# x = None # <=========== problem is here ========= I need to see the data that I created in the ready method
....
【问题讨论】:
-
你需要
setup- docs.djangoproject.com/en/3.2/topics/testing/overview/…。还有setupclass -
看看这个答案 我刚刚添加到另一个问题中,即在测试期间创建数据; stackoverflow.com/a/68163463/1199464
标签: django