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。

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

测试策略:

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

  • 对于每个模型和视图都建立单独的 "测试类"
  • 每个测试方法只测试一个功能
  • 给每个测试方法起个能描述其功能的名字
相关推荐
轻语呢喃8 分钟前
JavaScript :事件循环机制的深度解析
javascript·后端
ezl1fe9 分钟前
RAG 每日一技(四):让AI读懂你的话,初探RAG的“灵魂”——Embedding
后端
经典199211 分钟前
spring boot 详解以及原理
java·spring boot·后端
Aurora_NeAr13 分钟前
Apache Iceberg数据湖高级特性及性能调优
大数据·后端
程序员清风24 分钟前
程序员要在你能挣钱的时候拼命存钱!
后端·面试·程序员
June bug41 分钟前
【Python基础】变量、运算与内存管理全解析
开发语言·python·职场和发展·测试
蹦蹦跳跳真可爱5891 小时前
Python----OpenCV(几何变换--图像平移、图像旋转、放射变换、图像缩放、透视变换)
开发语言·人工智能·python·opencv·计算机视觉
蹦蹦跳跳真可爱5891 小时前
Python----循环神经网络(Transformer ----Layer-Normalization(层归一化))
人工智能·python·rnn·transformer
m0_625686551 小时前
Day58
python
夜阳朔1 小时前
Conda环境激活失效问题
人工智能·后端·python