我正在测试django应用程序中的视图。模型(用户、部门、报表等)之间存在大量的OneToMany和ManyToMany关系。在创建我根本不使用的fixture时,需要花费大量时间来填充某些字段,如姓名、出生日期等。我怎么能忽略它们呢?另外,创建一个fixture时的最佳实践是什么?我的是这样的
class TestReportModel(TestCase):
allow_database_queries = True
@classmethod
def setUpTestData(cls):
cls.report_id = 99
cls.factory = RequestFactory()
cls.user_with_access = User.objects.create(username="user1", password="password")
cls.employee = Employee.objects.create(user=cls.user_with_access, fio="name1 surname1",
date_of_birth="2012-12-12")
cls.indicator = Indicator.objects.create(context_id=10, set_id=10)
cls.ife = IndicatorsForEmployees.objects.create(employee=cls.employee, indicator=cls.indicator)
cls.report = Report.objects.create(owner=cls.ife)
cls.report.id = cls.report_id
cls.report.save()
cls.user_with_no_access = User.objects.create(username="user_with_no_access", password="password")
cls.employee_with_no_access = Employee.objects.create(user=cls.user_with_no_access, fio="name2 surname2",
date_of_birth="2018-12-12")发布于 2018-05-09 02:34:27
听起来您需要在设置文件中指定一个测试数据库,使用syncdb加载一个fixture,然后使用keepdb标志。
在设置文件中,可以在数据库中指定测试数据库名称。https://docs.djangoproject.com/en/2.0/ref/settings/#std:setting-DATABASE-TEST
如果未找到此数据库,则会在运行测试时创建该数据库。一旦创建了fixture,就可以使用syncdb加载该数据库。https://code.djangoproject.com/wiki/Fixtures#Fixtures
然后,当您运行单元测试时,将--keepdb与它一起传递,数据库将在两次测试之间持久化。https://docs.djangoproject.com/en/2.0/ref/django-admin/#cmdoption-test-keepdb
https://stackoverflow.com/questions/50240163
复制相似问题