Document directory
- Benefits of Unit Testing
- Difficulties faced by unit testing
From today on, I will introduce test-driven Javascript development to you through reading notes. I am not sure whether this book has a Chinese translation. If you are interested, you can search for it or read the original English version directly. Reading Notes is a reference material for everyone. Therefore, the definition or explanation of many knowledge or concepts in this article may not be so accurate. As long as you can understand what I express, there is no need to be too truthful.
If you are interested in the qunit series, you can read it. It is a popular JavaScript testing framework. By studying qunit related knowledge, I feel it is necessary to enrich my theoretical knowledge. Only with a strong theoretical foundation can you be more instructive and targeted at your work.
This article describes the knowledge of unit testing, the benefits of unit testing, and the difficulties it faces.
What is unit test?
In short, a unit test is a piece of code used to test the production code. It sets one or more objects in a known state and runs the test code to check whether the output result is consistent with the expected value. He should have the following features: easy to develop, fast to run, testing software components in an isolated environment (each test cannot affect each other, maintain atomicity), and repeated execution. During unit testing, we may also need to use mock or stub to simulate dependency operations.
Unit testing should be run in the following situations:
• Verify the correctness of relevant functions after code development;
• When the code changes, verify that its behavior has not changed;
• When a new unit item is added to the system, ensure that its functions are correct and do not affect the system.
In actual work, you can use existing testing frameworks on the market for development, and there is no need to automatically create your own frameworks. In this way, the development time can be reduced and the development speed can be improved. In addition, other frameworks have provided well-developed APIs and frameworks that are relatively stable, you cannot guarantee that your framework is better than others. You can select multiple frameworks, such as qunit and jsunit. If you want to learn qunit, refer to my previous blog "qunit series" or go to the official website to learn.
Benefits of Unit Testing
Testing is an investment, but some people oppose unit testing and think it is a waste of time. Writing a unit test does take time, but if you don't use it, is there a better way. What we can do is probably only execute a low-efficiency manual test over and over again, and it may not be able to guarantee a full test of the system, or we will try again until the problem arises. Obviously, unit testing still has its value. Writing a test code and running it multiple times also requires maintenance of the test code.
1. regression testing
During the development process, we will inevitably make mistakes and generate some bugs that are not found in the production environment. Sometimes we fix this bug, but other errors may occur during the product release process, resulting in this bug not fixed or re-appearing. Regression testing can help us solve this problem. Before a product is released, it is prioritized to help us capture problems in real time and ensure product quality. As the system changes, the system will become larger and larger, and the business may become more and more complex. Traditional manual regression testing may be difficult to cope with (not to say that manual testing is not important), and automated testing will become more and more important.
I have a deep understanding of this point, because the previous companies are very important to the product quality, the system has established a complete set of mechanisms to help developers and maintenance personnel detect system problems in a timely manner. This mechanism includes recording system error logs, automated testing, building systems integrating [Code embedding-unit test-demo Server Release], and traditional manual testing. Before the product is released, run the automated test to ensure that the core functions of the system in the demo environment are not affected. After the automated test is passed, the product can be released. After the product is released, the automated test is run again to ensure that the core functions of the current network are correct. Automated testing plays an important role in ensuring product quality.
Of course, automated testing has its own advantages and disadvantages. Therefore, manual testing is required to jointly ensure product quality.
2. refactoring
Refactoring is to modify the code without affecting its function. For example, a helper method is extracted from a method to ensure code reusability. This is refactoring, or the object or method name is modified, which is also a refactoring. Restructuring plays a very important role in maintaining the robustness, reusability, and flexibility of the evolving system architecture design. Of course, you need to run the unit test after refactoring to ensure that your refactoring has no adverse impact on the system.
3. cross-browser testing
For Web developers, the code we develop must run on different platforms and clients. Unit Tests can effectively reduce the workload of our tests. This is what we call writing a test code and running it multiple times. We can run our test code on different clients on different platforms to ensure that our products work normally in different environments.
4. Other benefits
A well-written test is equivalent to a good instruction document for the system. It helps new developers better understand the system and related functions. In addition, writing and testing can also help developers to clarify the code development ideas.
Difficulties faced by unit testing
Some unit tests are not easy. Writing good unit tests requires constant practice, which is a challenge. The benefits of unit testing mentioned in the previous section are obtained based on best practices. If you write a bad unit test, the situation is completely different. You will find that this test is a waste of time and increases the maintenance cost.
To write a good unit test, you must ensure that your code is testable. When you introduce testing to a system with poor testability, you will find that your work is completely an insurmountable challenge. In turn, unit tests are helpful for exposing tightly coupled code and separation concerns.
This book will introduce how to write testable code and how to write a good unit test through some specific examples.
Learning Unit Tests are really good. In addition to improving the product quality, they are helpful for improving the quality of your code writing, so that you can develop a good habit of writing code.