当前位置:网站首页>When should I write unit tests? What is TDD?
When should I write unit tests? What is TDD?
2022-04-23 01:39:00 【Wei Bo-】
One 、 Traditional methods and TDD Method
1、 Some people think that writing unit tests after software coding ( traditional method ), The process is as follows :
shortcoming : Writing unit tests after writing functional code will lead to unit tests “ Particle size ” It's coarser . For the same function code , If the TDD programme , The result may be 10 individual “ Small ” Single test to cover , Each single test is relatively simple and easy to understand , Readability and maintainability are good , The single test changes little during reconstruction ; If you write a single test in the traditional way , It's often done with 1 individual “ Big ” Single test to cover , This single test logic is more complex , Because it has a lot to measure , Readability and maintainability are poor .
2、 Later, more and more people chose to write unit tests before software coding , This approach becomes test first or test driven development (Test-Driven Development,TDD), The process is as follows :
explain : First write a failed test , Then create the product code , And make sure that this test passes , The next step is to refactor the code or create another test that will fail . The specific process is to write a small amount of function code first , Then write unit tests , Repeat the two processes , Until the function code development is completed . The result is : Basically, the function code has been developed , Unit testing is almost complete .
(1) Write a test that will fail , To prove the lack of code or function in the product .
When writing tests , Assume that the product code is already working , In this way, the failure of the test indicates that there are defects in the product code . This test will initially fail to compile , Only after adding the required code , Only after compiling can it pass , Then the test can run , But it will fail , Because the required functions have not been realized yet .
(2) Write product code that meets the test expectations , Make the test pass .
(3) Refactoring code .
If the test passes , You can write the next unit test or refactor , Make the code more readable or remove duplicate code, etc . Refactoring can be done after writing multiple tests , It can also be done after each test . Refactoring is an important practice , He makes sure the code is easier to read , Better maintenance , At the same time, it can still pass all the tests written before .
Two 、 Succeed in TDD Three core skills
Succeed in TDD Three skills are required :
1、 Write excellent tests , You can maintain 、 Can be read 、 reliable .
2、 Write tests before coding .
3、 Good test design .
版权声明
本文为[Wei Bo-]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230136077734.html
边栏推荐
- 电子采购如何成为供应链中的增值功能?
- 2n皇后问题
- [蓝桥杯][2019年第十届真题]外卖店优先级
- Soatest preliminary understanding
- Introduction to PCIe xdma IP core (with list) - mingdeyang science and Education (mdy edu. Com)
- Detonate the bomb (DFS)
- Traversal of ladder race l2-6 tree
- Summary of LSF usage
- (product resources) mingdeyang ad8488 module high performance digital X-ray FMC interface 128 analog channel high-speed ADC chip
- Rôles attributs personnels Abréviations
猜你喜欢
How to introduce SPI into a project
Innovative practice of short video content understanding and generation technology in meituan
Chapter 9 of C language programming (fifth edition of Tan Haoqiang) analysis and answer of exercises for users to establish their own data types
FL studio20.8最新中文版本安装下载图文教程
Redis implements distributed locks
CDR2022首发全新版本性能介绍
NR polar code VII - SCL (successful cancellation list coding)
In the context of Internet plus, how can enterprises innovate customer service?
[经验教程]支付宝余额自动转入余额宝怎么设置关闭取消支付宝余额自动转入余额宝?
Google developer tool preserve log
随机推荐
Vscode + PHP debug + namespace guidelines
Counting garlic customers: Sudoku (DFS)
角色個人屬性英文縮寫
Prince saves Princess (DFS)
Use Charles to grab app package
角色个人属性英文缩写
Learning of gin framework -- golang
安装mysql出问题求解决
Itextsharp infrastructure
mb_substr()、mb_strpos()函数(故事篇)
gin--hello
Android sqliteopenhelper data table structure upgrade
四级城市地区表 xlsx, sql文件,国内,中国省市县街道乡镇四级地址 (名称,联动ID,层级,是否末级(1-是))
Jerry's AI server [chapter]
学习方法与职业发展指南(2022年版)
引爆炸弹(DFS)
The most easy to understand service container and scope of dependency injection
第六章 使用 matplotlib 绘制热力图
DO447管理用户和团队的访问
Chapter 6 uses Matplotlib to draw thermodynamic diagram