当前位置:网站首页>Overestimated test driven development?
Overestimated test driven development?
2022-07-29 03:35:00 【Luo Zhaocheng CSDN】
Test-driven development (TDD) It began in the last century 90 years , From time to time, from time to time , Only a few developers are still practicing . From the perspective of software developers , Once again, it helps us define test driven development , Answered many development questions TDD Common fallacies .
author | Tylor Borgeson, Authorized by author
translator | Luo Zhao Cheng
original text | “Test Driven Development is overrated”
This article was first published in CSDN WeChat (ID:CSDNnews)
The following is the translation :
This is me 「 Popular software development practices 」 The second part of the series , In this series of articles , My plan includes a series of methods for software engineers to improve software development by improving development processes and practices . I was there ThoughtWorks As a software consultant , Now I work in a large retail company in Germany , These methods have been studied and tested in my career .
I once discussed with a customer's developer about software development . Our discussion is a little too much , He mentioned , As a software developer, I'm very lucky , because “ We can cheat the company into paying a considerable income for our basic work ”…… In my submission , Regardless of your programming level , You can't belittle so many software development engineers .
We talked like silk , The discussion gradually goes deep into the practice of agile software development . For the methods mentioned in agile development , They said they could try , Used to improve the current way of working , however , When I mentioned test driven development , They all feel :
Test driven development doesn't live up to its name .
Hear their comment , I was shocked . meanwhile , They also made me realize , In many people's minds , Test driven development is just a way of agile development practice , In the hearts of more people , It is more like a mirage , It looks beautiful , But it's just like this . In order to let more people know about test driven development , I want to use this article to tell you what is test driven development .
First , Let me define what is test driven development (TDD).
seeing the name of a thing one thinks of its function ,TDD Is a software development strategy , It guides the development process by writing tests .Kent Beck 2003 In his 《 Test-driven development 》 This concept is mentioned in the book ( translator's note :Kent Beck, Famous American software engineer and writer , He has made great contributions to software engineering . He is Smalltalk Software developers , A pioneer of design patterns , Supporters of Test Driven Development , It is also one of the founders of extreme programming .), The implementation of test driven development mainly follows the following three steps :
Write a failed test case for a small part of the function you want to write .
Realize your functional logic , Let your test case pass .
Refactoring code , Ensure the structure and readability of the code .
This process can be simplified to “ Red , green , restructure ”.
below , I will use the pattern of test driven development , use Python Realize the calculation of oblique boundary value in Pythagorean theorem .

△ Write the test code first , Don't worry about the implementation of the method

△ Run test code , A red error appears

△ Implement code logic , Let the test code pass

△ Refactoring code , Make code logic more elegant
This year is already 2020 Years. ,17 Years have passed , For many developers , The benefits of test driven development are still uncertain , therefore , Most developers have never used TDD .
all the time , I am keen to recommend it to my friends TDD , But there are still many people who choose to refuse , When I asked why , Here are some of the answers I hear most :
We have a testing team , Writing tests is their job .
When writing test cases , need mock Context object , So we need to spend more time and energy .
It doesn't bring me much benefit .
It's too slow .
below , Let me analyze these answers .
scene 1:“ We have a testing team , Writing tests is their job ”
I always thought , Who wrote the code , Who should ensure that it can work normally , So every time I think of this answer, I feel funny . If you think that only writing logical code is your job , Then your ability will stagnate .
The code written by every programmer should have the following properties :
Choose and use the right technology stack according to business needs
The code is easy to understand
Testable
Scalable
Simple
In my submission , Give the unit tests that developers can do to the testing department , Is a very unwise act . The testing department has more and more important things to do , Instead of wasting time doing black box tests .
scene 2:“ When writing test cases , need mock Some context objects ”
Your thoughts and feelings of helplessness , I can understand , I used to be like you . There is a way in front of you , This method has three different parameters , And every parameter is an object , meanwhile , Their properties are not empty . Now you need to test this method , Meet the test conditions , A lot of... Needs to be done Mock, This can lead to a lot of extra code written to test a method .
Now? , I have been able to deal with this problem well , And I think it's worth doing these things .
Whenever I think about how to reduce the cost of writing tests , These two things will haunt my mind , Namely SOLID( translator's note :S Refers to the principle of single responsibility ,O Opening and closing principle ,L Richter's principle of substitution ,I Interface isolation principle ,D The principle of Dependence Inversion ) And test pyramid .
It's important to note that ,S —— Principle of single responsibility , This principle tells us , A class or a method can only do one thing , This prevents them from having side effects on other parts of the system .
That's true. , The code logic in a company is certainly not as simple as adding two integers , however , If we can simplify the responsibilities of these methods , Then it will become very easy to write tests .
△ Test pyramids , source :martinfowler
This test pyramid is usually used to measure the priority of different test types .
UI Testing and service testing can take a lot of time , Cost is very high , Unit tests can run in milliseconds , So a system should have more unit tests , Less service testing and UI test . Doing so and DevOps Consistent goals , It can increase the feedback cycle in the whole system development process .
Unit testing requires the new code to be coupled with the original code in the system as little as possible , This makes it easier to code unit tests .
Test driven development is a development mode based on unit testing .
Of course , It's much easier to say than to do . For all types of tests , There will be costs , Unit testing is no exception . If one day , You find that the cost of writing unit tests has increased a lot , You can ask yourself , Are there the following two questions :
Whether the current test is within the scope of unit test , Whether it is necessary to upgrade to the scope of service testing ?
Is your code structure reasonable , Whether your classes and methods have a single responsibility ?
If you are writing unit test code , Need to constantly invest a lot of costs , That means , There are some problems with your code , You need to refactor it , Make the code structure clearer .
scene 3:“ It doesn't bring me much benefit ”
It's funny , Most people who say so , Are people who don't really use the test driven development mode . It's on paper , We must know that we must do it .
below , I'll show you how to use TDD Two big benefits :
The first one is obvious , according to TDD The definition of , Before writing real logic code , All need to be tested before writing , The code produced in this way is self-test . just as Martin Fowler( translator's note :Martin Fowler, Famous British Software Engineer , He is also a writer and internationally renowned speaker in software development , Focus on object-oriented analysis and Design , Unified modeling language , Domain modeling , And agile software development methods , Including extreme programming .) said :
If your code is self testing , So as long as you run a series of automated tests against the code , Wait until it passes , You can guarantee that your code will not show any real defects .
let me put it another way , Your code can run normally according to the process you defined in the test code .
Unit tests are very useful , It can make you confident in your code . In refactoring , It can find the problems caused by refactoring at the first time , also , And unit testing can also let you know whether there is Bug .
Self testing code can benefit the whole team in the process of development and integration .
Use TDD Another benefit is , It allows developers to write logical code before , Think carefully about what kind of code to write , And how to write code . Thinking before coding , It allows developers to really go deep into business needs , Consider possible boundary problems and possible challenges , This process is time-consuming and painful , But it's not a waste of developer's energy . More Than This , Writing the test first , It will also encourage developers to write code from the beginning , Consider the design and architecture of the system , This can greatly improve the system scalability .
scene 4:“ It's too slow ”
In my experience , People who have this idea , Most of them are those who try to use TDD People who , However, they use the previous development mode , Write code first. , Finally, let's write the test . This is also the reason why most developers cannot adhere to the implementation TDD One of the most common reasons .
I deeply understand the thoughts of these developers , Before writing code , Think first , Then spend time writing test code , Compared with not writing tests , It really takes more time . occasionally , simulation mock It is not only troublesome to generate effective data in the scene , It will also make people feel very depressed .
Although I say a lot TDD The advantages of , But it doesn't help , Many companies' business departments take the time of development and delivery as a measure , The whole team is working hard to develop new requirements .
In Agile Development , There are four key indicators to measure software development ( I often refer to these indicators ), Two of them are change failure rate and deployment frequency . As I said above , Suppose the system uses TDD structure , When you submit code , The whole team will know whether the modification of the code will lead to the collapse of other functions of the system , At the same time, it can improve the efficiency Bug The speed of being found . The locality of unit testing , Developers can quickly find the cause of the problem . So back and forth , The possibility of causing abnormal functions will be smaller and smaller , Thus reducing the change failure rate .
The code passed unit tests , It can also make us have stronger confidence in the robustness of software functions , The whole team can decide whether to deploy through the unit test results , Anyone can deploy on demand .
《 Phoenix Project 》 Four types of work are listed in , One of them is “ Unplanned work or fire fighting work ”. This kind of work is usually caused by mistakes , Developers must stop working and give priority to . Self testing code can minimize such errors , That means , It can help us minimize “ Unplanned work or fire fighting work ”, It can also maximize the satisfaction of developers . Happy development is good development .
When developers don't have to worry about online problems , They can focus more on the development of new functions . Before they write code , Have considered how to write code , Can write in line with SOLID Code for principles . The code will be refactored constantly in the development , Technology debt will also be minimized . The team is confident in code quality , It can be deployed online at any time , This will make the whole team run faster .
Everything is difficult at the beginning , Like other tools and practices , At the beginning of test driven development , It may be very uncomfortable , And it will reduce your development efficiency , however , Just like the father of continuous delivery Jez Humble said :
Do things that make you feel painful early and often .
Now start your test driven development journey , Until it makes you feel comfortable . Thanks for reading .
quote :
Series reading
1. Why continuous integration and deployment are important in development ?
2. Overestimated Test Driven Development ?
3. Why are programmers so “ Dislike ” Backbone development mode ?
4. Why don't programmers make blind efforts ?
边栏推荐
- Various minor problems of jupyter notebook, configuration environment, code completion, remote connection, etc
- Singleton mode (hungry and lazy)
- 1.4 nn. Module neural network (II)
- 深入C语言(2)——结构的定义与使用
- Score addition and subtraction of force deduction and brushing questions (one question per day 7/27)
- How to realize shortcut keys for interface scaling by vscade
- MOS管 —— 快速复苏应用笔记(贰)[参数与应用]
- 深入C语言(3)—— C的输入输出流
- three.js 第五十四用如何给shader传递结构体数组
- Matlab learning - accumulation of small knowledge points
猜你喜欢
![MOS管 —— 快速复苏应用笔记(贰)[参数与应用]](/img/54/eb040a51304192def8cfb360c7c213.png)
MOS管 —— 快速复苏应用笔记(贰)[参数与应用]

(nowcoder22529C)dinner(容斥原理+排列组合)

Rdkit I: using rdkit to screen the structural characteristics of chemical small molecules

暴力递归到动态规划 01 (机器人移动)

Singleton mode (hungry and lazy)

(2022杭电多校三)1011-Link is as bear(思维+线性基)

ShardingSphere之水平分表实战(三)

exness:鸽派决议帮助黄金反弹,焦点转向美国GDP

for_ Example of each usage

Bingbing learning notes: operator overloading -- implementation of date class
随机推荐
AI_ Drug: VAE of molecular generation model (I)
mysql的timestamp存在的时区问题怎么解决
How close can QA be to business code Direct exposure of defects through codediff
Matlab learning -- structured programs and user-defined functions
GJB常见混淆概念
Sleuth+zipkin to track distributed service links
Simple understanding of CDN, SDN and QoS
Practical guidance for interface automation testing (Part I): what preparations should be made for interface automation
容斥原理
The difference between int and integer. Is int or integer used in practical applications?
Rdkit: introduce smiles code, smart code and Morgan fingerprint (ECFP)
How to deploy sentinel cluster of redis
A simple and general method to obtain the size of function stack space
深入C语言(2)——结构的定义与使用
Redis之sentinel哨兵集群怎么部署
Complexity analysis learning
Asynchronous callback future mode of concurrent mode
(2022杭电多校三)1011-Link is as bear(思维+线性基)
Shardingsphere's level table practice (III)
Naive Bayes -- continuous data