2 Basic principles
The basic idea of test-driven development is to write the test code first, then develop the functional code, and then only write the functional code that makes the test pass, so that the test drives the whole development process. This is helpful to write concise, usable and high-quality code, with high flexibility and robustness, and can quickly respond to changes and accelerate the development process.
The basic process of test-driven development is as follows:
① Add a test quickly.
(2) Run all the tests (sometimes only one or part of them is needed) and find that the newly added test failed.
③ Make some minor changes to make the test program run as soon as possible, so that some unreasonable methods can be used in the program.
④ Run all tests and pass them all.
⑤ Reconstruct the code, eliminate duplicate design and optimize the design structure.
Simply put, it is not runnable/runnable/refactoring-this is the slogan of test-driven development.
Essence and advantages
Perhaps only by understanding the nature and advantages of test-driven development can you appreciate its infinite charm. Test-driven development is not a test technology, but an analysis technology, a design technology and a technology for organizing all development activities. Compared with the traditional structured development process method, it has the following advantages: [3]
1) TDD compiles test cases and designs functional processes and interfaces according to customers' needs, and this kind of code design from the user's point of view is usually more in line with the needs of later development. Because we attach importance to user feedback, we can respond to changes in demand in time, and at the same time, because of the simple design from the user's point of view, we can adapt to changes faster.
2) Due to the requirement of easy testing and testing independence, we will realize loosely coupled design, and rely more on interfaces than concrete classes to improve the scalability and robustness of the system. Moreover, TDD obviously shortens the feedback period of design decisions, so that we can get feedback in a few seconds or minutes.
3) Putting tests before coding and running all tests frequently can avoid and find errors as early as possible, greatly reducing the cost of subsequent testing and repair and improving code quality. Under the protection of testing, the code is constantly reconstructed, repetitive design is eliminated, design structure is optimized, and code reusability is improved, thus improving the quality of software products.
4) TDD provides continuous regression testing, which gives us the courage to refactor, because the test will immediately inform us of any abnormality in other parts of the system caused by code changes. A complete test will help us track the status of the whole system, so we don't need to worry about any unpredictable side effects.
The unit test code generated by TDD is the most perfect developer document, which shows how all APIs should be used and how they work, and they are always up to date with the working code.
6) TDD can reduce stress, reduce anxiety, improve our confidence in the code, and give us the courage to refactor. These are all important prerequisites for happy work.
7) The development efficiency has improved rapidly.
Present situation and prospect
Test-driven development technology has been paid more and more attention, but due to the short development time, the related applications are not very mature. Nowadays, more and more companies are trying to practice test-driven development, but it is difficult to practice because it requires more developers and violates their traditional thinking habits. Many famous software companies in the United States, such as IBM, began to transform to agile very early. In this process, TDD is usually the most important and difficult one. As Sue Mckinney, vice president of IBM's development transformation department, said: The prospect of test-driven development is very attractive, but "we may also pay the most in this process." Dave West, a senior analyst at Forrester, believes that test-driven development (TDD) is like the Holy Grail, but "it is worthwhile to work hard to achieve this goal."
In my opinion, in the process of promoting test-driven development, the first problem is to change the thinking concept and ideology that developers have formed for a long time. Developers only like coding and don't like testing, and they can't understand why they should write unit tests first without product code. Secondly, related technical support and test-driven development put forward higher requirements for developers, not only to master testing and refactoring, but also to know design knowledge such as design patterns.
Just like every revolutionary product has to go through a difficult course at the beginning, test-driven development is also going through, but it is gradually maturing and has a bright future. I believe that in the next few years, more and more domestic software companies will begin to popularize test-driven development.