<Test-Driven Development with Python>学习笔记 第一部分 测试驱动开发基础

本文是《Test-Driven Development with Python》的学习笔记,介绍了TDD的基础概念、测试分类(功能测试和单元测试)、TDD开发流程及规则,并分享了作者在实践过程中的体会。TDD强调在开发前先编写测试,通过测试驱动整个开发过程,以确保代码质量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原创时间: 2016-01-06
更新时间: 2016-01-06

TDD简介

Test-Driven Development,驱动测试开发,是一种软件开发的开发方式.
它要求在编写某个功能代码之前先编写测试代码,然后只编写使测试通过的功能代码,通过测试来推动整个开发的进行.

我看的这本书叫,可以通过访问官网来购买或免费在线阅读
此书讲解TDD的方式是通过Django框架来开发一个Web应用.

我在学习的过程中感觉收获良多,因此将学习的过程和心得体会记录下来,希望对想要学习了解TDD的朋友也能提供一些帮助.

因为TDD的规则跟传统的开发方式不同,所以如果要使用TDD,就得转变以前的传统观念.这一点其实挺难的

测试分类

功能测试 Functional Test

以用户的角度来在外部测试应用,可以写一个小故事,来模拟用户的整个操作流程,根据这个流程来写出测试代码,从这个角度上说功能测试可以看作是你开发的应用(或者模块)的一份使用说明书

功能测试可以只在一个模块内,也可以包含多个模块,不过按道理讲用户是不了解也不需要了解他们需求的功能是在一个模块内还是包含多个模块的,所以我认为功能测试应该是一个全局的测试方式

功能测试也叫验收测试,端对端测试,黑盒测试

功能测试使用的主要的工具是Selenium,一个可以控制浏览器行为的工具集

单元测试 Unit Test

以程序员的角度从内部来测试应用,测试的内容是某个模块内部的具体实现

单元测试只负责测试某个模块内的功能

TDD开发流程

先借用书中的一张图来有一个直观的印象
这里写图片描述

  1. 遍写一个功能测试
    从用户角度用故事的方式来描述新功能,将故事翻译成测试代码

  2. 运行功能测试
    如果功能测试出错,就考虑如何编写应用代码来通过测试,
    如果功能测试出现的异常是预料中的,其实是好事,至少我知道下一步的工作是什么
    如果功能测试出现了非预期的异常,需要仔细阅读出错代码,很可能是因为之前的修改导致了更多的错误.可以通过多种方式来调试代码:

    • 添加print语句打印运行过程
    • 改进错误输出来获得更多的当前状态的信息
    • 手动访问网站
    • 添加sleep函数来暂停运行过程
  3. 编写一个(多个)单元测试
    针对上一步想到的编码方式,

Title: Test-Driven Development with Python, 2nd Edition Length: 592 pages Edition: 2 Language: English Publication Date: 2017-08-10 ISBN-10: 1491958707 ISBN-13: 9781491958704 Table of Contents Part I. The Basics of TDD and Django Chapter 1. Getting Django Set Up Using a Functional Test Chapter 2. Extending Our Functional Test Using the unittest Module Chapter 3. Testing a Simple Home Page with Unit Tests Chapter 4. What Are We Doing with All These Tests? (And, Refactoring) Chapter 5. Saving User Input: Testing the Database Chapter 6. Improving Functional Tests: Ensuring Isolation and Removing Voodoo Sleeps Chapter 7. Working Incrementally Part II. Web Development Sine Qua Nons Chapter 8. Prettification: Layout and Styling, and What to Test About It Chapter 9. Testing Deployment Using a Staging Site Chapter 10. Getting to a Production-Ready Deployment Chapter 11. Automating Deployment with Fabric Chapter 12. Splitting our tests into multiple files, and a generic wait helper Chapter 13. Validation at the Database Layer Chapter 14. A Simple Form Chapter 15. More Advanced Forms Chapter 16. Dipping Our Toes, Very Tentatively, into JavaScript Chapter 17. Deploying Our New Code Part III. More Advanced Topics in Testing Chapter 18. User Authentication, Spiking and De-Spiking Chapter 19. Using Mocks to Test External Dependencies or Reduce Duplication Chapter 20. Test Fixtures and a Decorator for Explicit Waits Chapter 21. Server-Side Debugging Chapter 22. Finishing “My Lists”: Outside-In TDD Chapter 23. Test Isolation, and “Listening to Your Tests” Chapter 24. Continuous Integration (CI) Chapter 25. The Token Social Bit, the Page Pattern, and an Exercise for the Reader Chapter 26. Fast Tests, Slow Tests, and Hot Lava
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值