Test case generation method, test case generation device and electronic equipment

By dynamically determining the selection probability and performing genetic operations, the problem of low search efficiency in the prior art test data generation and search is solved, and more efficient and accurate test data generation is achieved.

CN119621548APending Publication Date: 2025-03-14CHINA MOBILE INTERNET CO LTD +1

Patent Information

Application Number
CN202411602815.7
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-11-11
Publication Date
2025-03-14

AI Technical Summary

Technical Problem

When generating test data, the prior art has large calculation volume and low search efficiency, and lacks a purposeful learning process, resulting in low search efficiency.

Method used

By obtaining the first fitness value of the target path and the actual path corresponding to each target test case in the target population, the selection probability of each target test case is dynamically determined, and a preset number of target test cases are selected from the target population based on these probabilities for genetic operations, and the population is updated until the stop condition is met.

Benefits of technology

It improves the global search capability of the algorithm, avoids falling into local optimal solutions, speeds up convergence speed and accuracy, and improves the accuracy and efficiency of test data generation.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119621548A_ABST
    Figure CN119621548A_ABST
Patent Text Reader

Abstract

The invention discloses a test case generation method, a test case generation device and electronic equipment. The method comprises the following steps: acquiring a first fitness value of a target path and an actual path corresponding to each target test case in a target population; dynamically determining a selection probability corresponding to each target test case based on the first fitness value and contribution degree corresponding to each target test case; according to the selection probability corresponding to each target test case, selecting a preset number of target test cases from the target population for genetic manipulation to obtain a next generation population; and updating the target population based on the next generation population, and returning to execute the obtaining of the first fitness value of the target path and the actual path corresponding to each target test case in the target population until a stop condition is satisfied, and outputting the target test case satisfying the target path.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application belongs to the field of computer technology, and specifically relates to a test case generation method, a test case generation device and an electronic device. Background Art

[0002] At present, test data is usually automatically generated based on genetic algorithms, that is, by constructing a fitness function, the algorithm continuously searches for the encoded parameters until the optimal solution is found or the maximum evolutionary generation is reached. However, this method has a large amount of calculation, and the average value is usually used as the individual fitness during calculation, which weakens the degree of matching of the individual to a single target path, and the search process lacks a purposeful learning process, resulting in low search efficiency. Summary of the invention

[0003] The embodiments of the present application provide a test case generation method, a test case generation device and an electronic device, which can solve the problem of low search efficiency.

[0004] In a first aspect, an embodiment of the present application provides a method for generating a test case, the method comprising: obtaining a first fitness value of an actual path corresponding to a target path and each target test case in a target population; dynamically determining a selection probability corresponding to each target test case based on the first fitness value and contribution corresponding to each target test case; selecting a preset number of target test cases from the target population for genetic operations according to the selection probability corresponding to each target test case to obtain a next generation population; updating the target population based on the next generation population, and returning to execute the step of obtaining the first fitness value of the actual path corresponding to the target path and each target test case in the target population until a stop condition is met, and outputting the target test case that meets the target path. In a second aspect, an embodiment of the present application provides a test case generation device, which includes: an acquisition module, used to obtain a first fitness value of an actual path corresponding to a target path and each target test case in a target population; a first determination module, used to dynamically determine the selection probability corresponding to each target test case based on the first fitness value and contribution corresponding to each target test case; a selection module, used to select a preset number of target test cases from the target population for genetic operations according to the selection probability corresponding to each target test case, to obtain a next generation population; a processing module, used to update the target population based on the next generation population, and return to execute the step of obtaining the first fitness value of the actual path corresponding to the target path and each target test case in the target population until a stop condition is met, and output the target test case that meets the target path.

[0005] In a third aspect, an embodiment of the present application provides an electronic device, which includes a processor, a memory, and a program or instruction stored in the memory and executable on the processor, wherein the program or instruction, when executed by the processor, implements the steps of the method described in the first aspect.

[0006] In a fourth aspect, an embodiment of the present application provides a computer-readable storage medium, on which a program or instruction is stored, and when the program or instruction is executed by a processor, the steps of the method described in the first aspect are implemented.

[0007] In a fifth aspect, an embodiment of the present application provides a chip, comprising a processor and a communication interface, wherein the communication interface is coupled to the processor, and the processor is used to run a program or instruction to implement the method described in the first aspect.

[0008] In a sixth aspect, an embodiment of the present application provides a computer program product, wherein the computer program product includes at least one computer program, and when the computer program is loaded and executed by a processor, the method described in the first aspect is implemented.

[0009] In an embodiment of the present application, a first fitness value of an actual path corresponding to a target path and each target test case in a target population is obtained, and then a selection probability corresponding to each target test case is dynamically determined based on the first fitness value and contribution corresponding to each target test case, and then a preset number of target test cases are selected from the target population for genetic operations based on the selection probability corresponding to each target test case to obtain a next generation population, and finally the target population is updated based on the next generation population, and the execution is returned to obtain the first fitness value of the actual path corresponding to the target path and each target test case in the target population until a stop condition is met, and a target test case that meets the target path is output, that is, the selection probability based on the dynamic determination can more flexibly adapt to different needs in the search process, which not only avoids falling into a local optimal solution and improves the global search capability of the algorithm, but also accelerates the convergence speed and accuracy, thereby improving the accuracy and efficiency of test data generation. BRIEF DESCRIPTION OF THE DRAWINGS

