← Back to Django Course | Chapter 13: Testing in Django | Lesson 6 of 9

Django का Test Client

Test client एक pretend web browser है जो आपके tests के अंदर रहता है, उन्हें असली browser खोले बिना pages visit करने देते हुए।
Syntax
markup
response = self.client.get('/url/')
response = self.client.post('/url/', {'field': 'value'})

self.assertEqual(response.status_code, 200)
self.assertRedirects(response, '/expected-url/')

Test Client Access करना

हर TestCase instance में अपने आप self.client होता है, एक Client object जो आपके URLs के against GET और POST requests simulate कर सकता है।

उदाहरण: Accessing the test client

Every TestCase instance automatically has self.client, a Client object that can simulate GET and POST requests against your URLs.

markup
from django.test import TestCase


class ClientTests(TestCase):
    def test_client_exists(self):
        self.assertIsNotNone(self.client)
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}

एक POST Request Simulate करना

client.post() URL और form data की एक dictionary लेता है, exactly जैसे एक असली browser एक form submit करता।

Note: Dictionary keys को form field के name attributes के साथ exactly match करें।

उदाहरण: Simulating a POST request

client.post() takes the URL and a dictionary of form data, exactly like a real browser submitting a form.

markup
def test_post_creates_object(self):
    response = self.client.post('/books/add/', {'title': 'New Book'})
    self.assertEqual(response.status_code, 302)
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}

Protected Views के लिए Login करना

client.login() authentication simulate करता है, tests को @login_required में wrapped views तक पहुंचने देते हुए।

उदाहरण: Logging in for protected views

client.login() simulates authentication, letting tests reach views wrapped in @login_required.

markup
from django.contrib.auth.models import User


class ProtectedViewTests(TestCase):
    def setUp(self):
        self.user = User.objects.create_user('alice', password='pass123')

    def test_dashboard_requires_login(self):
        self.client.login(username='alice', password='pass123')
        response = self.client.get('/dashboard/')
        self.assertEqual(response.status_code, 200)
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. यह मानना कि self.client test methods के across login persist करता है — हर test method को एक fresh, logged-out client मिलता है।
  2. POST data एक plain string के रूप में भेजना बजाय एक dictionary के, जिसे client सही से parse नहीं कर सकता।
  3. Authentication चाहने वाला एक view test करने से पहले self.client.login() call करना भूल जाना।
चैप्टर सारांश
  • self.client किसी भी Django TestCase subclass पर अपने आप available है।
  • client.get(url) और client.post(url, data_dict) असली HTTP requests simulate करते हैं।
  • client.login(username=..., password=...) protected views के लिए एक logged-in session simulate करता है।
  • Client कभी network को नहीं छूता — यह Django के request/response cycle के through सीधे आपके views को call करता है।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.