Skip to Content
Flask Web开发:基于Python的Web应用开发实战(第2版)
book

Flask Web开发:基于Python的Web应用开发实战(第2版)

by Miguel Grinberg
August 2018
Intermediate to advanced
230 pages
6h 42m
Chinese
O'Reilly Media, Inc. Beijing
Content preview from Flask Web开发:基于Python的Web应用开发实战(第2版)
15
测试
编写单元测试主要有两个目的。实现新功能时,单元测试能够确保新添加的代码按预期方
式运行。当然,这个过程也可手动完成,不过自动化测试显然能节省时间和精力,因为自
动化测试能轻松地重复运行。
另外,一个更重要的目的是,每次修改应用后,运行单元测试能保证现有代码的功能没有
回归
,即新改动没有影响原有代码的正常运行。
从一开始我们就为
Flasky
应用编写了单元测试,检查数据库模型类有没有实现特定的功
能。模型类很容易在运行中的应用上下文之外进行测试,因此不用花费太多精力,为数据
库模型中实现的全部功能编写单元测试至少能有效保证应用的这一部分在不断完善的过程
中仍能按预期运行。
本章将讨论如何改进和增强单元测试,并覆盖应用的其他部分。
15.1
 获取代码覆盖度报告
编写测试组件很重要,但知道测试的状况同样重要。代码覆盖度工具用于统计单元测试检
查了应用的多少功能,并提供一份详细的报告,说明应用的哪些代码没有测试到。这个信
息非常重要,因为它能指引你为最需要测试的部分编写新测试。
Python
提供了一个优秀的代码覆盖度工具,名为
coverage
。这个工具使用
pip
安装:
(venv) $
pip install coverage
这个工具本身是一个命令行脚本,可在任何一个
Python
应用中检查代码覆盖度。除此之
外,它还提供了更方便的脚本访问功能,使用编程方式启动覆盖检查引擎。为了能更好地
把覆盖检测集成到第
7
章添加的 flask test 命令中,我们可以添加一个 --coverage 选项,
169
170 ...
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

Python机器学习基础教程

Python机器学习基础教程

Andreas C. Müller, Sarah Guido
Python编程入门与实战

Python编程入门与实战

Posts & Telecom Press, Fabrizio Romano
程序设计导论:Python语言实践

程序设计导论:Python语言实践

罗伯特 塞奇威克, 凯文 韦恩, 罗伯特 唐德罗

Publisher Resources

ISBN: 9787115489456