Test case generation method, device, equipment, storage medium and program product

By obtaining the target path set of the program under test, and using the ant colony algorithm and the improved hybrid frog jumping algorithm to perform local and global optimization of the initial test cases, the problems of low test case generation efficiency and insufficient path coverage are solved, and efficient test case generation is achieved.

CN123048397APending Publication Date: 2026-11-06CHINA TELECOM CORP LTD TECHNOLOGY INNOVATION CENTER +1
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202611237661.5
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2026-08-14
Publication Date
2026-11-06

AI Technical Summary

Technical Problem

Existing test case generation methods suffer from low efficiency and insufficient path coverage.

Method used

By obtaining the target path set of the program under test, the initial test cases are locally and globally optimized using the ant colony algorithm and the improved hybrid frog jumping algorithm. The fitness is determined by combining the Monte Carlo algorithm, and an efficient test case set is generated.

Benefits of technology

It improves the efficiency of test case generation and path coverage, avoids invalid paths consuming algorithm resources, and enhances the guidance of path coverage.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN123048397A_ABST
    Figure CN123048397A_ABST
Patent Text Reader

Abstract

This application relates to a test case generation method, apparatus, device, storage medium, and program product. The method includes: obtaining a target path set corresponding to the program under test; determining fitness based on the actual execution path and target path of each initial test case in the initial test case set; performing local optimization on the initial test cases corresponding to the worst fitness in each subset of the initial test case set to obtain a first test case set; updating the pheromone of each target path for each first test case in the first test case set that meets preset conditions; performing global optimization on each first test case to obtain a second test case set; using the second test case set as the initial test case set; continuing to perform local and global optimization on the initial test case set; and using the initial test cases that meet the optimization termination conditions as target test cases, thereby improving the generation efficiency and path coverage of target test cases.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This application relates to the field of software testing technology, and in particular to a test case generation method, apparatus, computer device, computer-readable storage medium, and computer program product. Background Technology

[0002] In the field of software engineering, software testing is a crucial step in the software development process, aimed at discovering and fixing defects in software and verifying whether the software meets design requirements. Test cases are the core of software testing, affecting the quality and reliability of software products.

[0003] The test case generation methods in related technologies suffer from low test case generation efficiency and low path coverage. Summary of the Invention

[0004] Therefore, it is necessary to provide a test case generation method, apparatus, computer equipment, computer-readable storage medium, and computer program product that can improve the efficiency of test case generation and path coverage in response to the above-mentioned technical problems.

[0005] Firstly, this application provides a test case generation method, the method comprising:

[0006] Obtain the set of target paths corresponding to the program under test; each target path in the target path set is the actual executable defined path used in the program under test.

[0007] The fitness of each initial test case is determined based on the actual execution path and target path of each initial test case in the initial test case set.

[0008] Local optimization is performed on the initial test cases corresponding to the worst fitness in each subset of the initial test case set to obtain the first test case set. For each first test case in the first test case set that meets the preset conditions, the pheromone of each target path is updated. Based on the updated pheromone of each target path, the first test cases are globally optimized to obtain the second test case set.

[0009] The second set of test cases is used as the initial set of test cases. Local and global optimizations are then performed on the initial set of test cases until the optimization termination condition is met. The initial test cases that meet the optimization termination condition are then used as the target test cases.

[0010] In one embodiment, the fitness of each initial test case is determined based on the actual execution path and target path of each initial test case in the initial test case set, including:

[0011] For each initial test case, obtain the actual execution path of the initial test case and the number of first nodes in the target path that are in the same position as the first node and the number of second nodes that contain consecutive identical nodes.

[0012] Calculate the first ratio of the number of first nodes to the total number of nodes in each target path, and the second ratio of the number of second nodes to the total number of nodes in each target path. Then, weight and fuse the first and second ratios to obtain the single-path fitness between the actual execution path of the initial test case and each target path.

[0013] Obtain the maximum single-path fitness of the initial test cases, and calculate the average of all single-path fitness of the initial test cases. Subtract the maximum single-path fitness from the average fitness to obtain the fitness of the initial test cases.

[0014] In one embodiment, a weighted fusion of the first ratio and the second ratio is performed to obtain the single-path fitness between the actual execution path of the initial test case and each target path, including:

[0015] Using the Monte Carlo algorithm, random sampling simulations are performed on the first weight corresponding to the first ratio and the second weight corresponding to the second ratio to calculate the weight evaluation index; the weight evaluation index is determined based on the path coverage success rate and the number of convergence iterations.

[0016] Based on the first and second weights when the weight evaluation index reaches its optimal value, the first ratio and the second ratio are weighted and summed to obtain the single-path fitness between the actual execution path of the initial test case and each target path.

[0017] In one embodiment, prior to the step of locally optimizing the initial test cases with the worst fitness in each subset of the initial test case set, the method further includes:

[0018] If the single-path fitness between the actual execution path of the initial test case and the first target path is a preset value, the first target path will be removed from the target path set; the first target path is one of the target paths in the target path set.

[0019] In one embodiment, local optimization is performed on the initial test cases with the worst fitness in each subset of the initial test case set to obtain a first test case set, including:

[0020] For each subset, obtain the current position of the initial test case with the worst fitness in the subset, the product of the best fitness and the preset threshold; the preset threshold is determined based on random numbers and preset coefficients, and the preset coefficients decrease non-linearly with the number of iterations.

[0021] Subtract the product from the worst fitness to obtain the difference;

[0022] Multiply the difference by the random number to obtain the position movement step size;

[0023] Based on the position movement step size, update the current position of the initial test case with the worst fitness, and iteratively optimize the initial test case with the worst fitness until the iteration ends. Then merge the subsets after each iteration to obtain the first test case set.

[0024] In one embodiment, updating the pheromones of each target path includes:

[0025] For each target path, obtain the pheromone increment of each elite test case in the first test case set to the target path; each elite test case is the first test case in the first test case set whose fitness is higher than a preset fitness threshold; the pheromone increment of each elite test case to the target path is determined based on the single-path fitness between the actual execution path of each elite test case and the target path.

[0026] Based on the pheromone increment of each elite test case on the target path, the current pheromone level of the target path is updated.

[0027] In one embodiment, based on the updated pheromones of each target path, each first test case is globally optimized to obtain a second test case set, including:

[0028] Using the ant colony algorithm, a new set of test cases is generated based on the updated pheromones of each target path and the preset ant colony state transition rules.

[0029] For each new test case in the new test case set, calculate the Hamming distance between the new test case and each first test case in the first test case set, and take the new test cases whose Hamming distance is greater than or equal to the preset distance threshold as candidate test cases.

[0030] Replace the first test case with the candidate test case to obtain the second set of test cases.

[0031] In one embodiment, obtaining the target path set corresponding to the program under test includes:

[0032] Obtain the control flow graph of the program under test and the path coverage information when the program under test is executed;

[0033] Based on the control flow graph, obtain the defined usage paths contained in the program under test;

[0034] Based on the path coverage information, a branch coverage table and an association tree are constructed. The branch coverage table is used to characterize whether each program branch of the program under test is traversed by the actual execution path contained in the path coverage information. Each node of the association tree is used to characterize each program branch, and the edges of the association tree are used to characterize the transit relationship between two connected nodes.

[0035] Based on the branch coverage table and association tree, generate hypothetical paths; hypothetical paths are defined paths where program branch conflicts exist.

[0036] The hypothetical paths are matched with the defined paths to eliminate the defined paths that are actually not executable, thus obtaining the target path set.

[0037] Secondly, this application also provides a test case generation apparatus, the apparatus comprising:

[0038] The target path set acquisition module is used to acquire the target path set corresponding to the program under test; each target path in the target path set is the actual executable defined path used in the program under test.

[0039] The fitness determination module is used to determine the fitness of each initial test case based on the actual execution path and target path of each initial test case in the initial test case set.

[0040] The initial test case optimization module is used to perform local optimization on the initial test cases corresponding to the worst fitness in each subset of the initial test case set to obtain the first test case set. For each first test case in the first test case set that meets the preset conditions, the pheromone of each target path is updated. Based on the updated pheromone of each target path, the first test cases are globally optimized to obtain the second test case set.

[0041] The target test case acquisition module is used to take the second test case set as the initial test case set, and continue to perform local and global optimization on the initial test case set until the optimization termination condition is met. The initial test cases that meet the optimization termination condition are then used as target test cases.

[0042] Thirdly, this application also provides a computer device, including a memory and a processor, wherein the memory stores a computer program, and the processor executes the computer program to implement the method steps of the first aspect.

[0043] Fourthly, this application also provides a computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, implements the method steps of the first aspect.

[0044] Fifthly, this application also provides a computer program product, including a computer program that, when executed by a processor, implements the method steps of the first aspect.

[0045] The aforementioned test case generation method, apparatus, computer equipment, computer-readable storage medium, and computer program product obtain a set of target paths corresponding to the program under test; each target path in the target path set is a defined and executable path in the program under test; the fitness of each initial test case is determined based on the actual execution path and target path of each initial test case in the initial test case set; local optimization is performed on the initial test cases corresponding to the worst fitness in each subset of the initial test case set to obtain a first test case set; for each first test case in the first test case set that meets preset conditions, the pheromone of each target path is updated; and global optimization is performed on each first test case based on the updated pheromone of each target path to obtain... A second set of test cases is obtained; this second set of test cases is used as the initial set of test cases, and local and global optimizations are performed on the initial set of test cases until the optimization termination condition is met. The initial test cases that meet the optimization termination condition are used as target test cases. In this way, this application obtains the target path set corresponding to the program under test. The target paths in this target path set are the actual executable defined paths in the program under test, thereby avoiding the consumption of algorithm resources by invalid defined paths. By determining the fitness of each initial test case and performing local and global optimizations on the initial test cases based on the fitness, the algorithm's guidance on path coverage can be enhanced, thereby improving the generation efficiency and path coverage of target test cases. Attached Figure Description

[0046] To more clearly illustrate the technical solutions in the embodiments of this application or related technologies, the drawings used in the description of the embodiments of this application or related technologies will be briefly introduced below. Obviously, the drawings described below are only some embodiments of this application. For those skilled in the art, other related drawings can be obtained based on these drawings without creative effort.

[0047] Figure 1 This is an application environment diagram generated from test cases in one embodiment;

[0048] Figure 2 This is a flowchart illustrating a test case generation method in one embodiment;

[0049] Figure 3 This is a schematic diagram of the system architecture of a test case generation method in one embodiment;

[0050] Figure 4 This is a structural block diagram of a test case generation device in one embodiment;

[0051] Figure 5 This is an internal structural diagram of a computer device in one embodiment. Detailed Implementation

[0052] To make the objectives, technical solutions, and advantages of this application clearer, the following detailed description is provided in conjunction with the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are merely illustrative and not intended to limit the scope of this application.

[0053] It should be noted that the terms "first," "second," etc., used in this application can be used to describe various elements, but these elements are not limited by these terms. These terms are only used to distinguish the first element from the second element. The terms "comprising" and "having," and any variations thereof, used in this application, are intended to cover non-exclusive inclusion. The term "multiple" used in this application refers to two or more. The term "and / or" used in this application refers to one of the embodiments, or any combination of multiple embodiments.

[0054] ACO-NSA (Ant Colony Optimization-Negative Selection Algorithm) is a hybrid model that combines the global search capability of the Ant Colony Algorithm (ACO) with the anomaly detection capability of the Negative Selection Algorithm (NSA).

[0055] ISFLA (Improved Shuffled Frog Leaping Algorithm): An optimized model of the standard Shuffled Frog Leaping Algorithm (SFLA). The core improvements include the introduction of a dynamic threshold to adjust the step size of the worst individual and the adjustment of the worst individual update strategy (from learning from random individuals to replace random jumping), which enhances the accuracy of local search and the efficiency of population information interaction.

[0056] SFLA (Shuffled Frog Leaping Algorithm): A swarm intelligence optimization algorithm that simulates the foraging behavior of frog groups, through iterative optimization of "population division - local search - population shuffling".

[0057] NSA (Negative Selection Algorithm): An algorithm derived from the artificial immune system that identifies "non-self" (abnormal data) by generating "detectors".

[0058] PSO (Particle Swarm Optimization): An optimization algorithm that simulates the foraging behavior of bird flocks, guiding particle iteration through individual and group optimal solutions.

[0059] CFG (Control Flow Graph): A graphical representation of the program execution path, with nodes representing statements or branches and edges representing the execution order.

