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

Views का Testing

एक view test करने का मतलब है एक page पर click करते visitor होने का pretend करना और जांचना कि page जिस तरह respond करना चाहिए वैसे ही respond करता है।
Syntax
markup
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.

markup
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 करता।

Note: URL को path सीधे type करने के बजाय इसके नाम से बनाने के लिए reverse() उपयोग करें।

उदाहरण: 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.

markup
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.

markup
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. #}
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.get() एक response object return करता है, और पहले response.status_code जांचे बिना HTML inspect करने की कोशिश करना।
  2. एक URL string hardcode करना बजाय URL name के साथ reverse() उपयोग करने के, जो चुपचाप break हो जाता है अगर URL pattern बदले।
  3. सिर्फ 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) उपयोग करें।

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.