简体   繁体   中英

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.

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.

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.

How can I check on the test that after the login the url redirected it's the correct? 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. For this task, you should use test case class from Django, for example django.test.TestCase . Then you can use method assertRedirects() and it will check where you've been redirected and with which code. 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.

To properly test redirects, use the follow parameter

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.

Then your code is as simple as

from django.test import 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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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