Django官网项目 五

Writing your first Django app, part 5 | Django documentation | Django

自动测试介绍

何为自动测试

测试有系统自动完成。你只需要一次性的编写测试代码,当程序代码变更后,不需要对原来的测试人工再重新测试一遍。系统可以自动运行原来编写的测试代码。

创建自动测试的原因

节约时间,

发现问题,并阻止问题发生

经过测试,是代码被使用的前提

促进团队合作

测试的策略

测试驱动开发(现有测试,再开发)

测试代码越早越好

需要变更或需要修改错误是编写测试代码

编写测试代码

创建测试来发现错误

修改app的test.py文件

运行测试

Terminal命令:python manage.py test polls

修改代码,重新测试成功。结果如下:

测试网页结果

手动测试模拟

django的shell中使用测试的例子,如图

上面是手工过程的结果,Django已经定义基于上述手工过程的方法。

测试列表显示页面

在test.py 中做变更,测试polls/index的网址是否能区分 pub_date 在过去和在将来的区别。

python 复制代码
def create_question(question_text, days):
    """
    Create a question with the given `question_text` and published the
    given number of `days` offset to now (negative for questions published
    in the past, positive for questions that have yet to be published).
    """
    time = timezone.now() + datetime.timedelta(days=days)
    return Question.objects.create(question_text=question_text, pub_date=time)


class QuestionIndexViewTests(TestCase):
    def test_no_questions(self):
        """
        If no questions exist, an appropriate message is displayed.
        """
        response = self.client.get(reverse("polls:index"))
        self.assertEqual(response.status_code, 200)
        self.assertContains(response, "No polls are available.")
        self.assertQuerySetEqual(response.context["latest_question_list"], [])

    def test_past_question(self):
        """
        Questions with a pub_date in the past are displayed on the
        index page.
        """
        question = create_question(question_text="Past question.", days=-30)
        response = self.client.get(reverse("polls:index"))
        self.assertQuerySetEqual(
            response.context["latest_question_list"],
            [question],
        )

另外在class QuestionIndexViewTests定义其他方法:

测试详细显示页面

对于输入的未来问题,输入问题ID的网址,不会显示未来问题。测试中对于未来的问题返回404,对于过去的问题,要显示问题的内容。

python 复制代码
class QuestionDetailViewTests(TestCase):
    def test_future_question(self):
        """
        The detail view of a question with a pub_date in the future
        returns a 404 not found.
        """
        future_question = create_question(question_text="Future question.", days=5)
        url = reverse("polls:detail", args=(future_question.id,))
        response = self.client.get(url)
        self.assertEqual(response.status_code, 404)

    def test_past_question(self):
        """
        The detail view of a question with a pub_date in the past
        displays the question's text.
        """
        past_question = create_question(question_text="Past Question.", days=-5)
        url = reverse("polls:detail", args=(past_question.id,))
        response = self.client.get(url)
        self.assertContains(response, past_question.question_text)

在terminal里运行,python manage.py test polls。

有三条测试失败。根据系统提示进行了相关的修正。其中还修复了一个没有发现的问题。

测试策略:

测试程序可能会超过代码数量。这个并不重要。测试是越多越好的,测试并不怕冗余。测试过程的黄金原则:

  • 对于每个模型和视图都建立单独的 "测试类"
  • 每个测试方法只测试一个功能
  • 给每个测试方法起个能描述其功能的名字
相关推荐
失败尽常态52318 分钟前
用Python实现Excel数据同步到飞书文档
python·excel·飞书
2501_9044477420 分钟前
OPPO发布新型折叠屏手机 起售价8999
python·智能手机·django·virtualenv·pygame
青龙小码农20 分钟前
yum报错:bash: /usr/bin/yum: /usr/bin/python: 坏的解释器:没有那个文件或目录
开发语言·python·bash·liunx
大数据追光猿26 分钟前
Python应用算法之贪心算法理解和实践
大数据·开发语言·人工智能·python·深度学习·算法·贪心算法
Leuanghing1 小时前
【Leetcode】11. 盛最多水的容器
python·算法·leetcode
2501_904447741 小时前
华为发力中端,上半年nova14下半年nova15,大力普及原生鸿蒙
华为·智能手机·django·scikit-learn·pygame
bing_1582 小时前
简单工厂模式 (Simple Factory Pattern) 在Spring Boot 中的应用
spring boot·后端·简单工厂模式
天上掉下来个程小白2 小时前
案例-14.文件上传-简介
数据库·spring boot·后端·mybatis·状态模式
xinxiyinhe2 小时前
如何设置Cursor中.cursorrules文件
人工智能·python
诸神缄默不语2 小时前
如何用Python 3自动打开exe程序
python·os·subprocess·python 3