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

Models का Testing

एक model test करने का मतलब है यह जांचना कि आपका data blueprint असल में save होता है, वापस पढ़ा जाता है, और जैसे आपने design किया वैसे ही behave करता है।
Syntax
markup
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.

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

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

markup
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. #}
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. असली production database के बजाय Django के automatic throwaway test database के against test करना।
  2. full_clean() call करना भूल जाना जब एक test को field validation जांचना हो, क्योंकि .save() अकेला validators skip करता है।
  3. सिर्फ यह 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 करें।

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.