Skip to Content
Python高级编程(第2版)
book

Python高级编程(第2版)

by Posts & Telecom Press, Michał Jaworski, Tarek Ziadé
February 2024
Intermediate to advanced
423 pages
6h 9m
Chinese
Packt Publishing
Content preview from Python高级编程(第2版)

第10章 测试驱动开发

测试驱动开发(Test-Driven Development,TDD)是一种生产高质量软件的简单技术。它在Python社区中被广泛使用,并且在其他社区也非常受欢迎。

由于Python语言的动态性质,测试在Python中尤其重要。它缺少静态类型,所以不到每行代码执行的那一刻,很多错误都无法发现。但问题不仅是Python中的类型如何工作。记住,大多数的bug与错误的语法使用无关,而是与逻辑错误和细微的误解有关,它们可能导致重大的失败。

本章分为两部分:

  • 我不测试,主张TDD,并快速地描述如何使用标准库进行。
  • 我做测试,这是为那些做测试且希望充分利用测试的开发人员准备的。

如果你已经对TDD深信不疑,你应该转到下一部分。它将专注于高级技术和工具,使用这些工具和技术可以更轻松地进行测试。这部分主要是为那些没有使用这种方法的开发人员准备的,并尝试倡导他们使用。

最简单形式的测试驱动开发的过程,包括3个步骤。

  • 为未实现的新功能或者改进编写自动化测试。
  • 提供通过所有定义的测试的最小代码量。
  • 重构代码以满足所需的质量标准。

记住这个开发周期的最重要事情是,测试应该在实现之前编写。对于没有经验的开发人员来说,这不是一件容易的任务,但它是唯一的方法,它保证你要编写的代码是可测试的。

例如,要求开发人员编写一个检查给定数字是否为质数的函数,写一些关于如何使用它的示例以及预期结果如下:

   assert is_prime(5)
   assert is_prime(7)
   assert not is_prime(8)

实现该功能的开发人员不需要是负责提供测试的唯一人员。这些示例也可以由另一个人提供。例如,网络协议或密码算法的官方规范经常提供旨在验证实现的正确性的测试向量。这些是测试用例的完美基础。 ...

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

Hadoop实际解决方案手册

Hadoop实际解决方案手册

Posts & Telecom Press, JONATHAN OWENS, Lentz Jon, Femiano Brian
TensorFlow机器学习

TensorFlow机器学习

Posts & Telecom Press, Quan Hua, Shams Ul Azeem, Saif Ahmed
Python和NLTK实现自然语言处理

Python和NLTK实现自然语言处理

Posts & Telecom Press, Nitin Hardeniya
Kafka权威指南(第2版)

Kafka权威指南(第2版)

Gwen Shapira, Todd Palino, Rajini Sivaram, Krit Petty

Publisher Resources

ISBN: 9781835887745