【问题标题】:How do I get the user in Django test?如何在 Django 测试中获取用户?
【发布时间】:2012-03-27 22:20:08
【问题描述】:

我有一些外部服务。我的 Django 应用程序构建在我的外部服务 API 之上。为了与我的外部服务通信,我必须传入一个身份验证 cookie,我可以通过阅读 User(那个 cookie != django cookie)来获得它。

使用webtestsrequests 等测试工具,我无法编写测试。

class MyTestCase(WebTest):
    def test_my_view(self):
        #client = Client()
        #response = client.get(reverse('create')).form
        form = self.app.get(reverse('create'), user='dummy').form
        print form.fields.values()
        form['name'] = 'omghell0'
        print form

        response = form.submit()

我需要提交一个表单,它会在我的外部服务上创建一个用户。但要做到这一点,我通常会传入request.user(为了验证我对外部服务的权限)。但我没有request.user

我对这类东西有什么选择?

谢谢...


假设这是我的tests.py

import unittest
from django.test.client import Client
from django.core.urlresolvers import reverse
from django_webtest import WebTest
from django.contrib.auth.models import User


class SimpleTest(unittest.TestCase):
    def setUp(self):
        self.usr = User.objects.get(username='dummy')
        print self.usr
    .......

我明白了

Traceback (most recent call last):
  File "/var/lib/graphyte-webclient/webclient/apps/codebundles/tests.py", line 10, in setUp
    self.usr = User.objects.get(username='dummy')
  File "/var/lib/graphyte-webclient/graphyte-webenv/lib/python2.6/site-packages/django/db/models/manager.py", line 132, in get
    return self.get_query_set().get(*args, **kwargs)
  File "/var/lib/graphyte-webclient/graphyte-webenv/lib/python2.6/site-packages/django/db/models/query.py", line 341, in get
    % self.model._meta.object_name)
DoesNotExist: User matching query does not exist

但如果我在视图中测试User.objects,我就没事了。

【问题讨论】:

    标签: django webtest django-testing


    【解决方案1】:

    您需要使用 setUp() 方法来创建用于测试的测试用户 - 测试从不使用实时数据,而是创建一个临时测试数据库来运行您的单元测试。阅读本文了解更多信息:https://docs.djangoproject.com/en/dev/topics/testing/?from=olddocs#writing-unit-tests

    编辑:

    这是一个例子:

    from django.utils import unittest
    from django.contrib.auth.models import User
    
    from myapp.models import ThisModel, ThatModel
    
    class ModelTest(unittest.TestCase):
        def setUp(self):
            # Create some users
            self.user_1 = User.objects.create_user('Chevy Chase', 'chevy@chase.com', 'chevyspassword')
            self.user_2 = User.objects.create_user('Jim Carrey', 'jim@carrey.com', 'jimspassword')
            self.user_3 = User.objects.create_user('Dennis Leary', 'dennis@leary.com', 'denisspassword')
    

    另外请注意,如果您要使用多个方法来测试不同的功能,您应该使用 tearDown 方法来销毁对象,然后再重新实例化它们以进行下一次测试。这是我花了一段时间才弄清楚的事情,所以我会为你省去麻烦。

    def tearDown(self):
            # Clean up after each test
            self.user_1.delete()
            self.user_2.delete()
            self.user_3.delete()
    

    【讨论】:

    【解决方案2】:

    Django 建议使用单元测试或文档测试,如here 所述。你可以把这些测试放到每个apps目录下的tests.py中,当使用`python manage.py test"命令时它们就会运行。

    Django 为单元测试提供了非常有用的类和函数,如here 所述。特别是django.test.Client这个类非常方便,可以让你像用户一样控制东西。

    【讨论】:

    • 谢谢。我想我应该继续在这里吗?我测试了这个User.objects.get(username='dummy'),在我看来会给我回dummy。但是如果我把它放在我的测试中,我会收到一个错误DoesNotExist: User matching query does not exist. 这是为什么?
    • 在您的单元测试中,您的数据库一开始都是完全空的(因此它们的行为是可预测的,因此不会影响您的真实数据库)。查找夹具 - 您可以将 SQL 转储到 .json 文件中,并在测试之前将测试负载作为其数据库
    【解决方案3】:

    https://docs.djangoproject.com/en/1.4/topics/testing/#module-django.test.client

    使用 django 测试客户端来模拟请求。如果您需要测试返回结果的行为,请使用 Selenium。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-12-20
      • 2018-03-02
      • 1970-01-01
      • 2017-04-10
      • 2012-05-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多