Document directory
- Goals and objectives of test-driven development
- Promote test-driven development
- Benefits of test-driven development
Through the introduction to unit testing in the previous file, we learned how to reduce code defects, capture bugs Through regression testing, and reduce dependencies on public testing to improve development efficiency. In this chapter, we will focus on test-driven development (TDD), which is the Development Mode of test first and development. He has many advantages, such as better testable code, simpler interfaces, and code quality that can improve developers' confidence.
Goals and objectives of test-driven development
The objective of test-driven development is concise code. TDD is an iterative development process. Each iteration begins with a write unit test. unit test can be used as a standard document for developing functions. Short-term iterations provide timely feedback on Code, which makes it easier to find out our poor design. Before writing development code, you can write unit tests to improve unit test coverage.
1. After Development
In the traditional development mode, the problem can be solved only after all the code is written. Ideally, the code will have overall architecture considerations, but in many cases, especially during Javascript development, this is often not the case. This solution solves the problem by guessing which code can solve the problem, resulting in code swelling and tight coupling. If there is no unit test, the product code may contain code that is not running, such as the exception handling logic. Also, the boundary value may not be tested.
TDD reversed the development sequence. The first thing we do is not compile functions to implement code, but to consider the formulation of goals, what functions to implement and how to implement them. Unit tests serve as specifications and documents. The goal of TDD is not a test, so it cannot guarantee to do better in processing the boundary value. Although TDD generates additional unit test code, it improves the robustness of the system and ensures that the system does not contain unexecuted code.
2. Design In TDD
TDD is characterized by "no large pre-design" rather than "no pre-design ". To write clean code, we need to measure the duration of the entire project and consider the development lifecycle, so we need to make a plan. TDD will not automatically generate a good design, but it will help us evolve our design. Through dependency on unit tests, TDD focuses more on independent and isolated components. This method can help us write the code for receiving even. The Code follows a single responsibility and avoids unnecessary expansion. TDD provides strict control that can delay many design decisions until they are actually needed. He can better cope with changes in requirements, because we seldom design unnecessary features or do not need to develop as expected.
TDD drives us to deal with design. When new features are available, we need to develop rational use cases in the form of unit tests. When writing unit tests, we need to describe what problems we are solving. Encoding can be started only after this task is completed. In other words, TDD requires us to think about the results before providing solutions.
Promote test-driven development
The most important part of test-driven development is to run the test, which must be able to run quickly and easily. If this is not the case, the developer ignores the test and does not run the test after the new function is developed. This will make development a mess. The worst thing is that when we spend extra time using the TDD development model, we don't have the expected effect, and the code we developed is a mess. Therefore, it is very important to run the test smoothly.
We recommend that you use Autotest. Each test is saved in an independent file and can be run independently. He can display the test results on the screen, telling us which tests have passed (green), which tests are running, and which tests have failed (red ). This can improve the development efficiency and help us refactor the code. We only need to pay attention to the test failure.
Benefits of test-driven development 1. code that can work
The biggest benefit of TDD is the production of code that can work. A basic unit test ensures the stability of a piece of code. Renewable unit tests are especially useful in Javascript, and we need to perform tests on many browser platforms. The test code only needs to be written once. Through testing, you can quickly find codes that cannot work and discover bugs.
2. Follow the single Responsibility Principle
You can describe and develop components under isolated conditions to easily write codes that are decoupled and conform to the single responsibility principle. TDD unit tests do not need to test component dependencies. They need to be replaced by mock or stub. In addition, unit testing can also help us find the tightly coupled code in the program.
3. Force conscious development
Before each iteration, we write tests that describe specific functions first. TDD forces us to think before writing code. Thinking Before solving the problem helps us find a more reliable solution. Using use cases to describe features also helps us develop more concise code. This will also prevent us from introducing unnecessary features.
4. Improve Production Efficiency
If you are in touch with TDD for the first time, you will feel that all tests and steps require you to spend more time. Using TDD is not that simple at the beginning. Writing a good unit test requires constant exercises. This series of courses will teach you how to do this through many examples. When you develop good TDD development habits, it can indeed improve your development efficiency. It may take you more time to write functional code and test code, but it can reduce the time for manual testing, instead of running unit testing. The most important thing is that you have developed code that is guaranteed by unit testing and can work, and the work of code refactoring will not become so thorny. Your work will become faster, less stressful, and happier.