TestCase Class की मूल बातें
In this page:
from django.test import TestCase
class ClassNameTests(TestCase):
def setUp(self):
# runs before each test
...
def test_name(self):
self.assertEqual(actual, expected)
TestCase को Subclass करना
Django की TestCase class Python की built-in unittest.TestCase को extend करती है और database transaction handling जोड़ती है, इसलिए एक test में किए changes कभी दूसरे test में leak नहीं होते।
उदाहरण: Subclassing TestCase
Django's TestCase class extends Python's built-in unittest.TestCase and adds database transaction handling, so changes made in one test never leak into another test.
from django.test import TestCase
class ExampleTests(TestCase):
def test_true_is_true(self):
self.assertTrue(True)
{# 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. #}
setUp Method
setUp() class के हर single test method से पहले अपने आप चलता है। यह वे objects बनाने की जगह है जिनकी कई tests को ज़रूरत है, repetition से बचते हुए।
उदाहरण: The setUp method
setUp() runs automatically before every single test method in the class. It is the place to create objects that multiple tests need, avoiding repetition.
from django.test import TestCase
from django.contrib.auth.models import User
class UserTests(TestCase):
def setUp(self):
self.user = User.objects.create_user('alice', password='pass123')
def test_user_created(self):
self.assertEqual(self.user.username, 'alice')
{# 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 Methods को Naming देना
Django का test runner सिर्फ उन methods को discover करता है जिनके नाम test से शुरू होते हैं। check_user() नाम का एक method चुपचाप कभी नहीं चलेगा।
उदाहरण: Naming test methods
Django's test runner only discovers methods whose names start with test. A method named check_user() would silently never run.
class NamingTests(TestCase):
def test_username_is_lowercase(self):
self.assertEqual('alice', 'alice'.lower())
{# 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. #}
- Django की TestCase के बजाय Python की plain unittest.TestCase से inherit करना, tests के बीच automatic database rollback खोते हुए।
- यह भूल जाना कि हर test method independently चलता है, फिर किसी पिछले test method से बची state पर depend करना।
- हर single test method के अंदर expensive setup code रखना बजाय setUp() में।
- django.test.TestCase हर test को एक database transaction में wrap करता है जो बाद में rolled back होता है।
- आपकी लिखी हर test class को TestCase से subclass करना चाहिए, plain unittest.TestCase से नहीं।
- Discover और run होने के लिए test methods को test शब्द से शुरू होना चाहिए।
- setUp() हर test method से पहले चलता है, हर test को एक clean, predictable starting point देते हुए।
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: