Test case generation method and device, computer equipment and storage medium

By generating initial test cases and using static and dynamic fitness function optimization, the problem of low matching in traditional test case generation methods is solved, and a higher matching degree and test effect between test cases and software to be tested is achieved.

CN119473914APending Publication Date: 2025-02-18CHINA TELECOM CLOUD TECH CO LTD
View PDF 0 Cites 2 Cited by

Patent Information

Application Number
CN202411737803.5
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-11-29
Publication Date
2025-02-18

AI Technical Summary

Technical Problem

In traditional test case generation methods, the matching degree of test cases and the software to be tested is low, resulting in poor test results.

Method used

The target test cases are generated by generating initial test cases based on the program structure of the program to be tested and optimizing the initial test cases using static and dynamic fitness functions. The method includes determining the initial population, selecting operations, iterative updates, and fitness function adjustments to improve the matching of the test cases.

Benefits of technology

It improves the matching degree between test cases and software to be tested, enhances the accuracy and effectiveness of test cases, and thus improves the quality and efficiency of software testing.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119473914A_ABST
    Figure CN119473914A_ABST
Patent Text Reader

Abstract

The invention relates to a test case generation method and device, computer equipment and a storage medium. The method comprises the steps of generating an initial test case according to a program structure of a to-be-tested program, wherein the initial test case comprises an initial execution path; according to the initial execution path, the path set of the to-be-tested program and the fitness function of the to-be-tested program, optimizing the initial test case, and generating a target test case of the to-be-tested program; the fitness function comprises a static fitness function and a dynamic fitness function, and the dynamic fitness function changes along with the number of iterations for optimizing the initial test case. The initial test case is generated in combination with the program structure of the to-be-tested program, so that the initial test case is more matched with the to-be-tested program, the accuracy of the initial test case is improved, updating of the initial test case is more effective due to the dynamic fitness function, and the matching degree of the target test case and the to-be-tested program is further improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the field of testing technology, and in particular to a method, apparatus, computer device and storage medium for generating a test case. Background Art

[0002] In the field of software testing, the automatic generation of test cases can not only eliminate the differences in test cases designed by different people's thinking patterns, but also improve the efficiency of software testing and reduce testing costs. The higher the coverage of the test path corresponding to the test case on the software to be tested, the higher the match between the test case and the software to be tested, and the better the test effect.

[0003] In traditional technologies, ant colony algorithms, differential evolution algorithms, particle swarm optimization algorithms, genetic algorithms, etc. are usually used to generate test cases for the software to be tested.

[0004] However, the traditional test case generation method has the problem of low matching degree between the test case and the software to be tested. Summary of the invention

[0005] Based on this, it is necessary to provide a method, device, computer equipment and storage medium for generating test cases that can improve the matching degree between test cases and software to be tested in order to solve the above technical problems.

[0006] In a first aspect, the present application provides a method for generating a test case, comprising:

[0007] Generate an initial test case according to the program structure of the program to be tested, wherein the initial test case includes an initial execution path;

[0008] According to the initial execution path, the path set of the program to be tested and the fitness function of the program to be tested, the initial test case is optimized to generate a target test case of the program to be tested; the fitness function includes a static fitness function and a dynamic fitness function, and the dynamic fitness function changes with the number of iterations of optimizing the initial test case.

[0009] In one embodiment, the step of optimizing the initial test case according to the initial execution path, the path set of the program to be tested, and the fitness function of the program to be tested to generate a target test case of the program to be tested includes:

[0010] Determine an initial population according to the initial execution path; each individual in the initial population includes the path of the individual and the individual fitness value;

[0011] Performing a selection operation on the initial population according to the fitness function and the individual fitness value to obtain a target individual and a selected population; the selected population includes individuals other than the target individual;

[0012] If the preset iteration condition is not met, iteratively updating the selected population according to the preset evolutionary algorithm to obtain a new initial population, and returning to the step of selecting the initial population according to the fitness function;

[0013] When the preset iteration conditions are met, the target test cases of the program to be tested are obtained according to the target individuals obtained in each iteration.

[0014] In one embodiment, the iterative updating of the selected population according to a preset evolutionary algorithm to obtain a new initial population includes:

[0015] According to the crossover operator and the search operator in the selected population, each individual in the selected population is updated to obtain the new initial population.

[0016] In one embodiment, the step of performing a selection operation on the initial population according to the fitness function and the individual fitness value to obtain a target individual and a selected population includes:

[0017] Determining a target fitness function from the static fitness function and the dynamic fitness function according to a preset number of iterations and selection results of selection operations corresponding to each iteration;

[0018] A target individual matching any one of the paths in the path set is determined from the initial population according to the target fitness function and the individual fitness value, and other individuals except the target individual are used as the selected population.

[0019] In one embodiment, determining a target fitness function from the static fitness function and the dynamic fitness function according to a preset number of iterations and a selection result of a selection operation corresponding to each iteration includes:

[0020] If the selection result indicates that the target individual has not been determined within the preset number of iterations, determining the dynamic fitness function as the target fitness function;

[0021] If the selection result indicates that the target individual is determined within the preset number of iterations, the static fitness function is determined as the target fitness function.

[0022] In one embodiment, generating an initial test case according to the program structure of the program to be tested includes:

[0023] Marking the program structure of the program to be tested by using an instrumentation function to obtain a plurality of marking points;

[0024] An initial test case is generated according to the multiple marking points.

[0025] In a second aspect, the present application also provides a test case generation device, comprising:

[0026] A generating module, used for generating an initial test case according to the program structure of the program to be tested, wherein the initial test case includes an initial execution path;

[0027] An optimization module is used to optimize the initial test case according to the initial execution path, the path set of the program to be tested and the fitness function of the program to be tested, so as to generate a target test case for the program to be tested; the fitness function includes a static fitness function and a dynamic fitness function, and the dynamic fitness function changes with the number of iterations of optimizing the initial test case.

[0028] In a third aspect, the present application further provides a computer device, including a memory and a processor, wherein the memory stores a computer program, and when the processor executes the computer program, the following steps are implemented:

[0029] Generate an initial test case according to the program structure of the program to be tested, wherein the initial test case includes an initial execution path;

[0030] According to the initial execution path, the path set of the program to be tested and the fitness function of the program to be tested, the initial test case is optimized to generate a target test case of the program to be tested; the fitness function includes a static fitness function and a dynamic fitness function, and the dynamic fitness function changes with the number of iterations of optimizing the initial test case.

[0031] In a fourth aspect, the present application further provides a computer-readable storage medium having a computer program stored thereon, wherein when the computer program is executed by a processor, the following steps are implemented:

[0032] Generate an initial test case according to the program structure of the program to be tested, wherein the initial test case includes an initial execution path;

[0033] According to the initial execution path, the path set of the program to be tested and the fitness function of the program to be tested, the initial test case is optimized to generate a target test case of the program to be tested; the fitness function includes a static fitness function and a dynamic fitness function, and the dynamic fitness function changes with the number of iterations of optimizing the initial test case.

[0034] In a fifth aspect, the present application further provides a computer program product, including a computer program, which implements the following steps when executed by a processor:

[0035] Generate an initial test case according to the program structure of the program to be tested, wherein the initial test case includes an initial execution path;

[0036] According to the initial execution path, the path set of the program to be tested and the fitness function of the program to be tested, the initial test case is optimized to generate a target test case of the program to be tested; the fitness function includes a static fitness function and a dynamic fitness function, and the dynamic fitness function changes with the number of iterations of optimizing the initial test case.

[0037] The above-mentioned test case generation method, device, computer equipment and storage medium generate initial test cases according to the program structure of the program to be tested, and the initial test cases include an initial execution path; according to the initial execution path, the path set of the program to be tested and the fitness function of the program to be tested, the initial test cases are optimized to generate target test cases of the program to be tested; the fitness function includes a static fitness function and a dynamic fitness function, and the dynamic fitness function changes with the number of iterations of optimizing the initial test cases. The initial test cases are generated in combination with the program structure of the program to be tested, so that the initial test cases are more matched with the program to be tested, and the accuracy of the initial test cases is improved. Further, the initial test cases are updated using the fitness function, and the fitness function includes a dynamic fitness function that changes with the number of iterations of optimizing the initial test cases, so that the update of the initial test cases is more effective, thereby further improving the matching degree of the target test cases with the program to be tested. BRIEF DESCRIPTION OF THE DRAWINGS

[0038] In order to more clearly illustrate the technical solutions in the embodiments of the present application or related technologies, the drawings required for use in the embodiments of the present application or related technical descriptions will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present application. For ordinary technicians in this field, other related drawings can be obtained based on these drawings without paying creative work.

[0039] FIG1 is an application environment diagram of a method for generating a test case in one embodiment;

[0040] Figure 2 A schematic diagram of a flow chart of a method for generating a test case in an embodiment;

[0041] Figure 3 A schematic diagram of a flow chart of a method for generating a test case in another embodiment;

[0042] Figure 4A schematic diagram of a flow chart of a method for generating a test case in another embodiment;

[0043] Figure 5 A schematic diagram of a flow chart of a method for generating a test case in another embodiment;

[0044] Figure 6 A schematic diagram of a flow chart of a method for generating a test case in another embodiment;

[0045] Figure 7 A schematic diagram of a flow chart of a method for generating a test case in another embodiment;

[0046] Figure 8 The figure is a structural block diagram of a test case generation device in an embodiment. DETAILED DESCRIPTION

[0047] In order to make the purpose, technical solution and advantages of the present application more clearly understood, the present application is further described in detail below in conjunction with the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are only used to explain the present application and are not used to limit the present application.

[0048] The method for generating a test case provided by an embodiment of the present application can be applied to an application environment as shown in Figure 1. Wherein, the computer device can be a server, and its internal structure diagram can be shown in Figure 1. The computer device includes a processor, a memory, an input / output interface (Input / Output, referred to as I / O) and a communication interface. Wherein, the processor, the memory and the input / output interface are connected through a system bus, and the communication interface is connected to the system bus through the input / output interface. Wherein, the processor of the computer device is used to provide computing and control capabilities. The memory of the computer device includes a non-volatile storage medium and an internal memory. The non-volatile storage medium stores an operating system, a computer program and a database. The internal memory provides an environment for the operation of the operating system and the computer program in the non-volatile storage medium. The database of the computer device is used to store the generation data of the test case. The input / output interface of the computer device is used to exchange information between the processor and an external device. The communication interface of the computer device is used to communicate with an external terminal through a network connection. When the computer program is executed by the processor, a method for generating a test case is implemented.

[0049] Those skilled in the art will understand that the structure shown in FIG. 1 is merely a block diagram of a partial structure related to the solution of the present application, and does not constitute a limitation on the computer device to which the solution of the present application is applied. The specific computer device may include more or fewer components than shown in the figure, or combine certain components, or have a different arrangement of components.

[0050] In one embodiment, Figure 2As shown, a method for generating a test case is provided, and the method is applied to the server in FIG1 as an example for explanation, including:

[0051] S201, generating an initial test case according to the program structure of the program to be tested, wherein the initial test case includes an initial execution path.

[0052] The program structure of the program to be tested may include a sequence structure, a branch structure, and a loop structure. The initial test case includes an initial execution path and other test information related to the program to be tested, and the other test information may include the name of the program to be tested, a test script, a test standard, and the like.

[0053] In the embodiment of the present application, the program running paths of the programs to be tested with different program structures are not the same, and therefore, the execution paths for testing the programs to be tested with different program structures are also different. In the present embodiment, an initial execution path is generated according to the program structure of the program to be tested, and further, an initial test case is generated according to the initial execution path and other test information related to the program to be tested.

[0054] Optionally, a path correspondence between the program structure and the test path can be established in advance. For example, the sequential structure can correspond to test path 1, the branch structure can correspond to test path 2, and the loop structure can correspond to test path 3. Furthermore, based on the program structure and path correspondence of the program to be tested, the test path corresponding to the program to be tested is determined, and the path is used as the initial execution path.

[0055] S202, optimizing the initial test case according to the initial execution path, the path set of the program to be tested and the fitness function of the program to be tested, and generating a target test case of the program to be tested; the fitness function includes a static fitness function and a dynamic fitness function, and the dynamic fitness function changes with the number of iterations of optimizing the initial test case.

[0056] The weight parameter in the static fitness function is a fixed weight parameter, and the weight parameter in the dynamic fitness function changes with the number of iterations of optimizing the initial test case. The target test case includes at least one target path.

[0057] In an embodiment of the present application, it is determined whether there is a path matching the initial path in the path set of the program to be tested based on the fitness function of the program to be tested, and the initial test case is optimized based on the test results, so that the execution path in the optimized initial test case has a higher coverage of the program to be tested.

[0058] Optionally, the static fitness function may be as shown in Formula 1, and the dynamic fitness function may be as shown in Formula 2:

[0059] (Formula 1)

[0060] (Formula 2)

[0061] Among them, the concept of node importance is introduced in the static fitness function and the dynamic fitness function. Ri Defined as the proportion of paths containing node Ri in all target paths, EX R and OB R are the fitness values ​​of the individual execution path and the target path, respectively. n is the number of nodes in the execution path and the target path. K is a constant. D(x) is the branch distance function and I Ri t is the weight parameter of the tth iteration, that is, the execution path contains node R i The proportion of individuals in a population.

[0062] Optionally, the initial test case can be optimized based on the initial execution path, the path set of the program to be tested and the static fitness function of the program to be tested to generate a target test case for the program to be tested; or, the initial test case can be optimized based on the initial execution path, the path set of the program to be tested and the dynamic fitness function of the program to be tested to generate a target test case for the program to be tested; or, the initial test case can be optimized based on the initial execution path, the path set of the program to be tested and the static fitness function and dynamic fitness function of the program to be tested to generate a target test case for the program to be tested.

[0063] In the above test case generation method, an initial test case is generated according to the program structure of the program to be tested, and the initial test case includes an initial execution path; the initial test case is optimized according to the initial execution path, the path set of the program to be tested and the fitness function of the program to be tested to generate a target test case of the program to be tested; the fitness function includes a static fitness function and a dynamic fitness function, and the dynamic fitness function changes with the number of iterations of optimizing the initial test case. The initial test case is generated in combination with the program structure of the program to be tested, so that the initial test case is more matched with the program to be tested, and the accuracy of the initial test case is improved. Further, the initial test case is updated using the fitness function, and the fitness function includes a dynamic fitness function that changes with the number of iterations of optimizing the initial test case, so that the update of the initial test case is more effective, thereby further improving the matching degree of the target test case with the program to be tested.

[0064] In one embodiment, an implementation of the above S202 is provided, such as Figure 3 As shown, the above “optimizing the initial test case according to the initial execution path, the path set of the program to be tested and the fitness function of the program to be tested to generate the target test case of the program to be tested” includes:

[0065] S301, determining an initial population according to an initial execution path; each individual in the initial population includes an individual path and an individual fitness value.

[0066] In an embodiment of the present application, a search is performed based on an initial execution path to obtain expressions of multiple individuals, thereby forming an initial population based on the multiple individuals. Optionally, the expression of each individual may include input variables required by the program to be tested, the execution path generated by the input variables acting on the program to be tested, and the fitness value of the individual. An array is used to store the input variables, and then the array storing the variables, the execution path, and the individual fitness value are encapsulated as a class to form an individual class.

[0067] Optionally, the quality of the initial population determines the starting point of the algorithm. A high-quality initial population can effectively reduce the number of population evolutions and improve the optimization efficiency of the algorithm. When the static analysis program is performed, the boundary values ​​of the individual input variables in each dimension are determined. When the number of populations is determined to be N, for problems where the variables are continuous, the continuous interval of the individual in each dimension is divided into N equal parts, and then a point is randomly selected from the fragments of each dimension to form the individual's variable value. When a fragment on each dimension is selected, the fragment will be deleted from that dimension, and the next individual will be randomly generated from the remaining fragments in each dimension until N individuals are generated. At this time, the fragment sets on each dimension are empty, and the generated N individuals are used as the initial population. For problems where the variables are discrete, determine the optional set of variables. When the number of elements in the optional set is greater than or equal to the population number N, randomly select an element from the optional set of each dimension to form an individual, and then remove the selected element from the optional set of each dimension, and so on, until N individuals are generated. When the number of elements in the optional set is less than the number of individuals in the population, still randomly select an element from the optional set of each dimension to generate an individual. There is no need to delete the optional set, but it is necessary to determine whether the generated new individual already exists. If so, it needs to be regenerated. Generating individuals in this way can not only prevent the generation of redundant individuals, but also enable individuals to be distributed in the solution space as much as possible to maintain the diversity of the population, which is extremely important for the early exploration of the algorithm.

[0068] Optionally, when the individual variable value acts on the program to be tested, an execution path is generated. By determining whether there are continuous path fragments in the loop structure in the execution path, such fragments are deduplicated in turn, that is, only the longest identical path fragment in the fragment is retained to replace the fragment generated in the loop structure. The deduplicated path is regarded as the individual execution path.

[0069] S302, performing a selection operation on the initial population according to the fitness function and the individual fitness value to obtain a target individual and a selected population; the selected population includes individuals other than the target individual.

[0070] In an embodiment of the present application, the fitness value of each individual in the initial population is substituted into the fitness function. If the value of the fitness function corresponding to the individual meets the preset selection condition, each individual is used as the target individual and the target individual is deleted from the initial population to obtain the selected population.

[0071] Optionally, the preset selection condition may be whether the value of the fitness function is equal to a first preset threshold, for example, the first preset threshold may be 0; or, the preset selection condition may be whether the value of the fitness function is less than a preset second threshold.

[0072] Optionally, the fitness value of each individual in the initial population is substituted into the static fitness function. If the value of the static fitness function corresponding to the individual meets the preset selection conditions, then each individual is taken as the target individual; alternatively, the fitness value of each individual in the initial population is substituted into the dynamic fitness function. If the value of the dynamic fitness function corresponding to the individual meets the preset selection conditions, then each individual is taken as the target individual; the fitness value of each individual in the initial population is substituted into the static fitness function and the dynamic fitness function. If the value of the static fitness function corresponding to the individual and any one of the values ​​of the static fitness function and the dynamic fitness function meet the preset selection conditions, then each individual is taken as the target individual.

[0073] S303, determining whether a preset iteration condition is met, if the preset iteration condition is not met, executing S304; if the preset iteration condition is met, executing S305.

[0074] S304, iteratively updating the selected population according to a preset evolutionary algorithm to obtain a new initial population, and returning to the step of performing a selection operation on the initial population according to the fitness function.

[0075] S305, obtaining target test cases of the program to be tested according to the target individuals obtained in each iteration.

[0076] In the embodiment of the present application, the preset iteration condition may be that the current iteration number reaches the preset iteration number, or the preset iteration condition may be that the total iteration duration reaches the preset iteration duration; or the preset iteration condition may be whether the execution paths corresponding to the individuals in the initial population have all been determined as target paths. If the preset iteration condition is not met, the individuals in the selected population are searched and updated according to the preset algorithm to form a new initial population, and the step of selecting the initial population according to the fitness function is returned until the preset iteration condition is met.

[0077] Optionally, the above-mentioned "iteratively updating the selected population according to a preset evolutionary algorithm to obtain a new initial population" includes: updating each individual in the selected population according to the crossover operator and the search operator in the selected population to obtain a new initial population. Exemplarily, for the crossover operator, alleles or allele fragments in individuals A and B can be randomly intercepted for exchange, and then a new individual C is generated. Considering that the value ranges or value types on different genes are different, the crossover operator cannot intercept non-allelic genes or non-allelic genes for exchange; for the search operator, a gene in individual A can be randomly selected and randomly transformed into a value on any fragment on the gene dimension or any element in the optional set.

[0078] In an embodiment of the present application, when the preset iteration conditions are met, the target individuals determined in each iteration process are determined, and further, the execution paths corresponding to the target individuals are determined to obtain multiple execution paths. The above multiple execution paths and other related information are encapsulated to obtain the target test case of the program to be tested.

[0079] Optionally, the iterative optimization is stopped when the total iteration time reaches the preset time and when all target paths are covered by individuals in the population. Compared with setting the preset iteration condition to the current number of iterations reaching the preset number of iterations, it avoids the situation where the algorithm may have found the optimal individual in a very short time when the preset number of iterations is too large, which wastes processing resources; and the algorithm may stop searching before finding the optimal individual when the preset number of iterations is too small.

[0080] In the above application embodiment, when the preset iteration conditions are not met, the initial population is iteratively updated, and a selection operation is performed on the initial population after each iteration, so that the determined target path is more prepared, and the preset iteration conditions are used as the standard for whether the iteration is completed, which can save processing resources and processing time.

[0081] In one embodiment, an implementation of the above S302 is provided, such as Figure 4 As shown, the above “selecting the initial population according to the fitness function and the individual fitness value to obtain the target individual and the selected population” includes:

[0082] S401, determining a target fitness function from a static fitness function and a dynamic fitness function according to a preset number of iterations and selection results of selection operations corresponding to each iteration.

[0083] As an optional implementation, if the current number of iterations reaches the preset number of iterations, and the number of target paths in the selection results of the selection operations corresponding to each iteration is less than a first preset number, the dynamic fitness function is determined to be the target fitness function; if the current number of iterations does not reach the preset number of iterations, and / or the number of target paths in the selection results of the selection operations corresponding to each iteration is not less than a first preset number, the static fitness function is determined to be the target fitness function.

[0084] As another optional implementation, if the target individuals determined within the preset number of iterations are less than the second preset number, the dynamic fitness function is determined as the target fitness function; if the target individuals determined within the preset number of iterations are not less than the second preset number, the static fitness function is determined as the target fitness function. Exemplarily, if the preset number of iterations is 4 and the second preset number is 2, if the number of target individuals determined within 4 iterations is less than 2, the dynamic fitness function is determined as the target fitness function; if the number of target individuals determined within 4 iterations is not less than 2, the static fitness function is determined as the target fitness function.

[0085] Optional, such as Figure 5 As shown, the above “determining the target fitness function from the static fitness function and the dynamic fitness function according to the preset number of iterations and the selection results of the selection operations corresponding to each iteration” includes:

[0086] S501: If the selection result indicates that the target individual has not been determined within a preset number of iterations, the dynamic fitness function is determined as the target fitness function.

[0087] S502: If the selection result indicates that the target individual is determined within a preset number of iterations, the static fitness function is determined as the target fitness function.

[0088] Optionally, in an embodiment of the present application, if the selection result indicates that the target individual has not been determined within the preset number of iterations, then the static fitness function may no longer be applicable to the iteration process of the current population, and the dynamic fitness function is determined as the target fitness function; if the selection result indicates that the target individual has been determined within the preset number of iterations, then the static fitness function is still accurate in determining whether there is a target path among the individuals in the population, and the static fitness function is used as the target fitness function. Exemplarily, the preset number of iterations may be 5 times. If the current number of iterations is the 7th, then it is determined whether the target individual has been determined during the 2nd to 7th iterations. If the target individual has been determined, the static fitness function is determined as the target fitness function; if the target individual has not been determined, the dynamic fitness function is determined as the target fitness function.

[0089] S402, determining a target individual matching any path in the path set from the initial population according to the target fitness function and the individual fitness value, and taking the other individuals except the target individual as the selected population.

[0090] In an embodiment of the present application, the fitness value of each individual in the initial population is substituted into the target fitness function in turn, and the execution path corresponding to each individual in the initial population is compared with the path in the path set in turn. If there is an individual whose output value of the fitness function with any path in the path set is 0, then the individual matches the path in the path set, and the individual is determined as the target individual, and the target individual is deleted from the initial population, and the individuals other than the target individual are used as the selected population.

[0091] In the above application embodiment, the target fitness function is flexibly adjusted according to the selection result in the iteration process, thereby improving the matching degree between the target fitness function and the current iteration situation, thereby making the target individual determined according to the target fitness function more accurate.

[0092] In one embodiment, an implementation of the above S201 is provided, such as Figure 6 As shown, the above “generating initial test cases according to the program structure of the program to be tested” includes:

[0093] S601, marking the program structure of the program to be tested by using an instrumentation function to obtain a plurality of marking points.

[0094] In the embodiment of the present application, the program structure includes a sequential structure, a branch structure, and a loop structure. The method of marking various program structures by inserting a stub function is as follows:

[0095] (1) There is only one path for a continuous sequence structure, so it is only necessary to insert a stub function after the last statement of the continuous sequence structure.

[0096] (2) For branch structures, insert a stub function after each branch judgment statement.

[0097] (3) For loop structures, the target path set includes two types of paths: not executed once and executed only once. Loop structures include for structure, while structure and do-while structure. For for structure and while structure, a stub function needs to be inserted after the for and while judgment statements; for do-while structure, a stub function needs to be added after the do statement.

[0098] It should be noted that function calls can be regarded as sequential structures, and stub functions are inserted into the function according to the rules of sequential structure, branch structure and loop structure. In addition, considering that the loop structure may cause the path length and number to grow exponentially due to the existence of the loop, resulting in inaccurate collection range of all target paths, two properties need to be output for the stub function, including: the number of lines in the program where the stub function is located and whether it is in a loop structure. The sequential output of the stub function is the individual execution path.

[0099] In this embodiment, after marking various program structures by inserting stub functions, the insertion positions of the stub functions and the output values ​​of the stub functions are determined by analyzing the program to form a plurality of marking points.

[0100] S602, generating an initial test case according to the multiple marking points.

[0101] In an embodiment of the present application, multiple marking points are connected in series according to their marking times to form an initial execution path. Further, an initial test case is generated according to the initial execution path and other test information related to the program to be tested.

[0102] In the above-mentioned application embodiment, various types of program structures are marked by the pile function, and considering the complex program structure, the insertion position of the pile function and the output value of the pile function are determined by analyzing the program, and the initial execution path is obtained, which improves the accuracy of the initial execution path.

[0103] In one embodiment, a complete test case generation is provided, such as Figure 7 As shown, the method includes:

[0104] S1, marking the program structure of the program to be tested by the stub function to obtain multiple marking points; generating an initial test case according to the multiple marking points; the initial test case includes an initial execution path.

[0105] S2, determining the initial population according to the initial execution path; each individual in the initial population includes the individual path and individual fitness value.

[0106] S3, if the selection result indicates that the target individual is not determined within the preset number of iterations, the dynamic fitness function is determined as the target fitness function; if the selection result indicates that the target individual is determined within the preset number of iterations, the static fitness function is determined as the target fitness function.

[0107] S4, determining the target individual that matches any path in the path set from the initial population according to the target fitness function and the individual fitness value, and taking the other individuals after the target individual as the selected population.

[0108] S5, determine whether the preset iteration condition is met, if not, execute S7 and return to execute S3; if met, execute S6.

[0109] S6, according to the crossover operator and the search operator in the selected population, update each individual in the selected population to obtain a new initial population, and return to execute S3.

[0110] S7, when the preset iteration conditions are met, the target test cases of the program to be tested are obtained according to the target individuals obtained in each iteration.

[0111] In the above test case generation method, an initial test case is generated according to the program structure of the program to be tested, and the initial test case includes an initial execution path; the initial test case is optimized according to the initial execution path, the path set of the program to be tested and the fitness function of the program to be tested to generate a target test case of the program to be tested; the fitness function includes a static fitness function and a dynamic fitness function, and the dynamic fitness function changes with the number of iterations of optimizing the initial test case. The initial test case is generated in combination with the program structure of the program to be tested, so that the initial test case is more matched with the program to be tested, and the accuracy of the initial test case is improved. Further, the initial test case is updated using the fitness function, and the fitness function includes a dynamic fitness function that changes with the number of iterations of optimizing the initial test case, so that the update of the initial test case is more effective, thereby further improving the matching degree of the target test case with the program to be tested.

[0112] It should be understood that, although the various steps in the flowcharts involved in the above-mentioned embodiments are displayed in sequence according to the indication of the arrows, these steps are not necessarily executed in sequence according to the order indicated by the arrows. Unless there is a clear explanation in this article, the execution of these steps does not have a strict order restriction, and these steps can be executed in other orders. Moreover, at least a part of the steps in the flowcharts involved in the above-mentioned embodiments can include multiple steps or multiple stages, and these steps or stages are not necessarily executed at the same time, but can be executed at different times, and the execution order of these steps or stages is not necessarily to be carried out in sequence, but can be executed in turn or alternately with other steps or at least a part of the steps or stages in other steps.

[0113] Based on the same inventive concept, the embodiment of the present application also provides a test case generation device for implementing the test case generation method involved above. The implementation scheme for solving the problem provided by the device is similar to the implementation scheme recorded in the above method, so the specific limitations in the embodiments of the device for generating one or more test cases provided below can refer to the limitations of the test case generation method above, and will not be repeated here.

[0114] In one embodiment, Figure 8 As shown, a test case generation device is provided, comprising: a generation module 10 and an optimization module 11, wherein:

[0115] A generating module 10, for generating an initial test case according to the program structure of the program to be tested, wherein the initial test case includes an initial execution path;

[0116] The optimization module 11 is used to optimize the initial test case according to the initial execution path, the path set of the program to be tested and the fitness function of the program to be tested, and generate the target test case of the program to be tested; the fitness function includes a static fitness function and a dynamic fitness function, and the dynamic fitness function changes with the number of iterations of optimizing the initial test case.

[0117] In one embodiment, the optimization module 11 includes: a first determination unit, a selection unit, an update unit, and a second determination unit, wherein:

[0118] The first determining unit is used to determine an initial population according to an initial execution path; each individual in the initial population includes an individual path and an individual fitness value.

[0119] The selection unit is used to perform a selection operation on the initial population according to the fitness function and the individual fitness value to obtain a target individual and a selected population; the selected population includes individuals other than the target individual.

[0120] The updating unit is used to iteratively update the selected population according to the preset evolutionary algorithm when the preset iteration condition is not met to obtain a new initial population, and return to the step of performing the selection operation on the initial population according to the fitness function.

[0121] The second determining unit is used to obtain a target test case of the program to be tested according to the target individuals obtained in each iteration when a preset iteration condition is met.

[0122] In one embodiment, the updating unit is specifically used to update each individual in the selected population according to the crossover operator and the search operator in the selected population to obtain a new initial population.

[0123] In one embodiment, the above-mentioned selection unit is specifically used to determine the target fitness function from the static fitness function and the dynamic fitness function according to the preset number of iterations and the selection results of the selection operations corresponding to each iteration; determine the target individual matching any path in the path set from the initial population according to the target fitness function and the individual fitness value, and use the other individuals except the target individual as the selected population.

[0124] In one embodiment, the above-mentioned selection unit is specifically used to determine the dynamic fitness function as the target fitness function if the selection result indicates that the target individual is not determined within the preset number of iterations; if the selection result indicates that the target individual is determined within the preset number of iterations, determine the static fitness function as the target fitness function.

[0125] In one embodiment, the generation module 10 includes: a marking unit and a generation unit, wherein:

[0126] The marking unit is used to mark the program structure of the program to be tested through the stub function to obtain multiple marking points.

[0127] The generation unit is used to generate initial test cases according to multiple marking points.

[0128] Each module in the above test case generation device can be implemented in whole or in part by software, hardware and a combination thereof. Each module can be embedded in or independent of a processor in a computer device in the form of hardware, or can be stored in a memory in a computer device in the form of software, so that the processor can call and execute operations corresponding to each module.

[0129] In one embodiment, a computer device is provided, including a memory and a processor, wherein a computer program is stored in the memory, and when the processor executes the computer program, the following steps are implemented:

[0130] generating an initial test case according to the program structure of the program to be tested, wherein the initial test case includes an initial execution path;

[0131] According to the initial execution path, the path set of the program to be tested and the fitness function of the program to be tested, the initial test case is optimized to generate the target test case of the program to be tested; the fitness function includes a static fitness function and a dynamic fitness function, and the dynamic fitness function changes with the number of iterations of optimizing the initial test case.

[0132] In one embodiment, when the processor executes the computer program, the processor further implements the following steps:

[0133] According to the initial execution path, the initial population is determined; each individual in the initial population includes the individual path and the individual fitness value;

[0134] The initial population is selected according to the fitness function and the individual fitness value to obtain the target individual and the selected population; the selected population includes individuals other than the target individual;

[0135] If the preset iteration condition is not met, the selected population is iteratively updated according to the preset evolutionary algorithm to obtain a new initial population, and the step of selecting the initial population according to the fitness function is returned;

[0136] When the preset iteration conditions are met, the target test cases of the program to be tested are obtained according to the target individuals obtained in each iteration.

[0137] In one embodiment, when the processor executes the computer program, the processor further implements the following steps:

[0138] According to the crossover operator and the search operator in the selected population, each individual in the selected population is updated to obtain a new initial population.

[0139] In one embodiment, when the processor executes the computer program, the processor further implements the following steps:

[0140] Determine a target fitness function from a static fitness function and a dynamic fitness function according to a preset number of iterations and selection results of selection operations corresponding to each iteration;

[0141] According to the target fitness function and the individual fitness value, the target individual matching any path in the path set is determined from the initial population, and the individuals after the target individual are taken as the selected population.

[0142] In one embodiment, when the processor executes the computer program, the processor further implements the following steps:

[0143] If the selection result indicates that the target individual has not been determined within the preset number of iterations, the dynamic fitness function is determined as the target fitness function;

[0144] If the selection result indicates that the target individual is determined within the preset number of iterations, the static fitness function is determined as the target fitness function.

[0145] In one embodiment, when the processor executes the computer program, the processor further implements the following steps:

[0146] The program structure of the program to be tested is marked by the stub function to obtain multiple marking points;

[0147] Generate initial test cases based on multiple marking points.

[0148] In one embodiment, a computer readable storage medium is provided, on which a computer program is stored, and when the computer program is executed by a processor, the following steps are implemented:

[0149] generating an initial test case according to the program structure of the program to be tested, wherein the initial test case includes an initial execution path;

[0150] According to the initial execution path, the path set of the program to be tested and the fitness function of the program to be tested, the initial test case is optimized to generate the target test case of the program to be tested; the fitness function includes a static fitness function and a dynamic fitness function, and the dynamic fitness function changes with the number of iterations of optimizing the initial test case.

[0151] In one embodiment, when the computer program is executed by a processor, the following steps are also implemented:

[0152] According to the initial execution path, the initial population is determined; each individual in the initial population includes the individual path and the individual fitness value;

[0153] The initial population is selected according to the fitness function and the individual fitness value to obtain the target individual and the selected population; the selected population includes individuals other than the target individual;

[0154] If the preset iteration condition is not met, the selected population is iteratively updated according to the preset evolutionary algorithm to obtain a new initial population, and the step of selecting the initial population according to the fitness function is returned;

[0155] When the preset iteration conditions are met, the target test cases of the program to be tested are obtained according to the target individuals obtained in each iteration.

[0156] In one embodiment, when the computer program is executed by a processor, the following steps are also implemented:

[0157] According to the crossover operator and the search operator in the selected population, each individual in the selected population is updated to obtain a new initial population.

[0158] In one embodiment, when the computer program is executed by a processor, the following steps are also implemented:

[0159] Determine a target fitness function from a static fitness function and a dynamic fitness function according to a preset number of iterations and selection results of selection operations corresponding to each iteration;

[0160] According to the target fitness function and the individual fitness value, the target individual matching any path in the path set is determined from the initial population, and the individuals after the target individual are taken as the selected population.

[0161] In one embodiment, when the computer program is executed by a processor, the following steps are also implemented:

[0162] If the selection result indicates that the target individual has not been determined within the preset number of iterations, the dynamic fitness function is determined as the target fitness function;

[0163] If the selection result indicates that the target individual is determined within the preset number of iterations, the static fitness function is determined as the target fitness function.

[0164] In one embodiment, when the computer program is executed by a processor, the following steps are also implemented:

[0165] The program structure of the program to be tested is marked by the stub function to obtain multiple marking points;

[0166] Generate initial test cases based on multiple marking points.

[0167] In one embodiment, a computer program product is provided, comprising a computer program, which, when executed by a processor, implements the following steps:

[0168] generating an initial test case according to the program structure of the program to be tested, wherein the initial test case includes an initial execution path;

[0169] According to the initial execution path, the path set of the program to be tested and the fitness function of the program to be tested, the initial test case is optimized to generate the target test case of the program to be tested; the fitness function includes a static fitness function and a dynamic fitness function, and the dynamic fitness function changes with the number of iterations of optimizing the initial test case.

[0170] In one embodiment, when the computer program is executed by a processor, the following steps are also implemented:

[0171] According to the initial execution path, the initial population is determined; each individual in the initial population includes the individual path and the individual fitness value;

[0172] The initial population is selected according to the fitness function and the individual fitness value to obtain the target individual and the selected population; the selected population includes individuals other than the target individual;

[0173] If the preset iteration condition is not met, the selected population is iteratively updated according to the preset evolutionary algorithm to obtain a new initial population, and the step of selecting the initial population according to the fitness function is returned;

[0174] When the preset iteration conditions are met, the target test cases of the program to be tested are obtained according to the target individuals obtained in each iteration.

[0175] In one embodiment, when the computer program is executed by a processor, the following steps are also implemented:

[0176] According to the crossover operator and the search operator in the selected population, each individual in the selected population is updated to obtain a new initial population.

[0177] In one embodiment, when the computer program is executed by a processor, the following steps are also implemented:

[0178] Determine a target fitness function from a static fitness function and a dynamic fitness function according to a preset number of iterations and selection results of selection operations corresponding to each iteration;

[0179] According to the target fitness function and the individual fitness value, the target individual matching any path in the path set is determined from the initial population, and the individuals after the target individual are taken as the selected population.

[0180] In one embodiment, when the computer program is executed by a processor, the following steps are also implemented:

[0181] If the selection result indicates that the target individual has not been determined within the preset number of iterations, the dynamic fitness function is determined as the target fitness function;

[0182] If the selection result indicates that the target individual is determined within the preset number of iterations, the static fitness function is determined as the target fitness function.

[0183] In one embodiment, when the computer program is executed by a processor, the following steps are also implemented:

[0184] The program structure of the program to be tested is marked by the stub function to obtain multiple marking points;

[0185] Generate initial test cases based on multiple marking points.

[0186] A person of ordinary skill in the art can understand that all or part of the processes in the above-mentioned embodiment method can be completed by instructing the relevant hardware through a computer program, and the computer program can be stored in a non-volatile computer-readable storage medium. When the computer program is executed, it can include the processes of the embodiments of the above-mentioned methods. Among them, any reference to the memory, database or other medium used in the embodiments provided in the present 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. As an illustration and not limitation, RAM can be in various forms, such as static random access memory (SRAM) or dynamic random access memory (DRAM). The database involved in each embodiment provided in this application may include at least one of a relational database and a non-relational database. Non-relational databases may include distributed databases based on blockchains, etc., but are not limited to this. The processor involved in each embodiment provided in this application may be a general-purpose processor, a central processing unit, a graphics processor, a digital signal processor, a programmable logic device, a data processing logic device based on quantum computing, an artificial intelligence (AI) processor, etc., but are not limited to this.

[0187] The technical features of the above embodiments may be combined arbitrarily. To make the description concise, 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.

[0188] The above-described embodiments only express several implementation methods of the present application, and the descriptions thereof are relatively specific and detailed, but they cannot be understood as limiting the scope of the present application. It should be pointed out that, for a person of ordinary skill in the art, several variations and improvements can be made without departing from the concept of the present application, and these all belong to the protection scope of the present application. Therefore, the protection scope of the present application shall be subject to the attached claims.

Claims

1. A method for generating a test case, characterized in that: The method comprises: Generate an initial test case according to the program structure of the program to be tested, wherein the initial test case includes an initial execution path; According to the initial execution path, the path set of the program to be tested and the fitness function of the program to be tested, the initial test case is optimized to generate a target test case of the program to be tested; the fitness function includes a static fitness function and a dynamic fitness function, and the dynamic fitness function changes with the number of iterations of optimizing the initial test case.

2. The method according to claim 1, characterized in that The step of optimizing the initial test case according to the initial execution path, the path set of the program to be tested and the fitness function of the program to be tested to generate a target test case of the program to be tested includes: Determine an initial population according to the initial execution path; each individual in the initial population includes the path of the individual and the individual fitness value; Performing a selection operation on the initial population according to the fitness function and the individual fitness value to obtain a target individual and a selected population; the selected population includes individuals other than the target individual; If the preset iteration condition is not met, iteratively updating the selected population according to the preset evolutionary algorithm to obtain a new initial population, and returning to the step of selecting the initial population according to the fitness function; When the preset iteration conditions are met, the target test cases of the program to be tested are obtained according to the target individuals obtained in each iteration.

3. The method according to claim 2, characterized in that The step of iteratively updating the selected population according to a preset evolutionary algorithm to obtain a new initial population includes: According to the crossover operator and the search operator in the selected population, each individual in the selected population is updated to obtain the new initial population.

4. The method according to claim 2, characterized in that: The step of performing a selection operation on the initial population according to the fitness function and the individual fitness value to obtain a target individual and a selected population includes: Determining a target fitness function from the static fitness function and the dynamic fitness function according to a preset number of iterations and selection results of selection operations corresponding to each iteration; A target individual matching any one of the paths in the path set is determined from the initial population according to the target fitness function and the individual fitness value, and other individuals except the target individual are used as the selected population.

5. The method according to claim 4, characterized in that According to the preset number of iterations and the selection results of the selection operations corresponding to each iteration, a target fitness function is determined from the static fitness function and the dynamic fitness function, including: If the selection result indicates that the target individual has not been determined within the preset number of iterations, determining the dynamic fitness function as the target fitness function; If the selection result indicates that the target individual is determined within the preset number of iterations, the static fitness function is determined as the target fitness function.

6. The method according to claim 1, characterized in that The generating of initial test cases according to the program structure of the program to be tested comprises: Marking the program structure of the program to be tested by using an instrumentation function to obtain a plurality of marking points; An initial test case is generated according to the multiple marking points.

7. A test case generation device, characterized in that: The device comprises: A generating module, used for generating an initial test case according to the program structure of the program to be tested, wherein the initial test case includes an initial execution path; An optimization module is used to optimize the initial test case according to the initial execution path, the path set of the program to be tested and the fitness function of the program to be tested, so as to generate a target test case for the program to be tested; the fitness function includes a static fitness function and a dynamic fitness function, and the dynamic fitness function changes with the number of iterations of optimizing the initial test case.

8. A computer device comprising a memory and a processor, wherein the memory stores a computer program, wherein: When the processor executes the computer program, the steps of the method according to any one of claims 1 to 6 are implemented.

9. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, the steps of the method according to any one of claims 1 to 6 are implemented.

10. A computer program product, comprising a computer program, characterized in that When the computer program is executed by a processor, the steps of the method according to any one of claims 1 to 6 are implemented.

Citation Information

Cited By

  • Abnormal program positioning method, testing system, device, equipment, medium and product

    CN120256188A

  • Test case combination generation method, device, system, equipment and medium

    CN120492333A