简体   繁体   English

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

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

I am having a hard time with tests in Django and Python, for my final project I am making a forums website, but I don't really have any idea how or what my tests should be. 我在Django和Python测试中遇到了困难,对于我的最终项目我正在建立一个论坛网站,但我真的不知道我的测试应该是什么或者什么。 Here is the views page from mysite file. 这是mysite文件的视图页面。 Could someone please walk me through what I should test for besides if a user is logged in. 如果用户登录,有人可以告诉我我应该测试的内容。

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

First read the Django testing documentation . 首先阅读Django测试文档 You might also want to read this book . 您可能还想阅读本书 It's dated in some areas, but testing is still pretty much the same now as it was in 1.1. 它在某些方面已经过时了,但现在测试仍然与1.1中的情况大致相同。

It's a bit much of a topic to cover in an SO answer. 这个问题在SO答案中有所涉及。

Well, you could test: 好吧,你可以测试一下:

  • If you have the right number of pages for the objects you're paginating. 如果您正在分页的对象具有正确的页数。
  • If the page you're viewing contains the right object range. 如果您正在查看的页面包含正确的对象范围。 If trying to access a page that doesn't exist returns the appropriate error. 如果尝试访问不存在的页面,则返回相应的错误。
  • If your views for listing objects and object detail return the correct HTTP status code (200) 如果您对列出对象和对象详细信息的视图返回正确的HTTP状态代码(200)

For starters. 对于初学者。 Hope that helps you out. 希望能帮到你。

暂无
暂无

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

相关问题 show() function 给了我 15 我明白但是为什么 print(x) function 返回 13 我不明白我是 Z23EEEB4347BDD26BFC6B7EE9A3B755DDs 的新手 - the show() function gives me 15 & I understand but why does the print(x) function return 13 I don't get it I am new to python can you pls help me 我不明白以下代码的行为。 有人可以帮我吗 - I don't understand the behavior of this below code. Can someone help me here 我不明白为什么,即使我想显示 A_inv 乘以来自变量 x1 的 y 的结果。 你能帮助我吗 - I don't understand why, even though I want to display the result of A_inv multiplied by y from variable x1. Can you help me 您能帮我理解在 dataframe 上使用 for 循环的逻辑吗? - Could you please help me to understand logic using for loop on dataframe? 请你帮我计算 LCM - Please can you help me to Calculate LCM 你能帮我理解这个 pandas 代码吗 - Can you help me to understand this pandas code 我试图查看一个列表的任何值是否与另一个列表的任何值匹配,但我不能这样。 请帮忙...我不明白为什么 - I am trying to see if any value of one list matches any value of the other list, But I can't make it this way. Please help!..I don't understand why 伙计们,我是 python 新手,请帮助我理解为什么我的代码给我“无”作为输出? - Guys I am new to python, can please help me understand why my code is giving me “none” as output? 嗨,有人可以帮助我吗? 我不知道问题出在哪里,而且我是编码新手。 我正在使用 python 并且我正在尝试创建一个 class 学生 - Hi can someone please help me. i don't know what the problem is and I'm new to coding. im using python and im trying to create a class Student 请帮我理解这个 python 递归代码? - please help me to understand this code of python recursion?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM