我有一个具有以下BookInstance模型的Django库应用程序:
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)
我已经检查了Due_back是否晚于或低于截止日期, 测试成功执行,但是实际上没有测试BookInstance模型的is_overdue()。 请指导我如何正确测试此功能!谢谢
答案 0 :(得分:1)
我认为您误解了测试用例在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
答案 1 :(得分:1)
这是一个适当的测试用例。请注意,1 /我为每个条件创建了一种测试方法(我单独测试了每个条件),2 /我在测试方法本身中创建了要测试的书(使测试更加明显和易于理解)。我仅将setUp()
用于测试所需的治具,而不是测试本身的一部分。另外,我在self.assertXXX
调用中添加了更明确的错误消息(因为具有“ False is not 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")
答案 2 :(得分:1)
您可以简单地创建一个实例并从该实例获取属性。
true