[0010] Figure 1 It is a flowchart of a method for generating a test case provided in an embodiment of the present application; Figure 2a It is a schematic diagram of a path structure provided in an embodiment of the present application; Figure 2b is a coding schematic diagram of a path structure provided in an embodiment of the present application; Figure 3 It is a flowchart of a method for generating a test case provided in an embodiment of the present application; Figure 4It is a structural schematic diagram of a test case generation device provided in an embodiment of the present application; Figure 5 It is a schematic diagram of the structure of an electronic device according to an embodiment of the present application. DETAILED DESCRIPTION

[0011] The following will be combined with the drawings in the embodiments of the present application to clearly and completely describe the technical solutions in the embodiments of the present application. Obviously, the described embodiments are part of the embodiments of the present application, not all of the embodiments. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of this application.

[0012] In conjunction with the accompanying drawings, a test case generation method, a test case generation device and an electronic device provided in an embodiment of the present application are described in detail through specific embodiments and their application scenarios.

[0013] Figure 1 A method for generating a test case provided by an embodiment of the present application is shown. The method can be executed by an electronic device, and the electronic device can include: a server and / or a terminal device. In other words, the method can be executed by software or hardware installed in the electronic device, and the method includes the following steps: S110: Obtain a first fitness value of the target path and the actual path corresponding to each target test case in the target population.

[0014] The target test case includes parameter values ​​of different types of parameters, and the first fitness value is used to indicate the degree of matching between the target path and the actual path.

[0015] S120: Dynamically determine the selection probability corresponding to each target test case based on the first fitness value and contribution corresponding to each target test case.

[0016] It can be understood that, for each target test case, after determining the first fitness value of the target path and the actual path corresponding to the target test case, the first fitness value corresponding to the target test case is also determined, and then the selection probability corresponding to each target test case is dynamically determined based on the first fitness value and the contribution. Among them, the contribution refers to the relative importance of the target test case in the target population. Individuals with higher contribution are more likely to be retained in the selection process of the genetic algorithm and pass their genes to the next generation through crossover and mutation.

[0017] S130: According to the selection probability corresponding to each of the target test cases, a preset number of the target test cases are selected from the target population for genetic operation to obtain a next generation population.

[0018] In this step, the selection probability is used as a genetic operator to determine the next generation population. Since the selection probability is dynamically determined based on the first fitness value and contribution, the adaptability of the search process at different times can be enhanced. Because in the initial stage of iteration, the individual diversity of the target population is low and the search space is small. If a fixed selection probability is used, and if the selection probability is small, the diversity of the target population will be suppressed, resulting in poor diversity and poor search ability of the target population in the early iteration process; if the selection probability is large, the better individuals will be destroyed in the later stage of iteration. Therefore, the selection probability based on dynamic determination can more flexibly adapt to different needs in the search process, not only to accelerate the convergence speed and accuracy, but also to avoid falling into the local optimal solution and improve the global search ability of the algorithm, thereby improving the accuracy and efficiency of test data generation.

[0019] S140: updating the target population based on the next generation population, and returning to execute the first fitness value of the actual path corresponding to the target path and each target test case in the target population until the stop condition is met, and outputting the target test case that meets the target path.

[0020] It is understandable that the test case generation method provided in the embodiment of the present application is based on a genetic algorithm. A genetic algorithm (GA) is an optimization algorithm that imitates natural selection and genetic mechanisms, and gradually evolves a better solution by simulating the selection, crossover and mutation processes in biological evolution. Therefore, in each iteration, the target population is updated so that the target population gradually approaches the global optimal solution, thereby obtaining the target test case that satisfies the target path.

[0021] In an embodiment of the present application, a first fitness value of an actual path corresponding to a target path and each target test case in a target population is obtained, and then a selection probability corresponding to each target test case is dynamically determined based on the first fitness value and contribution corresponding to each target test case, and then a preset number of target test cases are selected from the target population for genetic operations based on the selection probability corresponding to each target test case to obtain a next generation population, and finally the target population is updated based on the next generation population, and the execution is returned to obtain the first fitness value of the actual path corresponding to the target path and each target test case in the target population until a stop condition is met, and a target test case that meets the target path is output, that is, the selection probability based on the dynamic determination can more flexibly adapt to different needs in the search process, which not only avoids falling into a local optimal solution and improves the global search capability of the algorithm, but also accelerates the convergence speed and accuracy, thereby improving the accuracy and efficiency of test data generation.

[0022] In one implementation, before obtaining the first fitness value of the actual path corresponding to the target path and each target test case in the target population, the method also includes: obtaining a program flow chart by analyzing the program to be tested; and determining an initial path set corresponding to the program to be tested based on the program flow chart, wherein the initial path set includes at least one initial path, and the target path is any one of the at least one initial path.

[0023] It can be understood that based on business needs, static analysis is performed on the program to be tested, and the logical relationships such as the sequence, branches, and loops of the program are sorted out to obtain a program flow chart corresponding to the program to be tested.

[0024] In addition, in the process of obtaining the initial path set based on the program flow chart, the sequential structure program does not involve path changes, so there is no need to distinguish this type of structure in the process of building the path; the selection branches include two cases, double branches and multiple branches, and multiple branches can be decomposed into a combination of multiple double branches; the loop structure adopts the Z path coverage principle to simplify the number of loops, and only considers the two cases of looping 1 time and 0 times.

[0025] In another implementation, after determining the initial path set corresponding to the program to be tested according to the program flow chart, the method further includes: encoding the initial path according to a coding rule. Optionally, the coding rule may be a Huffman coding convention, in which it is agreed that in a branch structure, -1 represents a left branch and 1 represents a right branch; in a loop structure, -1 represents 0 loops and 1 represents 1 loop, which can be represented by the following formula (1): (1) The encoding of each initial path is composed of the characters on the path branch from the root node to the leaf node in the initial path. For example, Figure 2a As shown in Figure 1, it is a path structure diagram, such as Figure 2b As shown in FIG. 1 , it is a schematic diagram of the encoding result of the path structure. The initial paths included in the initial path set may be [-1] [1,-1] [1,1,-1] [1,1,1].

[0026] By weighted encoding of the initial path, the program can be represented as a binary tree, where each path from the root node to the leaf node is a path. All initial paths are encoded into data strings using Huffman coding. The encoding of the initial path can be weighted based on the front and back positions of the mismatched nodes. The closer the position of the mismatched node is, the greater the degree of mismatch between the actual path and the path in the initial path set. Therefore, the difference in matching paths can be appropriately increased to accelerate the convergence speed of the algorithm in later iterations.

[0027] In one implementation, before obtaining the first fitness value of the actual path corresponding to the target path and each target test case in the target population, the method also includes: obtaining an initial population, wherein the initial population includes multiple initial test cases; obtaining a set of paths to be processed by copying the initial path set; and obtaining the target population corresponding to each initial path in the set of paths to be processed by determining the target test case corresponding to each initial path in the set of paths to be processed based on the initial population.

[0028] Among them, the initial population is obtained based on chaos initialization. Chaos initialization has the characteristics of randomness, ergodicity and regularity. It can traverse the search space non-repeatedly according to its own rules within a certain range. The initial population generated in this way is more evenly distributed in the solution space, which improves the solution accuracy and convergence speed. Since the initial path set needs to be dynamically updated when determining the target test case corresponding to each initial path, the path set to be processed can be obtained by copying the initial path set. In this way, only the path set to be processed needs to be updated without changing the initial path set.

[0029] Among them, in one implementation, the obtaining of the target population corresponding to each of the initial paths in the set of paths to be processed based on the multiple initial test cases may include the following steps: Step 1: Based on each of the initial test cases in the initial population, the instrumented program to be tested is tested and run to obtain an actual path corresponding to each of the initial test cases.

[0030] It can be understood that by passing each initial test case to the instrumented program to be tested, an actual path corresponding to each initial test case can be obtained.

[0031] Step 2: For each of the initial test cases, obtain the second fitness value of the actual path and each of the initial paths, determine the third fitness value corresponding to the initial test case based on each of the second fitness values, and add the initial test case as the target test case to the target population of the initial path corresponding to the third fitness value.

[0032] Among them, in one implementation, obtaining the second fitness value of the actual path and each of the initial paths includes: for each of the initial paths, based on the number of nodes included in the initial path, the weighted value of each node in the initial path, and the weighted value of each node in the actual path corresponding to the initial test case, determining the second fitness value of the initial path and the actual path corresponding to the initial test case. It can be understood that when calculating the second fitness, the node weight is added to represent the weight of each node in the current path. The closer the node is to the starting point, the greater its influence on the selection of the subsequent path, and therefore a larger weight is assigned. Assume that an initial path has n nodes, and the weight of the i-th node is: ). The weighted path node sequence value is: , the preprocessed path set P is as follows: =[-1]; =[2,-1]; =[3,2,-1]; =[3,2,1]. Then, the actual execution path of the initial test case j is The weighted fitting calculation is performed sequentially with each initial path p of the same length to obtain a second fitness value, wherein the second fitness value can be expressed by the following formula (2):

[0033] Among them, n is the actual path / Initial Path The total number of nodes, (m) is the actual path weight value at node m, is the weighted value of the target path at node m. is the weighted mean of the nodes in the actual path, is the weighted mean of the nodes of the target path. The value range of G is [-1,1]. When the value is >0, it indicates positive correlation, which means that the node trajectory of the actual path traversal is highly synchronized with the initial path; when the value is <0, it indicates negative correlation, which means that the node trajectory of the actual path traversal is opposite to the initial path; when the value is =0, it indicates no correlation.

[0034] In addition, for an initial path whose length is not equal to the length of the actual execution path corresponding to the initial test case, its second fitness value may be set to 0.

[0035] In another implementation, determining the third fitness value corresponding to the initial test case based on each of the second fitness values ​​includes one of the following: (1) When all the second fitness values ​​are greater than 0, determine that the third fitness value corresponding to the initial test case is the largest second fitness value.

[0036] It can be understood that since the fitness value range includes [0,1], when the second fitness values ​​of the initial test case j and each initial path are positive, select is the third fitness value D of the initial test case j.

[0037] (2) When all the second fitness values ​​are less than 0, determine that the third fitness value corresponding to the initial test case is the absolute value of the smallest second fitness value.

[0038] It can be understood that since the fitness value range includes [-1,0], when the initial test case j and the objective function values ​​of each initial path are negative, select is the third fitness value D of the initial test case j.

[0039] (3) In the case where there are second fitness values ​​greater than 0 and there are second fitness values ​​less than 0 among each second fitness value, when the product of the first numerical value and the absolute value of the smallest second fitness value is greater than the largest second fitness value, the third fitness value corresponding to the initial test case is determined to be the absolute value of the smallest second fitness value.

