Test case automatic generation method and device, electronic equipment and storage medium

By automatically generating test cases using an improved artificial bee colony-genetic algorithm, the problems of low test case generation efficiency and high redundancy in the existing technology are solved, and efficient and low-redundancy test case set generation is achieved.

CN120743780APending Publication Date: 2025-10-03AGRICULTURAL BANK OF CHINA
View PDF 0 Cites 2 Cited by

Patent Information

Application Number
CN202511034228.7
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-07-25
Publication Date
2025-10-03

AI Technical Summary

Technical Problem

Existing automated testing tools have deficiencies in test case generation, resulting in low testing efficiency, reliance on manual operations, high redundancy and blindness, and difficulty in achieving full coverage of test paths.

Method used

An improved artificial bee colony-genetic algorithm is used to obtain the program to be tested, generate the initial population, determine the fitness function, and iteratively optimize the initial population based on the fitness function to obtain the optimal individual as the target test case.

Benefits of technology

The efficiency and coverage of test case generation are improved, while the number of redundant cases is reduced, and the automated optimization generation of test case sets is achieved.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120743780A_ABST
    Figure CN120743780A_ABST
Patent Text Reader

Abstract

The invention discloses a test case automatic generation method and device, electronic equipment and a storage medium. The method comprises the steps of obtaining a to-be-tested program; analyzing the to-be-tested program to generate an initial population; determining a fitness function; performing iterative optimization on the initial population based on the fitness function to obtain an optimal individual; and taking the optimal individual as a target test case for the to-be-tested program. According to the technical scheme, the boundary value use case generated based on the program control flow diagram is fused with the candidate use case to generate the initial population, the coverage rate of critical paths in the population can be effectively increased, the proportion of redundant use cases can be effectively reduced, and under the same test target, the scale of the use case set is reduced, and the test execution time is shortened. The hierarchical fitness function design reduces the proportion of high-time-consumption operation cases and improves the test efficiency. All stages of operation of the genetic algorithm are combined with all stages of operation of the artificial bee colony algorithm, and algorithm efficiency is improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the technical field of use case generation, and in particular to a method, device, electronic device and storage medium for automatically generating test cases. Background Art

[0002] With the rapid development of computer software technology, the scale and complexity of software programs are increasing, and software quality has become a key factor in determining the success of software products. During software development, test case generation is a core component of the entire testing process. High-quality test cases can effectively discover software defects while reducing the generation of invalid test cases, significantly improving testing efficiency. However, current automated testing tools primarily focus on the test execution and management phases, and still have significant shortcomings in the automated generation of test cases. In practical applications, test case generation still relies heavily on manual operations. This approach is not only labor-intensive but also suffers from high blindness and case redundancy, making it difficult to achieve full test path coverage and resulting in low testing efficiency. Summary of the Invention

[0003] The present invention provides a method, device, electronic device and storage medium for automatically generating test cases, which can improve the efficiency and coverage of test case generation and reduce the number of redundant test cases by automatically generating test cases.

[0004] According to one aspect of the present invention, a method for automatically generating test cases is provided, the method comprising:

[0005] Get the program to be tested;

[0006] Analyzing the program to be tested to generate an initial population; wherein the initial population is composed of a plurality of individuals;

[0007] Determining a fitness function; wherein the fitness function is used to evaluate the test effectiveness of individuals in the initial population for the degree to be tested;

[0008] Based on the fitness function, iteratively optimizing the initial population to obtain the optimal individual;

[0009] The optimal individual is used as a target test case for the program to be tested.

[0010] According to another aspect of the present invention, there is provided a device for automatically generating test cases, the device comprising:

[0011] A program-to-be-tested acquisition module, used for acquiring the program-to-be-tested;

[0012] An initial population generation module, configured to analyze the program to be tested and generate an initial population; wherein the initial population is composed of a plurality of individuals;

[0013] A fitness function determination module is used to determine a fitness function; wherein the fitness function is used to evaluate the test effectiveness of individuals in the initial population for the degree to be tested;

[0014] An optimal individual obtaining module is used to iteratively optimize the initial population based on the fitness function to obtain the optimal individual;

[0015] The target test case determination module is used to use the optimal individual as the target test case for the program to be tested.

[0016] According to another aspect of the present invention, an electronic device is provided, comprising:

[0017] At least one processor; and a memory communicatively connected to the at least one processor; wherein the memory stores a computer program executable by the at least one processor, and the computer program is executed by the at least one processor so that the at least one processor can execute a method for automatically generating test cases as described in any embodiment of the present invention.

[0018] According to another aspect of the present invention, a computer-readable storage medium is provided, wherein the computer-readable storage medium stores computer instructions, and the computer instructions are used to enable a processor to implement a test case automatic generation method according to any embodiment of the present invention when executed.

[0019] The technical solution of the embodiment of the present invention obtains a program to be tested; analyzes the program to be tested to generate an initial population; determines a fitness function; iteratively optimizes the initial population based on the fitness function to obtain the optimal individual; and uses the optimal individual as the target test case for the program to be tested. This technical solution, through automated test case generation, can improve the efficiency and coverage of test case generation while reducing the number of redundant test cases.

[0020] It should be understood that the content described in this section is not intended to identify the key or important features of the embodiments of the present invention, nor is it intended to limit the scope of the present invention. Other features of the present invention will become readily understood through the following description. BRIEF DESCRIPTION OF THE DRAWINGS

[0021] In order to more clearly illustrate the technical solutions in the embodiments of the present invention, the following briefly introduces the drawings required for use in the description of the embodiments. Obviously, the drawings described below are only some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without creative work.

[0022] Figure 1This is a flowchart of a method for automatically generating test cases according to the first embodiment of the present invention;

[0023] Figure 2 The test case automatic generation system model based on the improved artificial bee colony-genetic algorithm provided in Example 1 of the present application;

[0024] Figure 3 The test case automatic generation algorithm framework provided in Example 1 of this application;

[0025] Figure 4 A schematic diagram of a method for automatically generating test cases provided in the second embodiment of the present invention;

[0026] Figure 5 A flowchart of a test case automatic generation process provided in Example 3 of the present invention;

[0027] Figure 6 A schematic diagram of the structure of a test case automatic generation device provided in the fourth embodiment of the present invention;

[0028] Figure 7 The present invention is a schematic diagram of the structure of an electronic device for implementing a method for automatically generating test cases according to an embodiment of the present invention. DETAILED DESCRIPTION

[0029] In order to enable those skilled in the art to better understand the solutions of the present invention, the technical solutions in the embodiments of the present invention will be clearly and completely described below in conjunction with the drawings in the embodiments of the present invention. Obviously, the embodiments described are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without making creative efforts should fall within the scope of protection of the present invention.

[0030] It should be noted that the terms "candidate", "target", etc. in the description and claims of the present invention and the above-mentioned drawings are used to distinguish similar objects and are not necessarily used to describe a specific order or sequence. It should be understood that the terms used in this way can be interchangeable where appropriate so that the embodiments of the present invention described herein can be implemented in an order other than those illustrated or described herein. In addition, the terms "including" and "having" and any variations thereof are intended to cover non-exclusive inclusions. For example, a process, method, system, product or device that includes a series of steps or units is not necessarily limited to those steps or units that are clearly listed, but may include other steps or units that are not clearly listed or that are inherent to these processes, methods, products or devices.

[0031] Example 1

[0032] Figure 1This is a flow chart of a test case automatic generation method provided in accordance with the first embodiment of the present invention. This embodiment is applicable to the case of automatically generating test cases. The method can be executed by a test case automatic generation device. The test case automatic generation device can be implemented in the form of hardware and / or software. The test case automatic generation device can be configured in a device. For example, the device can be a background server or other device with communication and computing capabilities. Figure 1 As shown, the method includes:

[0033] S110: Obtain the program to be tested.

[0034] The program to be tested refers to the target program that needs to be optimized, tested or analyzed by the artificial bee colony algorithm and the genetic algorithm. The program to be tested can be expressed as y=f(x), where x is the input data (constituting the input space V) and y is the output result.

[0035] In this embodiment, the program to be tested may be obtained from a database; or a program to be tested that meets the requirements may be evolved through a genetic algorithm.

[0036] S120: Analyze the program to be tested to generate an initial population; wherein the initial population consists of a plurality of individuals.

[0037] In this scheme, the initial population is the basis for subsequent iterations of the artificial bee colony algorithm and the genetic algorithm, and each individual in the population represents a potential solution in the solution space.

[0038] In this embodiment, the program to be tested may be analyzed to generate a plurality of input data, and then the input data may be used as an individual in the initial population to form an initial population.

[0039] S130, determining a fitness function; wherein the fitness function is used to evaluate the test effectiveness of individuals in the initial population for the degree to be tested.

[0040] In this embodiment, the fitness function is used to evaluate the effectiveness of the test of the individuals in the initial population on the degree to be tested.

[0041] Specifically, a mathematical model corresponding to the optimization objective is defined, and the mathematical model includes at least one objective function and a set of constraints; the objective function is converted into a non-negative fitness value, wherein when the objective function is a maximization problem, the fitness function is the objective function value or a monotonically increasing transformation thereof; a penalty mechanism is implemented for individuals that violate the constraints, and the penalty mechanism includes at least one of a static penalty function, a dynamic penalty function, or an adaptive penalty function; and the fitness function parameters are dynamically adjusted according to the evolutionary state of the population, and the parameters include a penalty coefficient, a scaling factor, or a target weight.

[0042] S140. Based on the fitness function, iteratively optimize the initial population to obtain the optimal individual.

[0043] Specifically, the fitness function is used to evaluate the quality of individuals in the initial population, retain individuals with high fitness, and generate a new population through operations such as selection, crossover, and mutation. This process is repeated until the individual with the best fitness is found.

[0044] Optionally, based on the fitness function, iteratively optimizing the initial population to obtain the optimal individual includes:

[0045] Determine initial parameters; wherein the initial parameters include the number of nectar sources, crossover probability, mutation probability, maximum number of bee collections, and maximum number of iterations; the crossover probability and the mutation probability are dynamically adjusted based on the current number of iterations and the maximum number of iterations;

[0046] Initialize honey source;

[0047] In the stage of employed bees searching for nectar sources, the crossover probability is introduced to update the nectar sources, and the number of times the nectar source has not been updated is determined by comparing the fitness function values ​​corresponding to different nectar sources.

[0048] In the observation bee stage, a preset search equation is used to perform a local search to determine a new first nectar source. The fitness function value corresponding to the nectar source searched by the employed bees is compared with the fitness function value corresponding to the new first nectar source, and the number of times the nectar source has not been updated is adjusted to obtain the number of times the target nectar source has not been updated.

[0049] In the scout bee stage, the number of times each target nectar source has not been updated is queried; if the number of times the target nectar source has not been updated is greater than a preset maximum threshold, a new second nectar source is regenerated, and a candidate nectar source is determined by introducing a mutation operation, and the target nectar source is screened out from the new second nectar source and the candidate nectar sources by comparing the fitness function value corresponding to the new second nectar source with the fitness function corresponding to the candidate nectar source;

[0050] If the current number of iterations reaches the maximum number of iterations, the target nectar source will be taken as the optimal individual.

[0051] In this embodiment, the genetic algorithm operates in each search phase. This part iteratively improves the population fitness by improving the collaborative optimization of the artificial bee colony and genetic algorithm. The genetic algorithm crossover and mutation concepts are introduced into the artificial bee colony algorithm to improve the algorithm's global search capability. Different improved search equations are used in the employed bee and observer bee stages to increase the algorithm's convergence speed. The specific operation methods of the three search phases of the improved artificial bee colony-genetic algorithm are given in detail. Compared with the genetic algorithm, the artificial bee colony has stronger local search capabilities and lower algorithm complexity. However, the artificial bee colony algorithm is not as good as the genetic algorithm in terms of searching for the global optimal value because the genetic algorithm also has crossover and mutation operations. Therefore, based on the introduction of genetic operator operation strategies into the artificial bee colony algorithm, the search capability of the artificial bee colony algorithm in the global space is optimized. At the same time, the search equation is improved to increase the algorithm's convergence speed.

[0052] Further, Figure 2 The test case automatic generation system model based on the improved artificial bee colony-genetic algorithm provided in Example 1 of this application is as follows: Figure 2 As shown in Figure 2, the optimal individual determination process includes:

