How to do unit testing well

Source: Internet
Author: User

Objective

Unit testing is the test of the basic components of the software, is a category of white box testing, it mainly through the logical structure of the code analysis to design test cases. In dynamic testing, unit testing is a very efficient test method and is the first test in a software testing cycle. From a cost perspective, the sooner the defect is found, the better the strength of the unit test to reduce the defect location and repair difficulty, thereby reducing the cost of defect resolution, while strengthening unit testing also reduces the burden of subsequent integration testing and system testing. According to the industry statistics, a BUG in the unit testing phase found that the cost is 1, to the integration test becomes 10, to the system test as high as 100, to the actual market volume after the postpartum up to 1000. However, unit testing is not good in the current domestic software enterprises, on the one hand, due to the lack of attention to unit testing, test input is insufficient, on the other hand, the unit testing practice is not enough, the unit test is in a state of groping.

The quality of the software is determined by three dimensions of organization, process, and technology, and no one dimension can determine the quality of the software alone. Good organization structure can ensure the smooth implementation of the process, good process can improve the standardization and controllability of software development, so as to improve the efficiency and quality of software development, and the use of good technology and the carrier of good technology-people, then fundamentally ensure the quality of software.

In a word, the organization, process and technology are the software quality triangle, this article will discuss how to do the unit test from these three aspects.

  • Organizational structure should ensure that test groups participate in unit testing

At present, both industry and academia believe that unit testing should be carried out by developers, because from the process of unit testing, unit tests generally adopt the White-box test method, can not be separated from the code of the object to be measured, but also need to construct the driving module, pile function, so the development of unit testing needs better knowledge. From the knowledge structure of personnel, the familiarity with the code, developers have a certain advantage.

Unit testing is carried out by developers to bring some special benefits. We know that in practice it is generally recommended that a cross-test approach be used for unit testing by developers, for example, by the caller of the unit being tested, that is, to avoid unit testing of their own code. This cross-test arrangement avoids the test being influenced by the development mentality, which is limited to the original idea and is not easily found in the development process, and the goal of a technical backup or full communication is beneficial to the organization. Even if the method of cross-testing is not adopted, and the producer of the arranging unit carries out unit test, it has great superiority, its biggest advantage is fast, and can better realize "prevention error". This self-test arrangement is also a good choice in the case of stressful people.

From the experience point of view, Unit test input and coding input is basically 1:1, if the full-time Test team to carry out unit testing, it is obviously not appropriate to maintain such a large single task force.

The above discussion is the advantage of unit testing by developers, which is mainly from the point of view of unit testing efficiency. However, from the perspective of the unit test effect, the test group must be guaranteed to participate in unit testing from an organizational structure because:

Firstly, from the current situation of domestic enterprises, the quality consciousness of testers is higher than that of the developers, and the test personnel can improve the quality of the test by participating in unit tests.

Secondly, the more understanding of the system under test, the more thorough testing is possible, the testers involved in unit testing will enable testers to familiarize themselves with the tested system from the code level, which is very helpful for the test group's late integration testing and system testing activities, and will greatly improve the quality of integration testing and system testing.

How the test group participates in unit testing should be based on the actual situation of the software organization. If the software organization has sufficient testing resources and the tester has a high proportion of developers, then the unit testing work of some important modules can be carried out independently by the tester, and if the test resources are low, the tester has a lower proportion to the developer, then the unit Test Plan and unit test design work can be taken by the tester. While the implementation and execution of unit tests is done by the developer, and if the test resources are scarce, even the unit test plan and unit test design cannot bear, then the test group should participate in the review of the relevant unit test documents and unit test reports of the development group at least, and ensure the quality of unit tests.

 • Enhanced standard of unit test processes

 • Process definition for unit testing

The improvement of software quality needs the standard process, and the process of software development should be defined according to the norm. Process definitions include phases, entry/exit criteria for stages, input/output of stages, roles and responsibilities, templates and checklist, and so on. It is easy to control the unit test process by dividing the unit test into several stages, which embodies the controllability of software testing. In order to improve the quality of unit test, we must first establish the standard unit test process, the development Group, test group, SCM Group, SQA group and so on can carry out their work according to the definition of unit test process, and jointly guarantee the quality of unit test.

The definition of unit test process needs to refer to the actual situation of the enterprise, for example, the stage division can be divided into four stages: planning, design, implementation and execution. The planning phase should consider the schedule of the entire unit test process, the workload, the Division of tasks, the staffing and resource arrangements, the testing tools and testing methods required, the criteria for the end of the unit test and the criteria for acceptance, as well as the possible risks, and the specific approach to these risks, and the output Unit Test Plan documentation as a guide to the entire unit testing process. The design phase requires specific consideration of which units are tested, the relationship between the units under test, and the relationships between the units in the module, the specific test strategy, how to design the unit test case, how to design the unit test code, what tools to use, and the output of the unit test Plan document. Used to guide specific unit test operations. The implementation stage needs to complete the unit test case design, the script writing, the test drive module writing, the test pile module's writing work, the output "unit test Case" the document, the related test code. The main task of the implementation phase is to build the unit test environment, execute the test script, record the test results, if the error is found, the developer will be responsible for the wrong modification, and the regression test, the end of this phase need to submit the unit test report.

When defining the unit test process, a certain reduction can be made, for example, to design and execute two phases, combining the unit test plan and the unit test case.

  • Unit test work products must be included in configuration management

The unit test work product refers to the test document, test Code and test tool that should be delivered after unit test is completed, generally including but not limited to the following work products, can be tailored according to the actual situation:

• Unit Test Plan

• Unit Test scenarios

• Unit Test Cases

• Unit test procedures

• Unit Test Daily

• Unit Test problem sheet

• Unit Test Report

• Unit test input and output data

• Unit Testing tools

• Unit test code and design documentation

In order to ensure the accuracy of the unit test work product, the test code and script need to be carried out on a daytime or review, the testing document is reviewed. These work products should be included in configuration management, the configuration change process for their modifications, and the timely release of their configuration status, which preserves the consistency and traceability of unit test work products.

• Coverage indicators and quality objectives must be developed to guide and accept unit tests

Unit testing must establish a certain coverage index and quality objectives to guide unit test design and execution, as well as the standard for unit test acceptance. When you design a use case, you can design the use case for the coverage metrics that you want to reach, and when you test execution, you can analyze the test to see if the coverage metric is met by the coverage analysis tool, and if not, you need to analyze which parts are not covered to supplement the use case to reach the coverage metric. The design of unit test quality target needs to conform to the actual process ability of software enterprise, which relies on the accumulation of measurement data in the previous unit test process of software enterprise, and cannot be made out of thin air. With the accumulation of previous metric data, you can fully understand the unit testing capabilities of your current organization, such as how many defects the unit tests each thousand lines of code finds. If the unit test statistical results do not fall within the scope of this quality target, it is explained that some aspects of the unit testing process have some problems, need to audit the process to identify the cause of the problem to improve.

When these indicators are determined, they must be strictly implemented. There will be some testers to find a variety of reasons to prove that coverage indicators do not reach, and this requires QA according to the actual situation analysis indicators are reasonable. It turns out that there is a relatively simple standard that is much better than no standard, and our practice has found that by pushing hard indicators, the number of problems found by unit tests is at least twice times higher than before without standards.

The following are the quality objectives of the Indian SASKEN Company:

India Sasken Company Quality objectives

Stage

Organizational goals

Target limit

Target Lower Limit

HLD (outline design)

Major defects/100 Pages

Major defects/100 Pages

Major defects/100 Pages

LLD (Detailed design)

Major defects/100 Pages

Major defects/100 Pages

Major defects/100 Pages

Unit Test Plan

(Unit test Plan)

Major defects/100 Pages

27.5 Major defects/100 Pages

22.5 Major defects/100 Pages

Code Review

(Code for the daytime)

Major Defects/kloc

Major Defects/kloc

Major Defects/kloc

Defects during unit test (unit tests)

Major Defects/kloc

16.5 Major Defects/kloc

13.5 Major Defects/kloc

Defects during integration test (integration test)

6 Major Defects/kloc

6.6 Major Defects/

   enhanced detailed design documentation review

Detailed design is the main input of unit testing, the quality of detailed design documents will directly affect the quality of unit testing, so it is important to strengthen the review of detailed design documents, especially to write the relevant test plan and test case design personnel, must be written test cases from the perspective of this detailed set whether the requirements of compliance, Otherwise, the unit test design can not be carried out according to the detailed design. The software organization can solidify the main points of the detailed design review in the form of a checklist, so that the review is based on a check list, which improves the efficiency of the evaluation and also guarantees the effectiveness of the review. The review process needs to be determined that if the criteria above the checklist n% are not met, the detailed design document is not approved and needs to be redesigned.

Usually the detailed design document has two forms, one is the form of the flowchart and the other is the form of the pseudo code. The advantages of using the flowchart is intuitive, conducive to unit test case design, the disadvantage is that the description is poor, document writing trouble, not conducive to document changes and changes; pseudo-code may be the opposite, the document changes are simple, can easily add text in any place, and translated into code more convenient, but not intuitive, is not conducive to unit test case design.

Detailed design and unit test design must be separated. This is not a problem if the unit tests are borne by the testers, and if the unit tests are undertaken by the developer, then the actual operation allows the members of the project group to exchange the same or similar tasks, and to design each other's unit tests according to each other's details. In this way, before the unit test begins the detailed design review phase will consider the latter division of labor, arrange the relevant unit test designers involved in the relevant detailed design review.

If your code does not have a detailed, post-reviewed design document, it is recommended that you do not unit-test, but instead use code reviews instead of unit tests.

In the process of coding, developers may find the problem in detail, and modify the code, this change should go back to the details, and the details of the corresponding changes, or to the unit test execution, you will find that the code and details are not at all, can not be executed. Detailed modifications are subject to control and change control processes are reviewed. Because unit testing is the downstream activity of detailed design, if the detailed design changes arbitrarily, the unit test document is difficult to be consistent with it, so the unit test also loses the basis and the significance. Only a detailed set-up is included in the configuration management to ensure the consistency of unit testing and detailed setup.

• Improvement of unit testers ' skills

1. Strengthen skills training for unit testers

The quality of unit tests is largely determined by the skill level of the person conducting the unit test. If the tester does not have the knowledge of unit testing, then the tester should be trained in the relevant. A unit test has not been done, the first time without training is difficult to do the unit test. Unit tests begin at the end of the elaboration phase, but unit testing-related training should be prepared and planned as early as possible, and the training can be in two stages, with similar content at each stage. The first stage is to write the unit test plan, the training object is the test program of the writer and the detailed writer, so that can be considered in the design of testability, the content of the training is the basic concept of unit testing, Unit test analysis method, unit test case writing, unit Test standard clear; the second stage is the unit test before execution, The object is the test performer, the training content is the execution of the specific unit test, including the driving function, the construction of the pile function, the use of coverage test tools (Truecoverage, Logiscope, etc.), the use of Automated Unit Test framework to construct unit test automation (TCL, CppUnit, JUnit, etc.). In the course of training, it is best to combine examples, which will be more vivid and enhance understanding.

Through the above system training, can unify unit test method, clear unit test standard, Master unit Test basic skill, for the later unit Test smooth development of the road.

2. Tools must be introduced to assist

Unit testing is very necessary to help the tool, especially the coverage tool can not be missing, or the use case can not get the test quality such as statement coverage, path coverage and so on, it is impossible to further analysis of the object being tested. There are Logiscope, Truecoverage, PureCoverage, and so on, which have a wide range of analysis coverage tools, which are strong and weak, and can be used according to the actual situation.

In order to improve the efficiency of unit testing, especially to improve the efficiency of regression testing, we need to introduce automation in unit testing. The current method is to use TCL language to write extension instructions, to construct their own unit test automation. Open-source automated testing frameworks such as CppUnit, JUnit, etc. can also be used directly.

In addition, before the unit test, we also need to use Pc_lint to check the code, to exclude the code syntax error, to ensure that the code for unit testing has the basic quality, to ensure that unit testing can proceed smoothly, improve unit test execution efficiency.

3. Unit testers strengthen the overall understanding of the software under test

The purpose of unit testing is to ensure the quality of the detailed design, in addition to discovering the errors introduced in the coding and where the discovery code is inconsistent with the detailed design. Because test analysis and test case design need to be based on detailed design, this process is actually a re-examination of the detailed design, in this process will be found in the previous review did not find problems.

Both in the design activities of unit tests and in the execution of unit tests, testers are required to understand the requirements and profiles of the software and to enhance the overall understanding of the software under test. Otherwise, the subject of the test is not deep understanding, can only be measured by the process of the unit and the flow, and the process is correct is not guaranteed.

The tester should pay attention to the communication with the development, so as to have a better understanding of the unit under test, and because of the progress of the document, including detailed documentation is often too late to update, so the latest and most correct ideas often exist in the brains of developers, timely communication with them to get the most timely information, reduce the workload of future update use cases.

 End

Unit testing is a very important method of quality assurance in software development, and strengthening unit testing is very important for improving software quality. And the unit test is not as long as the unit test method can be, this need from the organization, process and technology three aspects to ensure.

Reprint: Http://wenku.baidu.com/link?url=dLdu8pCSiDwYtJwXT6pLF_ Ql5sazct5iffmt9kepxb39zicrqhz85nmcpjs5q1kd299yqbytl0dpjo1zi94urhufyir5rourl0oksiaqa73

How to do unit tests

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.