[0040] (4) In the case where there are second fitness values ​​greater than 0 and second fitness values ​​less than 0 among the second fitness values, when the product of the first numerical value and the absolute value of the smallest second fitness value is less than the largest second fitness value, the third fitness value corresponding to the initial test case is determined to be the largest second fitness value.

[0041] The first value is the difference between 1 and the second value, and the second value is the ratio of the current number of iterations to the first preset maximum number of iterations.

[0042] It can be understood that in different stages of iteration, in order to balance the global search capability and local improvement capability of the algorithm, when the initial test case j and the second fitness value of each initial path have both positive and negative values, it is necessary to dynamically adjust the proportion of negatively correlated initial test cases. A larger proportion is conducive to global search, and a smaller proportion is conducive to accelerating the convergence speed of the algorithm. More positively correlated initial test cases can improve the detailed search for local areas. Exemplarily, the third fitness value corresponding to the initial test case can be expressed by the following formula (3):

[0043] in, is the minimum value of the second fitness value of the actual path corresponding to the initial test case j and each initial path, is the maximum value of the second fitness value of the actual path corresponding to the initial test case j and each initial path, t is the current iteration number, is the maximum number of iterations.

[0044] Step 3: In the case that the initial path whose target population is empty exists in the set of paths to be processed, the initial path whose target population is not empty is deleted from the set of paths to be processed, and the initial population is updated by performing genetic operations on the initial test cases whose absolute values ​​of the third fitness values ​​are greater than a preset threshold, and the test run of the plugged program to be tested based on each initial test case in the initial population is returned to obtain the actual path corresponding to each initial test case, until the target population of each initial path includes at least one target test case.

[0045] The target population being empty means that the target population does not include the target test case.

[0046] It is understandable that if there are still initial paths with empty target populations in the set of paths to be processed, it is necessary to continue to determine the corresponding target test cases for these initial paths with empty target populations. Before the next iteration, it is also necessary to update the initial population, select initial test cases with higher absolute values ​​of the third fitness values ​​for crossover and mutation, so as to obtain a new initial population, and then continue searching based on the new initial population until the target population of each of the initial paths includes at least one target test case.

[0047] In addition, the traditional search algorithm only selects the individuals with the highest fitness for screening and inheritance, which easily causes more individuals to concentrate on a certain initial path, while some difficult-to-reach initial paths are more difficult to produce genetic individuals. According to the correlation coefficient calculation results, the path information of positive correlation and negative correlation can be obtained. In the embodiment of the present application, the initial test case whose absolute value of the third fitness value is greater than the preset threshold is selected for genetic operation, that is, by retaining the negatively correlated individuals, that is, selecting individuals that are completely out of sync with the trend of the initial path nodes to evolve, so that the population evolves in the direction of the path with fewer test cases, thereby generating test cases for multiple paths, improving the search capability, and solving the problem of premature convergence; at the same time, the retained positively correlated individuals can be closer to the initial path after evolution and inheritance, giving the individuals a certain self-learning ability and improving the convergence efficiency.

[0048] Optionally, the preset threshold may be dynamically adjusted in different iteration periods to further balance the global search capability and local improvement capability of the algorithm.

[0049] In one implementation, after obtaining the initial population, the method further includes at least one of the following: The initial test cases whose parameter values ​​exceed the preset range are removed from the initial population. For example, it is assumed that the range of each parameter in the initial test case is 0-1000 and the value interval is 0- -24, the precision is 1, so the initial test cases exceeding the preset range can be deleted, that is, the initial test cases are eliminated and do not participate in the search.

[0050] (2) Eliminate a first test case and a second test case from the initial population, wherein the length of the actual path corresponding to the first test case is less than the length of the shortest initial path in the initial path set, and the length of the actual path corresponding to the second test case is greater than the length of the longest initial path in the initial path set.

[0051] That is, the initial test cases corresponding to the unreachable paths are filtered out, for example, the actual paths corresponding to the initial test cases are shorter than the shortest path in the target path set, or longer than the longest path in the target path set.

[0052] In one implementation, the method of obtaining the first fitness value of the target path and the actual path corresponding to each target test case in the target population includes: testing and running the instrumented program to be tested based on each target test case in the target population to obtain the actual path corresponding to each target test case; for each target test case, determining the first fitness value of the target path and the actual path corresponding to the target test case based on the number of nodes included in the target path, the weighted value of each node in the target path, and the weighted value of each node in the actual path corresponding to the target test case.

[0053] The calculation process of the first fitness value is the same as the above-mentioned process of calculating the second fitness value, and the above-mentioned process of calculating the second fitness value may be referred to, which will not be repeated here.

[0054] In one implementation, dynamically determining the selection probability corresponding to each target test case based on the first fitness value and contribution corresponding to each target test case includes: Step 1: Obtain a first dynamic factor and a second dynamic factor.

[0055] It is understandable that the mapping distribution between the value range of the target test case and the corresponding actual path is uneven, that is, the value range of the test data of some main process paths is wider. When determining the target population, more initial test cases will be concentrated on some common target paths, while the paths of abnormal scenarios will concentrate fewer or even no population individuals, making these paths more difficult to cover. Therefore, in the iterative process of each target path, it is necessary to determine the contribution of the target test cases in each stage of the algorithm search based on dynamic factors, and then dynamically determine the selection probability corresponding to each of the target test cases.

[0056] Among them, the first dynamic factor is used to represent the target test case's own learning ability, and the learning goal is to match the target path to which it belongs as much as possible; the second dynamic factor is used to represent the social nature of the target test case, that is, the characteristic of the individual exploring the global path.

[0057] In one implementation, the obtaining of the first dynamic factor and the second dynamic factor includes: determining the first dynamic factor by using a first function based on a current number of iterations and a second preset maximum number of iterations; and determining the second dynamic factor by using a second function based on the current number of iterations and the second preset maximum number of iterations.

[0058] For example, the first dynamic factor can be expressed by the following formula (4), and the second dynamic factor can be expressed by the following formula (5):

[0059] Where t is the current iteration number, Preset maximum number of iterations for the second.

[0060] Step 2: For each of the target test cases, when the contribution corresponding to the target test case is greater than 0, determine the selection probability based on the first dynamic factor and the second dynamic factor, wherein the weight of the first dynamic factor is greater than the weight of the second dynamic factor.

[0061] Step 3: For each of the target test cases, when the contribution corresponding to the target test case is less than 0, determine the selection probability based on the first dynamic factor and the second dynamic factor, wherein the weight of the first dynamic factor is less than the weight of the second dynamic factor.

[0062] Before step 2, the method further includes: determining the contribution corresponding to the target test case. Exemplarily, the contribution can be calculated by the following formula (6):

[0063] Wherein, i represents the target test case, n is the total number of target test cases in the target population corresponding to the target path, G(i) is the first fitness value of the target test case, and when F is positive, it represents that the node trajectory of the actual path traversal has a high degree of synchronization with the target path, and can continue to cross and mutate to search for the target path. When F is negative, it represents that the node trajectory of the actual path traversal is opposite to the target path, and can continue to cross and mutate to expand the path search range and search for other more difficult to reach paths. The former will suppress population diversity in the early stage of evolution, and the latter will destroy better individuals in the later stage of evolution. Therefore, in an embodiment of the present application, a dynamic factor is introduced to avoid the disadvantage of determining the selection probability only by contribution. Exemplarily, the selection probability can be expressed by the following formula (7):

[0064] Where D(i) is the first fitness value of the target test case i, 0- Contribution is a positive target test case, -n is the contribution It is a negative target test case. From the above, we can see that by dynamically adjusting the value of the learning factor, the group can expand the search range in a short time and quickly search for the optimal value in the early stage of evolution, and can quickly and accurately converge to the optimal solution in the later stage of evolution.

[0065] In one implementation, the stop condition includes one of the following: (1) Obtaining the actual path that completely matches the target path from the actual path corresponding to each target test case.

[0066] That is to say, the iterative search process of a target path stops only when the actual path corresponding to a target test case is found to completely match the target path.

[0067] It can be understood that the target path is any initial path of the initial path set. In each iteration cycle, each initial path of the initial path set is iterated based on the corresponding target population to determine the best matching target test case. In a certain iteration cycle, if a first initial path in the initial path set searches for a completely matching actual path, the optimal target test case can be determined. In the middle and later stages of subsequent iterations, the first initial path stops iterating. If there is still a second initial path in the initial path set that has not searched for a completely matching actual path after the end of the current iteration cycle, the search will continue in the next iteration cycle.

[0068] (2) The current number of iterations reaches the second preset maximum number of iterations.

[0069] It is understandable that if the current number of iterations has reached the second preset maximum number of iterations, and there is still an initial path in the initial path set that has not been searched for a completely matched actual path, the iteration is stopped. Assuming that there are m branch paths and n input parameters in the program to be tested, the branch function expression is: (x1, x2, ...xn),... (x1, x2, ..., xn), if a target test case passes through the i-th target path in the running program, then define =1, that is, the fitness is 1, otherwise = 0. The solution goal of this problem is to maximize the coverage of the test path. The corresponding evaluation function can be determined by the following formula (8):

[0070] When the evaluation function is m, or the current number of iterations reaches the second preset maximum number of iterations, the iteration is stopped.

[0071] Based on the above embodiments, the present application also provides another flow chart of the test data generation method, such as Figure 3 As shown, the following steps may be included: S310: Analyzing the tested environment.

[0072] In this step, the program under test is statically combed to obtain a program flow chart, the program flow chart is encoded to obtain a target path set, and the program is instrumented in the target path set to obtain an instrumented test program.

[0073] S320: Test and run the instrumented test program based on the test data parameters.

[0074] In this step, the input data is binary encoded and the binary encoded input data is obfuscated and initialized to obtain an initial input parameter cluster. The initial input parameter cluster is then decoded to obtain test data parameters, and the instrumented test program is tested and run based on the test data parameters to obtain tracking information.

[0075] S330: Searching for test data of each target path.

[0076] In this step, individuals in the abnormal input and unreachable path clusters are filtered iteratively, and the dynamic fitness values ​​of the individuals in the filtered clusters are calculated according to the pre-built objective function and tracking information. Based on the fitness value, it is determined whether the paths in the target path set match, and the matching paths are saved as test sets and the path set is updated. For unmatched paths, the selection probability is weighted by dynamic factors to select, cross, and mutate the clusters to optimize the test parameters until the optimal solution is obtained or the limited number of runs is reached. Finally, for each target path in the target path set, test data is selected from the test set, and different test data are combined to output a test data set covering all target paths.

[0077] Figure 4 A schematic diagram of the structure of a test case generation device provided in an embodiment of this specification is shown, such as Figure 4 As shown, the test case generation device 400 may include: an acquisition module 410, a determination module 420, a selection module 430 and a processing module 440. In this embodiment, the acquisition module 410 is used to obtain the first fitness value of the actual path corresponding to the target path and each target test case in the target population; the first determination module 420 is used to dynamically determine the selection probability corresponding to each target test case based on the first fitness value and contribution corresponding to each target test case; the selection module 430 is used to select a preset number of the target test cases from the target population for genetic operation according to the selection probability corresponding to each target test case, and obtain the next generation population; the processing module 440 is used to update the target population based on the next generation population, and return to execute the acquisition of the first fitness value of the actual path corresponding to the target path and each target test case in the target population until the stop condition is met, and output the target test case that meets the target path.

[0078] In one implementation, the test case generation device 400 may further include: an analysis module, configured to obtain a program flow chart by analyzing the program to be tested; a second determination module, configured to determine an initial path set corresponding to the program to be tested according to the program flow chart, wherein the initial path set includes at least one initial path, and the target path is any of the initial paths in the at least one initial path. In one implementation, the test case generation device 400 may also include: a third determination module, used to obtain an initial population, wherein the initial population includes multiple initial test cases; by copying the initial path set, a set of paths to be processed is obtained; based on the initial population, by determining the target test case corresponding to each of the initial paths in the set of paths to be processed, the target population corresponding to each of the initial paths is obtained.

[0079] In one implementation, the third determination module is further specifically used to: test and run the instrumented program to be tested based on each of the initial test cases in the initial population to obtain an actual path corresponding to each of the initial test cases; for each of the initial test cases, obtain the second fitness value of the actual path and each of the initial paths, determine the third fitness value corresponding to the initial test case based on each of the second fitness values, and add the initial test case as the target test case to the target population of the initial path corresponding to the third fitness value; in the case where there are initial paths whose target population is empty in the set of paths to be processed, delete the initial paths whose target population is not empty from the set of paths to be processed, and update the initial population by performing genetic operations on the initial test cases whose absolute values ​​of the third fitness values ​​are greater than a preset threshold, and return to execute the test and run of the instrumented program to be tested based on each of the initial test cases in the initial population to obtain the actual path corresponding to each of the initial test cases, until the target population of each of the initial paths includes at least one of the target test cases.

[0080] In one implementation, the third determination module is also specifically used for one of the following: when each of the second fitness values ​​is greater than 0, determining that the third fitness value corresponding to the initial test case is the largest second fitness value; when each of the second fitness values ​​is less than 0, determining that the third fitness value corresponding to the initial test case is the absolute value of the smallest second fitness value; when there is a second fitness value greater than 0 and a second fitness value less than 0 among each of the second fitness values, when the product of the first value and the absolute value of the smallest second fitness value is greater than the largest second fitness, determining that the third fitness value corresponding to the initial test case is the absolute value of the smallest second fitness value; when there is a second fitness value greater than 0 and a second fitness value less than 0 among each of the second fitness values, when the product of the first value and the absolute value of the smallest second fitness value is less than the largest second fitness, determining that the third fitness value corresponding to the initial test case is the largest second fitness value; wherein, the first value is the difference between 1 and the second value, and the second value is the ratio of the current number of iterations to the first preset maximum number of iterations.

[0081] In one implementation, the third determination module is further specifically used to: eliminate the initial test cases whose parameter values ​​exceed a preset range from the initial population; eliminate the first test case and the second test case from the initial population, wherein the length of the actual path corresponding to the first test case is less than the length of the shortest initial path in the initial path set, and the length of the actual path corresponding to the second test case is greater than the length of the longest initial path in the initial path set.

[0082] In one implementation, the acquisition module 410 is specifically used to: test and run the plugged program to be tested based on each target test case in the target population to obtain the actual path corresponding to each target test case; for each target test case, based on the number of nodes included in the target path, the weighted value of each node in the target path, and the weighted value of each node in the actual path corresponding to the target test case, determine the first fitness value of the target path and the actual path corresponding to the target test case.

[0083] In one implementation, the determination module 430 is specifically used to obtain a first dynamic factor and a second dynamic factor; for each of the target test cases, when the contribution corresponding to the target test case is greater than 0, the selection probability is determined based on the first dynamic factor and the second dynamic factor, wherein the weight of the first dynamic factor is greater than the weight of the second dynamic factor; for each of the target test cases, when the contribution corresponding to the target test case is less than 0, the selection probability is determined based on the first dynamic factor and the second dynamic factor, wherein the weight of the first dynamic factor is less than the weight of the second dynamic factor.

[0084] In one implementation, the determination module 430 is specifically used to: determine the first dynamic factor by using a first function based on the current number of iterations and the second preset maximum number of iterations; determine the second dynamic factor by using a second function based on the current number of iterations and the second preset maximum number of iterations.

[0085] In one implementation, the stop condition includes one of the following: obtaining the actual path that completely matches the target path from the actual path corresponding to each of the target test cases; the current number of iterations reaches a second preset maximum number of iterations.

[0086] The test case generation device provided in the embodiment of the present application can realize Figure 1 To avoid repetition, the various processes implemented in the illustrated method embodiment will not be described again here.

[0087] A test case generation device in the embodiment of the present application may be a device, or a component, an integrated circuit, or a chip in an electronic device. The embodiment of the present application is not specifically limited.

[0088] A test case generation device in an embodiment of the present application may be a device having an operating system. The operating system may be an Android operating system, an iOS operating system, or other possible operating systems, which are not specifically limited in the embodiment of the present application.

[0089] Optional, such as Figure 5 As shown, an embodiment of the present application also provides an electronic device 500, including a processor 510, a memory 520, and a program or instruction stored in the memory 520 and executable on the processor 510. When the program or instruction is executed by the processor 510, each process of the above-mentioned test case generation method embodiment is implemented, and the same technical effect can be achieved. To avoid repetition, it will not be repeated here.

[0090] An embodiment of the present application also provides a computer-readable storage medium, on which a program or instruction is stored. When the program or instruction is executed by a processor, the various processes of the above-mentioned test case generation method embodiment are implemented, and the same technical effect can be achieved. To avoid repetition, it will not be repeated here.

[0091] The processor is the processor in the electronic device described in the above embodiment. The readable storage medium includes a computer readable storage medium, such as a computer read-only memory (ROM), a random access memory (RAM), a magnetic disk or an optical disk.

[0092] An embodiment of the present application further provides a chip, which includes a processor and a communication interface, wherein the communication interface is coupled to the processor, and the processor is used to run programs or instructions to implement the various processes of the above-mentioned test case generation method embodiment, and can achieve the same technical effect. To avoid repetition, it will not be repeated here.

[0093] It should be understood that the chip mentioned in the embodiments of the present application can also be called a system-level chip, a system chip, a chip system or a system-on-chip chip, etc.

[0094] An embodiment of the present application also provides a computer program product, which includes at least one computer program. When the computer program is loaded and executed by a processor, it implements the various processes of the above-mentioned test case generation method embodiment and can achieve the same technical effect. To avoid repetition, it will not be repeated here.

[0095] It should be noted that, in this article, the terms "comprise", "include" or any other variant thereof are intended to cover non-exclusive inclusion, so that the process, method, article or device including a series of elements includes not only those elements, but also includes other elements not explicitly listed, or also includes elements inherent to such process, method, article or device. In the absence of further restrictions, the elements defined by the sentence "comprise one..." do not exclude the presence of other identical elements in the process, method, article or device including the element. In addition, it should be pointed out that the scope of the methods and devices in the embodiments of the present application is not limited to performing functions in the order shown or discussed, and may also include performing functions in a substantially simultaneous manner or in reverse order according to the functions involved, for example, the described method may be performed in an order different from that described, and various steps may also be added, omitted, or combined. In addition, the features described with reference to certain examples may be combined in other examples.

[0096] Through the description of the above implementation methods, those skilled in the art can clearly understand that the above-mentioned embodiment methods can be implemented by means of software plus a necessary general hardware platform, and of course by hardware, but in many cases the former is a better implementation method. Based on such an understanding, the technical solution of the present application, or the part that contributes to the prior art, can be embodied in the form of a software product, which is stored in a storage medium (such as ROM / RAM, a magnetic disk, or an optical disk), and includes a number of instructions for a terminal (which can be a mobile phone, a computer, a server, an air conditioner, or a network device, etc.) to execute the methods described in each embodiment of the present application.

[0097] The embodiments of the present application are described above in conjunction with the accompanying drawings, but the present application is not limited to the above-mentioned specific implementation methods. The above-mentioned specific implementation methods are merely illustrative and not restrictive. Under the guidance of the present application, ordinary technicians in this field can also make many forms without departing from the purpose of the present application and the scope of protection of the claims, all of which are within the protection of the present application.

Claims

1. A method for generating a test case, characterized in that: The method comprises: Obtain a first fitness value of the target path and an actual path corresponding to each target test case in the target population; Dynamically determine the selection probability corresponding to each target test case based on the first fitness value and contribution corresponding to each target test case; According to the selection probability corresponding to each of the target test cases, a preset number of the target test cases are selected from the target population for genetic operation to obtain a next generation population; The target population is updated based on the next generation population, and the first fitness value of the actual path corresponding to the target path and each target test case in the target population is returned to execute until a stop condition is met, and the target test case that meets the target path is output.

2. The method according to claim 1, characterized in that Before obtaining the first fitness value of the target path and the actual path corresponding to each target test case in the target population, the method further includes: By analyzing the program to be tested, a program flow chart is obtained; According to the program flow chart, an initial path set corresponding to the program to be tested is determined, wherein the initial path set includes at least one initial path, and the target path is any initial path in the at least one initial path.

3. The method according to claim 2, characterized in that Before obtaining the first fitness value of the target path and the actual path corresponding to each target test case in the target population, the method further includes: Acquire an initial population, wherein the initial population includes a plurality of initial test cases; Obtaining a to-be-processed path set by copying the initial path set; Based on the initial population, the target test case corresponding to each initial path in the set of paths to be processed is determined to obtain the target population corresponding to each initial path.

