Django如何測試視圖

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)

首頁視圖list

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})
發佈了85 篇原創文章 · 獲贊 14 · 訪問量 20萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章