[0053] Step 1: Design the encoding and decoding method and fitness function.

[0054] Step 2: Set the initial parameters of the algorithm: The initial parameters include the number of nectar sources, crossover probability, mutation probability, maximum number of bee collections, and maximum number of iterations.

[0055] The crossover probability in a genetic algorithm refers to the probability of two parent individuals undergoing a crossover operation (simulating genetic recombination in biological reproduction). The mutation probability refers to the probability of a random change in a gene position in an individual (simulating genetic mutation). The crossover and mutation probabilities are dynamically adjusted based on the current and maximum iteration counts. In other words, the current iteration count controls the rate of parameter change.

[0056] The crossover probability P is calculated using the following formula: c :

[0057]

[0058] The mutation probability P is calculated using the following formula: m :

[0059]

[0060] Among them, generation is the current number of iterations, and maxGeneration is the maximum number of iterations.

[0061] Step 3: Initialize the honey source.

[0062] Among them, the initialization honey source is intended to provide the algorithm with an initial search starting point.

[0063] Step 4: In the stage of employed bees searching for nectar sources, an improved crossover strategy is introduced to update the nectar sources and calculate the fitness value fit(x i ), and further search for nectar sources to calculate the fitness of new candidate nectar sources Compare and fit(x i ), the greedy strategy retains the better solution. If the new solution is retained, the number of times the nectar source has not been updated must be reset to 0. If the old solution is better, the number of times the nectar source has not been updated must be increased by 1.

[0064] Specifically, the crossover operation is introduced into the hired bee stage to enhance the algorithm's global search capabilities. The crossover operation swaps certain gene segments between two parent chromosomes to produce the next individual. The resulting offspring chromosome inherits the excellent genes of the parent. The crossover operation is responsible for searching for more solutions in the entire solution space, ensuring the swarm's ability to update and iterate, and expanding the search range of the solution space. Therefore, the crossover operation needs to generate as many different chromosomes as possible to more easily obtain the optimal chromosome. Position-based crossover and POX (precedented operation crossover) crossover can effectively inherit the excellent characteristics of the parent.

[0065] Step 5: In the observation bee stage, first use the roulette wheel method to select the nectar source x that the follow bee searches for. i , and calculate x i The fitness value fit(x i ), and then use the search equation to perform local search to determine the new first nectar source Use the greedy strategy to retain the better solution. If the new solution is retained, the number of times the nectar source has not been updated must be reset to 0. If the old solution is better, the number of times the nectar source has not been updated must be increased by 1.

[0066] Among them, the employed bee stage uses the following improved search equation, which takes into account the optimal value x of the group compared with the basic artificial bee colony. gbest The influence of can interact with the current optimal nectar source, which is beneficial to balancing the global and local search capabilities of the algorithm.

[0067]

[0068] During the observation bee stage, the search equation shown in the following formula is used. The characteristic of this equation is that it generates new candidate solutions around the optimal location of the current nectar source, which can effectively accelerate the convergence of the algorithm.

[0069]

[0070] in, is a random number between [-1,1], xk,j is the random nectar source location in the population, is a new candidate solution, x i,j For the old solution, x gbest,j is the optimal value for the group.

[0071] Step 6: Scouting bee stage. First, traverse the nectar sources found by the observation bees in turn and query the number of times each nectar source has not been updated. If the number of times it has not been updated is greater than the set maximum threshold, the nectar source is abandoned and a new nectar source x is generated. i , and calculate x i The fitness value fit(x i ), and introduce three mutation operations to generate three different solutions, and compare x i with x j 、x k and x l The fitness of the algorithm is determined, and a better nectar source is selected to enter the next iteration process, thereby enhancing the algorithm's ability to escape from the local optimum.

[0072] Specifically, in the basic artificial bee colony algorithm, a solution is generated in the scout bee stage with the expectation of jumping out of the local optimal solution. In order to increase the probability of the algorithm jumping out of the local optimal solution, the mutation operation is introduced in the scout bee stage to improve the algorithm's ability to jump out of the local optimal solution.

[0073] Furthermore, there are three most commonly used mutation operations:

[0074] (1) Swap operation: Randomly swap genes at two different positions a1 and a2 in the chromosome. The offspring and parent generations in the swap mutation differ only in two points, and the difference between the offspring and the parent is not large. The quality of the searched solution does not change significantly, and the ability to search for an optimal solution is poor. Therefore, the swap mutation operation is rarely used when solving large-scale problems.

[0075] (2) Reverse operation: First, two different random positions a1 and a2 in the chromosome are selected, and then the gene string between a1 and a2 is reversed.

[0076] (3) The specific steps of the insertion operation are: first, randomly select a point a2, and then insert the gene of a2 into a different random position a1 in the gene string, while the other positions maintain the original order. This operation causes the gene string between the selected a1 and a2 to change.

[0077] Compared to the swap operation, the reversal and insertion operations produce much larger sequence differences between the parent and child chromosomes. However, both methods have a significant drawback: in these two mutation operations, the probability of gene changes at both ends of the chromosome is small, resulting in blind spots in the algorithm search. To increase the probability of changes at both ends of the chromosome sequence, an improved mutation method is proposed. First, the gene strings are connected end to end, and then a random gene segment is selected for the reversal operation. The specific steps are as follows:

[0078] Step 1: Copy the chromosome twice and store them, randomly generate a random number a1 between 1-n*m, intercept the gene string a2=a1+n*m-1 between a1-a2, and form a new chromosome P.

[0079] Step 2: Randomly generate two numbers r1 and r2, and reverse the order of the gene string between r1 and r2, leaving other positions unchanged, to generate new offspring.

[0080] After the improved mutation operation, the chromosome sequences of the parent and daughter generations have undergone great changes, and the structure can effectively solve the blind spots at both ends of the reverse operation and insertion operation, which greatly improves the range of the expanded solution and makes the algorithm search more powerful.

[0081] Step 7: Stop running if the maximum number of iterations (maxGeneration) is reached. Otherwise, jump to step 4.

[0082] Specifically, it is determined whether the current number of iterations reaches the maximum number of iterations. If so, the target nectar source is taken as the optimal individual.

[0083] S150: Use the optimal individual as a target test case for the program to be tested.

[0084] In this scheme, the program to be tested is represented as y=f(x), where x is the input parameter (constituting the input space V) and y is the output result. The program structure is represented by a control flow graph G=(V,E,s,e), where V represents the statement node, E represents the potential control flow between statements, s is the start statement of the program, and e is the corresponding end statement. If there is an ordered sequence <V0,...,V i ,...,V n >, where V i ∈V, and V i+1 Can be in V i Node is executed immediately after the sequence, then the sequence constitutes a path of the program G. In the test case generation process, first randomly obtain a path T in G g As a reference path, the improved artificial bee colony-genetic algorithm is used to find the optimal individual X, so that G can obtain the execution path T when it takes X as input. m When T m With Tg When they coincide, X is the generated target test case.

[0085] Improve the division of labor between artificial bee colony and genetic algorithm (employed bees - selection, observer bees - crossover, scout bees - mutation), combine the operations of each stage of genetic algorithm with the operations of each stage of artificial bee colony algorithm, and improve the efficiency of the algorithm.

[0086] Further, Figure 3 The test case automatic generation algorithm framework provided in Example 1 of this application. Figure 3 As shown, first, a structured analysis is performed on the program under test, the test objectives and test data requirements are output, and the program is instrumented. Test data is generated, selected, and corrected according to the algorithm used, that is, the input data is corrected. The test information fed back by the test results is used to determine whether the coverage target is achieved. If so, the available test data is output. If not, jump to the algorithm used to generate, select, and correct the test data.

[0087] The technical solution of the embodiment of the present invention is to obtain a program to be tested; analyze the program to be tested to generate an initial population; determine a fitness function; iteratively optimize the initial population based on the fitness function to obtain the optimal individual; and use the optimal individual as the target test case for the program to be tested. By executing this technical solution, by combining the improved artificial bee colony algorithm and the genetic algorithm, the fast search capability of the artificial bee colony algorithm and the global optimization characteristics of the genetic algorithm are utilized. In the test case generation process, a dynamic parameter adjustment mechanism, a hierarchical fitness function and a hybrid crossover mutation strategy, and an improved artificial bee colony algorithm search equation are designed. Through the four stages of initializing the population, evaluating the quality of the nectar source, crossover mutation optimization, and retaining high-quality nectar sources, the automatic optimization generation of the test case set is realized to improve the efficiency and coverage of the test case generation, while reducing the number of redundant cases.

[0088] Example 2

[0089] Figure 4 This is a schematic diagram of a test case automatic generation method provided by the second embodiment of the present invention. The relationship between this embodiment and the above embodiment is a detailed description of the initial population generation process. Figure 4 As shown, the method includes:

[0090] S410: Obtain the program to be tested.

[0091] S420: Analyze the program to be tested, randomly generate multiple initial input data, and use each initial input data as an individual in a candidate population to form the candidate population.

[0092] In this scheme, the program to be tested is analyzed based on the artificial bee colony algorithm and the genetic algorithm, multiple initial input data are randomly generated, and each initial input data is used as an individual in the candidate population to form a candidate population.

[0093] Among them, the genetic algorithm is a random global search optimization method that simulates phenomena such as replication, crossover, and mutation that occur in natural selection and genetics. Starting from any initial population, through random selection, crossover, and mutation operations, the population evolves to better and better areas in the search space. After generations of evolution, it finally converges to the optimal solution to the problem.

[0094] In this embodiment, the Artificial Bee Colony Algorithm (ABC) simulates the intelligent nectar-collecting behavior of a bee colony. It primarily consists of employed bees, observer bees, and scout bees. The employed bees are responsible for searching for nectar sources and feeding this information back to the observer bees. The observer bees prioritize high-quality nectar sources and follow them, searching near them. When the upper limit of nectar sources is reached and the quality of new sources does not improve, the corresponding employed bees transform into scout bees and randomly search for new nectar sources. ABC compares the search for a global optimal solution to a bee colony's search for the highest-yielding nectar source.

[0095] S430. Analyze the program to be tested, extract execution statements, branch conditions, and jump relationships in the program to be tested; map the execution statements into nodes of a program control flow graph, map the branch conditions and jump relationships into directed edges of the program control flow graph, and generate a program control flow graph of the program to be tested.

[0096] Specifically, the program to be tested is analyzed to extract the execution statements, branch conditions and jump relationships therein; then, the extracted execution statements are mapped to nodes of the program control flow graph, and the branch conditions and jump relationships are mapped to directed edges of the program control flow graph, thereby generating a program control flow graph corresponding to the program to be tested.

[0097] S440. Traverse the program control flow graph to determine the boundary value of the initial input data, and generate a boundary value use case based on the boundary value of the initial input data.

[0098] Specifically, by traversing the program control flow graph to identify branch conditions and constraints related to the input data, the boundary values ​​(such as maximum and minimum values) of the initial input data are extracted, and then boundary value use cases covering various boundary scenarios are generated based on these boundary values.

[0099] S450: Fusing the boundary value use case and the candidate population to generate an initial population.

[0100] In this scheme, adding boundary value use cases to the candidate population will effectively improve the coverage of critical paths (loops, abnormal branches). Randomly generated candidate use cases and boundary value use cases can be mixed in a ratio of 4:6 to form a candidate population.

[0101] S460, determining a fitness function; wherein the fitness function is used to evaluate the test effectiveness of individuals in the initial population for the degree to be tested.

[0102] S470. Based on the fitness function, iteratively optimize the initial population to obtain the optimal individual.

[0103] S480: Use the optimal individual as a target test case for the program to be tested.

[0104] The technical solution of the embodiment of the present invention is to obtain a program to be tested; analyze the program to be tested to generate candidate populations and boundary value use cases, and fuse the candidate populations and boundary value use cases to generate an initial population; determine a fitness function; iteratively optimize the initial population based on the fitness function to obtain the optimal individual; and use the optimal individual as the target test case for the program to be tested. By executing this technical solution, the boundary value use cases generated based on the program control flow graph are fused with the candidate use cases to generate the initial population, which can effectively improve the critical path coverage rate in the population and effectively reduce the proportion of redundant use cases. Under the same test objectives, the size of the use case set is reduced and the test execution time is shortened.

[0105] Example 3

[0106] Figure 5 This is a flowchart of a test case automatic generation process provided by the third embodiment of the present invention. The relationship between this embodiment and the above embodiment is a detailed description of the fitness function generation process. Figure 5 As shown, the method includes:

[0107] S510: Obtain the program to be tested.

[0108] S520: Analyze the program to be tested to generate an initial population; wherein the initial population consists of a plurality of individuals.

[0109] S530. Determine a path similarity function, a branch path function, and a time evaluation function; wherein the path similarity function is used to evaluate the similarity between the tested path and the reference path; the branch path function is used to evaluate the degree of deviation between the actual execution path of the tested unit in the tested path and the specified logical path; and the time evaluation function is used to evaluate the execution time of the use case.

[0110] In this scheme, the fitness function is used to evaluate the quality of individuals, that is, the quality of the solution to the optimization problem. Choosing an appropriate fitness function helps to solve the problem. This scheme uses a combination of path similarity function, branch path function and time evaluation function as the fitness function.

[0111] Optionally, determine a path similarity function, including:

[0112] Obtain data on the reference path and the tested path where the same element nodes appear continuously;

[0113] Sequentially extracting the element node sequence of the reference path and the element node sequence of the tested path; comparing the element nodes of corresponding positions in the two element node sequences to determine consecutive identical element node segments; recording the element node identifiers of the consecutive identical element node segments and the starting and ending positions in their respective paths as data on consecutive identical element node occurrences;

[0114] Counting a first total occurrence number of the target element node in the reference path and a second total occurrence number of the target element node in the measured path; calculating a difference or a ratio between the first total occurrence number and the second total occurrence number as a data volume result for the same element node;

[0115] Extracting first test data of an element node at a corresponding position in a reference path and second test data of an element node at a corresponding position in a tested path; comparing the first test data and the second test data to obtain a comparison result;

[0116] Determine the weight of the element node;

[0117] The data of consecutive occurrences of the same element nodes, the data volume results, the comparison results and the weights are used to construct a path similarity function.

[0118] Specifically, the following formula is used to describe the path similarity function;

[0119]

[0120] Among them, f similarity is the path similarity function, T g is the reference path, T m is the path to be measured, c(T m ,T g ) is the data of the same element node appearing continuously in two paths, s i is the data volume result of the i-th element node, d i is the comparison result of the i-th element node, and ω is the weight.

[0121] In this embodiment, the element node sequence of the reference path and the element node sequence of the measured path are extracted in sequence; the element nodes with corresponding positions in the two element node sequences are compared to determine continuous identical element node segments; the element node identifiers of the continuous identical element node segments and the starting and ending positions in their respective paths are recorded as data of continuous occurrence of identical element nodes.

[0122] Furthermore, the first total occurrence number of the target element node in the reference path is counted and the second total occurrence number of the target element node in the measured path is counted; the difference or ratio between the first total occurrence number and the second total occurrence number is calculated as the data volume result of the same element node.

[0123] In this solution, when the first test data of the element node at the corresponding position in the reference path and the second test data of the element node at the corresponding position in the tested path are the same, d i The value is 1; when the first test data of the element node at the corresponding position in the reference path and the second test data of the element node at the corresponding position in the tested path are different, d i The value is 0.

[0124] Specifically, for a control flow graph, the next edge can only be entered after passing the previous edge. Therefore, it is necessary to consider the different importance of different element nodes and use the weight coefficient to control the node weight. ω is determined by the following formula:

[0125]

[0126] The hierarchical fitness function design reduces the proportion of time-consuming operation cases and improves testing efficiency.

[0127] Optionally, determine the branch path function, including:

[0128] Constructing a branch function based on input data of the unit under test in the path under test; wherein, when the input data is the same as the input data of the specified logic path, the branch function is 0; when the input data is different from the input data of the specified logic path, the branch function is greater than 0;

[0129] A branch path function is constructed based on the branch function.

[0130] In this solution, the following formula is used to describe the branch path function:

[0131]

[0132] Among them, f branchdis is the branch path function, and f(i) is the branch function.

[0133] Specifically, the branch predicate is converted into a branch function, and a real-valued function f(x1, x2, Λ, x n ), (where x1, x2, Λ, x n is the formal parameter variable of the unit under test), f i It is used to evaluate the deviation between the actual execution path of the unit under test and the specified logic path. i , its value depends on the branch selected.

[0134] 1)f i When =0, it indicates that the data meets the value condition of this branch path, and the branch judgment is true.

[0135] 2)f i When >0, the branch is judged as false.

[0136] The coverage of the test data on the branches of the tested path is calculated using the branch superposition method. Assume that there are m branch paths in a tested path, and f(1), f(2), ... f(m) are the expressions for inserting each node into the branch. Assume that F = f(1) + f(2) + Λ + f(m). If this value is 0, it indicates that the input data completely covers the tested path and is considered the optimal test case. The function is evaluated using the branch superposition method: the higher the path coverage, the larger the function value, indicating that the data has a higher fitness, and the branch path function is designed.

[0137] The hierarchical fitness function design reduces the proportion of time-consuming operation cases and improves testing efficiency.

[0138] Optionally, determine the time evaluation function, including:

[0139] Get the use case execution time of the tested path;

[0140] The inverse of the use case execution time is used as the time evaluation function.

[0141] In this embodiment, the following formula is used to describe the time evaluation function:

[0142]

[0143] Among them, f time is the time evaluation function, where time is the execution time of the test case of the path being tested. The shorter the execution time, the higher the fitness.

[0144] The hierarchical fitness function design reduces the proportion of time-consuming operation cases and improves testing efficiency.

[0145] S540: Combine the path similarity function, the branch path function, and the time evaluation function to obtain a fitness function.

[0146] In this scheme, the path similarity function, the branch path function and the time evaluation function are added together to obtain the fitness function.

[0147] Specifically, combining layer similarity and branch distance, the fitness function of the individual for each target path is designed as:

[0148] fit=f branchdis +f similarity +f time ;

[0149] Among them, fit is the fitness function. The larger the function value, the better the test data.

[0150] Preferably, the path similarity function, the branch path function, and the time evaluation function may be weighted and combined to obtain the fitness function. For example, the weight of the path similarity function may be set to 60%, the weight of the branch path function may be set to 20%, and the weight of the time evaluation function may be set to 20%.

