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

अपना पहला Test लिखना

अपना पहला test लिखना एक tiny checklist लिखने जैसा है जो computer को exactly बताती है कि 'सही से काम करना' क्या मतलब रखता है।
Syntax
markup
from django.test import TestCase
from .utils import function_name

class FunctionTests(TestCase):
    def test_description(self):
        result = function_name(input_value)
        self.assertEqual(result, expected)

Test करने के लिए एक simple function लिखना

मानिए एक utils.py file एक function define करती है जो एक number को double करता है। यह एक plain, predictable piece of logic है जो एक ideal पहला test बनता है।

उदाहरण: Writing a simple function to test

Suppose a utils.py file defines a function that doubles a number. This is a plain, predictable piece of logic that makes an ideal first test.

markup
def double(value):
    return value * 2
{# 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 Method लिखना

Test function import करता है, इसे एक known input के साथ call करता है, और assertEqual उपयोग करके assert करता है कि output expected result के बराबर है।

Note: Test का नाम उस behavior के बाद रखें जो यह जांचता है, जैसे test_double_returns_twice_the_value।

उदाहरण: Writing the test method

The test imports the function, calls it with a known input, and asserts the output equals the expected result using assertEqual.

markup
from django.test import TestCase
from myapp.utils import double


class DoubleTests(TestCase):
    def test_double_returns_twice_the_value(self):
        self.assertEqual(double(5), 10)
{# 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 Run करना

python manage.py test project में हर discovered test चलाता है और हर pass के लिए एक dot print करता है, या हर failure के लिए एक traceback।

उदाहरण: Running the test

python manage.py test runs every discovered test in the project and prints a dot for each pass, or a traceback for each failure.

bash
python manage.py test

⚠️ Run this in your own terminal or Node.js environment.

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. Implementation details (internal variable names) को test करना बजाय observable behavior (return values, output) के।
  2. बिना किसी assertion के एक test लिखना, इसलिए यह चाहे कुछ भी हो हमेशा pass होता है।
  3. Test लिखने के बाद इसे run न करना, इसलिए एक typo unnoticed रह जाता है।
चैप्टर सारांश
  • एक test method code को known input के साथ call करता है और self.assertX methods उपयोग करके result assert करता है।
  • हर test को एक specific behavior पर focus करना चाहिए।
  • यह confirm करने के लिए कि यह असल में execute होता है और pass करता है, test लिखने के बाद python manage.py test चलाएं।
  • एक अच्छा पहला test simple है: जांचें कि एक function वह return करता है जो आप expect करते हैं।

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.