繁体   English   中英

我不懂Django的测试,你能帮我吗?

[英]I don't understand tests in Django, Can you help me please?

我在Django和Python测试中遇到了困难,对于我的最终项目我正在建立一个论坛网站,但我真的不知道我的测试应该是什么或者什么。 这是mysite文件的视图页面。 如果用户登录,有人可以告诉我我应该测试的内容。

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")

首先阅读Django测试文档 您可能还想阅读本书 它在某些方面已经过时了,但现在测试仍然与1.1中的情况大致相同。

这个问题在SO答案中有所涉及。

好吧,你可以测试一下:

  • 如果您正在分页的对象具有正确的页数。
  • 如果您正在查看的页面包含正确的对象范围。 如果尝试访问不存在的页面,则返回相应的错误。
  • 如果您对列出对象和对象详细信息的视图返回正确的HTTP状态代码(200)

对于初学者。 希望能帮到你。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM