Testing in Django§

Testing Django§

Writing a Unit Test§

import django.http
import django.utils.unittest as unittest2

class LocaleMiddlewareTests(unittest2.TestCase):

    def test_request_not_processed(self):

        middleware = LocaleMiddle()
        response = django.http.HttpResponse()
        middleware.process_response(none, response)

        self.assertFalse(response.cookies)

Test Client§

from django.test.client import Client

c = Client()

response = c.get('/login')
self.assertEqual(response.status_code, 200)

response = c.post('/login/', {'username': 'john', 'password': 'smith'})

Request Factory§

Running Tests§

$ ./manage.py test

Further Reading§