[0151] S550: Based on the fitness function, iteratively optimize the initial population to obtain the optimal individual.

[0152] S560: Use the optimal individual as a target test case for the program to be tested.

[0153] The technical solution of the embodiment of the present invention is to obtain a program to be tested; analyze the program to be tested to generate an initial population; combine the path similarity function, the branch path function and the time evaluation function to obtain a fitness function; based on the fitness function, iteratively optimize the initial population to obtain the optimal individual; and use the optimal individual as the target test case for the program to be tested. By executing this technical solution, the boundary value use case generated based on the program control flow graph is fused with the candidate use case to generate the initial population, which can effectively improve the critical path coverage in the population and effectively reduce the proportion of redundant use cases. Under the same test objectives, the size of the use case set is reduced and the test execution time is shortened. The hierarchical fitness function design reduces the proportion of time-consuming operation use cases and improves test efficiency. The operations of each stage of the genetic algorithm are combined with the operations of each stage of the artificial bee colony algorithm to improve the efficiency of the algorithm.

[0154] Example 4

[0155] Figure 6 This is a schematic diagram of the structure of a test case automatic generation device provided by the fourth embodiment of the present invention. Figure 6 As shown, the device includes:

[0156] A program-to-be-tested acquisition module 610 is configured to acquire the program to be tested;

[0157] The initial population generation module 620 is used to analyze the program to be tested and generate an initial population; wherein the initial population is composed of a plurality of individuals;

[0158] The fitness function determination module 630 is used to determine the fitness function; wherein the fitness function is used to evaluate the test effectiveness of the individuals in the initial population for the degree to be tested;

[0159] An optimal individual obtaining module 640 is configured to iteratively optimize the initial population based on the fitness function to obtain the optimal individual;

[0160] The target test case determination module 650 is configured to use the optimal individual as a target test case for the program to be tested.

[0161] Optionally, the initial population generation module 620 is specifically configured to:

[0162] Analyzing the program to be tested, randomly generating a plurality of initial input data, and taking each initial input data as an individual in a candidate population to form the candidate population;

[0163] Analyzing the program to be tested, extracting execution statements, branch conditions, and jump relationships in the program to be tested; mapping the execution statements into nodes of a program control flow graph, and mapping the branch conditions and jump relationships into directed edges of the program control flow graph, thereby generating a program control flow graph of the program to be tested;

[0164] Traversing the program control flow graph to determine the boundary value of the initial input data, and generating a boundary value use case based on the boundary value of the initial input data;

[0165] The boundary value use case and the candidate population are merged to generate an initial population.

[0166] Optionally, the fitness function determination module 630 includes:

[0167] A function determination submodule is used to determine a path similarity function, a branch path function, and a time evaluation function; wherein the path similarity function is used to evaluate the similarity between the tested path and the reference path; the branch path function is used to evaluate the degree of deviation between the actual execution path of the unit under test in the tested path and the specified logical path; and the time evaluation function is used to evaluate the execution time of the use case;

[0168] The fitness function obtaining submodule is used to combine the path similarity function, the branch path function and the time evaluation function to obtain the fitness function.

[0169] Optionally, the function determines the submodule, specifically used to:

[0170] Obtain data on the reference path and the tested path where the same element nodes appear continuously;

[0171] Sequentially extracting the element node sequence of the reference path and the element node sequence of the tested path; comparing the element nodes of corresponding positions in the two element node sequences to determine consecutive identical element node segments; recording the element node identifiers of the consecutive identical element node segments and the starting and ending positions in their respective paths as data on consecutive identical element node occurrences;

[0172] Counting a first total occurrence number of the target element node in the reference path and a second total occurrence number of the target element node in the measured path; calculating a difference or a ratio between the first total occurrence number and the second total occurrence number as a data volume result for the same element node;

[0173] Extracting first test data of an element node at a corresponding position in a reference path and second test data of an element node at a corresponding position in a tested path; comparing the first test data and the second test data to obtain a comparison result;

[0174] Determine the weight of the element node;

[0175] The data of consecutive occurrences of the same element nodes, the data volume results, the comparison results and the weights are used to construct a path similarity function.

[0176] Optionally, the function determines the submodule, specifically used to:

[0177] Constructing a branch function based on input data of the unit under test in the path under test; wherein, when the input data is the same as the input data of the specified logic path, the branch function is 0; when the input data is different from the input data of the specified logic path, the branch function is greater than 0;

[0178] A branch path function is constructed based on the branch function.

[0179] Optionally, the function determines the submodule, specifically used to:

[0180] Get the use case execution time of the tested path;

[0181] The inverse of the use case execution time is used as the time evaluation function.

[0182] Optionally, the optimal individual obtaining module 640 is specifically configured to:

[0183] Determine initial parameters; wherein the initial parameters include the number of nectar sources, crossover probability, mutation probability, maximum number of bee collections, and maximum number of iterations; the crossover probability and the mutation probability are dynamically adjusted based on the current number of iterations and the maximum number of iterations;

[0184] Initialize honey source;

[0185] In the stage of employed bees searching for nectar sources, the crossover probability is introduced to update the nectar sources, and the number of times the nectar source has not been updated is determined by comparing the fitness function values ​​corresponding to different nectar sources.

[0186] In the observation bee stage, a preset search equation is used to perform a local search to determine a new first nectar source. The fitness function value corresponding to the nectar source searched by the employed bees is compared with the fitness function value corresponding to the new first nectar source, and the number of times the nectar source has not been updated is adjusted to obtain the number of times the target nectar source has not been updated.

[0187] In the scout bee stage, the number of times each target nectar source has not been updated is queried; if the number of times the target nectar source has not been updated is greater than a preset maximum threshold, a new second nectar source is regenerated, and a candidate nectar source is determined by introducing a mutation operation, and the target nectar source is screened out from the new second nectar source and the candidate nectar sources by comparing the fitness function value corresponding to the new second nectar source with the fitness function corresponding to the candidate nectar source;

[0188] If the current number of iterations reaches the maximum number of iterations, the target nectar source will be taken as the optimal individual.

[0189] An automatic test case generation device provided by an embodiment of the present invention can execute an automatic test case generation method provided by any embodiment of the present invention, and has functional modules and beneficial effects corresponding to the execution method.

