简体   繁体   English

如何在Django中进行测试

[英]How do test in Django

I'm trying to do my first tests on Django and I don't know do it or after reading the docs (where it explains a very easy test) I still don't know how do it. 我正在尝试在Django上进行我的第一个测试,但我不知道这样做,或者在阅读了文档(它解释了一个非常简单的测试)之后,我仍然不知道如何做。

I'm trying to do a test that goes to "login" url and makes the login, and after a succesfull login redirects to the authorized page. 我正在尝试进行一个测试,以转到“登录” URL并进行登录,并在成功登录后将重定向到授权页面。

from unittest import TestCase

from django.test.client import Client


class Test(TestCase):
    def testLogin(self):
        client = Client()
        headers = {'X-OpenAM-Username': 'user', 'X-OpenAM-Password': 'password', 'Content-Type': 'application/json'}
        data = {}
        response = client.post('/login/', headers=headers, data=data, secure=False)
        assert(response.status_code == 200)

And the test success, but I don't know if it's beacuse the 200 of loading "/login/" or because the test do the login and after redirect get the 200 code. 测试成功,但是我不知道它是因为200加载了“ / login /”还是因为测试执行登录并在重定向后获得200代码。

How can I check on the test that after the login the url redirected it's the correct? 我如何检查登录后重定向的URL是否正确的测试? There is a plugin or something that helps with the test? 是否有插件或有助于测试的工具? Or where I can find a good tutorial to test my views and the model? 或者在哪里可以找到好的教程来测试我的观点和模型?

Thanks and regards. 谢谢并恭祝安康。

Django have plenty of tools for testing. Django有很多测试工具。 For this task, you should use test case class from Django, for example django.test.TestCase . 对于此任务,您应该使用Django的测试用例类,例如django.test.TestCase Then you can use method assertRedirects() and it will check where you've been redirected and with which code. 然后,您可以使用assertRedirects()方法,它将检查您被重定向到的位置以及使用的代码。 You can find any info you need here . 您可以在此处找到所需的任何信息。 I've tried to write the code for your task: 我尝试为您的任务编写代码:

from django.test import TestCase

class Test(TestCase):
    def test_login(self):
        data = {'X-OpenAM-Username': 'user', 'X-OpenAM-Password': 'password'}
        response = client.post('/login/', data=data, content_type='application/json', secure=false)
        assertRedirects(response, '/expected_url/', 200)

Then you can use python3 manage.py test to run all tests. 然后,您可以使用python3 manage.py test来运行所有测试。

To properly test redirects, use the follow parameter 要正确测试重定向,请使用follow参数

If you set follow to True the client will follow any redirects and a redirect_chain attribute will be set in the response object containing tuples of the intermediate urls and status codes. 如果将follow设置为True,则客户端将遵循任何重定向,并且将在包含中间URL和状态代码的元组的响应对象中设置redirect_chain属性。

Then your code is as simple as 然后,您的代码就像

from django.test import TestCase 从django.test导入TestCase

class Test(TestCase):
    def test_login(self):
        client = Client()
        headers = {'X-OpenAM-Username': 'user', 'X-OpenAM-Password': 'password', 'Content-Type': 'application/json'}
        data = {}
        response = client.post('/login/', headers=headers, data=data, secure=False)
        self.assertRedirects(response,'/destination/',302,200)

Note that it's self.assertRedirects rather than assert or assertRedirects 请注意,它是self.assertRedirects而不是assertassertRedirects

Also note that the above test will most likely fail because you are posting an empty dictionary as the form data. 另请注意,上述测试很可能会失败,因为您要发布空字典作为表单数据。 Django form views do not redirect when the form is invalid and an empty form will probably be invalid here. 当表单无效时,Django表单视图不会重定向,此处的空表单可能无效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM