Why do you write unit tests? Advantages and benefits of unit testing

Source: Internet
Author: User
Tags requires

Why do you write unit tests? The reason is that unit testing has a lot of advantages, can give us a lot of help.

Advantages of Unit Testing

1. Help developers write code to improve quality and reduce bugs. If you analyze the makeup of the causes of our bugs, I think there is a part of the bug because developers don't take some case or marginal conditions into account when they write work code. This problem is caused by a number of reasons, one of the most important reason is that we do not think about the functions of the work code to complete, and writing unit tests, especially the first to write unit tests and then write work code can help developers think about what the code is going to implement what functionality. For example, to implement a simple user registration function of the business class method, with unit test and write work code to work in the way

Developers will first consider various scenarios, such as normal registration, user name duplication, failure to fill in the necessary content ... And so on, and then write the relevant test cases

public Class UserSerivceTest(){
public userRegister_Ok(){
......
}
public userRegister_nameDuplicated(){
......
}
public userRegister_emailEmpty(){
......
}
}

The process of writing unit test code is to induce developers to think about the process of implementing content and logic of work code, and then to implement working code, the developer's thinking will be clearer and the quality of the code will be improved accordingly.

2. Improve the speed of feedback, reduce duplication of work, improve development efficiency. Developers implement a feature or fix a bug, and if there is a unit test support, developers can immediately verify that the completed code is correct by running unit tests without having to iterate through the release of the war package, start JBoss, Verify the functionality that you have done by using cumbersome steps such as entering data into your browser. The efficiency of validating Code with unit test code and using it in a manual way to validate code is far less efficient, and it's painful to see many developers repeatedly executing n-post scripts every day (such as Antx).

3. Ensure that your final code changes do not break the functionality of the previous code. The bigger the project, the more code, especially related to some of the common interface code or the underlying base library, no one can not guarantee that the modified code will not destroy the previous function, so the relevant requirements will be shelved or postponed, because dare not improve the code, the code has become increasingly difficult to maintain, the quality is getting worse. Unit testing is a good way to solve this problem (not daring to say the best). Because the history function of the code has the corresponding unit test guarantee, after modifying some code, by running the related unit test can verify whether the new adjustment function has the influence to the previous function. Of course, to achieve this level requires a lot of pay, not only to be able to achieve a relatively high test coverage, and unit test code to write quality must be guaranteed.

4. Make code maintenance easier. As many unit tests are written to the code, which is equivalent to adding specifications to the code, developers can also help developers understand existing code by reading Unit test code. Very opensource projects have a considerable amount of unit test code that can help you understand the production source code by reading these test codes.

5. Helps improve code quality and design. In addition to the code written by the Montana, I believe that a lot of code that is easy to maintain and design well is achieved through constant refactoring. Although the unit test itself does not directly improve the quality of the production code, it provides a "safety net" for the production code, allowing developers to bravely improve the code so that the clean and beautiful code is no longer a dream.

Disadvantages of Unit Testing

1. The learning cost of unit testing is higher. There are a lot of techniques involved in writing unit tests, and if you're simply using a base unit test framework such as JUnit or testng, it's often difficult to deal with complex unit tests, so many third-party frameworks and technologies (EASYMOCK,JMOCK,DBUNIT, etc.) are bound to The study of these frameworks and techniques will also increase the cost and difficulty of learning.

2. Writing unit tests increases the programmer's workload. Unit tests are the same as the production code, and will not be used for testing is different, developers also face the test code writing, maintenance and so on, but also to avoid repeated code, such as a series of problems, whether to write good test code or depends on the design and coding capabilities of developers.

3. The promotion and application of unit testing requires considerable input. Only when every developer has written enough, good quality unit test code can we really enjoy the benefits of unit testing. Before reaching this level, a lot of implementation and resource inputs are required.

Summarize

Although unit testing has some drawbacks and negative effects, it is worth the effort to overcome and resolve these shortcomings compared to the merits of unit testing.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.