[0190] Example 5

[0191] Figure 7 A schematic diagram of the structure of an electronic device 10 that can be used to implement an embodiment of the present invention is shown. The electronic device is intended to represent various forms of digital computers, such as laptop computers, desktop computers, workstations, personal digital assistants, servers, blade servers, mainframe computers, and other suitable computers. The electronic device can also represent various forms of mobile devices, such as personal digital processing, cellular phones, smart phones, wearable devices (such as helmets, glasses, watches, etc.) and other similar computing devices. The components shown herein, their connections and relationships, and their functions are merely examples and are not intended to limit the implementation of the present invention described and / or claimed herein.

[0192] like Figure 7As shown, the electronic device 10 includes at least one processor 11 and a memory, such as a read-only memory (ROM) 12, a random access memory (RAM) 13, etc., which is communicatively connected to the at least one processor 11. The memory stores a computer program that can be executed by the at least one processor. The processor 11 can perform various appropriate actions and processes according to the computer program stored in the read-only memory (ROM) 12 or the computer program loaded from the storage unit 18 into the random access memory (RAM) 13. Various programs and data required for the operation of the electronic device 10 can also be stored in the RAM 13. The processor 11, ROM 12, and RAM 13 are connected to each other via a bus 14. An input / output (I / O) interface 15 is also connected to the bus 14.

[0193] Multiple components in the electronic device 10 are connected to the I / O interface 15, including an input unit 16, such as a keyboard, a mouse, etc.; an output unit 17, such as various types of displays, speakers, etc.; a storage unit 18, such as a magnetic disk, an optical disk, etc.; and a communication unit 19, such as a network card, a modem, a wireless communication transceiver, etc. The communication unit 19 allows the electronic device 10 to exchange information / data with other devices via a computer network such as the Internet and / or various telecommunication networks.

[0194] The processor 11 can be any general-purpose and / or specialized processing component with processing and computing capabilities. Some examples of the processor 11 include, but are not limited to, a central processing unit (CPU), a graphics processing unit (GPU), various specialized artificial intelligence (AI) computing chips, various processors that run machine learning model algorithms, a digital signal processor (DSP), and any appropriate processor, controller, microcontroller, etc. The processor 11 executes the various methods and processes described above, such as a method for automatically generating test cases.

[0195] In some embodiments, a method for automatically generating test cases can be implemented as a computer program, which is tangibly contained in a computer-readable storage medium, such as a storage unit 18. In some embodiments, part or all of the computer program can be loaded and / or installed on the electronic device 10 via the ROM 12 and / or the communication unit 19. When the computer program is loaded into the RAM 13 and executed by the processor 11, one or more steps of the method for automatically generating test cases described above can be performed. Alternatively, in other embodiments, the processor 11 can be configured to perform a method for automatically generating test cases by any other appropriate means (e.g., by means of firmware).

[0196] Various embodiments of the systems and techniques described above can be implemented in digital electronic circuit systems, integrated circuit systems, field programmable gate arrays (FPGAs), application specific integrated circuits (ASICs), application specific standard products (ASSPs), systems on a chip (SOCs), complex programmable logic devices (CPLDs), computer hardware, firmware, software, and / or combinations thereof. These various embodiments can include being implemented in one or more computer programs that are executable and / or interpreted on a programmable system that includes at least one programmable processor, which can be a special purpose or general purpose programmable processor that can receive data and instructions from a storage system, at least one input device, and at least one output device, and transmit data and instructions to the storage system, the at least one input device, and the at least one output device.

[0197] Computer programs for implementing the methods of the present invention may be written in any combination of one or more programming languages. These computer programs may be provided to a processor of a general-purpose computer, a special-purpose computer, or other programmable data processing device, such that when the computer program is executed by the processor, the functions / operations specified in the flowcharts and / or block diagrams are implemented. The computer program may be executed entirely on the machine, partially on the machine, as a stand-alone software package, partially on the machine and partially on a remote machine, or entirely on a remote machine or server.

[0198] In the context of the present invention, computer-readable storage media can be tangible media that can contain or store a computer program for use with an instruction execution system, device or equipment or used in combination with an instruction execution system, device or equipment. Computer-readable storage media can include but are not limited to electronic, magnetic, optical, electromagnetic, infrared or semiconductor systems, devices or equipment, or any suitable combination of the foregoing. Alternatively, computer-readable storage media can be machine-readable signal media. More specific examples of machine-readable storage media can include electrical connections based on one or more lines, portable computer disks, hard disks, random access memories (RAM), read-only memories (ROM), erasable programmable read-only memories (EPROM or flash memory), optical fibers, portable compact disk read-only memories (CD-ROM), optical storage devices, magnetic storage devices, or any suitable combination of the foregoing.

[0199] To provide interaction with a user, the systems and techniques described herein can be implemented on an electronic device having: a display device (e.g., a CRT (cathode ray tube) or LCD (liquid crystal display) monitor) for displaying information to the user; and a keyboard and pointing device (e.g., a mouse or trackball) through which the user can provide input to the electronic device. Other types of devices can also be used to provide interaction with the user; for example, the feedback provided to the user can be any form of sensory feedback (e.g., visual feedback, auditory feedback, or tactile feedback); and input from the user can be received in any form (including acoustic input, voice input, or tactile input).

[0200] The systems and techniques described herein can be implemented in a computing system that includes back-end components (e.g., as a data server), or a computing system that includes middleware components (e.g., an application server), or a computing system that includes front-end components (e.g., a user computer with a graphical user interface or web browser through which a user can interact with implementations of the systems and techniques described herein), or a computing system that includes any combination of such back-end components, middleware components, or front-end components. The components of the system can be interconnected by any form or medium of digital data communication (e.g., a communication network). Examples of communication networks include: a local area network (LAN), a wide area network (WAN), a blockchain network, and the Internet.

[0201] A computing system may include clients and servers. The clients and servers are typically remote from each other and typically interact via a communication network. This client-server relationship arises through computer programs running on the respective computers, creating a client-server relationship. The server may be a cloud server, also known as a cloud computing server or cloud host. This server is a hosting product within the cloud computing service ecosystem that addresses the management difficulties and limited scalability of traditional physical hosting and VPS services.

