Preface
Recently, I have seen a survey on the Internet that describes how programmers write unit tests in project development ". Of course, as for the results of the investigation, I think you can already guess it. A Ratio of up to 58.3%. In general, no unit test is written, and only occasionally write data. 16.6% of Programmers never write unit tests. Only a few programmers Perform unit tests in their own code and ensure that the method test passes. What do you think of when you see this?
Status quo
Although this survey may be one-sided, it basically reflects the development status of programmers in China. Few programmers can carefully write unit tests. Moreover, some programmers do not even know why to write unit tests (which makes me very depressed ). They often say that there are not Testers in the company. testing should be what they want to do. Our job is just development (this guy must have never studied software engineering ). Of course, these are not accidental. As the Buddhist scriptures refer to the "causal cycle", there must be a cause. So what is the reason why programmers are so cool about unit testing?
Found
Through discussions with a few friends and online surveys, the main reasons are as follows. As a result, programmers are very reluctant to perform unit tests.
- I don't know how to write unit tests.
- Project is not required, so do not write
- Unit testing is a waste of time because it is of low value.
- The business logic is relatively simple and it is not worth writing unit tests.
- In any case, the integration test will catch all bugs and no unit test is required.
- In the early stage of the project, we try to write unit tests as much as possible, but the more we get out of control at the later stage of the project.
- In order to complete the coding task, there is not enough time to write unit tests. Writing unit tests may result in a failure to complete coding tasks on time, resulting in project delays.
Obviously, in the final analysis of these reasons, I don't know much about unit testing. I think I am very smart and I am not willing to perform tests, so I am not able to control the project's time and progress. Next, I will analyze and analyze the development psychology of programmers, so as to give friends a wake-up, and finally be intelligent.
Analysis
I don't know how to write unit tests.
This problem is that you have not been involved in unit testing and have never experienced enterprise-level code development. I don't know what unit tests can bring to you. Imagine, when you have developed a functional module, how do you determine that your module has no bugs? If specific services are involved, will you execute the debug mode and go deep into the code? If you have always been like this, you have already been out. Get to know the unit test tool, and you will get a lot of money.
Project is not required, so do not write
This problem reflects a phenomenon and also a habit. If there are any requirements for a project, it can only indicate that the project management is not strict, not the reason why programmers do not write unit tests. In their past development, they did not develop a good habit of writing unit tests. As you can imagine, I am afraid to compliment their code quality. Give a suggestion and try to write beautiful code. The reason for being beautiful is that it is healthy, concise, and robust. Of course, unit testing is essential to complete beautiful code.
Unit testing is a waste of time because it is of low value.
This statement is actually incorrect. Why? In daily development, Code Completion is not the same as development completion. If there is no unit test, how can we ensure that the code runs properly? Testers only perform business integration testing, that is, black box testing. There is no way to test a single method, and the scope of the tested bug is also very wide, the scope of the Bug cannot be determined at all, and it takes time to determine where the bug is located. Isn't that a waste of time? Even in this way, more time is wasted.
The business logic is relatively simple and it is not worth writing unit tests.
The so-called business logic is relatively simple, but it is actually relative. When you are familiar with a piece of business logic, you will naturally think it is very simple. However, the necessity of unit testing is not just to test whether the code is functional correctly, but also to know your business as other colleagues, you can quickly get familiar with the code functions through unit tests, and even know what it has done without having to read the code. Therefore, writing unit tests not only free yourself, but also make it easier for others.
The project is still being tested in the early stage, and it will be out of control in the later stage.
This problem is caused by lack of control over the project progress, the research time of technical points in the project, the communication of personnel, and the degree of familiarity with business needs. This is not a personal issue, but a reflection of the problems in project management. Of course, personal reasons also exist, that is, how to improve efficiency in a limited time. You need to think about this. I suggest you make more plans and change the plans based on the actual situation. Communicate with the project leader and members. Respond to problems in the project in a timely manner.
Insufficient time to write unit tests to complete coding tasks
This problem is that the tasks that programmers receive are complicated or their development efficiency needs to be improved. In fact, development tasks include coding and unit testing. When receiving tasks, you should communicate with the team lead or manager based on your own abilities to set aside some testing time. Of course, it is also necessary to improve the coding efficiency. As for how to improve development efficiency, there are many such articles on the Internet, so I will not repeat them here.
Importance
Testing is often a very boring activity for programmers. What can a test bring to us? It is very important to understand this. testing cannot ensure that a program is completely correct, but testing can enhance our confidence in program integrity, testing can convince us that the program did what I expected it to do. Tests enable us to detect program bugs and deficiencies as early as possible.
The longer a bug is hidden, the higher the cost of fixing it. A large amount of research data has been cited in the quick software development book, pointing out that the price of modifying a bug is 10 times the price of modifying it when a bug is generated.
Of course, we mainly discuss unit testing. Unit testing is a method-Level Test and the most fine-grained test. Each method used to test a class has met the functional requirements of the method. During development, modules developed by myself can be submitted to the svn or git Library only after the unit test is passed.
Application
It is precisely because of the important position of testing in development that it will take a torch of TDD in the IT industry. TDD, that is, the test-driven development model. It aims to emphasize writing test code before developing functional code. That is to say, after developing a function, first think about how to test the function, write the test code, and then write the relevant code to meet these test cases. Add other functions cyclically until all functions are developed.
Tools
So many tools (frameworks) are available to help us complete repeated unit tests? Below I will introduce several common ones. This section only describes the Java language. For other languages, Google ).
- JUnit (junit4 is recommended)
JUnit is often used in daily development, and various Java IDES (Eclipse, myeclipse, and intellij idea) Integrate the JUnit components. Of course, it is convenient to add plug-ins by yourself. The JUnit framework is an all-in-one solution for Java unit testing. This framework is commendable because it introduces the test-driven development idea to Java developers and teaches them how to effectively write unit tests.
Testng, testing next generation, the next-generation testing technology. It is a testing framework built based on JUnit and nunit ideas and the annotation Technology of JDK to enhance the testing function and enhance the testing organization structure with the help of XML files. The strength of testng is that it can be used not only for unit testing, but also for integration testing.
Here we will only introduce the most commonly used Java unit testing tools. We will discuss how to use these tools in subsequent blogs. If you are interested, please pay attention to the subsequent articles.
Conclusion
As the saying goes, how can a house be swept away. During development, our own code cannot ensure the correctness of the function. So what is the efficiency? After doing more tasks and writing more code, you just need to build a nest and do the same repetitive work on the machine. There is a principle in the IT field, and the dry principle -- don't repeat yourself! Only by constantly checking and testing your work can you continuously break through and stand out. Of course, you can continue to improve.
Test day up! Experience day up! And money day up too!
Oh, mom. Chinese English is coming again!
Let's talk about unit testing (1): Why do we need to perform annoying unit tests?