Unit Test Big Uncover

Source: Internet
Author: User
Tags logical operators

unit Test Big uncover

Author: Zhang Yuanli

Http://blog.csdn.net/vincetest1 The importance of unit testing 1.1 Some misconceptions of the understandingin the actual unit testing process there will always be some mistakes in the understanding of us, making it the biggest obstacle to unit testing, which is analyzed as follows: "article source: Zhang Yuanli's Blog http://blog.csdn.net/vincetest" It's a waste of time, Now it's time to catch up, not to be allowed at all, or to do whatever it takes to manage the leadership. I'm a great programmer and I'm sure there's no problem with the code I write. Do unit test too annoying, direct integration, when there are problems in the integration test will certainly be able to find, it is not in the system test can always find it. It just proves what the code is doing. In the final analysis of the above error understanding or because the unit test understanding is not enough, do not really recognize the importance of unit testing. 1.2 Importance of the test Unit Testing is the foundation of software testing, so the effect of unit tests will directly affect the late testing of the software, and ultimately, to a large extent, the quality of the product. There are several ways to see the importance of unit testing. Time: If the unit test carefully, in the system integration is very smooth, so it will save a lot of time, whereas those because of the time reason does not do unit test or do not do, in the integration will always encounter those who should be in the unit test can find problems, And this problem in the integration is often difficult to allow developers to anticipate, and finally found in the hard to find this is a very low-level error in remorse when they have wasted a lot of time, such a waste of time is not worthwhile, is the so-called outweigh the gains.   Test results: According to the previous Test experience, the effect of unit testing is very obvious, first of all, it is the basis of the test phase, the unit test, in the late integration test and system testing is very smooth. Second, some deep problems can be found in the unit testing process, and some problems that are easy to find and difficult to find in integration and system testing are also found. Unit testing is also focused on a particular area, not just what the code does, but what the code does, whether it does what it has to do without doing what it shouldn't. "Article Source: Zhang Yuanli's blog http://blog.csdn.net/vincetest" test cost: Some problems are easy to find in unit tests, and the cost of finding problems in later tests will multiply. For example, when the unit test found that 1 problems need 1 hours, the integration test found that the problem will take 2 hours, when the system test to find that it will take 3 hours, the same as the same location problem and the cost of solving problems is also increased exponentially, This is one of the factors that we need to eliminate as many bugs as possible to reduce later costs.   Product quality: The good and bad of unit tests directly affect the quality of the product, probably because a small error in the code leads to an indicator of the quality of the entire product, or more serious consequences, which can be avoided if we do a unit test. "Source: Zhang Yuanli Blog http://blog.csdn.net/vincetest"     to sum up, unit testing is to build the cornerstone of product quality, We do not want to save the unit test time does not do unit test or do not let us waste too much time later, we also do not like because of saving those times caused by the development of the entire product failure or restart. 1.3 has the advantage1. It is a validation behavior. Each function in the program is tested to verify its correctness and provide support for future development. Even in the late stages of development, we can easily add functionality or change the structure of the program without worrying about breaking important things in the process. And it provides protection for the refactoring of the code, so that we can be more free to improve the program. 2. It is a design behavior. Writing unit tests will enable us to observe, think, and especially write Tests (Test-first) from the caller, forcing us to make the program easy to invoke and testable, which forces us to release the coupling in the software. In addition, the coder can generate a predictive test when coding, and minimize the defect of the program. "Article Source: Zhang Yuanli's Blog http://blog.csdn.net/vincetest" 3. It's a document-writing behavior. A unit test is a priceless document that is the best document to show how a function or class is used. This document is compiled, can be run, and it stays up to date and always synchronizes with code. 4. It has a regressive nature.   Automated unit testing avoids the return of code, and can quickly run tests anytime, anywhere, after writing is complete. 2 basic theory of unit testing 2.1 Basic Concepts 1. Unit Test: Unit test is also called module testing, which belongs to the white box test, is the smallest unit of testing. Modules are divided into program modules and functional modules. Function module refers to the realization of a complete function of the module (unit), a complete program unit with input, processing and output three links. and each program unit should have a formal specification, so that its input, processing and output of the relationship to make a clear name description. 2. Test driver: The entity that drives the test module to run normally 3. Test pile: The entity that replaces the sub module called by the measured module, which is generally a pile function. 4. Test coverage: Evaluate how much code has been executed during the test. 5. Coverage: The extent of code coverage, a measure. There are a number of metrics for the test coverage of your code, defined as follows:   statement coverage (Statementcoverage): Also known as row covering (Lin EC), Segment Overlay (Segmentcoverage) and basic block overlay (b as). It measures whether each executable statement has been executed. Icblockcoverageoverage decision Overlay (Decisioncoverage): Also known as Branch Overlay (branchcoverage), all border overlays (all-edgescoverage), Basic path Overlay (Basispathcoverage), which determines the path overlay (Decision-decision-path or ddptesting). It measures whether each B-oo expression evaluates to True and false is tested in the control structure. L   Conditional override (Con DI): It measures each subexpression independently, reporting the result of each subexpression to true or false. This metric is similar to the decision Overlay (Decisioncoverage), but is more sensitive to the control flow. However, full conditional coverage does not guarantee full coverage of the decision. Tioncoverage   Path Overlay (pathcoverage): Also known as assertion Overlay (PR EDI), it measures whether every possible branch of a function is executed. One of the benefits of path coverage is that thorough testing is required. But there are two disadvantages: first, the path is increased by the exponential level of the branch, for example: A function contains 10 if statements, there are 1024 paths to test. If you add an if statement, the number of paths reaches 2048, and the second is that many paths cannot be independent of the data being executed. Catecoverage Cyclic Overlay (L OOP): This metric reports whether you have executed each loop body 0 times, once or twice (continuously). For Do-while loops, the circular overlay reports whether you have executed each loop body only once or once (continuously). of this metricThe valuable aspect is determining whether the while loop and the for loop have been executed more than once, and this information is not available in other coverage reports. "Article Source: Zhang Yuanli's Blog http://blog.csdn.net/vincetest"

  2.2      test contents Unit Test object is the smallest unit of software design--module or function, Unit test is based on detailed description. Testers are required to understand the I/O condition of the module and the logical structure of the module according to the detailed design instructions and the source program list. The main use of white box test test cases, supplemented by black box test test cases, so that any reasonable and unreasonable input can identify and respond. Requires a desktop check and code review for all local and global data structures, external interfaces, and key parts of program code. In unit testing, you need to test the contents of the following 5 aspects, which is also the basis for constructing test cases.

1)    Module interface: Test the data flow of the module. If the data does not enter and output correctly, there is no other test. Therefore, the following test items are required for the module interface: whether the input parameters of the measured module and the form parameters of the module are matched in number, attribute and order, and when the module is called, it enters the parameters of the module and the form parameters of the sub module to match the number, the attribute and the order. Whether to modify the formal parameters for input only, the parameters output to the standard function are matched in number, property, order, the definition of global variable is consistent in each module,   limit is transmitted through formal parameters. 2)    Local data structure testing: The local data structure of the module is the most common source of error, test cases should be designed to check the following errors:   Check for incorrect or inconsistent data type description; Use variables that have not yet been assigned or have not been initialized;   Wrong initial value or wrong default value, variable name spelling error or writing error,   inconsistent data type. "Article Source: Zhang Yuanli's blog http://blog.csdn.net/vincetest" 3 path Test: Testing the basic execution path and loop will reveal a large number of errors. Design test cases according to white box test and black box test case design method. Design test Cases to find errors caused by incorrect calculations, incorrect comparisons, or abnormal control flow. Common incorrect calculations have:ø         operations with incorrect precedence or misunderstanding of operation Precedence;ø          operation error (the objects of the operation are incompatible with each other on the type);ø         algorithm error;ø          initialization Incorrect;ø         operation accuracy is not enough; Ø The symbol for the          expression is incorrect.   Common comparisons and control flow errors have:ø         different dataA comparison of types;ø         incorrect logical operators or precedence;ø          the difference between two values due to the problem of floating-point arithmetic accuracy the incorrect variables and;ø         in the relational expression;ø          "1 Error", that is, incorrectly multiple cycles or less cycle once;ø          Incorrect or impossible cyclic termination conditions;ø         cannot terminate loops when encountering divergent iterations;ø          the cyclic variables are not modified properly. 4 Error-handling test: The more perfect module design requirements can foresee error conditions, and set appropriate error handling countermeasures, in order to make errors in the program, the error can be rescheduled to ensure the correctness of the logic. This error handling is also part of the module's functionality. An error or defect is indicated in the error-handling module: The description of the error is difficult to understand, the description of the error is not sufficient to locate and determine the cause of the error, the error displayed is not in conformity with the actual error, the error condition is not handled correctly, and the error condition has caused the system to intervene before the error is processed;   If the error condition is not considered, then check that the module will work correctly after the recovery is normal. 5 Boundary Test: Errors on the boundary are common. Design test Case Check:   If there is an error in the No. 0, 1, n times of the n cycle, if there is an error when the maximum minimum value is taken in the operation or judgment;

The 2.3 test environment constitutes when unit testing is performed. Unit tests are performed during the encoding phase. After the source code is compiled, reviewed and validated, and no grammatical errors are confirmed, the test case design for unit testing can begin. To design documents with software, design multiple test cases that can verify program functionality and identify program errors. "Source: Zhang Yuanli Blog http://blog.csdn.net/vincetest" For each group of inputs, should have the expected correct results. In unit testing, if the module is not a stand-alone program, requires a secondary test module, there are two kinds of auxiliary modules: Drive Module (DRIVER): The main program of the test module. It receives the test data, passes the data to the tested module, and then outputs the test results.  When the test module can complete a certain function, also can not drive the module.       Stub: A module used to replace the module called by the test. The test module, the driving module and the pile module together constitute a test environment, as shown in the following illustration:


3 test methods and processes 3.1 use case design 1. Composition of test cases (test cases are basically composed of test scripts in unit tests) use case run precondition tested module/unit required environment (global variable assignment or initialization entity) Start test drive set pile call the tested module set the expected output condition to judge the recovery environment (including the removal of piles) 2. Design principles for test cases a good test case is to discover errors that have not been discovered so far; the test case should be composed of the test input data and the corresponding expected output, which should include reasonable input conditions and unreasonable input conditions in the test case design, and design test cases for the system to run. Designing test cases for forward testing, designing test cases for reverse testing, designing test cases to meet special requirements, and designing test cases for code coverage; 3. Use case Design Method 1         Specification (specification) export 2)         equivalence class partitioning Method 3)         Boundary value analysis Method 4         State Transfer test method 5)         Branch test Method 6)          conditions Test 7)         data definition-use test method (also known as Data Flow test method) 8)         internal Boundary Value test method 9)         error guessing method 4.  Specific use case test design 1 declare test: Check that all variables in the module are declared. Experience shows that a large number of important errors are caused by the fact that variables are not declared or are not properly declared. 2 path test: Require all possible paths in the module to be executed again, which is a logical coverage test. Basic path test: Because in practice, the path in a module can be very many, due to limited time and resources, it is impossible to test one by one. This requires reducing the target of all possible paths to testing enough paths to gain confidence in the module. The minimum set of paths to test is the base test path set. Basic Test RoadPath set to ensure that each determination of the statement in each direction to be tested, each statement at least one execution. 3 cycle test: The key to check the cycle of the condition-judgment part and boundary conditions. Test loops are a special path test because loops are more complex than other statements. There are more opportunities for errors in loops than in other code components. Therefore, for any given loop test, you should include test cases that test each of the following conditions:   loop not executed,   perform a loop, perform two cycles, reflect the execution of typical loops, and if maximum cycle times, maximum cycle times minus 1; maximum cycle times; is greater than the maximum number of loops. For statements that increment and decrement are not 1, pay special attention because programmers are accustomed to an increment of 1. 4)   Loop nesting: Looping nesting makes the number of logic increases exponentially, test cases designed to test nested loops should include: Set the outer loop to a minimum and run all possible conditions within the loop, set the inner loop to a minimum, and run the outer loop in all possible cases; All the loop variables are set to the minimum operation; set all the loop variables to the maximum operation;   Set the outer loop to the maximum and run all possible cases within the loop; set the inner loop to the maximum and run the outer loop all possible; 5)   Boundary value test: refers to the internal boundary test of the program. Check to make sure that the code does not go wrong under any boundary conditions. The emphasis is on checking for cases of less than, equal to, and greater than boundary conditions. A boundary value test is a test that is specifically designed to test the system reflected when a value referenced in a conditional statement is near a boundary or boundary. The best example of a test statement is "If-then ... Else-endif "section. Examples of such statements are as follows: "article source: Zhang Yuanli's blog http://blog.csdn.net/vincetest" If a <= 123 THEN B = 1 else if a >= 123 THEN B = 2 ELSE b = 3 End IF             the boundary value test case in the above example should include at least the following value of a: 122,123,124. When a=123, B=1 is still 2. (Finding the paradox of logical judgment) 6 interface test: Check the data flow (input, output) of the module is correct. Check the number of input parameters and declared arguments, and whether the data type and input order are the same. Check that global variables are correctly defined and used, and so on. 7 Confirm test: Accept valid input data (operation), reject invalid data (operation). 8 Transaction test: Input-> output, error handling. 3.2 use case execution

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.