US 11,720,477 B2
Unit testing method based on automatic generation of path coverage test cases
Han Huang, Guangdong (CN); Muming Lian, Guangdong (CN); Fangqing Liu, Guangdong (CN); Zhongming Yang, Guangdong (CN); and Zhifeng Hao, Guangdong (CN)
Assigned to SOUTH CHINA UNIVERSITY OF TECHNOLOGY, Guangdong (CN)
Appl. No. 17/58,110
Filed by SOUTH CHINA UNIVERSITY OF TECHNOLOGY, Guangdong (CN)
PCT Filed Oct. 31, 2018, PCT No. PCT/CN2018/113236
§ 371(c)(1), (2) Date Nov. 23, 2020,
PCT Pub. No. WO2019/223247, PCT Pub. Date Nov. 28, 2019.
Claims priority of application No. 201810501975.0 (CN), filed on May 23, 2018.
Prior Publication US 2021/0209008 A1, Jul. 8, 2021
Int. Cl. G06F 11/36 (2006.01)
CPC G06F 11/3684 (2013.01) [G06F 11/3676 (2013.01); G06F 11/3688 (2013.01); G06F 11/3692 (2013.01)] 8 Claims
OG exemplary drawing
 
1. A unit testing method based on automatic generation of path coverage test cases, wherein the unit testing method comprises the following steps:
(a) performing file pre-processing on an input source program, which is an input source code, to make the input source code conform to syntactic structures required by lexical analysis and syntactic analysis;
(b) performing the lexical analysis on the input source code pre-processed in step (a), and establishing a symbol table;
(c) performing the syntactic analysis on the input source code pre-processed in step (a), establishing a control flow graph (CFG) corresponding to the input source code, which comprises a control node, an ordinary node and a terminal node, converting the input source code into an executable intermediate code, and inserting an executable code into the control node;
(d) generating test cases by an automatic test case generation algorithm;
(e) acquiring the test cases and driving the executable code of the control node in the control flow graph to execute, by a test case execution module, and calculating a fitness value of a fitness function on a basis of an execution result of the executable code;
(f) if a current node is not the terminal node, selecting next sub-node according to the execution result of the executable code, and repeating steps (c), (d) and (e); otherwise, proceeding to next step;
(g) if the current node is the terminal node, acquiring a final result of the fitness value, and generating a path code corresponding to the test case;
(h) determining whether path coverage is 100% or whether an execution is timed out, proceeding to next step if the path coverage is 100%, and exiting if the execution is timed out; otherwise, if the path coverage is not 100%, and the execution is not timed out, repeating steps (d), (e), (f) and (g); and
(i) generating a test report containing all paths covered by the test cases correspondingly and a path coverage rate,
wherein in step (a), in case of multiple files in file pre-processing, the files are identified by an include statement in C/C++, and files needing the include statement are placed in a same source file; a block end identifier is inserted into an elseif statement and an else statement in an if-elseif-else structure, the if-elseif-else structure is converted into an if-else nested structure, and a case multi-branch structure in a switch statement structure is converted into an if-else multi-nested structure, thus reducing a code abstraction degree and a realization difficulty of the control flow graph,
wherein in steps (b) and (c), an intermediate code representation method of the input source code is stored in the ordinary node and the control node, the execution of the program is able to be abstracted into modification of a variable value in a memory, a variable is represented as an ID symbol and pushed in the intermediate code representation method; meanwhile, an address of the variable in the symbol table is pushed, current processing being the variable is known through an ID identifier in a later intermediate code execution stage, then a position of the variable is able to be accessed through an address behind an ID, and the variable is operated; if the variable is a number, a NUM symbol is pushed; meanwhile, a value of the number is pushed, an operator is directly pushed, reduction of an abstraction degree of a high-level programming language is represented by the intermediate code, and then an intermediate code interpreter is established, thus directly executing the intermediate code; and a basic principle of the intermediate code interpreter is to use a mechanism of a stack computer to execute the intermediate code through one stack and multiple registers, and an execution result of the intermediate code is returned; and
after converting the input source code into the intermediate code, an abstraction degree of the input source code is reduced, during actual execution of the intermediate code, a method of converting an infix expression into a suffix expression is used to calculate a result of the intermediate code through a data structure of the stack,
wherein in step (c), execution of the input source program is simulated by executing the intermediate code.