[0060] DU Path (Definition-Use Path): The complete execution path of a variable in a program from "definition" (assignment) to "use" (participation in calculations or branch judgments).

[0061] Infeasible Path: A path in a program that cannot be executed by any test data. It mainly includes two types: "branch-branch" conflict paths (such as if-else condition contradictions) and "assignment-branch" conflict paths (such as variables participating in branch judgments without being assigned values).

[0062] Path Coverage: One of the coverage criteria in software testing, which requires test data to cover all possible execution paths in the program.

[0063] Fitness Function: A function that quantifies the degree of matching between test data and the target path, used to guide the iterative optimization of intelligent algorithms.

[0064] Monte Carlo Algorithm: A statistical simulation method based on random sampling.

[0065] Automatic instrumentation is a technique that inserts "path marker probes" at critical locations in a program (such as branch and loop entry points) through lexical and syntactic analysis.

[0066] The test case generation method provided in this application embodiment can be applied to, for example... Figure 1In the application environment shown, terminal 102 acquires the target path set corresponding to the program under test; each target path in the target path set is a defined executable path in the program under test; based on the actual execution path and target path of each initial test case in the initial test case set, the fitness of each initial test case is determined; local optimization is performed on the initial test cases corresponding to the worst fitness in each subset of the initial test case set to obtain a first test case set; for each first test case in the first test case set that meets the preset conditions, the pheromone of each target path is updated; based on the updated pheromone of each target path, each first test case is globally optimized to obtain a second test case set; the second test case set is used as the initial test case set, and local and global optimization is continued on the initial test case set until the optimization termination condition is met; the initial test cases that meet the optimization termination condition are used as target test cases. Terminal 102 can be, but is not limited to, various personal computers, laptops, smartphones, and tablets.

[0067] In one embodiment, such as Figure 2 As shown, a test case generation method is provided. This embodiment applies this method to... Figure 1 Taking terminal 102 as an example, the method includes the following steps:

[0068] Step S210: Obtain the target path set corresponding to the program under test; each target path in the target path set is the actual executable defined path in the program under test.

[0069] In this context, the program under test refers to a software program that requires test cases to be generated to verify its correctness. For example, the program under test could be a program that classifies triangles.

[0070] In this embodiment, static analysis can be performed on the program under test to obtain its program structure information. Static analysis refers to the process of obtaining program structure information by analyzing the source code or intermediate representation of the program under test without actually executing it. For example, analyzing the source code of the program under test can identify conditional statements and loop structures. Program structure information refers to data describing the internal logical structure of the program; for example, the program's control flow graph, the location information of all conditional branch points, and function call relationships.

[0071] Execute tests on the program under test and collect path coverage information during the test process. Path coverage information includes data collected after the test is executed regarding the programs that have been executed in the program under test; for example, the records show that the test covered the program branch in the triangle classification program that determines the angle size.

[0072] Based on the program structure information, extract the defined usage paths. Based on the path coverage information, eliminate the actually unexecutable defined usage paths (infeasible paths) to obtain the target path.

[0073] Step S220: Determine the fitness of each initial test case based on the actual execution path and target path of each initial test case in the initial test case set.

[0074] The initial test case set includes multiple initial test cases, each of which is used to test the program under test.

[0075] Among them, the fitness of the initial test cases is used to quantify the degree of matching between the actual execution path of the initial test cases and each target path.

[0076] In this embodiment, an initial test case set is generated using the NSA algorithm. Each initial test case in the initial test case set is input into the program under test to obtain the actual execution path of each initial test case, and the fitness of the actual execution path of each initial test case with respect to each target path is calculated.

[0077] Step S230: Locally optimize the initial test cases corresponding to the worst fitness in each subset of the initial test case set to obtain a first test case set. For each first test case in the first test case set that meets the preset conditions, update the pheromone of each target path. Based on the updated pheromone of each target path, globally optimize each first test case to obtain a second test case set.

[0078] Among them, the preset conditions include at least: the fitness of the first test case is less than the average fitness of all first test cases in the first test case set.

[0079] Pheromones are used to characterize the search value of a target path. The higher the pheromone concentration, the more worthwhile it is to focus on finding corresponding test cases for that target path.

[0080] In this embodiment, the initial test case set is divided into multiple subsets, and the initial test cases within each subset are sorted according to their fitness. The initial test cases corresponding to the worst fitness and the best fitness in each subset are then determined. Using the ISFLA algorithm, the parameters of the initial test cases corresponding to the worst fitness are fine-tuned to those corresponding to the best fitness. A local iteration upper limit is set, retaining only the initial test cases with improved fitness. After the local iteration is complete, all subsets are merged to obtain the first test case set.

[0081] Iterate through all the first test cases in the first test case set and select the first test cases that meet the preset conditions. These preset conditions include: the fitness of the first test case is lower than the average fitness of all first test cases in the first test case set, and the random number is less than a threshold q0 (e.g., q0 = 0.5). The random number is a pseudo-random floating-point number that follows a uniform distribution; each time the random number is called, it is an independent resampling process, and the numbers are uncorrelated.

[0082] The ACO algorithm is initiated to globally optimize the first test cases that meet the preset conditions. Specifically, in the first iteration of the ACO algorithm, initial pheromones are assigned to each target path; thereafter, the initial pheromones are not reassigned in each iteration. Based on the fitness of the first test cases in the first test case set whose fitness is higher than a preset fitness threshold, the initial pheromones of each target path are updated to obtain the updated pheromones for each target path. Based on the updated pheromones of each target path, candidate test cases are generated, and the candidate test cases replace the first test cases that meet the preset conditions to obtain the second test case set.

[0083] Step S240: Use the second test case set as the initial test case set, and continue to perform local and global optimization on the initial test case set until the optimization termination condition is met. Use the initial test case that meets the optimization termination condition as the target test case.

[0084] The optimization termination conditions include at least: reaching the maximum number of optimization iterations, and / or, all target paths being fully covered by the initial test cases.

[0085] In this embodiment of the application, after performing local and global optimization on the initial test case set, it is determined whether the optimization termination condition is met. If the optimization termination condition is not met, the second test case set is used as the initial test case set, and steps S220 to S230 are continued to start the next local and global optimization until the optimization termination condition is met. The initial test case that meets the optimization termination condition is used as the target test case.

[0086] The above test case generation method involves obtaining a set of target paths corresponding to the program under test; each target path in the target path set is a defined executable path in the program under test; determining the fitness of each initial test case based on its actual execution path and target path; performing local optimization on the initial test cases corresponding to the worst fitness in each subset of the initial test case set to obtain a first test case set; updating the pheromones of each target path for each first test case in the first test case set that meets preset conditions; and performing global optimization on each first test case based on the updated pheromones of each target path to obtain a second test case set; and then... The initial test case set is used as the test case set. Local and global optimizations are then performed on this initial test case set until the optimization termination condition is met. The initial test cases that meet the termination condition are then used as target test cases. In this way, this application obtains the target path set corresponding to the program under test. The target paths in this target path set are the actually executable defined paths in the program under test, thus avoiding the consumption of algorithm resources by invalid defined paths. By determining the fitness of each initial test case and performing local and global optimizations based on the fitness, the algorithm's guidance on path coverage can be enhanced, thereby improving the generation efficiency and path coverage of target test cases.

[0087] In one embodiment, obtaining the set of target paths corresponding to the program under test includes:

[0088] Step S310: Obtain the control flow graph corresponding to the program under test and the path coverage information when the program under test is executed.

[0089] In this embodiment, the Soot tool is used to perform static analysis on the program under test and generate a control flow graph (CFG). The PSO algorithm is run to generate test cases, drive the execution of the automatically instrumented program under test, and collect path coverage information.

[0090] Step S320: Based on the control flow graph, obtain the defined usage paths contained in the program under test.

[0091] In this embodiment of the application, the definition statements and usage statements of each variable in the control flow graph are identified to determine the complete execution path of each variable in the program under test from "definition" (assignment) to "use" (participation in calculation or branch judgment), and the definition and usage path of each variable is obtained.

[0092] Step S330: Based on the path coverage information, construct a branch coverage table and an association tree; the branch coverage table is used to characterize whether each program branch of the program under test is traversed by the actual execution path contained in the path coverage information, each node of the association tree is used to characterize each program branch, and the edges of the association tree are used to characterize the transit relationship between two connected nodes.

[0093] The branch coverage table can be in matrix form (n×m), where n represents the number of test cases and m represents the number of program branches. A value of 1 in the i-th row and j-th column of the branch coverage table indicates that the j-th program branch is activated when the i-th test case executes the program under test; a value of 0 in the i-th row and j-th column indicates that the j-th program branch is not activated when the i-th test case executes the program under test.

[0094] Program branches include, but are not limited to, conditional branches (such as if-else, switch) and loop structure branches (such as while, for).

[0095] In this embodiment, the branch coverage table is populated based on the path coverage information of each test case. The association relationships of program branches are traced backward from the program's end node, and edges marked as "transitive (T) / non-transitive (F)" are used to obtain the association tree. A transitive relationship indicates that two program branches can be traversed simultaneously by the actual execution path; a non-transitive relationship indicates that two program branches cannot be traversed simultaneously by the actual execution path.

[0096] Step S340: Generate hypothetical paths based on the branch coverage table and the association tree; the hypothetical paths are defined paths where program branch conflicts exist.

[0097] Among them, program branch conflicts include, but are not limited to, "branch-branch" conflicts (such as if-else conditional contradictions) and "assignment-branch" conflicts (such as variables not being assigned values, yet participating in branch judgments).

[0098] In this embodiment, a branch coverage table can be used to determine whether a single program branch can be traversed by the actual execution path. An association tree can be used to determine whether a combination of multiple program branches can be traversed by the actual execution path. A hypothetical path generator component generates hypothetical paths based on the marked edges in the branch coverage table and the association tree.

[0099] Step S350: Match the hypothetical paths with the defined usage paths to eliminate the defined usage paths that are actually not executable, and obtain the target path set.

[0100] In this embodiment, each defined usage path is traversed, and each defined usage path is matched with each hypothetical path. If a defined usage path matches a hypothetical path, the defined usage path is determined to be an actually non-executable defined usage path. All actually non-executable defined usage paths are then removed to obtain the target path set.

[0101] This application's embodiments improve the identification rate of actually unexecutable defined usage paths by constructing a branch coverage table and an association tree, thus avoiding the waste of algorithm resources by invalid paths.

[0102] In one embodiment, the fitness of each initial test case is determined based on the actual execution path and target path of each initial test case in the initial test case set, including:

[0103] Step S410: For each initial test case, obtain the actual execution path of the initial test case and the number of first nodes in each target path that are in the same position, as well as the number of second nodes that contain consecutive identical nodes.

[0104] In this context, a node refers to a program statement contained within a path.

[0105] The number of first nodes is used to reflect the degree of matching between discrete nodes in the actual execution path and the target path, but it cannot reflect the continuity of the execution process.

[0106] The number of the second node reflects the coherence of the actual execution path, meaning that the initial test cases are more likely to be executed completely along the target path, and have a stronger guiding effect on path coverage.

[0107] In the embodiments of this application, for example, if the target path P={1,2,3,4,5}, the actual execution path of the initial test case P1={1,2,3,6,7}, P2={1,3,2,4,7}, then the number of the first node and the number of the second node of P1 are both 3, the number of the first node of P2 is 3, and the number of the second node is 1.

[0108] Step S420: Calculate the first ratio of the number of first nodes to the total number of nodes in each target path, and the second ratio of the number of second nodes to the total number of nodes in each target path. Then, perform a weighted fusion of the first and second ratios to obtain the single-path fitness between the actual execution path of the initial test case and each target path.

[0109] In this embodiment of the application, for example, the nodes in the path are represented by numbers. If the target path P={1,2,3,4,5}, the actual execution path of the initial test case P1={1,2,3,6,7}, and P2={1,3,2,4,7}, then the total number of nodes in the target path is 5. Then the first ratio and the second ratio corresponding to P1 are both 3 / 5=0.6, the first ratio corresponding to P2 is 3 / 5=0.6, and the second ratio corresponding to P2 is 1 / 5=0.2.

[0110] After obtaining the first ratio and the second ratio corresponding to the actual execution path of the initial test cases, the first ratio and the second ratio are weighted and summed according to the preset first weight and second weight to obtain the single-path fitness between the actual execution path of the initial test cases and each target path. The sum of the first weight and the second weight is 1.

[0111] Step S430: Obtain the maximum single-path fitness of the initial test cases, calculate the average of all single-path fitness of the initial test cases, and subtract the maximum single-path fitness from the average to obtain the fitness of the initial test cases.

[0112] In this embodiment of the application, there are multiple target paths. First, the single-path fitness of the actual execution path and each target path is calculated. Then, the maximum single-path fitness of the initial test cases is selected. Finally, the maximum single-path fitness is subtracted from the average value to obtain the fitness of the initial test cases.

[0113] For example, in a scenario with 3 target paths, if the initial test case has a single-path fitness of M1=0.8 for path 1, M2=0.5 for path 2, and M3=0.3 for path 3, then the maximum single-path fitness of the initial test case is 0.8, the average single-path fitness of all initial test cases is 0.53, and the fitness of the initial test case = 0.8 - 0.53 = 0.27.

[0114] In one embodiment, a weighted fusion of the first ratio and the second ratio is performed to obtain the single-path fitness between the actual execution path of the initial test case and each target path, including:

[0115] Step S510: Using the Monte Carlo algorithm, random sampling simulation is performed on the first weight corresponding to the first ratio and the second weight corresponding to the second ratio to calculate the weight evaluation index; the weight evaluation index is determined based on the path coverage success rate and the number of convergence iterations.

[0116] In this embodiment, the Monte Carlo algorithm is used to simulate the Markowitz risk-reward model to quantitatively allocate the first and second weights. The specific simulation process is as follows: With path coverage success rate (reward) and convergence iteration count (risk) as objectives, 1000 random sampling simulations are performed on the weight combination of the first weight w1 and the second weight w2 (w1∈[0,1], w2=1-w1) to calculate the weight evaluation index of the algorithm under different weights. The weight evaluation index is defined as: path coverage success rate × (1 / convergence iteration count). Simulation results show that the weight evaluation index reaches its optimum when w1=0.0125 and w2=0.9875.

[0117] Step S520: Based on the first weight and the second weight when the weight evaluation index reaches its optimal value, the first ratio and the second ratio are weighted and summed to obtain the single-path fitness between the actual execution path of the initial test case and each target path.

[0118] In this embodiment, the first weight when the weight evaluation index reaches its optimal value is multiplied by the first ratio to obtain a first product result. The second weight when the weight evaluation index reaches its optimal value is multiplied by the second ratio to obtain a second product result. The first product result and the second product result are summed to obtain the single-path fitness between the actual execution path of the initial test case and each target path.

[0119] This application utilizes the Monte Carlo algorithm to determine the first weight corresponding to the first ratio and the second weight corresponding to the second ratio. Since the second weight is greater than the first weight, it indicates that continuous node matching dominates the contribution to fitness. Therefore, it retains the basic information of discrete node matching while strengthening the guiding role of path execution coherence, solving the problem of "fuzzy factor influence" in traditional qualitative analysis, and thus improving the accuracy of fitness.

[0120] In one embodiment, prior to the step of locally optimizing the initial test cases with the worst fitness in each subset of the initial test case set, the method further includes:

[0121] Step S610: Traverse each target path. If the single-path fitness between the actual execution path of the initial test case and the target path is a preset value, delete the target path from the target path set.

[0122] The default value is 1.

[0123] In this embodiment of the application, when calculating the single-path fitness of the actual execution path of the initial test case and each target path, if the single-path fitness of the actual execution path of the initial test case and a certain target path is a preset value, it means that the target path has been covered by the initial test case. The target path is then deleted from the target path set, reducing the target dimension of subsequent optimization of the initial test case and improving the generation efficiency of the target test case.

[0124] In one embodiment, local optimization is performed on the initial test cases with the worst fitness in each subset of the initial test case set to obtain a first test case set, including:

[0125] Step S710: For each subset, obtain the current position of the initial test case with the worst fitness in the subset, the product of the best fitness and the preset threshold; the preset threshold is determined based on random numbers and preset coefficients, and the preset coefficients decrease non-linearly with the number of iterations.

[0126] Step S720: Subtract the product result from the worst fitness to obtain the difference;

[0127] Step S730: Multiply the difference by the random number to obtain the position movement step size;

[0128] Step S740: Update the current position of the initial test case with the worst fitness according to the position movement step size, and iteratively optimize the initial test case with the worst fitness until the iteration ends. Then merge the subsets after each iteration to obtain the first test case set.

[0129] In this embodiment of the application, the ISFLA algorithm is used to locally optimize the initial test cases with the worst fitness in each subset of the initial test case set to obtain the first test case set.

[0130] The expression for the position movement step size D is: D=rand×(A×Xb - Xw)), where the preset threshold A=2×a×rand -a, a is a preset coefficient that decreases non-linearly with the number of iterations, rand is a random number, Xb is the best fitness in the subset, and Xw is the worst fitness in the subset.

[0131] Based on the position movement step size, update the current position of the initial test case with the worst fitness, so that the initial test cases with the worst fitness in the subset can continuously fine-tune their parameters towards the initial test cases with the best fitness.

[0132] In this embodiment, since the preset threshold changes with the number of iterations and random numbers, a dynamic preset threshold is introduced to adjust the position movement step size of the initial test case with the worst fitness, which can enhance the accuracy of local optimization and improve the quality of the first test case set.

[0133] In one embodiment, updating the pheromones of each target path includes:

[0134] Step S810: For each target path, obtain the pheromone increment of each elite test case in the first test case set to the target path; each elite test case is the first test case in the first test case set whose fitness is higher than a preset fitness threshold; the pheromone increment of each elite test case to the target path is determined based on the single-path fitness between the actual execution path of each elite test case and the target path.

[0135] The preset fitness threshold can be set according to actual needs.

[0136] In the embodiments of this application, during each round of global optimization, the first test cases in the first test case set are sorted in descending order according to fitness, and elite test cases with higher fitness rankings are selected.

[0137] Each elite test case For each target path Each test case has its own dedicated pheromone increment, which can be generated by elite test cases. The actual execution path and the target path The single-path fitness between test cases is determined. Specifically, a mapping relationship between single-path fitness and pheromone increment can be pre-established, and the corresponding pheromone increment can be found from the mapping relationship based on the single-path fitness of the elite test cases.

[0138] Step S820: Update the current pheromone level of the target path based on the pheromone increment of each elite test case.

[0139] In this embodiment of the application, the pheromone update expression is:

[0140]

[0141] in, Indicates the target path The updated pheromones Indicates the target path before the update. The current pheromones, Elite test cases The pheromone increment for the target path, This indicates the preset coefficient.

[0142] In one embodiment, based on the updated pheromones of each target path, each first test case is globally optimized to obtain a second test case set, including:

[0143] Step S1010: Using the ant colony algorithm, a new set of test cases is generated based on the updated pheromones of each target path and the preset ant colony state transition rules.

[0144] In this embodiment, based on the updated pheromone of each target path and the preset ant colony state transition rules, each ant in the ant colony algorithm can walk in the ant path network of the ant colony algorithm to generate a path. Based on the preset value represented by the node on each path, each path is transformed into a new test case, and a new set of test cases is obtained.

[0145] Step S1020: For each new test case in the new test case set, calculate the Hamming distance between the new test case and each first test case in the first test case set, and select new test cases whose Hamming distance is greater than or equal to a preset distance threshold as candidate test cases.

[0146] The preset distance threshold can be set according to actual needs.

[0147] In this embodiment of the application, the NSA algorithm is used to calculate the Hamming distance between the new test case and each first test case in the first test case set, and new test cases with a Hamming distance less than a preset distance threshold are eliminated, while new test cases with a Hamming distance greater than or equal to the preset distance threshold are selected as candidate test cases.

[0148] Step S1030: Replace the first test case with the candidate test case to obtain the second test case set.

[0149] In this embodiment of the application, a second set of test cases is obtained by replacing the first test cases that meet the preset conditions one by one with candidate test cases.

[0150] In this embodiment of the application, by obtaining a new set of test cases and removing new test cases whose Hamming distance is less than a preset distance threshold, the amount of redundant data can be reduced, thereby improving the efficiency of generating subsequent target test cases.

[0151] To understand the above method embodiments, such as Figure 3 As shown, a system architecture for a test case generation method is provided. The system includes an input layer, a preprocessing layer, a search layer, a feedback layer, a computation layer, and an output layer. The input layer is used to input the source code of the program under test, the range of variable values, and path coverage criteria. The preprocessing layer is used to generate a control flow graph, extract defined usage paths, construct a branch coverage table and association tree, and eliminate infeasible paths. The search layer is used to execute the ISFLA algorithm and the ACO-NSA algorithm to perform local and global optimization on the initial test case set. The feedback layer is used to collect path coverage information. The computation layer is used to determine the fitness of the initial test cases. The output layer is used to output the target test cases.

[0152] It should be understood that although the steps in the flowcharts of the above embodiments are shown sequentially according to the arrows, these steps are not necessarily executed in the order indicated by the arrows. Unless explicitly stated herein, there is no strict order restriction on the execution of these steps, and they can be executed in other orders. Moreover, at least some steps in the flowcharts of the above embodiments may include multiple steps or multiple stages. These steps or stages are not necessarily completed at the same time, but can be executed at different times. The execution order of these steps or stages is not necessarily sequential, but can be performed alternately or in turn with other steps or at least some of the steps or stages in other steps. It is understood that the steps in different embodiments can be freely combined as needed, and all non-contradictory solutions formed by such combinations are within the scope of protection of this application.

[0153] In one exemplary embodiment, such as Figure 4 As shown, a test case generation device is provided, the device comprising:

[0154] The target path set acquisition module 410 is used to acquire the target path set corresponding to the program under test; each target path in the target path set is the actual executable defined path used in the program under test.

[0155] The fitness determination module 420 is used to determine the fitness of each initial test case based on the actual execution path and target path of each initial test case in the initial test case set.

[0156] The initial test case optimization module 430 is used to perform local optimization on the initial test cases corresponding to the worst fitness in each subset of the initial test case set to obtain a first test case set. For each first test case in the first test case set that meets the preset conditions, the pheromone of each target path is updated. Based on the updated pheromone of each target path, the first test cases are globally optimized to obtain a second test case set.

[0157] The target test case acquisition module 440 is used to take the second test case set as the initial test case set, and continue to perform local and global optimization on the initial test case set until the optimization termination condition is met. The initial test case that meets the optimization termination condition is taken as the target test case.

[0158] In one embodiment, the target path set acquisition module includes:

[0159] The control flow graph acquisition unit is used to acquire the control flow graph corresponding to the program under test and the path coverage information when the program under test is executed.

[0160] Define the usage path acquisition unit, which is used to obtain the defined usage paths contained in the program under test based on the control flow graph;

[0161] The branch coverage table construction unit is used to construct a branch coverage table and an association tree based on the path coverage information. The branch coverage table is used to characterize whether each program branch of the program under test is traversed by the actual execution path contained in the path coverage information. Each node of the association tree is used to characterize each program branch, and the edge of the association tree is used to characterize the transit relationship between two connected nodes.

[0162] The hypothetical path generation unit is used to generate hypothetical paths based on the branch coverage table and the association tree; the hypothetical path is a defined path that has program branch conflicts.

[0163] The target path set acquisition unit is used to match the hypothetical paths with the defined usage paths in order to eliminate the defined usage paths that are actually not executable, and obtain the target path set.

[0164] In one embodiment, the fitness determination module includes:

[0165] The node count acquisition unit is used to acquire, for each initial test case, the number of first nodes in the actual execution path of the initial test case and the number of second nodes containing consecutive identical nodes in each target path.

[0166] The single-path fitness acquisition unit is used to calculate the first ratio of the number of first nodes to the total number of nodes in each target path, and the second ratio of the number of second nodes to the total number of nodes in each target path. The first ratio and the second ratio are weighted and fused to obtain the single-path fitness between the actual execution path of the initial test case and each target path.

[0167] The fitness acquisition unit is used to obtain the maximum single-path fitness of the initial test cases, calculate the average of the single-path fitness of the initial test cases, and subtract the average from the maximum single-path fitness to obtain the fitness of the initial test cases.

[0168] In one embodiment, the single-path fitness acquisition unit includes:

[0169] The weight evaluation index calculation subunit is used to perform random sampling simulation on the first weight corresponding to the first ratio and the second weight corresponding to the second ratio using the Monte Carlo algorithm to calculate the weight evaluation index; the weight evaluation index is determined based on the path coverage success rate and the number of convergence iterations.

[0170] The single-path fitness acquisition sub-unit is used to obtain the single-path fitness between the actual execution path of the initial test case and each target path by weighted summing of the first ratio and the second ratio based on the first and second weights when the weight evaluation index reaches its optimal value.

[0171] In one embodiment, the device further includes:

[0172] The target path deletion module is used to traverse each target path and delete the target path from the target path set if the single-path fitness between the actual execution path of the initial test case and the target path is a preset value.

[0173] In one embodiment, the initial test case optimization module includes:

[0174] The location acquisition unit is used to obtain the current location of the initial test case with the worst fitness in each subset, the product of the best fitness and the preset threshold; the preset threshold is determined based on random numbers and preset coefficients, and the preset coefficients decrease non-linearly with the number of iterations.

[0175] The difference obtaining unit is used to subtract the worst fitness from the product result to obtain the difference;

[0176] The position movement step size acquisition unit is used to multiply the difference by a random number to obtain the position movement step size;

[0177] The first test case set acquisition unit is used to update the current position of the initial test case with the worst fitness according to the position movement step size, so as to iteratively optimize the initial test case with the worst fitness until the iteration ends, and merge the subsets after each iteration to obtain the first test case set.

[0178] In one embodiment, the initial test case optimization module further includes:

[0179] The pheromone increment acquisition unit is used to acquire the pheromone increment of each elite test case in the first test case set for each target path; each elite test case is the first test case in the first test case set whose fitness is higher than a preset fitness threshold; the pheromone increment of each elite test case for the target path is determined based on the single-path fitness between the actual execution path of each elite test case and the target path.

[0180] The pheromone update unit is used to update the current pheromone level of the target path based on the pheromone increment of each elite test case.

[0181] In one embodiment, the initial test case optimization module further includes:

[0182] The test case set generation unit is used to generate a new test case set using the ant colony algorithm, based on the updated pheromones of each target path and the preset ant colony state transition rules.

[0183] The candidate test case acquisition unit is used to calculate the Hamming distance between each new test case in the new test case set and each first test case in the first test case set, and to take new test cases whose Hamming distance is greater than or equal to a preset distance threshold as candidate test cases.

[0184] The second test case set acquisition unit is used to replace the first test case with the candidate test case to obtain the second test case set.

[0185] Each module in the aforementioned test case generation device can be implemented entirely or partially through software, hardware, or a combination thereof. These modules can be embedded in or independent of the processor in a computer device, or stored in the memory of a computer device as software, so that the processor can call and execute the operations corresponding to each module.

[0186] In one exemplary embodiment, a computer device is provided, which may be a server, and its internal structure diagram may be as follows. Figure 5 As shown, this computer device includes a processor, memory, input / output interfaces (I / O), and a communication interface. The processor, memory, and I / O interfaces are connected via a system bus, and the communication interface is also connected to the system bus via the I / O interfaces. The processor provides computational and control capabilities. The memory includes non-volatile storage media and internal memory. The non-volatile storage media stores the operating system, computer programs, and a database. The internal memory provides the environment for the operation of the operating system and computer programs stored in the non-volatile storage media. The database stores test case generation data. The I / O interfaces are used for exchanging information between the processor and external devices. The communication interface is used for communicating with external terminals via a network connection. When the computer program is executed by the processor, it implements a test case generation method.

[0187] Those skilled in the art will understand that Figure 5The structures shown are merely block diagrams of some structures related to the present application and do not constitute a limitation on the computer device to which the present application is applied. Specific computer devices may include more or fewer components than shown in the figures, or combine certain components, or have different component arrangements. In one embodiment, a computer device is provided, including a memory and a processor. The memory stores a computer program, which, when executed by the processor, causes the processor to perform the steps of the aforementioned test case generation method. The steps of the test case generation method here may be steps from one of the test case generation methods in the various embodiments described above.

[0188] In one embodiment, a computer-readable storage medium is provided, storing a computer program that, when executed by a processor, causes the processor to perform the steps of the test case generation method described above. The steps of the test case generation method described here may be steps from one of the test case generation methods in the various embodiments described above.

[0189] In one embodiment, a computer program product is provided, including a computer program that, when executed by a processor, causes the processor to perform the steps of the test case generation method described above. The steps of the test case generation method described here may be steps from one of the test case generation methods in the various embodiments described above.

[0190] It should be noted that the user information (including but not limited to user device information, user personal information, etc.) and data (including but not limited to data used for analysis, data stored, data displayed, etc.) involved in this application are all information and data authorized by the user or fully authorized by all parties, and the collection, use and processing of the relevant data must comply with relevant regulations.

[0191] Those skilled in the art will understand that all or part of the processes in the methods of the above embodiments can be implemented by a computer program instructing related hardware. The computer program can be stored in a non-volatile computer-readable storage medium. When executed, the computer program can include the processes of the embodiments of the above methods. Any references to memory, databases, or other media used in the embodiments provided in this application can include at least one of non-volatile memory and volatile memory. Non-volatile memory can include read-only memory (ROM), magnetic tape, floppy disk, flash memory, optical memory, high-density embedded non-volatile memory, resistive random access memory (ReRAM), magnetic random access memory (MRAM), ferroelectric random access memory (FRAM), phase change memory (PCM), graphene memory, etc. Volatile memory can include random access memory (RAM) or external cache memory, etc. By way of illustration and not limitation, RAM can take many forms, such as Static Random Access Memory (SRAM) or Dynamic Random Access Memory (DRAM). The databases involved in the embodiments provided in this application may include at least one type of relational database and non-relational database. Non-relational databases may include, but are not limited to, blockchain-based distributed databases. The processors involved in the embodiments provided in this application may be general-purpose processors, central processing units, graphics processing units, digital signal processors, programmable logic devices, quantum computing-based data processing logic devices, artificial intelligence (AI) processors, etc., and are not limited to these.

[0192] The technical features of the above embodiments can be combined in any way. For the sake of brevity, not all possible combinations of the technical features in the above embodiments are described. However, as long as there is no contradiction in the combination of these technical features, they should be considered to be within the scope of this application.

[0193] The above embodiments are merely illustrative of several implementation methods of this application, and their descriptions are relatively specific and detailed. However, they should not be construed as limiting the scope of this application. It should be noted that those skilled in the art can make various modifications and improvements without departing from the concept of this application, and these all fall within the protection scope of this application. Therefore, the protection scope of this application should be determined by the appended claims.

