Evolutionary generation method of test data based on the importance of key program entities
Through the test data generation method based on the importance of program key program entities, the control flow diagram is constructed and the fitness value is adjusted, the problem of uneven branch coverage in the existing technology is solved, efficient test data generation is achieved, and the program being tested is ensured. branch overlay.
Patent Information
- Application Number
- CN202410098043.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-01-23
- Publication Date
- 2024-11-19
- Estimated Expiration
- 2044-01-23
AI Technical Summary
When the existing test data generation method contains branch structures, it is difficult to effectively balance the coverage of each branch of the program, which reduces the evolutionary generation efficiency of the test data.
Based on the importance of program key program entities, the evolutionary generation method of test data is calculated by constructing a control flow chart to calculate the coverage of individuals in the population to key program entities, and adjust the fitness value to ensure that important program entities are covered, and the group intelligence algorithm is used for selection. , cross, mutation, until the iteration terminates the condition outputs the test data.
It improves the efficiency of the evolutionary generation of test data, ensures the rapid and efficient generation of the target paths covering the target, balances the branch coverage of the tested program, and improves the quality of the test data.
Smart Images

Figure CN117909240B_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of software technology, and in particular to a method for generating test data evolution based on the importance of key program entities of a program. Background Art
[0002] Software testing runs through the entire process of software development and accounts for about half of the software development cost. Software testing is an evaluation method and means to ensure software quality by designing test data to find defects or errors in the software. The design of test data is the core and most important part of the software testing process, which directly determines the quality and efficiency of software testing. Due to the various data types, huge input space and complex internal structure of complex software, test data generation has always been one of the important and difficult problems. How to effectively generate test data that meets the target requirements is the key to software testing.
[0003] At present, some scholars have conducted research on software test data generation methods, such as Zhang Yan et al. (Zhang Yan, Gong Dunwei, Evolutionary Generation Method of Path Coverage Test Data Based on Rare Data Capture [J]. Journal of Computers, 2013, 36(12): 2429-2440). In the process of evolutionary generation of test data, dynamic capture of rare data is considered, and the fitness value of the evolved individuals is adjusted by calculating the contribution of individuals to the generation of test data across the target path to effectively generate test data. Fan Shuping et al. (Fan Shuping, Zhang Yan, Ma Baoying et al., Evolutionary Generation of Path Coverage Test Data Based on Equilibrium Optimization Theory [J]. Journal of Electronics, 2020, 48(7): 1303-1310.) used the degree of equilibrium of individuals in the population crossing the various branches of the program to design a fitness function calculation method to adjust the evolution process, and verified the effectiveness of the proposed method through experiments.
[0004] However, when the program under test contains a branch structure, these existing test data generation methods only focus on the effectiveness of individuals in generating data covering the target path, but often cannot effectively and evenly cover each branch of the program, reducing the evolutionary generation efficiency of test data. Summary of the invention
[0005] In response to the above problems and technical requirements, this application proposes a test data evolution generation method based on the importance of key program entities. The technical solution of this application is as follows:
[0006] A test data evolution generation method based on the importance of key program entities of a program, the test data evolution generation method comprising:
[0007] Each program entity in the program under test is abstracted as a node, and the control dependency relationship between program entities is abstracted as directed edges between corresponding nodes, so as to construct a control flow graph corresponding to the program under test;
[0008] Initialize the population of the swarm intelligence algorithm for the program under test, the population includes M individuals, and each individual corresponds to a set of test data;
[0009] Based on the control flow graph, calculate the node n corresponding to any key program entity on the target path by M individuals in the population j The coverage degree of the key program entity is obtained by j Entity importance diff j , M individuals in the population have a relationship with node n j The higher the coverage, the higher the node n j Entity importance diff j The higher;
[0010] According to the entity importance of the nodes corresponding to each key program entity on the target path, we can get the entity importance of any individual x in the population. k The fitness value f(x k ), individual x k The greater the impact on the entity importance of the nodes corresponding to each key program entity, the greater the individual x k The fitness value f(x k ) is larger, the parameter 1≤k≤M;
[0011] The swarm intelligence algorithm is used to select, crossover, mutate and form the next generation population according to the fitness value of each individual in the current population for the next iteration operation, until the iteration termination condition is reached, and the individual with the largest fitness value in the population is output as the test data covering the target path obtained by evolution.
[0012] Its further technical solution is to obtain any individual x in the population k The fitness value f(x k )include:
[0013] Calculate the number of individuals in the population except for individual x based on the control flow graph k The other M-1 individuals other than the node n corresponding to the key program entity j The coverage degree of the key program entity is obtained by j Without considering individual x k Entity importance diff j ′;
[0014] According to node n j Entity importance diff when considering all individuals in the population j and ignoring individual x k Entity importance diff j ′, calculate the individual x k The fitness value f(xk ).
[0015] Its further technical solution is to calculate the individual x k The fitness value f(x k )for:
[0016]
[0017] Among them, KE is a set of nodes corresponding to all key program entities on the target path.
[0018] Its further technical solution is to obtain node n j Entity importance diff when considering all individuals in the population j and ignoring individual x k Entity importance diff j 'include:
[0019] Based on the control flow graph, determine the node n corresponding to the key program entity according to the hierarchical relationship of each node on the target path j The weight w j ;
[0020] According to node n j The weight w j Combine the M individuals in the population to node n j The coverage degree of node n is obtained j Entity importance diff when considering all individuals in the population j ; According to node n j The weight w j Combine the population except individual x k The other M-1 individuals are connected to node n j The coverage degree of node n is obtained j Without considering individual x k Entity importance diff j ′.
[0021] A further technical solution is that the key program entities on the target path include program entities in the tested program that are located on the target path and belong to loop program entities or selection program entities, and each key program entity includes multiple sub-nodes to form multiple branches.
[0022] Its further technical solution is to obtain node n j Entity importance diff when considering all individuals in the population j and ignoring individual x k Entity importance diff j 'include:
[0023] According to node nj The weight w j Combine the M individuals in the population to node n j The coverage degree of each branch of node n is obtained j Entity importance diff when considering all individuals in the population j ;
[0024] According to node n j The weight w j Combine the population except individual x k The other M-1 individuals are connected to node n j The coverage degree of each branch of node n is obtained j Without considering individual x k Entity importance diff j ′.
[0025] Its further technical solution is, according to the node n j The weight w j Combine the M individuals in the population to node n j The coverage degree of each branch of node n is obtained j Entity importance diff when considering all individuals in the population j for:
[0026]
[0027] in, is any individual x in the population m For node n j The coverage of branch t, when individual x m Covering Node n j When the branch t otherwise T j is node n j The total number of branches included; Represents the coverage node n j The total number of test data for branch t, is the coverage node n j The maximum value among the total number of test data of all branches.
[0028] Its further technical solution is, according to the node n j The weight w j Combine the population except individual x k The other M-1 individuals are connected to node n j The coverage degree of each branch of node n is obtained j Without considering individual x k Entity importance diff j 'for:
[0029]
[0030] in, is any individual x in the population m For node n j The coverage of branch t, when individual x m Covering Node n j When the branch t otherwise T j is node n j The total number of branches included; It means that without considering individual x k Covering node n j The total number of test data for branch t, is not considering individual x k Covering node n j The maximum value among the total number of test data of all branches.
[0031] A further technical solution is to determine the node n corresponding to the key program entity j Weight Among them, level j is node n j Level, max level It is the maximum value of the levels of all nodes on the target path.
[0032] A further technical solution is that any node n on the target path i Level level s is node n i The parent node n s The level at which you are located.
[0033] The beneficial technical effects of this application are:
[0034] The present application discloses a method for evolutionary generation of test data based on the importance of key program entities of a program. The method takes the importance of program entities into consideration during the process of evolutionary generation of test data, so as to retain test data covering important program entities during the test data generation process, which is beneficial to effectively improve the efficiency of evolutionary generation of test data.
[0035] This method considers the structure of the program under test, defines entity hierarchy and program entity weight, realizes the distinction of importance of different program entities, and proposes a test data generation framework based on the importance of key entities, which integrates program entity hierarchy, entity weight and test data coverage of the program. This framework considers the basic structure of the program under test, the characteristics of program entities and the coverage of program entities by test data, and realizes the rapid and efficient generation of test data covering the target path.
[0036] In the test data generation of this method, the true and false branches of the branch nodes of the program under test that are not covered by test data are taken into account and used as components of the fitness function of the genetic algorithm to quickly balance the test data covering each branch on the target path of the program under test, thereby achieving the rapid generation of test data covering the target path. BRIEF DESCRIPTION OF THE DRAWINGS
[0037] Figure 1 It is a method flow chart of a test data evolution generation method according to an embodiment of the present application.
[0038] Figure 2 It is a schematic diagram of the control flow graph abstracted from the program under test in an instance.
[0039] Figure 3 It is a flow chart of a method for obtaining fitness values of individuals in a population in another embodiment. DETAILED DESCRIPTION
[0040] The specific implementation of the present application is further described below in conjunction with the accompanying drawings.
[0041] This application discloses a test data evolution generation method based on the importance of key program entities. The test data evolution generation method includes the following steps. Figure 1 The flowchart shown:
[0042] Step 1: abstract each program entity in the program under test into a node, abstract the control dependency relationship between program entities into directed edges between corresponding nodes, and construct a control flow graph corresponding to the program under test.
[0043] The program under test targeted by the present application includes a branch structure in addition to a sequential structure. The branch structure referred to in the present application includes at least one of a selection structure and a loop structure, wherein the loop structure can be converted into a branch structure (such as a common conversion into a double-branch selection structure) according to the number of execution loop bodies, and the selection structure (switch statement) itself can be expressed as a branch structure, so the present application uniformly refers to the selection structure and the loop structure as a branch structure. The program entity in the program under test is a statement or function in the program under test.
[0044] When the program entity S abstracts the node ns Controls another program entity I abstracted node n i , or a node n abstracted from a program entity I i Depends on another program entity S abstracted node n s When , it represents the node n abstracted from the program entity S s Node n abstracted from program entity I i The parent node of , and the abstracted control flow graph has node n s Points to node n i The directed edge of .
[0045] For example, in one example, part of the program under test is as follows Figure 2 As shown in (a) of the figure, the control flow graph obtained by abstracting the program under test is as follows Figure 2 As shown in (b) in the figure, the abstracted control flow graph includes 8 nodes n1 to n8 abstracted from the program entity. Figure 2 In the simplified form, it is written as 1 to 8. In order to facilitate the display of information start, Figure 2 (b) in FIG. 5 also shows a starting node s and an ending node e.
[0046] Step 2, initialize the population of the swarm intelligence algorithm for the program under test, the population includes M individuals, each individual corresponds to a set of test data, when constructing the individuals in the population, the test data can be encoded using a pre-defined encoding method, such as the common binary encoding method. The swarm intelligence algorithm used includes any one of the genetic algorithm, particle swarm algorithm and ant colony algorithm.
[0047] Step 3: Based on the control flow graph, calculate the node n corresponding to any key program entity on the target path for M individuals in the population j The coverage degree of the key program entity is obtained by j Entity importance diff j .
[0048] The M individuals in the population correspond to the node n of the key program entity j The higher the coverage, the higher the node n corresponding to the key program entity. j Entity importance diff j The higher.
[0049] In one embodiment, the program entities in the branch structure of the tested program are taken as the key program entities, and the key program entities on the target path include program entities in the tested program that are located on the target path and belong to loop program entities or selection program entities. The loop program entity is a program entity in the loop structure, and the selection program entity is a program entity in the selection structure.
[0050] For example, Figure 2 In the example, nodes n2, n3, n5, and n6 in the program under test are all key program entities. The target path starts from the starting node s and passes through n1, n2, n5, n6, and n8 to the ending node e. The key program entities on the target path include n2, n5, and n6. The set of nodes corresponding to all key program entities on the target path constitutes KE = {n2, n5, n6}.
[0051] In one embodiment, please refer to Figure 3 The flowchart shown in the figure determines the node n j Entity importance diff when considering all individuals in the population j include:
[0052] (1) Based on the control flow graph, determine the node n corresponding to the key program entity according to the hierarchical relationship of each node on the target path j The weight w j .
[0053] First, calculate the level of each node on the target path. i level i The calculation method is:
[0054]
[0055] In the above formula, level s is node n i The parent node n s At the level, KE is a set of nodes corresponding to all key program entities on the target path.
[0056] For example, Figure 2 In the example of , for nodes n1, n2, n5, n6, n8 on the target path: (a) Node So the level 1 of node n1 is 0. (b) Node n2∈KE, and the parent node of node n2 is node n1, so the level 2 of node n2 is level 1+1=1. (c) Node n5∈KE, and the parent node of node n5 is node n2, so the level 5 of node n5 is level 2+1=2. (d) Node n6∈KE, and the parent node of node n6 is node n5, so the level 6 of node n6 is level 5+1=3. (e) Node Therefore, the level 8 of node n8 is 0.
[0057] Then we can determine the node n corresponding to the key program entity j Weight Among them, levelj is node n j Level, max level It is the maximum value of the levels of all nodes on the target path.
[0058] For example, Figure 2 In the example of , the maximum value max of all the five nodes on the target path is level =3, the weight of node n2 corresponding to a key program entity The weight of node n5 corresponding to another key program entity The weight of node n6 corresponding to another key program entity
[0059] (2) According to node n j The weight w j Combine the M individuals in the population to node n j The coverage degree of node n is obtained j Entity importance diff when considering all individuals in the population j .
[0060] Regardless of whether the key program entity belongs to a loop program entity or a selection program entity, the key program entity must include multiple child nodes to form multiple branches. j The weight w j Combine the M individuals in the population to node n j The coverage degree of each branch of node n is obtained j Entity importance diff when considering all individuals in the population j , the calculation formula is:
[0061]
[0062] in, is any individual x in the population m For node n j The coverage of branch t, when individual x m Covering Node n j When the branch t otherwise T j is node n j The total number of branches included. In practical applications, key program entities are generally double-branch structures, and the nodes corresponding to the key program entities usually contain 2 branches.
[0063] Indicates that all M individuals in the population cover node n j The total number of test data for branch t. is the coverage node nj The maximum value among the total number of test data of all branches.
[0064] For example, Figure 2 In the example, the node n5 corresponding to the key program entity contains T5 = 2 branches. The population includes two individuals, namely individual x1 and individual x2. The test data corresponding to individual x1 covers the first branch of the node n5 corresponding to the key program entity but does not cover its second branch. Then The test data corresponding to individual x2 covers the first branch of node n5 corresponding to the key program entity but does not cover its second branch, then
[0065] but The entity importance of node n5 corresponding to the key program entity when considering all individuals in the population
[0066] Step 4: According to the entity importance of the nodes corresponding to each key program entity on the target path, the k The fitness value f(x k ).
[0067] Among them, individual x k The greater the impact on the entity importance of the nodes corresponding to each key program entity, the greater the individual x k The fitness value f(x k ) is larger, the parameter 1≤k≤M.
[0068] In one embodiment, in addition to calculating the node n corresponding to the key program entity using step 3 j Entity importance diff when considering all M individuals in the population j In addition, the control flow graph is used to calculate the population except for individual x k The other M-1 individuals other than the node n corresponding to the key program entity j The coverage degree of the key program entity is obtained by j Without considering individual x k Entity importance diff j ′.
[0069] Node n corresponding to the identified key program entity j Entity importance diff when considering all individuals in the population j The method is similar to that of determining the node n corresponding to the key program entity. j Without considering individual x k Entity importance diff j ' includes: determining the node n corresponding to the key program entity jThe weight w j Then, according to node n j The weight w j Combine the population except individual x k The other M-1 individuals are connected to node n j The coverage degree of node n is obtained j Without considering individual x k Entity importance diff j '. Similarly, according to the node n j The weight w j Combine the population except individual x k The other M-1 individuals are connected to node n j The coverage degree of each branch of node n is obtained j Without considering individual x k Entity importance diff j ′, the calculation formula is:
[0070]
[0071] in, is any individual x in the population m For node n j The coverage of branch t, when individual x m Covering Node n j When the branch t otherwise T j is node n j The total number of branches included.
[0072] It means that without considering individual x k In the case of k The M-1 individuals outside cover node n j The total number of test data for branch t.
[0073] is not considering individual x k Covering node n j The maximum value among the total number of test data of all branches.
[0074] Then according to node n j Entity importance diff when considering all individuals in the population j and ignoring individual x k Entity importance diff j ′, we can calculate the individual x k The fitness value f(x k ). Including calculation according to the following formula:
[0075]
[0076] Step 5, using the swarm intelligence algorithm to select, cross, mutate and form the next generation population according to the fitness value of each individual in the current population, and use the new population to re-execute steps 3 to 5 for the next iteration operation. The specific selection, crossover and mutation methods can refer to the algorithm standard of the swarm intelligence algorithm for operation, and this embodiment will not be repeated.
[0077] Iterate until the iteration termination condition is reached, and then output the individual with the largest fitness value in the population as the test data covering the target path obtained by evolution. When the number of iterations reaches the iteration number threshold, or the maximum fitness value in the population reaches the fitness value threshold, it is determined that the iteration termination condition is reached.
[0078] The above is only a preferred embodiment of the present application, and the present application is not limited to the above embodiments. It is understood that other improvements and changes directly derived or associated by those skilled in the art without departing from the spirit and concept of the present application should be considered to be included in the protection scope of the present application.
Claims
1. A test data evolution generation method based on the importance of key program entities, characterized in that: The test data evolution generation method comprises: Each program entity in the program under test is abstracted as a node, and the control dependency relationship between program entities is abstracted as directed edges between corresponding nodes, so as to construct a control flow graph corresponding to the program under test; Initializing a population of a swarm intelligence algorithm for the program under test, wherein the population includes M individuals, and each individual corresponds to a set of test data; Based on the control flow graph, the nodes corresponding to any key program entity on the target path are calculated by M individuals in the population. The coverage degree of the key program entity is obtained The entity importance , the M individuals in the population have nodes The higher the coverage, the higher the node The entity importance The higher; According to the entity importance of the nodes corresponding to each key program entity on the target path, the entity importance of any individual in the population is obtained. The fitness value of ,individual The greater the impact on the entity importance of the nodes corresponding to each key program entity, the greater the individual The fitness value of The larger the parameter ; Using a swarm intelligence algorithm to select, crossover, mutate and form the next generation population according to the fitness value of each individual in the current population for the next iteration operation, until the iteration termination condition is reached, the individual with the largest fitness value in the population is output as the test data covering the target path obtained by evolution; Get any individual in the population The fitness value of Including: calculating the population except individuals based on the control flow graph The other M-1 individuals other than the node corresponding to the key program entity The coverage degree of the key program entity is obtained Without considering the individual Entity importance at , according to the node The importance of the entity when considering all individuals in the population and not considering individuals Entity importance at , calculate the individual The fitness value of for: ; in, It is a set consisting of nodes corresponding to all key program entities on the target path.
2. The test data evolution generation method according to claim 1, characterized in that: Get Node The importance of the entity when considering all individuals in the population and not considering individuals Entity importance at include: Based on the control flow graph, determine the node corresponding to the key program entity according to the hierarchical relationship of each node on the target path Weight ; According to the node Weight Combine the M individuals in the population to form nodes The coverage degree of the node is obtained The importance of the entity when considering all individuals in the population ; According to the node Weight Combined population except individuals The M-1 individual pairs of nodes other than The coverage degree of the node is obtained Without considering the individual Entity importance at .
3. The test data evolution generation method according to claim 2, characterized in that: The key program entities on the target path include program entities in the tested program that are located on the target path and belong to loop program entities or selection program entities. Each key program entity includes multiple sub-nodes to form multiple branches.
4. The test data evolution generation method according to claim 3, characterized in that: Get Node The importance of the entity when considering all individuals in the population and not considering individuals Entity importance at include: According to the node Weight Combine the M individuals in the population to form nodes The coverage degree of each branch of the node The importance of the entity when considering all individuals in the population ; According to the node Weight Combined population except individuals The M-1 individual pairs of nodes other than The coverage degree of each branch of the node Without considering the individual Entity importance at .
5. The test data evolution generation method according to claim 4, characterized in that: According to the node Weight Combine the M individuals in the population to form nodes The coverage degree of each branch of the node The importance of the entity when considering all individuals in the population for: in, Any individual in the population For Node Branches The coverage of Covering Nodes Branches hour ,otherwise , Is a node The total number of branches included; Represents a covered node Branches The total number of test data, Is a covered node The maximum value among the total number of test data of all branches.
6. The test data evolution generation method according to claim 4, characterized in that: According to the node Weight Combined population except individuals The M-1 individual pairs of nodes other than The coverage degree of each branch of the node Without considering the individual Entity importance at for: in, Any individual in the population For Node Branches The coverage of Covering Nodes Branches hour ,otherwise , Is a node The total number of branches included; Indicates that without considering individual Covering nodes in case of Branches The total number of test data, Without considering the individual Covering nodes in case of The maximum value among the total number of test data of all branches.
7. The test data evolution generation method according to claim 3, characterized in that: Determine the nodes corresponding to key program entities Weight ,in, Is a node The level, It is the maximum value of the levels of all nodes on the target path.
8. The test data evolution generation method according to claim 7, characterized in that: Any node on the target path Level , Is a node The parent node of The level at which you are located.
Citation Information
Patent Citations
MC / DC test data automatic generation method based on genetic algorithm
CN102323906A
Important statement-based branch coverage testing data generation method
CN105930272A
Genetic algorithm optimization-based software test data generation method
CN108509335A