[0202] It should be understood that the various forms of the processes shown above can be used to reorder, add, or delete steps. For example, the steps described in the present invention can be performed in parallel, sequentially, or in a different order, as long as the desired results of the technical solution of the present invention can be achieved. This is not limited herein.

[0203] The above specific embodiments do not limit the scope of protection of the present invention. Those skilled in the art will appreciate that various modifications, combinations, sub-combinations, and substitutions may be made based on design requirements and other factors. Any modifications, equivalent substitutions, and improvements made within the spirit and principles of the present invention are intended to be included within the scope of protection of the present invention.

Claims

1. A method for automatically generating test cases, characterized in that: include: Get the program to be tested; Analyzing the program to be tested to generate an initial population; wherein the initial population is composed of a plurality of individuals; Determining a fitness function; wherein the fitness function is used to evaluate the test effectiveness of individuals in the initial population for the degree to be tested; Based on the fitness function, iteratively optimizing the initial population to obtain the optimal individual; The optimal individual is used as a target test case for the program to be tested.

2. The method according to claim 1, characterized in that Analyzing the program to be tested to generate an initial population includes: Analyzing the program to be tested, randomly generating a plurality of initial input data, and taking each initial input data as an individual in a candidate population to form the candidate population; Analyzing the program to be tested, extracting execution statements, branch conditions, and jump relationships in the program to be tested; mapping the execution statements into nodes of a program control flow graph, and mapping the branch conditions and jump relationships into directed edges of the program control flow graph, thereby generating a program control flow graph of the program to be tested; Traversing the program control flow graph to determine the boundary value of the initial input data, and generating a boundary value use case based on the boundary value of the initial input data; The boundary value use case and the candidate population are merged to generate an initial population.

3. The method according to claim 1, characterized in that Determine the fitness function, including: Determine a path similarity function, a branch path function, and a time evaluation function; wherein the path similarity function is used to evaluate the similarity between the tested path and the reference path; the branch path function is used to evaluate the degree of deviation between the actual execution path of the unit under test in the tested path and the specified logical path; and the time evaluation function is used to evaluate the execution time of the use case; The path similarity function, the branch path function and the time evaluation function are combined to obtain a fitness function.

4. The method according to claim 3, characterized in that Determine the path similarity function, including: Obtain data on the reference path and the tested path where the same element nodes appear continuously; Sequentially extracting the element node sequence of the reference path and the element node sequence of the tested path; comparing the element nodes of corresponding positions in the two element node sequences to determine consecutive identical element node segments; recording the element node identifiers of the consecutive identical element node segments and the starting and ending positions in their respective paths as data on consecutive identical element node occurrences; Counting a first total occurrence number of the target element node in the reference path and a second total occurrence number of the target element node in the measured path; calculating a difference or a ratio between the first total occurrence number and the second total occurrence number as a data volume result for the same element node; Extracting first test data of an element node at a corresponding position in a reference path and second test data of an element node at a corresponding position in a tested path; comparing the first test data and the second test data to obtain a comparison result; Determine the weight of the element node; The data of consecutive occurrences of the same element nodes, the data volume results, the comparison results and the weights are used to construct a path similarity function.

5. The method according to claim 3, characterized in that Determine the branch path function, including: Constructing a branch function based on input data of the unit under test in the path under test; wherein, when the input data is the same as the input data of the specified logic path, the branch function is 0; when the input data is different from the input data of the specified logic path, the branch function is greater than 0; A branch path function is constructed based on the branch function.

6. The method according to claim 3, characterized in that Determine the time evaluation function, including: Get the use case execution time of the tested path; The inverse of the use case execution time is used as the time evaluation function.

7. The method according to claim 1, characterized in that Based on the fitness function, iteratively optimizing the initial population to obtain the optimal individual includes: Determine initial parameters; wherein the initial parameters include the number of nectar sources, crossover probability, mutation probability, maximum number of bee collections, and maximum number of iterations; the crossover probability and the mutation probability are dynamically adjusted based on the current number of iterations and the maximum number of iterations; Initialize honey source; In the stage of employed bees searching for nectar sources, the crossover probability is introduced to update the nectar sources, and the number of times the nectar source has not been updated is determined by comparing the fitness function values ​​corresponding to different nectar sources. In the observation bee stage, a preset search equation is used to perform a local search to determine a new first nectar source. The fitness function value corresponding to the nectar source searched by the employed bees is compared with the fitness function value corresponding to the new first nectar source, and the number of times the nectar source has not been updated is adjusted to obtain the number of times the target nectar source has not been updated. In the scout bee stage, the number of times each target nectar source has not been updated is queried; if the number of times the target nectar source has not been updated is greater than a preset maximum threshold, a new second nectar source is regenerated, and a candidate nectar source is determined by introducing a mutation operation, and the target nectar source is screened out from the new second nectar source and the candidate nectar sources by comparing the fitness function value corresponding to the new second nectar source with the fitness function corresponding to the candidate nectar source; If the current number of iterations reaches the maximum number of iterations, the target nectar source will be taken as the optimal individual.

8. A test case automatic generation device, characterized in that: include: A program-to-be-tested acquisition module, used for acquiring the program-to-be-tested; An initial population generation module, configured to analyze the program to be tested and generate an initial population; wherein the initial population is composed of a plurality of individuals; A fitness function determination module is used to determine a fitness function; wherein the fitness function is used to evaluate the test effectiveness of individuals in the initial population for the degree to be tested; An optimal individual obtaining module is used to iteratively optimize the initial population based on the fitness function to obtain the optimal individual; The target test case determination module is used to use the optimal individual as the target test case for the program to be tested.

9. An electronic device, characterized in that: The electronic device comprises: At least one processor; and a memory communicatively connected to the at least one processor; wherein the memory stores a computer program executable by the at least one processor, and the computer program is executed by the at least one processor so that the at least one processor can execute a test case automatic generation method according to any one of claims 1 to 7.

10. A computer-readable storage medium, characterized in that The computer-readable storage medium stores computer instructions, and the computer instructions are used to enable a processor to implement a test case automatic generation method according to any one of claims 1 to 7 when executed.

Citation Information

Cited By

  • Test case generation method and related equipment

    CN120929388A

  • Test case generation method and related device

    CN120929388B