Skip to Content
Python测试驱动开发实践, 3rd Edition
book

Python测试驱动开发实践, 3rd Edition

by Harry Percival
October 2025
Intermediate to advanced
712 pages
8h 38m
Chinese
O'Reilly Media, Inc.
Content preview from Python测试驱动开发实践, 3rd Edition

第三章. 使用单元测试测试 简单主页 使用单元测试

本作品已使用人工智能进行翻译。欢迎您提供反馈和意见:translation-feedback@oreilly.com

上一章结束时功能测试(FT)失败, 提示网站首页标题需包含“待办事项”。 现在开始着手开发应用程序。 本章将创建首个HTML页面,了解URL处理机制, 并使用Django视图函数生成HTTP请求响应。

我们的首个Django应用与单元测试

Django鼓励将代码结构化为应用程序。 理论上一个项目可包含多个应用程序; 你可以使用他人开发的第三方应用程序, 甚至可能在不同项目中复用自己的应用程序...​虽然 我必须承认,我自己从未真正实现过后者! 但应用程序仍是保持代码条理性的好方法。

现在开始创建待办事项应用:

$ python manage.py startapp lists

这将在manage.py和现有superlists文件夹旁创建名为lists的文件夹, 其中包含若干占位文件,用于存放模型、视图等组件, 以及我们当前最关心的测试文件:

.
├── db.sqlite3
├── functional_tests.py
├── lists
│   ├── __init__.py
│   ├── admin.py
│   ├── apps.py
│   ├── migrations
│   │   └── __init__.py
│   ├── models.py
│   ├── tests.py
│   └── views.py
├── manage.py
└── superlists
    ├── __init__.py
    ├── asgi.py
    ├── settings.py
    ├── urls.py
    └── wsgi.py

单元测试与功能测试的区别

如同我们给事物贴上的许多标签, 单元测试与功能测试的界限有时会变得模糊。 但基本区别在于: 功能测试从外部视角检验应用程序,即用户视角; 而单元测试则从内部视角检验应用程序,即程序员视角。

我演示的TDD方法同时运用这两类测试 来驱动应用程序的开发并确保其正确性。 我们的工作流程大致如下:

  1. 我们首先编写功能测试,从用户视角描述新功能的典型用例。

  2. 当功能测试失败时, 我们开始思考如何编写代码使其通过 (或至少绕过当前失败点)。 此时我们通过一个或多个单元测试定义 代码的行为规范——核心理念是 每行生产代码都应由(至少)一个单元测试覆盖。

  3. 当单元测试失败时, 我们编写最小量的应用程序代码 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Tcl/Tk, 3rd Edition

Tcl/Tk, 3rd Edition

Clif Flynt
算法精讲视频课程:24篇系列讲座

算法精讲视频课程:24篇系列讲座

罗伯特·塞奇威克, 凯文·韦恩(Kevin Wayne)
Python-Tricks

Python-Tricks

Dan Bader
AI工程

AI工程

Chip Huyen

Publisher Resources

ISBN: 0642572274801