अपना पहला Test लिखना
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.
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 के बराबर है।
उदाहरण: 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.
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.
python manage.py test
⚠️ Run this in your own terminal or Node.js environment.
- Implementation details (internal variable names) को test करना बजाय observable behavior (return values, output) के।
- बिना किसी assertion के एक test लिखना, इसलिए यह चाहे कुछ भी हो हमेशा pass होता है।
- 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 करते हैं।
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: