【问题标题】:How to initialize test database in django?如何在 django 中初始化测试数据库?
【发布时间】:2015-12-17 16:11:24
【问题描述】:

我正在为我用 django 编写的项目编写测试用例,它给出了一个意想不到的输出,看起来像 {u'message': u'', u'result': {u'username': u'john', u'user_fname': u'', u'user_lname': u'', u'cur_time': 1442808291000.0, u'dofb': None, u'sex': u'M', u'u_email': u'', u'role': u'', u'session_key': u'xxhhxhhhx', u'mobile': None}, u'error': 0} 在这里我们可以看到其他字段是空的,因为我只是在测试用例中创建了用户,但没有给出其他信息。数据库是从生产数据库创建的,但未初始化,它保持为空。 这就是为什么它让其他字段为空。它正在查询空数据库。

我为 login REST API 编写了以下测试用例。并通过 python manage.py test 运行它。请告诉我如何解决上述问题。

注意:如果以下方法不正确,您可以建议其他方法。

from django.test import TestCase
from django.test.client import Client
from django.contrib.auth.models import User
import json

class TestAPI(TestCase):

      def setUp(self):
            self.c=Client() #Create Client object that simulates request to a url similar to a browser can
            User.objects.create_user(username="john", password="xxx")

      def test_login_api(self):
            credential_test=dict()
            c_test =Client()

            credential_test["username"]="john"
            credential_test["password"]="xxx"
            data=json.dumps(credential_test)
            #print 'data is'
            #print data
            response_test =c_test.put('/api/login', data)
            content_test=json.loads(response_test.content)
            print 'content'

【问题讨论】:

  • Django 测试客户端需要一个字典作为 postdata,而不是字典的 json 转储。为什么您使用 PUT 而不是 POST 进行身份验证? put i 通常用于文件上传

标签: python django django-testing django-tests


【解决方案1】:

尝试改变它:

User.objects.create(username="john", password="xxx")

到:

User.objects.create_user(username='john', password='xxx')

方法create_user使用set_password方法。

class UserManager(models.Manager):
    # ...   
    def create_user(self, username, email=None, password=None):
        """
        Creates and saves a User with the given username, email and password.
        """
        now = timezone.now()
        if not username:
            raise ValueError('The given username must be set')
        email = UserManager.normalize_email(email)
        user = self.model(username=username, email=email,
                          is_staff=False, is_active=True, is_superuser=False,
                          last_login=now, date_joined=now)

        user.set_password(password)
        user.save(using=self._db)
        return user

【讨论】:

  • 感谢它的工作,但作为响应,其他详细信息为空,例如 user_first name 和 user_last name,这就是我尝试使用生产数据库初始化的原因..
  • 你是如何试图抓住他们的,在哪里?
  • 此用户表与其他表相关,因此它将为空,这就是我想用生产数据库初始化测试数据库的原因。
  • 我认为这可以帮助你,stackoverflow.com/questions/1646468/…
【解决方案2】:

两种方法:

  1. 扩展您对 setUp() 的使用,为其他模型创建记录,并在您创建的模型之间建立一组有效的关系。这是通过代码配置的方法。
  2. 使用固定装置预填充您的测试数据库。如果您进行一些研究,您可以了解如何使用现有的有效数据库创建一些固定装置。但是,我建议您清理用于测试的任何生产数据。也就是通过数据进行配置的方法。

【讨论】:

  • 我目前的方法测试很慢所以我想使用 sqlite3 来加速所以请告诉我如何使用生产数据库初始化 sqlite3...
  • @geeks 这听起来像是一个不同的问题。这个问题的答案还不错:stackoverflow.com/questions/4606756/…
猜你喜欢
  • 1970-01-01
  • 2013-08-05
  • 2011-07-27
  • 2019-02-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-10
  • 1970-01-01
相关资源
最近更新 更多