【发布时间】:2018-12-16 10:10:08
【问题描述】:
我正在尝试使用我的 Django 项目为我的表单编写测试。 我尝试测试以下 BookForm: forms.py
class BookForm(ModelForm):
class Meta:
model = Book
fields = ['title', 'author', 'summary', 'tag', 'genre', 'language', 'book_format', 'read_date']
#labels =
widgets = {
'author': AddAnotherWidgetWrapper(
forms.Select,
reverse_lazy('author_form'),),
'tag': AddAnotherWidgetWrapper(
forms.SelectMultiple,
reverse_lazy('author_form'),),
}
permission_required = 'libraryapp.can_edit'
def __init__(self, *args, **kwargs):
self.request = kwargs.pop('request', None)
# print(self.request.user)
super(BookForm, self).__init__(*args, **kwargs)
我的 test_forms.py 看起来像这样:
class BookFormTest(TestCase):
def test_read_date_in_the_past(self):
print('BookForm TEST Lounched')
title = 'TestBookPast'
author = 'TestAuthorPast'
date = datetime.date.today() + datetime.timedelta(days=2)
form_data = {'title': title, 'author': author, 'read_date': date}
form = BookForm(data=form_data)
self.assertTrue(form.is_valid())
我不知道为什么它不起作用。也许我忘记了一些非常重要的事情?
我总是得到这个 AssertionError:
self.assertTrue(form.is_valid()) AssertionError: False 不正确
【问题讨论】:
标签: python django forms sqlite testing