Code review for white box testing


The test is a bit like digging wells and fetching water. Some people are in one place. Some people fetch water in another place because of the soft soil.

White box testing is divided into two levels: code review and unit testing

Code review

Can understand the logic, and can find some problems, which plays a verification role in the code review conference initiated by R & D

Code review scope
Qualified code should have correctness, clarity, standardization, consistency and efficiency (efficiency: the code should have as little copy code as possible, high cohesion and low coupling)
◆ In summary, the code review work covers the following aspects

1.业务逻辑的审查
For example, ordering an app to Alipay to make payment. If a problem occurs in Alipay and it crashes, it may be overtime and unable to pay, then this problem may not be found during the test, so you need to check the business logic to troubleshoot the problem
2.算法的效率(space complexity , A measure of time complexity)
3.代码风格
4.编程规则

Code review method
Mutual check : is to check each other's code between programmers of the same module or similar modules (pair programming in agile)
Walk-through : check the written program from beginning to end-once again
code review (meeting, in The code is reviewed in accordance with certain rules during the meeting)

Code review rules

◆ Java's most basic statement used

Review of overloaded functions (for overloading, see if the overloading is correct)

◆ ** Memory allocation and management: ** When performing memory allocation and management, it is very important to ensure the timely release of memory and avoid buffer overflow. () (You can play some memory recycling in the code, etc. to facilitate the subsequent location of the problem)

program performance review
1. reduction to create objects (whether or not each time a new large number of objects)
2. reduce loop code execution body, the code can be placed in circulation outside the body to try to put vitro cycle
3. improve the processing abnormal error Efficiency
4. Reduce I / O operation time (less interaction with the outside world)

unit test

How to cover logic for unit tests

unit test
** Unit testing is the most basic test in software testing. Integration testing, functional testing and system testing are all based on unit testing. ** The object of unit testing is the smallest independent unit that constitutes a software product or system, such as packaged Classes or objects, independent functions, processes, sub-processes, components or modules, etc. (99% of unit testing is done by development)
** Unit testing is to check and verify the smallest verifiable unit in the software. ** For example, testing of classes and methods in Java.

Testing principles:
1. As far as possible, ensure that the test cases are independent of each other (the methods of other classes cannot be directly called in the test cases, but the simulation methods should be rewritten in the test cases);
2. This stage is generally implemented by the software developer. Come from my quiz. It is used to verify that the developed code function meets its own design requirements.

The benefits of unit testing:
1. Find defects as early as possible;
2.
Facilitate refactoring; 3. Simplify integration;
4. Documentation;
5. Used for design.

The shortcomings of unit testing:
1. It is impossible to cover all execution paths, so it is impossible to ensure that all path errors are caught;
2. Each line of code requires 3 to 5 lines of code for unit testing, and there is a balance between input and output.

Unit test case design

◆ For the design of unit test cases and the process of program implementation, the main focus is on white box test methods, and strive to meet the following test requirements

  1. Test all independent execution paths of the program module at least once.
    2. For all logical judgments, the results are true and false at least once.
    3. Check the boundary of the program (common, such as data cross-border inspection).
    4. Check the validity of the internal data structure. (Whether the design structure of the database can meet the requirements)

Logic coverage method of white box test method

(Use code to verify how code should be done)
Logical coverage method is the most important test method in white box testing
Logical coverage is divided into statement coverage, judgment coverage, condition coverage, judgment-condition coverage, condition combination coverage, and its original Different ideas, different test cases, and different coverage

Give an example of how to understand the logic overlay method:
The code is as follows: the
Insert picture description here
code logic is transformed with a wireframe flowchart:

Insert picture description here

Statement coverage (the earliest logical coverage method)

◆ Basic idea: design several test cases and run the tested program so that each executable statement in the program is executed at least once
According to the above logic diagram, there are four different paths for the program module:
two different judgment conditions are true or false to determine the path
Insert picture description here

Through observation, we found that: P1 contains all executable statements. According to the test case design principles covered by the statement, as long as a set of test cases is designed to cover the PI path, the requirements can be met.
◆ Solution: If the input data: a = 2, b = 1, c = 6

Insufficient statement coverage
◆ When using the statement coverage method to design test cases, all the executed statements can be tested, but the logical relationship in the operation cannot be accurately judged.
◆ In this example, if the program writes the condition "(a> 0 AND b> 0)" of M as "(a> 0 OR b> 0)", the test case at this time can still cover all executable statements, But can't find the logic error

Decision coverage

◆ Basic idea: make each judgment take true and false values ​​at least once
Insert picture description here
Insert picture description here

Determine coverage limits
For example, in the above example, if the condition N is "c <1" instead of 'c> 1 ", there is no difference in the test result of combination 1.

Conditional coverage

◆ The basic idea: is so taken is TRUE and false every condition experience at least one
Insert picture description here
then:
For the first determination condition M, can be further divided into the following two types:
conditions a> 0: When taking the truth is T1, F1 when taken false
condition b> 0: take true is T2, when taken false F2
for the second determination condition N, may be further divided into the following two:
● condition a> 1: taken as true T3, taking F3 when false
● Condition c> 1: T4 when true, F4 when false

Insert picture description here
◆ Ensure that each condition has at least one true and false (TI / FI, T2 / F2, T3 / F3T4 / F4), meet the condition coverage, but cover the same path P3. It means that even if condition coverage is achieved , it cannot guarantee to cover all paths (PI, P2, P3, and P4). It can be observed that the above value condition will always be M for the judgment condition and N for true. Instead, the path passed is simpler.
** Test cases meet the seemingly more complicated and demanding conditional coverage, and in fact may not necessarily meet the requirements of judgment coverage. ** Here, each condition has at least one true and false (TI / FI, T2 / F2, T3 / F3, T4 / F4). While M only obtains a false value and N only obtains a true value, not every determination condition (M or N) is "true", and the "false" value is executed at least once.

Solution:
It only meets a kind of coverage condition, and also misses the logic path of the program or makes a wrong decision. There is still a big risk. Therefore, the white-box test requirement meets more than two test coverage requirements at the same time, and the test coverage rate can meet the quality requirements and reduce the risk to a very low level.

Decision-Condition Coverage

Basic idea: Ensure that enough or elaborate test cases are designed so that the possible values ​​of all conditions in the judgment conditions are executed at least once, and at the same time, the possible results of all judgments are executed at least once.

Insert picture description here

Each judgment condition is true. At the same time, every decision sentence is true and
every decision condition is false. At the same time, each decision statement is also false
but still has not completed all paths

Conditional combination coverage

◆ Basic idea:
Design enough test cases so that all of the conditions in the ** judgment may appear at least once, and the judgment result of each judgment itself also appears at least once. ** The difference with judging a conditional coverage is that conditional combination coverage does not simply require each condition to have "true" and "false" results, but requires that all possible combinations of these results appear at least once

(The difference with condition judgment is that the concept of combination is added. Two conditions, each of which is true or false, will generate four combinations)
Insert picture description here

Decide the scope of test coverage to determine the method of logic coverage. (At least the coverage of judgment conditions)

Published 82 original articles · won praise 7 · views 4170

Guess you like

Origin blog.csdn.net/sunshine612/article/details/105420816