4. The method according to claim 3, characterized in that The step of obtaining the target population corresponding to each of the initial paths by determining the target test case corresponding to each of the initial paths in the set of paths to be processed based on the multiple initial test cases includes: Based on each of the initial test cases in the initial population, the instrumented program to be tested is tested and run to obtain an actual path corresponding to each of the initial test cases; For each of the initial test cases, obtaining the second fitness values ​​of the actual path and each of the initial paths, determining the third fitness value corresponding to the initial test case based on each of the second fitness values, and adding the initial test case as the target test case to the target population of the initial path corresponding to the third fitness value; In the case that the initial path whose target population is empty exists in the set of paths to be processed, the initial path whose target population is not empty is deleted from the set of paths to be processed, and the initial population is updated by performing genetic operations on the initial test cases whose absolute values ​​of the third fitness values ​​are greater than a preset threshold, and the test run of the plugged program to be tested based on each of the initial test cases in the initial population is returned to obtain the actual path corresponding to each of the initial test cases, until the target population of each of the initial paths includes at least one of the target test cases.

5. The method according to claim 4, characterized in that The determining, based on each of the second fitness values, a third fitness value corresponding to the initial test case comprises one of the following: When all the second fitness values ​​are greater than 0, determining that the third fitness value corresponding to the initial test case is the largest second fitness value; When all the second fitness values ​​are less than 0, determining that the third fitness value corresponding to the initial test case is the absolute value of the smallest second fitness value; In the case where there are second fitness values ​​greater than 0 and second fitness values ​​less than 0 among the second fitness values, when the product of the first value and the absolute value of the smallest second fitness value is greater than the largest second fitness value, determining that the third fitness value corresponding to the initial test case is the absolute value of the smallest second fitness value; In the case where there are second fitness values ​​greater than 0 and second fitness values ​​less than 0 among the second fitness values, when the product of the first value and the absolute value of the smallest second fitness value is less than the largest second fitness value, determining that the third fitness value corresponding to the initial test case is the largest second fitness value; The first value is the difference between 1 and the second value, and the second value is the ratio of the current number of iterations to the first preset maximum number of iterations.

6. The method according to claim 3, characterized in that After obtaining the initial population, the method further includes at least one of the following: Eliminating the initial test cases whose parameter values ​​exceed a preset range from the initial population; A first test case and a second test case are eliminated from the initial population, wherein the length of the actual path corresponding to the first test case is smaller than the length of the shortest initial path in the initial path set, and the length of the actual path corresponding to the second test case is larger than the length of the longest initial path in the initial path set.

7. The method according to claim 1, characterized in that The step of obtaining a first fitness value of an actual path corresponding to a target path and each target test case in a target population includes: Based on each of the target test cases in the target population, the instrumented program to be tested is tested and run to obtain an actual path corresponding to each of the target test cases; For each of the target test cases, determine the first fitness value of the target path and the actual path corresponding to the target test case based on the number of nodes included in the target path, the weighted value of each node in the target path, and the weighted value of each node in the actual path corresponding to the target test case.

8. The method according to claim 1, characterized in that The dynamically determining the selection probability corresponding to each target test case based on the first fitness value and contribution corresponding to each target test case includes: Obtain a first dynamic factor and a second dynamic factor; For each of the target test cases, when the contribution corresponding to the target test case is greater than 0, determining the selection probability based on the first dynamic factor and the second dynamic factor, wherein the weight of the first dynamic factor is greater than the weight of the second dynamic factor; For each of the target test cases, when the contribution corresponding to the target test case is less than 0, the selection probability is determined based on the first dynamic factor and the second dynamic factor, wherein the weight of the first dynamic factor is less than the weight of the second dynamic factor.

9. The method according to claim 8, characterized in that The obtaining of the first dynamic factor and the second dynamic factor comprises: Determining the first dynamic factor by using a first function based on a current number of iterations and a second preset maximum number of iterations; The second dynamic factor is determined by using a second function based on the current iteration number and the second preset maximum iteration number.

10. The method according to claim 1, characterized in that The stop condition includes one of the following: Acquire the actual path that completely matches the target path from the actual path corresponding to each target test case; The current number of iterations reaches the second preset maximum number of iterations.

11. A test case generation device, characterized in that: include: An acquisition module, used for acquiring a first fitness value of an actual path corresponding to a target path and each target test case in a target population; A first determination module, configured to dynamically determine a selection probability corresponding to each of the target test cases based on the first fitness value and contribution corresponding to each of the target test cases; A selection module, configured to select a preset number of the target test cases from the target population for genetic operation according to the selection probability corresponding to each of the target test cases, to obtain a next generation population; A processing module is used to update the target population based on the next generation population, and return to execute the first fitness value of the actual path corresponding to the target path and each target test case in the target population until the stop condition is met, and output the target test case that meets the target path.

12. An electronic device, characterized in that: The invention comprises a processor, a memory and a program or instruction stored in the memory and executable on the processor, wherein the program or instruction, when executed by the processor, implements the steps of the test case generation method as described in any one of claims 1 to 10.

13. A readable storage medium, characterized in that: The readable storage medium stores a program or instruction, and when the program or instruction is executed by a processor, the steps of the test case generation method according to any one of claims 1 to 10 are implemented.

14. A computer program product, characterized in that The computer program product includes program instructions, and when the program instructions are executed by a computer, the computer implements the steps of the test case generation method according to any one of claims 1 to 10.

Citation Information

Patent Citations

  • Test case generation method and device based on improved genetic algorithm

    CN116450494A

  • Method and system for quickly positioning and detecting taint data based on IAST

    CN117349837A

  • Multi-path coverage test case generation method and system

    CN117632770A

  • Variation test generation method and system and computer storage medium

    CN117951001A

Cited By

  • Rapid spectrum detection method for veterinary drug residues

    CN120577247A