1、使用Django测试客户端
def test_uses_list_template(self):
list_ = List.objects.create()
response = self.client.get('/lists/%d/'%(list_.id,))#使用Django测试客户端
self.assertTemplateUsed(response,'list.html')#检查所使用的模板。然后在模板的上下文中检查各个待办事项
2、检查所使用的模板,如上所示
3、检查每个对象都是希望得到的,或者查询集合中包含正确的待办事项
def test_passes_correct_list_to_template(self):
correct_list = List.objects.create()
response = self.client.get('/lists/%d/'%(correct_list.id,))
self.assertEqual(response.context['list'],correct_list)#检查每个对象都是希望得到的,或者查询集合中包含正确的待办事项
4、检查表单使用正确的类
def test_displays_item_form(self):
list_ = List.objects.create()
response = self.client.get('/lists/%d/' % (list_.id,))
self.assertIsInstance(response.context['form'],ExistingListItemForm)#检查表单使用正确的类
self.assertContains(response,'name="text"')
5、检查模板逻辑,每个if和for都要做最简单的逻辑
def test_displays_only_item_for_that_list(self):
current_list = List.objects.create()
Item.objects.create(text='itemey 1',list = current_list)
Item.objects.create(text='itemey 2',list = current_list)
other_list = List.objects.create()
Item.objects.create(text='other list item 1',list = other_list)
Item.objects.create(text='other list item 2',list = other_list)
response = self.client.get('/lists/%d/'%(current_list.id,))
self.assertContains(response,'itemey 1')#检查模板逻辑
self.assertContains(response,'itemey 2')#检查模板逻辑
self.assertNotContains(response,'other list item 1')#检查模板逻辑
self.assertNotContains(response,'other list item 2')
6、对于处理POST请求的视图,确保有效和无效两种情况都要测试
def test_can_save_a_POST_request_to_an_existing_list(self):
correct_list = List.objects.create()
self.client.post(
'/lists/%d/'%(correct_list.id),
data={'text':'A new item for an existing list'}
)
self.assertEqual(Item.objects.count(),1)#
new_item = Item.objects.first()
self.assertEqual(new_item.text,'A new item for an existing list')#
self.assertEqual(new_item.list,correct_list)
def test_POST_redirects_to_list_view(self):
correct_list = List.objects.create()
response = self.client.post(
'/lists/%d/'%(correct_list.id,),
data={'text': 'A new list for an existing list'}
)
self.assertRedirects(response,'/lists/%d/'%(correct_list.id,))#
def post_invalid_input(self):
list_ = List.objects.create()
return self.client.post('/lists/%d/' % (list_.id,), data={'text':''})
def test_for_invalid_input_nothing_saved_to_do(self):
self.post_invalid_input()
self.assertEqual(Item.objects.count(),0)#
def test_for_invalid_input_renders_list_template(self):
response = self.post_invalid_input()
self.assertEqual(response.status_code,200)
self.assertTemplateUsed(response,'list.html')#
7、健全性检查,检查是否渲染指定的表单,而且是否有显示错误消息
def test_for_invalid_input_passes_form_to_template(self):
response = self.post_invalid_input()
self.assertIsInstance(response.context['form'],ExistingListItemForm)#渲染指定的表单
def test_for_invalid_input_show_error_on_page(self):
response = self.post_invalid_input()
self.assertContains(response,EMPTY_LIST_ERROR)#错误消息
def test_duplicate_item_validation_errors_end_up_on_lists(self):
list1 = List.objects.create()
item1 = Item.objects.create(list=list1,text='textey')
response = self.client.post('/lists/%d/' % (list1.id,), data={'text':'textey'})
expected_error = DUPLICATE_ITEM_ERROR
self.assertContains(response,expected_error)
self.assertTemplateUsed(response,'list.html')
self.assertEqual(Item.objects.all().count(),1)
Views.py
from django.shortcuts import redirect,render
from lists.models import Item,List
from lists.forms import ItemForm,ExistingListItemForm
# Create your views here.
def home_page(request):
return render(request,'home.html',{'form':ItemForm()})
def view_list(request,list_id):
list_ = List.objects.get(id = list_id)
form = ExistingListItemForm(for_list=list_)
if request.method == 'POST':
form = ExistingListItemForm(for_list=list_,data=request.POST)
if form.is_valid():
form.save()
return redirect(list_)
return render(request,'list.html',{'list':list_,'form':form})
def new_list(request):
form = ItemForm(data=request.POST)
if form.is_valid():
list_ = List.objects.create()
form.save(for_list=list_)
return redirect(list_)
else:
return render(request,'home.html',{'form':form})