2011-12-06 68 views
3

我很難在Django和Python中進行測試,對於我正在製作論壇網站的最終項目,我真的不知道我的測試應該如何或什麼。這裏是來自mysite文件的views頁面。可能有人請我走我通過應該測試什麼,除了用戶是否登錄。我不明白Django中的測試,你能幫助我嗎?

from django.core.urlresolvers import reverse 
from settings import MEDIA_ROOT, MEDIA_URL 
from django.shortcuts import redirect, render_to_response 
from django.template import loader, Context, RequestContext 
from mysite2.forum.models import * 

def list_forums(request): 
    """Main listing.""" 
    forums = Forum.objects.all() 
    return render_to_response("forum/list_forums.html", {"forums":forums},  context_instance=RequestContext(request)) 



def mk_paginator(request, items, num_items): 
    """Create and return a paginator.""" 
    paginator = Paginator(items, num_items) 
    try: page = int(request.GET.get("page", '1')) 
    except ValueError: page = 1 

    try: 
     items = paginator.page(page) 
    except (InvalidPage, EmptyPage): 
     items = paginator.page(paginator.num_pages) 
    return items 


def list_threads(request, forum_slug): 
    """Listing of threads in a forum.""" 
    threads = Thread.objects.filter(forum__slug=forum_slug).order_by("-created") 
    threads = mk_paginator(request, threads, 20) 
    template_data = {'threads': threads} 
    return render_to_response("forum/list_threads.html", template_data, context_instance=RequestContext(request)) 

def list_posts(request, forum_slug, thread_slug): 
    """Listing of posts in a thread.""" 
    posts = Post.objects.filter(thread__slug=thread_slug, thread__forum__slug=forum_slug).order_by("created") 
    posts = mk_paginator(request, posts, 15) 
    thread = Thread.objects.get(slug=thread_slug) 
    template_data = {'posts': posts, 'thread' : thread} 
    return render_to_response("forum/list_posts.html", template_data, context_instance=RequestContext(request)) 

def post(request, ptype, pk): 
    """Display a post form.""" 
    action = reverse("mysite2.forum.views.%s" % ptype, args=[pk]) 
    if ptype == "new_thread": 
     title = "Start New Topic" 
     subject = '' 
    elif ptype == "reply": 
     title = "Reply" 
     subject = "Re: " + Thread.objects.get(pk=pk).title 
    template_data = {'action': action, 'title' : title, 'subject' : subject} 

    return render_to_response("forum/post.html", template_data, context_instance=RequestContext(request)) 

def new_thread(request, pk): 
    """Start a new thread.""" 
    p = request.POST 
    if p["subject"] and p["body"]: 
     forum = Forum.objects.get(pk=pk) 
     thread = Thread.objects.create(forum=forum, title=p["subject"], creator=request.user) 
     Post.objects.create(thread=thread, title=p["subject"], body=p["body"], creator=request.user) 
    return HttpResponseRedirect(reverse("dbe.forum.views.forum", args=[pk])) 

def reply(request, pk): 
    """Reply to a thread.""" 
    p = request.POST 
    if p["body"]: 
     thread = Thread.objects.get(pk=pk) 
     post = Post.objects.create(thread=thread, title=p["subject"],   body=p["body"], 
     creator=request.user) 
    return HttpResponseRedirect(reverse("dbe.forum.views.thread", args=[pk]) +  "?page=last") 
+0

您可以通過並測試以確保每個模板加載。使用django測試客戶端 – dm03514

回答

1

好了,你可以測試:

  • 如果你對你的分頁頁面對象正確數量。
  • 如果您正在查看的頁面包含正確的對象範圍。如果嘗試訪問不存在的頁面,則會返回 相應的錯誤。
  • 如果上市對象和對象詳細視圖返回正確的HTTP狀態代碼(200)

對於初學者。希望能幫助你。

+0

是的,謝謝這是一個好開始 – Robert

+1

不客氣。一個好的經驗法則是,你需要測試幾乎任何你自己寫的東西,無論是模型上的方法或屬性,還是預計會產生特定結果的視圖函數等等。看看Karen Tracey關於Django測試和調試的出色書籍:http://www.amazon.com/Django-Testing-Debugging-Karen-Tracey/dp/1847197566/ref=sr_1_1?ie=UTF8&qid=1323227880&sr=8-1 – Brandon

3

首先閱讀Django testing documentation。您可能還需要閱讀this book。它在某些領域有點過時,但現在測試結果與1.1版本基本相同。

這是一個很大的話題來覆蓋在一個SO回答。

+0

我已經閱讀了他們,並且我有一本書,但除登錄驗證外我不太瞭解,我是否應該甚至打擾其他任何驗證 – Robert

+1

驗證和測試不是一回事。驗證是您檢查用戶輸入是否合理的地方。對於初學者來說,測試就是將已知輸入提供給代碼並檢查輸出是否符合期望值。你在說什麼? –