Claims

1. A test case generation method characterized by, The method includes: Obtain the set of target paths corresponding to the program under test; each target path in the set of target paths is a defined and used path that is actually executable in the program under test. The fitness of each initial test case is determined based on the actual execution path of each initial test case in the initial test case set and the target path. Local optimization is performed on the initial test cases corresponding to the worst fitness in each subset of the initial test case set to obtain a first test case set. For each first test case in the first test case set that meets the preset conditions, the pheromone of each target path is updated. Based on the updated pheromone of each target path, each first test case is globally optimized to obtain a second test case set. The second test case set is used as the initial test case set. Local and global optimizations are then performed on the initial test case set until the optimization termination condition is met. The initial test cases that meet the optimization termination condition are then used as target test cases.

2. The test case generation method of claim 1, wherein, The step of determining the fitness of each initial test case based on the actual execution path of each initial test case in the initial test case set and the target path includes: For each of the initial test cases, obtain the number of first nodes in the actual execution path of the initial test case that are in the same position as the nodes in each of the target paths, and the number of second nodes that contain consecutive identical nodes; Calculate a first ratio of the number of the first node to the total number of nodes in each target path, and a second ratio of the number of the second node to the total number of nodes in each target path. Then, perform a weighted fusion of the first ratio and the second ratio to obtain the single-path fitness between the actual execution path of the initial test case and each target path. Obtain the maximum single-path fitness of the initial test case, calculate the average of all single-path fitness of the initial test case, and subtract the maximum single-path fitness from the average to obtain the fitness of the initial test case.

3. The test case generation method of claim 2, wherein, The step of weighting and fusing the first ratio and the second ratio to obtain the single-path fitness between the actual execution path of the initial test case and each of the target paths includes: Using the Monte Carlo algorithm, random sampling simulations are performed on the first weight corresponding to the first ratio and the second weight corresponding to the second ratio to calculate the weight evaluation index; the weight evaluation index is determined based on the path coverage success rate and the number of convergence iterations. Based on the first and second weights when the weight evaluation index reaches its optimal value, the first ratio and the second ratio are weighted and summed to obtain the single-path fitness between the actual execution path of the initial test case and each of the target paths.

4. The test case generation method of claim 2, wherein, Before the step of locally optimizing the initial test cases with the worst fitness in each subset of the initial test case set, the method further includes: Traverse each target path. If the single-path fitness between the actual execution path of the initial test case and the target path is a preset value, delete the target path from the target path set.

5. The test case generation method of claim 1, wherein, The step of locally optimizing the initial test cases with the worst fitness in each subset of the initial test case set to obtain the first test case set includes: For each subset, obtain the current position of the initial test case with the worst fitness in the subset, the product of the best fitness and a preset threshold; the preset threshold is determined based on random numbers and preset coefficients, and the preset coefficients decrease non-linearly with the number of iterations. Subtract the product result from the worst fitness to obtain the difference; Multiply the difference by the random number to obtain the position movement step size; Based on the position movement step size, update the current position of the initial test case of the worst fitness, and iteratively optimize the initial test case of the worst fitness until the iteration ends. Then merge the subsets after each iteration to obtain the first test case set.

6. The test case generation method according to claim 1, characterized in that, The updating of the pheromones for each of the target paths includes: For each target path, the pheromone increment of each elite test case in the first test case set to the target path is obtained; each elite test case is a first test case in the first test case set with a fitness higher than a preset fitness threshold; the pheromone increment of each elite test case to the target path is determined based on the single-path fitness between the actual execution path of each elite test case and the target path. The current pheromone level of the target path is updated based on the pheromone increment of each elite test case.

7. The test case generation method according to claim 1, characterized in that, The step of globally optimizing each of the first test cases based on the updated pheromones of each target path to obtain a second test case set includes: Using the ant colony algorithm, a new set of test cases is generated based on the updated pheromones of each target path and the preset ant colony state transition rules. For each new test case in the new test case set, calculate the Hamming distance between the new test case and each first test case in the first test case set, and select new test cases whose Hamming distance is greater than or equal to a preset distance threshold as candidate test cases; The candidate test cases are used to replace the first test cases to obtain the second set of test cases.

8. The test case generation method according to claim 1, characterized in that, The process of obtaining the target path set corresponding to the program under test includes: Obtain the control flow graph corresponding to the program under test and the path coverage information when the program under test is executed; Based on the control flow graph, obtain the defined usage paths contained in the program under test; Based on the path coverage information, a branch coverage table and an association tree are constructed; the branch coverage table is used to characterize whether each program branch of the program under test is traversed by the actual execution path contained in the path coverage information, each node of the association tree is used to characterize each program branch, and the edge of the association tree is used to characterize the transit relationship between two connected nodes. Based on the branch coverage table and the association tree, a hypothetical path is generated; the hypothetical path is the defined path used when there is a program branch conflict. The hypothetical path is matched with the defined usage path to eliminate the defined usage path that is actually not executable, thereby obtaining the target path set.

9. A test case generation device, characterized in that, The device includes: The target path set acquisition module is used to acquire the target path set corresponding to the program under test; each target path in the target path set is a defined and used path that is actually executable in the program under test. The fitness determination module is used to determine the fitness of each initial test case based on the actual execution path of each initial test case in the initial test case set and the target path; The initial test case optimization module is used to perform local optimization on the initial test cases corresponding to the worst fitness in each subset of the initial test case set to obtain a first test case set. For each first test case in the first test case set that meets the preset conditions, the pheromone of each target path is updated. Based on the updated pheromone of each target path, the first test cases are globally optimized to obtain a second test case set. The target test case acquisition module is used to take the second test case set as the initial test case set, and continue to perform local and global optimization on the initial test case set until the optimization termination condition is met, and take the initial test case that meets the optimization termination condition as the target test case.

10. A computer device comprising a memory and a processor, wherein the memory stores a computer program, characterized in that, When the processor executes the computer program, it implements the steps of the method according to any one of claims 1 to 8.

11. A computer-readable storage medium having a computer program stored thereon, characterized in that, When the computer program is executed by a processor, it implements the steps of the method according to any one of claims 1 to 8.

12. A computer program product, comprising a computer program, characterized in that, When the computer program is executed by a processor, it implements the steps of the method according to any one of claims 1 to 8.