当前位置:网站首页>什么时候应该编写单元测试?什么是TDD?
什么时候应该编写单元测试?什么是TDD?
2022-04-23 01:36:00 【魏波-】
一、传统方法与TDD方法
1、有人认为软件编码完成后编写单元测试(传统方法),流程如下:
缺点:编写完功能代码再写单元测试会导致单元测试“粒度”比较粗。对同样的功能代码,如果采用TDD方案,结果可能是用10个“小”的单测来覆盖,每个单测比较简单易懂,可读性可维护性都比较好,重构时单测的改动不大;如果用传统方法写的单测,往往是用1个“大”的单测来覆盖,这个单测逻辑就比较复杂,因为它要测的东西很多,可读性可维护性就比较差。
2、后来越来越多的人选择在软件编码之前写单元测试,这种方法成为测试优先或测试驱动开发(Test-Driven Development,TDD),流程如下:
说明:首先编写一个失败的测试,然后创建产品代码,并确保这个测试通过,接下来是重构代码或创建另一个会失败的测试。具体流程是先写少量功能代码,紧接着写单元测试,重复这两个过程,直到完成功能代码开发。这样做的结果是:基本上功能代码开发完,单元测试也差不多完成了。
(1)编写一个会失败的测试,以证明产品中代码或者功能的缺失。
编写测试的时候,要假设产品代码已经能工作了,这样测试的失败就说明产品代码中有缺陷。这个测试最初会编译失败,只有在添加了需要的代码后,编译才能通过,然后测试可以运行,但是会失败,因为还没有实现所需的功能。
(2)编写符合测试预期的产品代码,使测试通过。
(3)重构代码。
如果测试通过了,你就可以编写下一个单元测试或者进行重构,使代码可读性更强或者去除重复代码等。重构可以在编写多个测试后进行,也可以在每个测试后都进行。重构是一项重要的实践,他确保代码更易读,更好维护,同时还依然能通过之前编写的所有测试。
二、成功进行TDD的三种核心技能
成功进行TDD需要三种技能:
1、编写优秀的测试,即可维护、可读、可靠。
2、在编码前编写测试。
3、良好的测试设计。
版权声明
本文为[魏波-]所创,转载请带上原文链接,感谢
https://weibo01.blog.csdn.net/article/details/124315663
边栏推荐
- Jerry's AI server [chapter]
- DFS parity pruning
- The most understandable life cycle of dependency injection
- 智能手表的下半场,机遇与挑战并存
- Introduction to gbase 8s storage structure and space management
- Completely uninstall antidote 10? What if the antidote uninstall is not clean?
- Basic operation of Android local database | multi thread operation database | addition, deletion, modification and query of database | batch insertion into database | basic use of thread pool | Yu nia
- VSCODE + PHP Debug + 名字空间指引
- "Self abuse artifact" exploded overnight: control your face with a handle, take your own code, and bear the consequences
- Analysis of uboot directory structure
猜你喜欢
Counting garlic customers: Sudoku (DFS)
Chris LATTNER, father of llvm: the golden age of compilers
安装mysql出问题求解决
四级城市地区表 xlsx, sql文件,国内,中国省市县街道乡镇四级地址 (名称,联动ID,层级,是否末级(1-是))
What is October 24th? Why are there no senior programmers in China in their fifties and sixties, while foreigners,,, Yu Nianyu Hui take you to celebrate the 1024 programmer Festival
In the context of Internet plus, how can enterprises innovate customer service?
引爆炸弹(DFS)
Is 2022 software testing easy to learn? How long will it take? (learning roadmap attached)
Introduction to Alibaba's super large-scale Flink cluster operation and maintenance system
代码实现发邮件---sendemails
随机推荐
Jerry's AI server [chapter]
Chris LATTNER, father of llvm: the golden age of compilers
Use yolov4 on colab
Unity结合iTextSharp生成PDF 准备dll
Counting garlic customers: Sudoku (DFS)
Introduction to Alibaba's super large-scale Flink cluster operation and maintenance system
gin -get请求的小示例1-Handle处理GET请求
Linux系统下以RPM方式如何安装mysql-5.7.9
Blocking granularity of gbase 8s concurrency control
DFS奇偶性剪枝
[registration] tf54: engineer growth map and excellent R & D organization building
What is October 24th? Why are there no senior programmers in China in their fifties and sixties, while foreigners,,, Yu Nianyu Hui take you to celebrate the 1024 programmer Festival
Gbase 8s存储结构简介及空间管理
engine.POST()处理POST请求
Vscode + PHP debug + namespace guidelines
Blocking type of gbase 8s concurrency control
Full Permutation (DFS and next_permutation solution)
UVC camera encapsulation class
清研环境深交所上市:年营收1.8亿 市值41亿
(product resources) mingdeyang ad8488 module high performance digital X-ray FMC interface 128 analog channel high-speed ADC chip