2011-12-06 9 views
3

DjangoとPythonのテストでは苦労しています。私の最終的なプロジェクトでは、私はフォーラムのWebサイトを作っていますが、どうやってテストをするべきか分かりません。ここにmysiteファイルからのビューページです。誰かがユーザーがログインしている場合、私はほかのテストすべきかを通して私を歩いてもらえ。私は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 testclientを使用してください – dm03514

答えて

1

さて、あなたはテストでした:

  • あなたがページ付けしているオブジェクトのためのページの右の数を持っている場合。
  • 表示しているページに適切なオブジェクト範囲が含まれている場合。存在しないページにアクセスしようとすると、 のエラーが返されます。
  • リストオブジェクトとオブジェクトの詳細についてご意見が正しいHTTPステータスコード(200)
  • 手始めに

を返す場合。それがあなたを助けることを願っています。

+0

はいありがとうそれは良いスタートです – Robert

+1

あなたは大歓迎です。経験則としては、モデルのメソッドやプロパティ、特定の結果が得られると予想されるビュー関数など、自分自身を書くことについてのテストが必要なことです。 Djangoのテストとデバッグに関するKaren Traceyの優れた本を見てください: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

検証とテストは同じものではありません。検証とは、ユーザーの入力が健全かどうかをチェックする場所です。テストでは、既知の入力をコードに送り、出力が期待値と一致するかどうかを確認します。それはあなたの話ですか? –

関連する問題