社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Django

django:model的@property函数的测试用例

Simran • 5 年前 • 1741 次点击  

我有一个django库应用程序,它具有以下bookInstance模型:

class BookInstance(models.Model):
    id = models.UUIDField(primary_key=True, default=uuid.uuid4,help_text='Unique ID for this particular book across whole library')
    book = models.ForeignKey('Book', on_delete=models.SET_NULL, null=True)
    due_back = models.DateField(null=True, blank=True)
    borrower = models.ForeignKey(User, on_delete=models.SET_NULL, null=True, blank=True)

    STATUS_MAINTENANCE = 'm'
    STATUS_ON_LOAN = 'o'
    STATUS_AVAILABLE = 'a'
    STATUS_RESERVED = 'r'

    LOAN_STATUS = (
      (STATUS_MAINTENANCE, 'Maintenance'),
      (STATUS_ON_LOAN, 'On loan'),
      (STATUS_AVAILABLE, 'Available'),
      (STATUS_RESERVED, 'Reserved'),
    )

    status = models.CharField(
       max_length=1,
       choices=LOAN_STATUS,
       blank=True,
       default=STATUS_MAINTENANCE,
       help_text='Book availability',
     )

    class Meta:
        ordering = ['due_back']
        permissions = (("can_mark_returned", "Set book as returned"), ("can_create_book", "Create new book"), ("can_update_book", "Update book details"), ("can_delete_book", "Delete book"),)


    # how to develop test case for this function
    @property
    def is_overdue(self):
       if self.due_back and date.today() > self.due_back:
        return True
    return False

在我的tests.py文件中, 我有test_object_name_is_name函数来测试它 ,但是我的覆盖率报告显示该函数没有得到测试。

class BookInstanceModelTest(TestCase):
"""
    Test case for Book Instance
"""
   @classmethod
   def setUpTestData(cls):
       # Set up non-modified objects used by all test methods
       test_user1 = User.objects.create_user(username='testuser1', password='1X<ISRUkw+tuK',
                                          email='testuser1@test.com')
       test_user2 = User.objects.create_user(username='testuser2', password='2HJ1vRV0Z&3iD',
                                          email='testuser2@test.com')

       test_author = Author.objects.create(first_name='William', last_name='Shakespeare')
       test_book = Book.objects.create(title='Hamlet', author=test_author, summary='Published in 1990',
                                    isbn='123456789123')
       genre_objects_for_book = Genre.objects.all()
       test_book.genre.set(genre_objects_for_book)

       return_date1 = datetime.date.today()
       BookInstance.objects.create(book=test_book, borrower=test_user1, due_back=return_date1,
                                status='o')
       return_date2 = datetime.date.today() + datetime.timedelta(days=5)
       BookInstance.objects.create(book=test_book, borrower=test_user2, due_back=return_date2,
                                status='o')

       return_date3 = datetime.date.today() - datetime.timedelta(days=5)
       BookInstance.objects.create(book=test_book, borrower=test_user2, due_back=return_date3,
                                status='o')

   # test case I developed
   def test_book_is_overdue(self):
       last_date = datetime.date.today()
       for book in BookInstance.objects.all():
          if book.due_back == last_date:
              self.assertTrue(True)
          elif book.due_back > last_date:
              self.assertTrue(True)
          elif book.due_back < last_date:
              self.assertFalse(False)

我查了一下你的回执是迟了还是迟了, 测试成功执行,但是 不实际测试BookInstance模型的Is_过期()。 请指导我如何正确测试此功能!谢谢

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/40542
 
1741 次点击  
文章 [ 2 ]  |  最新文章 5 年前
bruno desthuilliers
Reply   •   1 楼
bruno desthuilliers    6 年前

这是一个合适的测试用例。注意,1/i为每个条件创建一个测试方法(我单独测试每个条件),2/i创建要在测试方法本身中测试的书(使测试更加明显和可读)。我只使用 setUp() 用于测试所需但不是测试本身一部分的装置。另外,我在 self.assertXXX 调用(因为将“false不是true”作为失败测试的报告并没有真正的帮助)。

class BookInstanceModelTest(TestCase):
    def setUp(self):
        self.test_user1 = User.objects.create_user(
            username='testuser1', 
            password='1X<ISRUkw+tuK',
            email='testuser1@test.com'
        )

        self.test_author = Author.objects.create(
           first_name='William', 
           last_name='Shakespeare'
        )

        self.test_book = Book.objects.create(
            title='Hamlet', 
            author=self.test_author, 
            summary='Published in 1990',                                       
            isbn='123456789123'
        )

        genre_objects_for_book = Genre.objects.all()
        self.test_book.genre.set(genre_objects_for_book)


    def test_book_is_not_overdue_if_due_back_date_is_today(self)            
        due_date = datetime.date.today()
        book = BookInstance.objects.create(
           book=self.test_book, 
           borrower=self.test_user1, 
           due_back=due_date, 
           status='o'
        )
        self.assertFalse(book.is_overdue, "book should not be overdue")


    def test_book_is_not_overdue_if_due_back_date_is_later_than_today(self):
        due_date = datetime.date.today() + datetime.timedelta(days=5)
        book = BookInstance.objects.create(
           book=self.test_book, 
           borrower=self.test_user1, 
           due_back=due_date, 
           status='o'
        )
        self.assertFalse(book.is_overdue, "book should not be overdue")


    def test_book_is_not_overdue_if_due_back_date_is_none(self):
        due_date = None
        book = BookInstance.objects.create(
           book=self.test_book, 
           borrower=self.test_user1, 
           due_back=due_date, 
           status='o'
        )
        self.assertFalse(book.is_overdue, "book should not be overdue")


    def test_book_is_overdue_if_due_back_date_is_past(self):
        due_date = datetime.date.today() - datetime.timedelta(days=5)
        book = BookInstance.objects.create(
           book=self.test_book, 
           borrower=self.test_user1, 
           due_back=due_date, 
           status='o'
        )
        self.assertTrue(book.is_overdue, "book should be overdue")
Guillaume Lebreton
Reply   •   2 楼
Guillaume Lebreton    6 年前

我认为您误解了django中测试用例的工作原理;您应该在测试方法前面加上 test_ 然后不管你想要什么名字,但它对功能并不重要,也不会调用函数。

要测试您的属性,您应该在测试中调用它:

 def test_book_is_overdue(self):
       last_date = datetime.date.today()
       for book in BookInstance.objects.all():
          value = book.is_overdue
          # test whatever you want