How to promote Unit Testing

Source: Internet
Author: User
How to promote Unit Testing
Ren Jialin posted on 9:05:00
I used to write a blog post about how to promote unit testing. Recently, a friend asked me the following questions, so I wrote this article again. When reading this article, please combine the original blog post.
Problem:
Some developers think that unit tests will take a lot of time to write test cases. Therefore, they have a low willingness to perform unit tests. Do you have any suggestions for improving unit tests?
Answer:
1. First, define the meaning of the unit. A unit refers to a class in an object-oriented program and a function in a structured method.
2. The unit test method should be clarified. Common Unit testing methods include:
(1) static check: use static code check tools to analyze internal logic of the program to analyze possible errors or bad tastes in the program.
(2) Dynamic Testing: Write unit testing programs, design unit test cases, and test the logic correctness of each function or class.
3 If a class or function is highly dependent on other classes or environments and needs to write a large number of programs or drivers, it just shows that there is a problem with the design of this class or function, it violates the basic design principles of "low coupling", which is also one of the functions of "test-driven development" advocated in agile methods.
4. Quality input and output are also a balance. To what extent should we invest in unit tests, first of all, a company's management policy. If each unit performs a unit test, the size of the test code and the size of the product code can reach. That is to say, the workload of writing the test code is relatively large, however, we also need to see the unit test output. In unit testing, integration testing, and system testing, unit testing is the most input-output ratio test type. That is, the number of defects detected by unit testing in a unit time is greater than that of integration and system testing. In principle, the unit test has the largest investment, the most defects found, and the integration test and system test decrease sequentially.
5. The following methods can be used to promote unit tests in practice:
(1) increase the intensity of static checks. Static check tools can be used to quickly identify errors, warnings, and bad tastes in the program. The company can specify which warnings and bad tastes must be modified, note that it may take a lot of effort to modify all warnings and bad smells. Static check is a unit test method with a high input-output ratio. Check style, source monitor, PMD, find bugs, and jslink can be used in Java.
(2) reduce the workload of the test program by selecting the test policy. Unit tests generally have three strategies:
Policy 1: bottom-up policy: first test the underlying functions or classes, and then test the upper-layer functions or classes. At this time, you only need to write the driver program without writing the pile program.
Policy 2: top-down policy: first test the upper-layer functions or classes, and then test the underlying function classes. At this time, you only need to write the program, and do not need or need to write the driver.
Policy 3: hybrid strategy: Based on the above two strategies, You need to compile the pile program and the driver.
If the tested unit needs to call many other units, you can use a bottom-up policy to reduce the amount of writing the driver. If the unit to be tested requires many external environment preparations, you can adopt a top-down policy.
(3) The timing for executing unit tests can be specified at the organization level, for example:
I) The most core and key functional modules in the system;
Ii) function modules with complex algorithms;
Iii) function modules with the most errors;
Iv) The most commonly used functional modules of the customer;
V) reusable underlying code
According to the Law of Pareto, we can select a small part of code to execute unit tests.
Unit 6 Testing Technology
(1) xunit tool.
(2) The following methods can be used to generate test cases:
I) unit Function Analysis
Ii) entrance parameter equivalence Analysis
Iii) interface parameter boundary analysis
Iv) Full-process variable, shared data equivalence and boundary analysis
V) Equivalence Class and boundary analysis for calling function return values
Vi) coverage rate analysis
The strict requirements of the above methods can be gradual, and the amount of effort required to be invested varies.

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.