How do test in Django
<p>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.</p>

<p>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.</p>

<pre><code>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)
</code></pre>

<p>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.</p>

<p>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?</p>

<p>Thanks and regards.</p>

<p>Django have plenty of tools for testing. For this task, you should use test case class from Django, for example <code>django.test.TestCase</code>.
Then you can use method <code>assertRedirects()</code> and it will check where you've been redirected and with which code. You can find any info you need <a href="https://docs.djangoproject.com/en/1.10/topics/testing/tools/" rel="nofollow">here</a>.
I've tried to write the code for your task:</p>

<pre><code>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)
</code></pre>

<p>Then you can use <code>python3 manage.py test</code> to run all tests.</p>

python django django-testing