Models का Testing
class ModelTests(TestCase):
def test_description(self):
obj = ModelName.objects.create(field=value)
self.assertEqual(obj.field, value)
self.assertEqual(str(obj), 'expected string')
Test करने के लिए एक simple model
मानिए एक Book model जिसमें एक title और एक CharField है। Model tests test database में असली rows बनाते हैं और उनकी saved values जांचते हैं।
उदाहरण: A simple model to test
Assume a Book model with a title and a CharField. Model tests create real rows in the test database and check their saved values.
from django.db import models
class Book(models.Model):
title = models.CharField(max_length=200)
def __str__(self):
return self.title
{# 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. #}
Object Creation Test करना
एक test के अंदर एक model instance बनाना isolated test database में लिखता है, और saved fields को सीधे assert किया जा सकता है।
उदाहरण: Testing object creation
Creating a model instance inside a test writes to the isolated test database, and the saved fields can be asserted directly.
from django.test import TestCase
from myapp.models import Book
class BookModelTests(TestCase):
def test_book_title_saved(self):
book = Book.objects.create(title='Django Basics')
self.assertEqual(book.title, 'Django Basics')
{# 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. #}
__str__ Method Test करना
चूंकि __str__ यह control करता है कि admin और shell में एक object कैसे दिखता है, इसे अपना explicit assertion मिलना चाहिए।
उदाहरण: Testing the __str__ method
Since __str__ controls how an object appears in the admin and shell, it deserves its own explicit assertion.
class BookStrTests(TestCase):
def test_book_str_returns_title(self):
book = Book.objects.create(title='Django Basics')
self.assertEqual(str(book), 'Django Basics')
{# 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. #}
- असली production database के बजाय Django के automatic throwaway test database के against test करना।
- full_clean() call करना भूल जाना जब एक test को field validation जांचना हो, क्योंकि .save() अकेला validators skip करता है।
- सिर्फ यह test करना कि एक object save होता है, इसके __str__ या computed properties सही value return करती हैं या नहीं यह जांचे बिना।
- Model tests test database में objects बनाते हैं और उनके saved fields पर assert करते हैं।
- TestCase अपने आप हर test को एक clean, isolated database state देता है।
- दोनों valid data (object सही से save होता है) और invalid data (validation expected रूप से fail होता है) test करें।
- Objects कैसे display होते हैं इसमें regressions पकड़ने के लिए __str__ output पर assert करें।
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: