Django का Test Client
In this page:
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.
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 करता।
उदाहरण: Simulating a POST request
client.post() takes the URL and a dictionary of form data, exactly like a real browser submitting a form.
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.
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. #}
- यह मानना कि self.client test methods के across login persist करता है — हर test method को एक fresh, logged-out client मिलता है।
- POST data एक plain string के रूप में भेजना बजाय एक dictionary के, जिसे client सही से parse नहीं कर सकता।
- 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 करता है।
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: