Views का Testing
In this page:
class ViewTests(TestCase):
def test_description(self):
response = self.client.get('/url/')
self.assertEqual(response.status_code, 200)
self.assertContains(response, 'text')
Test करने के लिए एक view setup करना
मानिए एक simple view जो एक greeting के साथ 200 response return करता है। Views को test Client के through test किया जाता है बजाय plain functions के रूप में call किए जाने के।
उदाहरण: Setting up a view to test
Assume a simple view that returns a 200 response with a greeting. Views are tested through the test Client rather than being called as plain functions.
from django.http import HttpResponse
def hello_view(request):
return HttpResponse('Hello, world!')
{# 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. #}
Test Client से View Call करना
self.client.get() एक URL को एक GET request simulate करता है और एक response object return करता है, exactly जैसे एक असली browser receive करता।
उदाहरण: Calling the view with the test client
self.client.get() simulates a GET request to a URL and returns a response object, exactly like a real browser would receive.
from django.test import TestCase
from django.urls import reverse
class HelloViewTests(TestCase):
def test_hello_view_status_code(self):
response = self.client.get(reverse('hello'))
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. #}
Response Content जांचना
response.content view द्वारा returned raw bytes रखता है, जिसे decode करके expected text के लिए जांचा जा सकता है।
उदाहरण: Checking response content
response.content holds the raw bytes returned by the view, which can be decoded and checked for expected text.
class HelloContentTests(TestCase):
def test_hello_view_message(self):
response = self.client.get(reverse('hello'))
self.assertIn(b'Hello, world!', response.content)
{# 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.get() एक response object return करता है, और पहले response.status_code जांचे बिना HTML inspect करने की कोशिश करना।
- एक URL string hardcode करना बजाय URL name के साथ reverse() उपयोग करने के, जो चुपचाप break हो जाता है अगर URL pattern बदले।
- सिर्फ success case test करना, एक missing object (404) जैसे edge case को test न करना।
- Django का test Client असली server चलाए बिना browser requests simulate करता है।
- self.client.get(url) और self.client.post(url, data) दो सबसे common calls हैं।
- response.status_code और response.content आपको assert करने देते हैं कि view ने असल में क्या return किया।
- Tests को URL changes के बाद भी survive करने के लिए hardcoded URLs के बजाय reverse(view-name) उपयोग करें।
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: