Code coverage rate testing method and device, electronic equipment and storage medium

Through niche genetic algorithm and breadth-first search algorithm to optimize code coverage testing, the problems of inefficient testing and invalid test cases in the existing technology are solved, and the effect of rapidly improving coverage and efficiency is achieved.

CN119127664APending Publication Date: 2024-12-13SHANDONG YUNHAI GUOCHUANG CLOUD COMPUTING EQUIP IND INNOVATION CENT CO LTD
View PDF 0 Cites 1 Cited by

Patent Information

Application Number
CN202411037938.0
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-07-31
Publication Date
2024-12-13

AI Technical Summary

Technical Problem

In the prior art, the testing method with code coverage as the optimization goal is inefficient in the later stage of the test, and is prone to fall into the local optimal solution and produces a large number of invalid test cases.

Method used

A niche genetic algorithm and breadth-first search algorithm are used to build a condition network through static analysis, determine uncovered conditional sub-items combination entries, and generate new test cases based on this, and iterate the code coverage until the preset threshold is reached.

Benefits of technology

Rapidly improve code coverage, avoid local optimal solutions, reduce the number of invalid test cases, and improve testing efficiency.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119127664A_ABST
    Figure CN119127664A_ABST
Patent Text Reader

Abstract

The invention provides a code coverage rate testing method and device, electronic equipment and a storage medium, and relates to the technical field of software test.The method comprises the steps that at least one condition subitem combination entry corresponding to a to-be-tested code is determined; iteratively determining a first total coverage rate corresponding to all the first test cases by using a niche genetic algorithm and all the first test cases in the first initial population and taking the coverage rate as an optimization target; under the condition that a target test parameter corresponding to the first total coverage rate meets a first iteration stop condition, determining an uncovered item of the condition subitem from the condition subitem combination item; determining a second test case in a second initial population based on the condition that the conditional subitems do not cover entries; and iteratively determining a second total coverage rate corresponding to all second test cases by utilizing the niche genetic algorithm and all the second test cases in the second initial population and by taking the coverage of items which are not covered by the condition subitems as an optimization target, and stopping iteration until the second total coverage rate reaches a first preset threshold value. According to the method and the device, the number of invalid test cases generated due to inaccurate target search can be greatly reduced, and the test efficiency in the later test period is improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the technical field of software testing, and in particular to a code coverage testing method, device, electronic equipment and storage medium. Background Art

[0002] After the code design is completed, a lot of testing is required to minimize the possibility of exceptions during code operation and improve the stability of code operation. Code coverage testing is the most commonly used testing method, including code line coverage testing, branch coverage testing, jump coverage testing, and condition coverage testing. During testing, by running a series of test cases, the test environment automatically counts each coverage. If the coverage does not meet the requirements, new test cases need to be added to improve the coverage until the coverage meets the requirements or the maximum number of runs is reached. In order to reduce resource consumption during code testing, an intelligent search algorithm is generally used to automatically generate appropriate test cases based on the historical running status of the code, so that the coverage reaches the target faster.

[0003] In the prior art, commonly used intelligent search algorithms mainly include genetic algorithms and their derivative algorithms, and use code coverage as the optimization target to automatically generate test cases for coverage testing. However, the test method with code coverage as the optimization target has low test efficiency in the later stage of testing, and is prone to fall into local optimal solutions and generate a large number of invalid test cases. Summary of the invention

[0004] The present invention provides a code coverage testing method, device, electronic device and storage medium, which are used to solve the defects of the prior art that the testing method with code coverage as the optimization target has low testing efficiency in the later stage of testing, is prone to fall into the local optimal solution and generates a large number of invalid test cases.

[0005] The present invention provides a code coverage testing method, comprising: Determine at least one conditional sub-item combination entry corresponding to the code to be tested; Using the niche genetic algorithm and all the first test cases in the first initial population, with coverage as an optimization target, iteratively determine a first total coverage corresponding to all the first test cases; until the target test parameter corresponding to the first total coverage satisfies a first iteration stop condition, determine the conditional sub-item uncovered items from the conditional sub-item combination items; Determining a second test case in a second initial population based on the conditional sub-item not covering the entry; Utilizing the niche genetic algorithm and all second test cases in the second initial population, with covering uncovered entries of the conditional sub-items as the optimization goal, iteratively determine the second total coverage rate corresponding to all the second test cases, and stop iterating when the second total coverage rate reaches a first preset threshold.

[0006] According to the code coverage testing method provided by the present invention, determining the second test case in the second initial population based on the uncovered entry of the conditional sub-item includes: Determine a target conditional branch from the target conditional sub-item uncovered entries; Static analysis is performed on the code to be tested to construct a conditional network corresponding to the target conditional branch; the conditional network is used to characterize the association relationship between the test cases involved in the iteration, at least one conditional node related to the target conditional branch, and the target conditional branch; Based on the conditional net, a second test case in a second initial population is determined.

[0007] According to the code coverage testing method provided by the present invention, determining the second test case in the second initial population based on the conditional network includes: Traversing each of the conditional nodes in the conditional network in ascending order of distance from the target conditional branch; Utilizing a breadth-first search algorithm, based on the test cases participating in the iteration, determine the third test cases covering each of the conditional nodes; stop traversing until the total number of third test cases of the traversed conditional nodes reaches a second preset threshold, and determine all third test cases corresponding to the traversed conditional nodes as the second test cases of the second initial population.

[0008] According to the code coverage testing method provided by the present invention, the determining of the second test case in the second initial population based on the conditional net further includes: When the total number of third test cases corresponding to all conditional nodes in the conditional network is less than the second preset threshold, determining the remaining number based on the total number of third test cases corresponding to all conditional nodes and the second preset threshold; Using a constrained random algorithm, randomly generate the remaining number of fourth test cases from the remaining test cases, where the remaining test cases are other test cases in the test cases participating in the iteration except all the third test cases; All third test cases and all fourth test cases are determined as second test cases of the second initial population.

[0009] According to the code coverage testing method provided by the present invention, the step of determining at least one conditional sub-item combination entry corresponding to the code to be tested includes: Identifying a first condition and a branch in the code to be tested; converting the branch to at least one second condition; Based on all the first conditions and all the second conditions, determining at least one condition item corresponding to the code to be tested; Based on the satisfaction of all the condition sub-items included in each of the condition items, a condition sub-item combination entry corresponding to each of the condition items is determined.

[0010] According to the code coverage testing method provided by the present invention, the method utilizes the niche genetic algorithm and all the first test cases in the first initial population, takes coverage as an optimization target, and iteratively determines the first total coverage corresponding to all the first test cases, including: S1, injecting all the first test cases into the code to be tested, and running the code to be tested in the DUT test environment to determine the coverage rate corresponding to each of the first test cases in the current iteration round; S2. Determine the individual fitness of each of the first test cases in the current iteration round based on the coverage rate corresponding to each of the first test cases and the historical total coverage rate corresponding to the previous iteration round; S3, using the niche genetic algorithm, based on the coverage entry values ​​corresponding to any two first test cases, updating the individual fitness of each of the first test cases in the current iteration round; S4. Determine a first total coverage rate corresponding to the current iteration round based on the coverage rates corresponding to each of the first test cases in the current iteration round.

[0011] S5. When the target test parameters corresponding to the first total coverage rate do not meet the first iteration stop condition, generate a third initial population and use the third initial population as the new first initial population; repeat steps S1 to S5 until the iteration is stopped when the target test parameters corresponding to the first total coverage rate meet the first iteration stop condition.

[0012] The present invention also provides a code coverage testing device, comprising: A first determination module, used to determine at least one conditional sub-item combination entry corresponding to the code to be tested; A first iteration module is used to use the niche genetic algorithm and all the first test cases in the first initial population, and to iteratively determine a first total coverage rate corresponding to all the first test cases with coverage rate as an optimization target; until a target test parameter corresponding to the first total coverage rate satisfies a first iteration stop condition, determining condition sub-item uncovered items from the condition sub-item combination items; A second determination module, configured to determine a second test case in a second initial population based on the condition sub-item not covering the entry; A second iteration module is used to utilize the niche genetic algorithm and all second test cases in the second initial population, to iteratively determine a second total coverage rate corresponding to all the second test cases with the goal of covering uncovered items of the conditional sub-items, and to stop the iteration when the second total coverage rate reaches a first preset threshold.

[0013] The present invention also provides an electronic device, comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein when the processor executes the computer program, any of the code coverage testing methods described above is implemented.

[0014] The present invention also provides a non-transitory computer-readable storage medium having a computer program stored thereon, and when the computer program is executed by a processor, the code coverage testing method as described above is implemented.

[0015] The present invention also provides a computer program product, comprising a computer program, wherein when the computer program is executed by a processor, the computer program implements any of the above-mentioned code coverage testing methods.

[0016] The code coverage test method, device, electronic device and storage medium provided by the present invention, after determining at least one conditional sub-item combination entry corresponding to the code to be tested, use a niche genetic algorithm and all first test cases, with coverage as the optimization goal, to perform an iterative test of the code coverage of the first stage on the code to be tested, iteratively determine the first total coverage corresponding to each iteration round to quickly improve the coverage, and determine the iteration termination time of the first stage by judging whether the target test parameter corresponding to the first total coverage meets the first iteration stop condition, determine the uncovered conditional sub-item uncovered entries from all conditional sub-item combination entries, and determine the optimal second test case based on the conditional sub-item uncovered entries, and again use the niche genetic algorithm and the second test case, with covering the conditional sub-item uncovered entries as the optimization goal, to perform an iterative test of the code coverage of the second stage on the code to be tested, iteratively determine the second total coverage corresponding to each iteration round, until the second total coverage reaches a first preset threshold, the iteration is stopped and the code coverage test is completed. In an embodiment of the present invention, the coverage rate can be quickly improved by iterative testing with coverage as the optimization goal in the first stage, and when falling into a local optimal solution in the first stage, it can quickly jump out and enter the second stage of iterative testing with coverage of uncovered items of conditional sub-items as the optimization goal. By adjusting the optimization goal in the second stage, the number of invalid test cases generated by inaccurate search targets can be greatly reduced, thereby improving the test efficiency in the later stage of testing. BRIEF DESCRIPTION OF THE DRAWINGS

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

[0018] Figure 1 This is one of the flow charts of the code coverage testing method provided by an embodiment of the present invention.

[0019] Figure 2 This is the second flowchart of the code coverage testing method provided by the embodiment of the present invention.

[0020] Figure 3 Schematic diagram of a conditional network provided by an embodiment of the present invention.

[0021] Figure 4 It is a schematic diagram of the structure of a code coverage testing device provided by an embodiment of the present invention.

[0022] Figure 5 It is a schematic diagram of the structure of an electronic device provided by an embodiment of the present invention. DETAILED DESCRIPTION

[0023] In order to make the purpose, technical solution and advantages of the present invention clearer, the technical solution of the present invention will be clearly and completely described below in conjunction with the drawings of the present invention. Obviously, the described embodiments are part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.

[0024] Aiming at the problem that the test method with code coverage as the optimization target in the prior art has low test efficiency in the later stage of testing and is prone to fall into the local optimal solution and generate a large number of invalid test cases, the embodiment of the present invention provides a code coverage test method. Figure 1 This is one of the flow charts of the code coverage testing method provided by the embodiment of the present invention. Figure 1 As shown, the code coverage testing method includes the following steps 110 to 140.

[0025] Step 110: Determine at least one conditional sub-item combination entry corresponding to the code to be tested.

[0026] Specifically, the code controls the execution path through a series of conditions (if-elseif-else) and branches (switch). If all conditions and branches are covered, it means that the code line is also completely covered. Therefore, in the embodiment of the present invention, after determining the code to be tested, at least one condition item included in the code to be tested is identified, and the condition sub-item combination entries corresponding to each condition item are determined. Based on the condition sub-item combination entries corresponding to all condition items, all control code paths that meet different situations included in the code to be tested can be determined. By subsequently determining the coverage of the condition sub-item combination entries, the code coverage test status of the code to be tested can be determined.

[0027] Further, the determining of at least one conditional sub-item combination entry corresponding to the code to be tested includes: Identifying a first condition and a branch in the code to be tested; converting the branch to at least one second condition; Based on all the first conditions and all the second conditions, determining at least one condition item corresponding to the code to be tested; Based on the satisfaction of all the condition sub-items included in each of the condition items, a condition sub-item combination entry corresponding to each of the condition items is determined.

[0028] Specifically, after obtaining the code to be tested, according to the coding structure of the code to be tested, the first condition and branch included in the code to be tested are identified, the first condition refers to the if-elseif-else code structure, and the branch refers to the switch code structure. After identifying the first condition and the branch, the first condition includes an if condition and an elseif condition, and the if condition and the elseif condition can be respectively generated into corresponding conditional items.

[0029] For example, the code corresponding to the first condition is as follows: "If (a>0·&&·c<9)·then

[0030] code

[0031] elseif (b>7)then

[0032] code

[0033] else

[0034] code".

[0035] It can be identified that the first condition includes an if condition and an elseif condition, and the if condition is "If (a>0·&&·c<9)", and the elseif condition is "elseif (b>7)". At this time, the if condition can be directly converted into the conditional item [(a>0), (c<9)], and the elseif condition can be directly converted into the conditional item [(b>7)].

[0036] It should be noted that each conditional item includes at least one conditional sub-item, which is a decomposition of the conditional item and is separated by logical relationship symbols. In this way, each conditional sub-item only restricts one variable, and no logical relationship is distinguished between the conditional sub-items. For example, the if condition (a>0·&&·c<9) is converted into the conditional item [(a>0),(c<9)], where the conditional sub-item (a>0) and the conditional sub-item (c<9) are separated by “,”, and there is no logical relationship “&&” between the two conditional sub-items. For another example, the first condition is: (a>0)&& (b>5&&b 10)||(c==7), the conditional items after converting the first condition are: [(a>0)、(b>5)、(b 10), (c==7)], where there is no logical relationship “&&” and “||” between the conditional sub-items.

[0037] After the branch is identified, each layer structure in the branch can be converted into an equivalent second condition, that is, each layer structure in the switch code structure is converted into a corresponding if-elseif-else code structure, and then each second condition is directly converted into a conditional sub-item.

[0038] For example, the code corresponding to the branch is as follows: “Switch a do

[0039] case 1 do

[0040] code

[0041] case 2 do

[0042] code

[0043] case 3·do

[0044] code

[0045] otherwise do".

[0046] From the above code, we can identify that the branch includes three layers of structure. After converting the three structures into equivalent second conditions, we can get the following code: “If a==1·then

[0047] code

[0048] elseif a==2 then

[0049] code

[0050] elseif a==3 then

[0051] code

[0052] else

[0053] code".

[0054] From the above code, we can identify one if condition and two elseif conditions, where the if condition is "If a==1", and the two elseif conditions are "elseif a==2" and "elseif a==3". At this point, the if condition can be directly converted to the conditional item [(a==1)], and the two elseif conditions can be converted to the conditional items [(a==2)] and [(a==3)] respectively.

[0055] After identifying all the condition items corresponding to the code to be tested, for each condition item, determine the condition sub-item combination item corresponding to the condition item according to the satisfaction of each condition sub-item in the condition item. For example, the condition items are [(a>0)、(b>5)、(b 10), (c==7)] as an example, the condition item includes 4 condition sub-items, each of which has two conditions: satisfied and not satisfied. Therefore, there are 16 combinations in the condition item, namely, 2 4 = 16. At this point, it can be determined that there are 16 conditional sub-item combination entries in the conditional item. In each subsequent iteration of the code coverage test, it is necessary to determine whether the 16 conditional sub-item combination entries are covered.

[0056] Step 120, using the niche genetic algorithm and all the first test cases in the first initial population, with coverage as the optimization target, iteratively determine the first total coverage corresponding to all the first test cases; until the target test parameter corresponding to the first total coverage satisfies the first iteration stop condition, determine the conditional sub-item uncovered items from the conditional sub-item combination items.

[0057] Specifically, Figure 2 FIG. 2 is a flow chart of the code coverage testing method provided by an embodiment of the present invention. Figure 2As shown, after determining all the conditional sub-item combination entries corresponding to the code to be tested, the code coverage test of the first stage is entered. In the first stage, with coverage as the optimization target, after determining all the first test cases in the first initial population, the code coverage iterative test is performed using the niche genetic algorithm and all the first test cases. The niche genetic algorithm can automatically generate new first test cases in each iteration round to achieve a rapid increase in coverage. Until the target test parameter corresponding to the first total coverage meets the first iteration stop condition, it indicates that it has fallen into the local optimal solution at this time. The test method with coverage as the optimization target generates a large number of invalid test cases. For the conditional sub-item combination entries at the corner positions in the test space, the invalid test case cannot be covered and the code coverage cannot be further increased. At this time, the conditional sub-item uncovered entries can be determined from all the conditional sub-item combination entries, and the code coverage test of the first stage is jumped out to avoid falling into the local optimal solution and never being able to cover the conditional sub-item uncovered entries, that is, the code coverage test of the code to be tested cannot be completed.

[0058] It should be noted that the target test parameters corresponding to the first total coverage rate may include the number of iterations or the total coverage rate change value, and the total coverage rate change value is the absolute value of the difference between the first total coverage rate corresponding to the current iteration round and the previous total coverage rate corresponding to the previous iteration round.

[0059] In addition, the first iteration stop condition may include that the number of iterations reaches the first stage threshold value, or the number of times the total coverage change value is lower than the third preset threshold value reaches the number threshold value. If the first iteration stop condition is met, the code coverage test of the first stage is stopped and the code coverage test of the second stage is entered. The first stage threshold value is the preset number of iterations threshold value corresponding to the first stage, which can be set according to experience, for example, it can be 100, 200 or 500. The third preset threshold value is a minimum value, which is used to reflect that the coverage change between two adjacent iterations is very small. The third preset threshold value can be set according to experience, for example, it can be 0.1, 0.15 or 0.2. The number threshold value is used to reflect that the total coverage change value is small in the number threshold number of iterations. The number threshold value can be set according to experience, for example, it can be 3, 5 or 7.

[0060] After determining all conditional sub-item combination entries corresponding to the code to be tested, the niche genetic algorithm can be initialized, that is, the population size in the niche genetic algorithm, the input parameter encoding method, the selection of the initial genetic algorithm, the method and parameters of the crossover and mutation operations, and the shared niche distance parameters are initialized. Afterwards, the first initial population, the population size M of the first initial population, and the first test case in the first initial population can be determined based on the constrained random algorithm.

[0061] Furthermore, if Figure 2As shown, the method utilizes the niche genetic algorithm and all the first test cases in the first initial population, takes coverage as an optimization target, and iteratively determines the first total coverage corresponding to all the first test cases, including: S1, injecting all the first test cases into the code to be tested, and running the code to be tested in the DUT test environment to determine the coverage rate corresponding to each of the first test cases in the current iteration round; S2. Determine the individual fitness of each of the first test cases in the current iteration round based on the coverage rate corresponding to each of the first test cases and the historical total coverage rate corresponding to the previous iteration round; S3, using the niche genetic algorithm, based on the coverage entry values ​​corresponding to any two first test cases, updating the individual fitness of each of the first test cases in the current iteration round; S4. Determine a first total coverage rate corresponding to the current iteration round based on the coverage rates corresponding to each of the first test cases in the current iteration round.

[0062] S5. When the target test parameters corresponding to the first total coverage rate do not meet the first iteration stop condition, generate a third initial population and use the third initial population as the new first initial population; repeat steps S1 to S5 until the iteration is stopped when the target test parameters corresponding to the first total coverage rate meet the first iteration stop condition.

[0063] Specifically, after initializing the niche genetic algorithm, all first test cases can be injected into the code to be tested, and the code to be tested can be run in the DUT (Design Under Test) test environment. During the iteration process, the condition item coverage of each individual in the current iteration round is recorded, that is, the coverage of each condition sub-item combination entry of each individual in the current iteration round is recorded. The coverage can be stored in the memory or file, and the coverage rate corresponding to each individual in the current iteration round is calculated. In the first stage test, the individual is the first test case in the current iteration process.

[0064] After determining the coverage rate of each individual in the current iteration round, the individual fitness of each individual can be calculated using formula (1) according to the coverage rate of each individual and the total historical coverage rate of the previous iteration round. Formula (1) is: .

[0065] in, Represents the individual fitness of the mth individual after the nth iteration. It represents the coverage of the mth individual in the nth iteration, which is determined by combining the coverage of the nth iteration with the total coverage of the previous n-1 iterations. Represents the historical total coverage rate of the n-1th iteration. The coverage of all individuals in the n-1th iteration is combined with the historical total coverage rate of the n-2th iteration to obtain the historical total coverage rate corresponding to the n-1th iteration. . n represents the number of iterations, n is a positive integer, and when n=1, . m is a positive integer.

[0066] Afterwards, according to the shared niche algorithm, the individual fitness of each individual in the current iteration round is updated using formula (2). Formula (2) is: .

[0067] in, It represents the individual fitness updated after the nth iteration of the mth individual after being processed by the shared niche algorithm. It represents the shared value of the mth individual at the nth iteration, and the calculation formula of the shared value is shown in formula (3), which is: .

[0068] Where M represents the population size. Represents the shared function, and the shared function is shown in formula (4), which is: .

[0069] in, Represents the microhabitat distance. Indicates the similarity between the mth individual and the jth individual at the nth iteration. The similarity is the Hamming distance between the mth individual and the jth individual. The calculation formula of is shown in formula (5), which is: .

[0070] Wherein, P represents the number of covered entries, which includes all conditional sub-item combination entries. It represents the coverage entry value corresponding to the ith coverage entry in the mth individual at the nth iteration, that is, if the ith coverage entry is covered, the coverage entry value is 1, and if the ith coverage entry is not covered, the coverage entry value is 0. Represents the coverage entry value corresponding to the i-th coverage entry in the j-th individual at the n-th iteration. Represents XOR operation. By determining the similarity between any two individuals based on the difference in their coverage, the contribution of individuals to coverage can be distinguished more objectively.

[0071] After updating the individual fitness corresponding to each individual, the coverage of each individual in the current iteration round is merged with the total coverage of the previous iteration round, that is, based on the coverage of each individual in the current iteration round and the historical total coverage of the previous iteration round, the first total coverage corresponding to the current iteration round is calculated. After calculating the first total coverage of the current iteration round, the first total coverage can be compared with the preset coverage threshold. If the first total coverage is equal to the preset coverage threshold, it indicates that the coverage of all conditional sub-item combination entries in the code to be tested has reached the target, that is, the code coverage test is completed. If the first total coverage is less than the preset coverage threshold, and the target test parameter corresponding to the first total coverage meets the first iteration stop condition, it indicates that the first stage test has little improvement on the coverage at this time, and the second stage test can be entered. If the first total coverage rate is less than the preset coverage rate threshold, and the target test parameter corresponding to the first total coverage rate does not meet the first iteration stop condition, it indicates that the first stage test has greatly improved the coverage rate. Based on any genetic algorithm or related derivative algorithm, according to the above-mentioned updated individual fitness, selection, crossover and mutation operations can be performed on each individual to generate a third initial population, and the third initial population is determined as the new first initial population. The above operations are repeated until the target test parameter corresponding to the new first total coverage rate meets the first iteration stop condition, then the first stage iterative test is stopped and the second stage iterative test is entered.

[0072] After entering the second phase of testing, the conditional sub-item combination entries covered after the first phase of iterative testing can be removed from all conditional sub-item combination entries to obtain the remaining uncovered conditional sub-item uncovered entries. The conditional sub-item uncovered entries are conditional sub-item combination entries that have at least one conditional sub-item uncovered. For example, the conditional items are [(a>0)、(b>5)、(b 10), (c==7)], and the condition sub-item combination entry corresponding to the condition item is [1, 0, 0, 0] (that is, [satisfied, not satisfied, not satisfied, not satisfied]), for example, the coverage of the condition sub-item combination entry is [0, 1, 1, 0] (that is, [not covered, covered, covered, not covered]). At this time, there are two condition sub-items that are not covered in the condition sub-item combination entry. Therefore, the condition sub-item combination entry is not covered. At this time, the condition sub-item combination entry can be determined as a condition sub-item uncovered entry.

[0073] Optionally, the uncovered entries of the conditional sub-item may be stored in a list form or other forms, which is not limited in the embodiment of the present invention.

[0074] Step 130: Determine a second test case in a second initial population based on the conditional sub-item not covering the entry.

[0075] Specifically, Figure 2As shown, after the first phase of iterative testing is completed, the optimal M second test cases are reselected according to the uncovered items of all conditional sub-items to form a second initial population, and the second phase of iterative testing is continued.

[0076] Further, the determining of a second test case in a second initial population based on the conditional sub-item not covering the entry includes: Determine a target conditional branch from the target conditional sub-item uncovered entries; Static analysis is performed on the code to be tested to construct a conditional network corresponding to the target conditional branch; the conditional network is used to characterize the association relationship between the test cases involved in the iteration, at least one conditional node related to the target conditional branch, and the target conditional branch; Based on the conditional net, a second test case in a second initial population is determined.

[0077] Specifically, after determining that all conditional sub-items are not covered, the conditional sub-item not covered item with the most uncovered conditional sub-items can be determined from all conditional sub-item not covered items, and one of the uncovered conditional sub-items is randomly selected as the target conditional branch. After determining the target conditional branch, the code to be tested is statically analyzed to construct the conditional network corresponding to the target conditional branch. That is, within the code to be tested, each conditional sub-item consists of at least one conditional branch path, with the input test case participating in the iteration as the starting point and the target conditional branch as the end point. There are multiple paths between the starting point and the end point, and the multiple paths include at least one conditional node, and there is an association relationship between each conditional node of the path, between the conditional node and the input test case participating in the iteration, and between the conditional node and the target conditional branch. The association relationship can be understood as a constraint relationship. Multiple paths, starting points, end points and constraint relationships constitute the conditional network corresponding to the target node branch. Among them, each conditional node represents a conditional item. After constructing the conditional network corresponding to the target conditional branch, M second test cases closest to the target conditional branch can be searched from the conditional network to form a second initial population, and the second phase of iterative testing is performed with the target conditional branch as the optimization target.

[0078] For example, Figure 3 is a schematic diagram of a conditional network provided by an embodiment of the present invention, such as Figure 3 As shown, T represents the target conditional branch, conditional nodes 1 to 11 are all conditional items, and the direction of the arrow represents the constraint relationship. For example, the arrow from conditional node 5 to conditional node 2 indicates that the conditional item corresponding to conditional node 2 may only be established when the conditional item corresponding to conditional node 5 is established.

[0079] Further, the determining a second test case in a second initial population based on the conditional network includes: Traversing each of the conditional nodes in the conditional network in ascending order of distance from the target conditional branch; Utilizing a breadth-first search algorithm, based on the test cases participating in the iteration, determine the third test cases covering each of the conditional nodes; stop traversing until the total number of third test cases of the traversed conditional nodes reaches a second preset threshold, and determine all third test cases corresponding to the traversed conditional nodes as the second test cases of the second initial population.

[0080] For example, Figure 3 Taking the conditional network shown as an example, after constructing the conditional network corresponding to the target conditional branch T, taking the conditional node 1 closest to the target conditional branch T as the starting point, using the breadth-first search algorithm, determine the third test case covering the conditional node 1 among all the test cases participating in the iteration. If there is a third test case covering the conditional node 1, determine whether the sum of the number of the third test cases covering the conditional node 1 reaches the second preset threshold. If it reaches, stop searching, and determine the second preset threshold third test cases as the second test cases, and the second preset number of second test cases constitute the second initial population. If it does not reach, retain the determined third test case, and remove the determined third test case from the search space. Afterwards, randomly determine a target conditional node from the next layer of conditional nodes 2, conditional nodes 3 and conditional nodes 4 that are closest to the conditional node 1. Taking conditional node 2 as the target conditional node as an example, continue to determine the third test case covering the conditional node 2. If the sum of the third test case covering the conditional node 2 and the third test case covering the conditional node 1 reaches the second preset threshold, all the determined third test cases are determined as the second test cases, and the second initial population is constructed. If the sum of the third test cases covering condition node 2 and the third test cases covering condition node 1 does not reach the second preset threshold, continue to randomly determine a target condition node from condition node 3 and condition node 4, repeat the above operation, continue to determine the third test cases corresponding to the target condition nodes, until the total number of all determined third test cases reaches the second preset threshold, stop searching, and construct a second initial population.

[0081] It should be noted that the second preset threshold is the population size M.

[0082] Furthermore, the determining of the second test case in the second initial population based on the conditional network further includes: When the total number of third test cases corresponding to all conditional nodes in the conditional network is less than the second preset threshold, determining the remaining number based on the total number of third test cases corresponding to all conditional nodes and the second preset threshold; Using a constrained random algorithm, randomly generate the remaining number of fourth test cases from the remaining test cases, where the remaining test cases are other test cases in the test cases participating in the iteration except all the third test cases; All third test cases and all fourth test cases are determined as second test cases of the second initial population.

[0083] Specifically, if after searching all conditional nodes in the conditional network, the sum of the number of all determined third test cases is still less than the second preset threshold, then all determined third test cases can be removed from the search space, and the difference between the second preset threshold and the sum of the number of all third test cases is calculated to determine the remaining number of test cases to be determined. Afterwards, the constrained random algorithm is used to randomly select the remaining number of fourth test cases from the test cases participating in the iteration other than the third test case, and all the fourth test cases and all the third test cases obtained are determined as second test cases.

[0084] Step 140, using the niche genetic algorithm and all the second test cases in the second initial population, with covering the uncovered items of the conditional sub-items as the optimization goal, iteratively determine the second total coverage corresponding to all the second test cases, and stop the iteration when the second total coverage reaches a first preset threshold.

[0085] Specifically, Figure 2 As shown, after determining all the second test cases in the second initial population, all the second test cases can be injected into the code to be tested, and the test code can be run in the DUT test environment, and the target conditional branch in the uncovered entry of the coverage condition sub-item is used as the optimization target to perform code coverage iteration test, and determine the second total coverage rate corresponding to each iteration round, and determine whether the first preset threshold is reached according to the second total coverage rate. If reached, it indicates that the coverage test of the code to be tested is completed. If not reached, continue to determine whether the coverage number or iteration number of the target conditional branch reaches the fourth preset threshold. If reached, it indicates that the code coverage test of the target conditional branch has been completed, and a new target conditional branch can be re-determined, and the code coverage iteration test is performed with the new target conditional branch as the optimization target. If not reached, it indicates that the code coverage test of the target conditional branch is not completed, and the second test case selection, crossover and mutation can be performed based on any genetic algorithm or related derivative algorithm to generate a new second initial population, and continue to perform code coverage iteration test.

[0086] It should be noted that before iteration, the program structure and logic of the code to be tested can be analyzed according to the code programming language syntax, and a condition tree can be generated using specific rules. Node extraction is performed on the condition number to obtain valid nodes, and initial weight values ​​are assigned to the invalid nodes. Afterwards, the second test case is used as the input parameter of the code to be tested, and the code to be tested is run in the DUT test environment. According to the recorded coverage times and initial weight values ​​of the valid nodes, the initial weight values ​​of the valid nodes are updated to obtain updated weight values. According to the updated weight values ​​and the coverage status of the valid nodes, the individual fitness of each second test case is calculated, and the individual fitness is updated for the shared niche algorithm. The update process of individual fitness in the iterative test of the second stage is the same as that in the iterative test of the first stage. The difference is that the calculation formula of the similarity degree is different. In the iterative test process of the second stage, the calculation formula of the similarity degree is shown in formula (6), which is: .

[0087] Among them, Q represents the total number of valid nodes determined, Indicates whether the sth condition node of the mth second test case is satisfied at the nth iteration. Indicates whether the sth condition node of the jth second test case is satisfied at the nth iteration. If so, it is 1; if not, it is 0.

[0088] The code coverage testing method provided by the embodiment of the present invention, after determining at least one conditional sub-item combination entry corresponding to the code to be tested, uses a niche genetic algorithm and all first test cases, with coverage as the optimization goal, to perform an iterative test of the code coverage of the first stage on the code to be tested, iteratively determines the first total coverage corresponding to each iteration round to quickly improve the coverage, and determines the iteration termination time of the first stage by judging whether the target test parameter corresponding to the first total coverage meets the first iteration stop condition, determines the uncovered conditional sub-item uncovered entries from all conditional sub-item combination entries, and determines the optimal second test case based on the conditional sub-item uncovered entries, and again uses the niche genetic algorithm and the second test case, with covering the conditional sub-item uncovered entries as the optimization goal, to perform an iterative test of the code coverage of the second stage on the code to be tested, iteratively determines the second total coverage corresponding to each iteration round, until the second total coverage reaches a first preset threshold, the iteration is stopped and the code coverage test is completed. In an embodiment of the present invention, the coverage rate can be quickly improved by iterative testing with coverage as the optimization goal in the first stage, and when falling into a local optimal solution in the first stage, it can quickly jump out and enter the second stage of iterative testing with coverage of uncovered items of conditional sub-items as the optimization goal. By adjusting the optimization goal in the second stage, the number of invalid test cases generated by inaccurate search targets can be greatly reduced, thereby improving the test efficiency in the later stage of testing.

[0089] The code coverage testing device provided by the present invention is described below. The code coverage testing device described below and the code coverage testing method described above can be referenced to each other.

[0090] The embodiment of the present invention also provides a code coverage testing device, Figure 4 is a schematic diagram of the structure of a code coverage testing device provided by an embodiment of the present invention, such as Figure 4 As shown, the code coverage testing device 400 includes: a first determination module 410 , a first iteration module 420 , a second determination module 430 and a second iteration module 440 .

[0091] A first determination module 410 is used to determine at least one conditional sub-item combination entry corresponding to the code to be tested; The first iteration module 420 is used to use the niche genetic algorithm and all the first test cases in the first initial population, and iteratively determine the first total coverage corresponding to all the first test cases with coverage as the optimization target; until the target test parameter corresponding to the first total coverage satisfies the first iteration stop condition, determine the condition sub-item uncovered items from the condition sub-item combination items; A second determination module 430, configured to determine a second test case in a second initial population based on the condition sub-item uncovered entry; The second iteration module 440 is used to utilize the niche genetic algorithm and all the second test cases in the second initial population, with the goal of covering the uncovered items of the conditional sub-items, to iteratively determine the second total coverage rate corresponding to all the second test cases, and stop the iteration when the second total coverage rate reaches a first preset threshold.

[0092] The code coverage testing device provided by the embodiment of the present invention, after determining at least one conditional sub-item combination entry corresponding to the code to be tested, uses a niche genetic algorithm and all first test cases to take coverage as an optimization goal, performs an iterative test of the code coverage of the first stage on the code to be tested, iteratively determines the first total coverage corresponding to each iteration round to quickly improve the coverage, and determines the iteration termination time of the first stage by judging whether the target test parameter corresponding to the first total coverage meets the first iteration stop condition, determines the uncovered conditional sub-item uncovered entries from all conditional sub-item combination entries, and determines the optimal second test case based on the conditional sub-item uncovered entries, and again uses the niche genetic algorithm and the second test case to take covering the conditional sub-item uncovered entries as an optimization goal, performs an iterative test of the code coverage of the second stage on the code to be tested, iteratively determines the second total coverage corresponding to each iteration round, and stops the iteration and completes the code coverage test when the second total coverage reaches a first preset threshold. In an embodiment of the present invention, the coverage rate can be quickly improved by iterative testing with coverage as the optimization goal in the first stage, and when falling into a local optimal solution in the first stage, it can quickly jump out and enter the second stage of iterative testing with coverage of uncovered items of conditional sub-items as the optimization goal. By adjusting the optimization goal in the second stage, the number of invalid test cases generated by inaccurate search targets can be greatly reduced, thereby improving the test efficiency in the later stage of testing.

[0093] Optionally, the first determining module 410 is specifically configured to: Identifying a first condition and a branch in the code to be tested; converting the branch to at least one second condition; Based on all the first conditions and all the second conditions, determining at least one condition item corresponding to the code to be tested; Based on the satisfaction of all the condition sub-items included in each of the condition items, a condition sub-item combination entry corresponding to each of the condition items is determined.

[0094] Optionally, the first iteration module 420 is specifically configured to: S1, injecting all the first test cases into the code to be tested, and running the code to be tested in the DUT test environment to determine the coverage rate corresponding to each of the first test cases in the current iteration round; S2. Determine the individual fitness of each of the first test cases in the current iteration round based on the coverage rate corresponding to each of the first test cases and the historical total coverage rate corresponding to the previous iteration round; S3, using the niche genetic algorithm, based on the coverage entry values ​​corresponding to any two first test cases, updating the individual fitness of each of the first test cases in the current iteration round; S4. Determine a first total coverage rate corresponding to the current iteration round based on the coverage rates corresponding to each of the first test cases in the current iteration round.

[0095] S5. When the target test parameters corresponding to the first total coverage rate do not meet the first iteration stop condition, generate a third initial population and use the third initial population as the new first initial population; repeat steps S1 to S5 until the iteration is stopped when the target test parameters corresponding to the first total coverage rate meet the first iteration stop condition.

[0096] Optionally, the second determining module 430 is specifically configured to: Determine a target conditional branch from the target conditional sub-item uncovered entries; Static analysis is performed on the code to be tested to construct a conditional network corresponding to the target conditional branch; the conditional network is used to characterize the association relationship between the test cases involved in the iteration, at least one conditional node related to the target conditional branch, and the target conditional branch; Based on the conditional net, a second test case in a second initial population is determined.

[0097] Optionally, the second determining module 430 is specifically configured to: Traversing each of the conditional nodes in the conditional network in ascending order of distance from the target conditional branch; Utilizing a breadth-first search algorithm, based on the test cases participating in the iteration, determine the third test cases covering each of the conditional nodes; stop traversing until the total number of third test cases of the traversed conditional nodes reaches a second preset threshold, and determine all third test cases corresponding to the traversed conditional nodes as the second test cases of the second initial population.

[0098] Optionally, the second determining module 430 is specifically configured to: When the total number of third test cases corresponding to all conditional nodes in the conditional network is less than the second preset threshold, determining the remaining number based on the total number of third test cases corresponding to all conditional nodes and the second preset threshold; Using a constrained random algorithm, randomly generate the remaining number of fourth test cases from the remaining test cases, where the remaining test cases are other test cases in the test cases participating in the iteration except all the third test cases; All third test cases and all fourth test cases are determined as second test cases of the second initial population.

[0099] Figure 5 is a schematic diagram of the structure of an electronic device provided by an embodiment of the present invention, such as Figure 5As shown, the electronic device may include: a processor 510, a communication interface 520, a memory 530 and a communication bus 540, wherein the processor 510, the communication interface 520 and the memory 530 communicate with each other through the communication bus 540. The processor 510 may call the logic instructions in the memory 530 to execute the code coverage test method, which includes: Determine at least one conditional sub-item combination entry corresponding to the code to be tested; Using the niche genetic algorithm and all the first test cases in the first initial population, with coverage as an optimization target, iteratively determine a first total coverage corresponding to all the first test cases; until the target test parameter corresponding to the first total coverage satisfies a first iteration stop condition, determine the conditional sub-item uncovered items from the conditional sub-item combination items; Determining a second test case in a second initial population based on the conditional sub-item not covering the entry; Utilizing the niche genetic algorithm and all second test cases in the second initial population, with covering uncovered entries of the conditional sub-items as the optimization goal, iteratively determine the second total coverage rate corresponding to all the second test cases, and stop iterating when the second total coverage rate reaches a first preset threshold.

[0100] In addition, the logic instructions in the above-mentioned memory 530 can be implemented in the form of a software functional unit and can be stored in a computer-readable storage medium when it is sold or used as an independent product. Based on such an understanding, the technical solution of the present invention, in essence, or the part that contributes to the prior art or the part of the technical solution, can be embodied in the form of a software product, and the computer software product is stored in a storage medium, including a number of instructions for a computer device (which can be a personal computer, a server, or a network device, etc.) to perform all or part of the steps of the method described in each embodiment of the present invention. The aforementioned storage medium includes: U disk, mobile hard disk, read-only memory (ROM, Read-Only Memory), random access memory (RAM, Random Access Memory), disk or optical disk, etc. Various media that can store program codes.

[0101] On the other hand, the present invention further provides a computer program product, the computer program product comprising a computer program, the computer program can be stored on a non-transitory computer-readable storage medium, when the computer program is executed by a processor, the computer can execute the code coverage testing method provided by the above methods, the method comprising: Determine at least one conditional sub-item combination entry corresponding to the code to be tested; Using the niche genetic algorithm and all the first test cases in the first initial population, with coverage as an optimization target, iteratively determine a first total coverage corresponding to all the first test cases; until the target test parameter corresponding to the first total coverage satisfies a first iteration stop condition, determine the conditional sub-item uncovered items from the conditional sub-item combination items; Determining a second test case in a second initial population based on the conditional sub-item not covering the entry; Utilizing the niche genetic algorithm and all second test cases in the second initial population, with covering uncovered entries of the conditional sub-items as the optimization goal, iteratively determine the second total coverage rate corresponding to all the second test cases, and stop iterating when the second total coverage rate reaches a first preset threshold.

[0102] In another aspect, the present invention further provides a non-transitory computer-readable storage medium having a computer program stored thereon, and when the computer program is executed by a processor, the code coverage testing method provided by the above methods is implemented, and the method includes: Determine at least one conditional sub-item combination entry corresponding to the code to be tested; Using the niche genetic algorithm and all the first test cases in the first initial population, with coverage as an optimization target, iteratively determine a first total coverage corresponding to all the first test cases; until the target test parameter corresponding to the first total coverage satisfies a first iteration stop condition, determine the conditional sub-item uncovered items from the conditional sub-item combination items; Determining a second test case in a second initial population based on the conditional sub-item not covering the entry; Utilizing the niche genetic algorithm and all second test cases in the second initial population, with covering uncovered entries of the conditional sub-items as the optimization goal, iteratively determine the second total coverage rate corresponding to all the second test cases, and stop iterating when the second total coverage rate reaches a first preset threshold.

[0103] The device embodiments described above are merely illustrative, wherein the units described as separate components may or may not be physically separated, and the components displayed as units may or may not be physical units, that is, they may be located in one place, or they may be distributed on multiple network units. Some or all of the modules may be selected according to actual needs to achieve the purpose of the scheme of this embodiment. Ordinary technicians in this field can understand and implement it without paying creative labor.

[0104] Through the description of the above implementation methods, those skilled in the art can clearly understand that each implementation method can be implemented by means of software plus a necessary general hardware platform, and of course, can also be implemented by hardware. Based on this understanding, the above technical solution is essentially or the part that contributes to the prior art can be embodied in the form of a software product, and the computer software product can be stored in a computer-readable storage medium, such as ROM / RAM, a disk, an optical disk, etc., including a number of instructions for a computer device (which can be a personal computer, a server, or a network device, etc.) to execute the methods described in each embodiment or some parts of the embodiments.

[0105] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention, rather than to limit it. Although the present invention has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that they can still modify the technical solutions described in the aforementioned embodiments, or make equivalent replacements for some of the technical features therein. However, these modifications or replacements do not deviate the essence of the corresponding technical solutions from the spirit and scope of the technical solutions of the embodiments of the present invention.

Claims

1. A code coverage testing method, characterized in that: include: Determine at least one conditional sub-item combination entry corresponding to the code to be tested; Using the niche genetic algorithm and all the first test cases in the first initial population, with coverage as an optimization target, iteratively determining a first total coverage corresponding to all the first test cases; Until the target test parameter corresponding to the first total coverage rate satisfies the first iteration stop condition, determining the condition sub-item uncovered items from the condition sub-item combination items; Determining a second test case in a second initial population based on the conditional sub-item not covering the entry; Utilizing the niche genetic algorithm and all second test cases in the second initial population, with covering uncovered entries of the conditional sub-items as the optimization goal, iteratively determine the second total coverage rate corresponding to all the second test cases, and stop iterating when the second total coverage rate reaches a first preset threshold.

2. The code coverage testing method according to claim 1, characterized in that: The determining of a second test case in a second initial population based on the conditional sub-item not covering the entry comprises: Determine a target conditional branch from the target conditional sub-item uncovered entries; Static analysis is performed on the code to be tested to construct a conditional network corresponding to the target conditional branch; the conditional network is used to characterize the association relationship between the test cases involved in the iteration, at least one conditional node related to the target conditional branch, and the target conditional branch; Based on the conditional net, a second test case in a second initial population is determined.

3. The code coverage testing method according to claim 2, characterized in that: The determining, based on the conditional network, a second test case in a second initial population comprises: Traversing each of the conditional nodes in the conditional network in ascending order of distance from the target conditional branch; Utilizing a breadth-first search algorithm, based on the test cases participating in the iteration, determine the third test cases covering each of the conditional nodes; stop traversing until the total number of third test cases of the traversed conditional nodes reaches a second preset threshold, and determine all third test cases corresponding to the traversed conditional nodes as the second test cases of the second initial population.

4. The code coverage testing method according to claim 3, characterized in that: The determining of a second test case in a second initial population based on the conditional network further includes: When the total number of third test cases corresponding to all conditional nodes in the conditional network is less than the second preset threshold, determining the remaining number based on the total number of third test cases corresponding to all conditional nodes and the second preset threshold; Using a constrained random algorithm, randomly generate the remaining number of fourth test cases from the remaining test cases, where the remaining test cases are other test cases in the test cases participating in the iteration except all the third test cases; All third test cases and all fourth test cases are determined as second test cases of the second initial population.

5. The code coverage testing method according to any one of claims 1 to 4, characterized in that: The step of determining at least one conditional sub-item combination entry corresponding to the code to be tested includes: Identifying a first condition and a branch in the code to be tested; converting the branch to at least one second condition; Based on all the first conditions and all the second conditions, determining at least one condition item corresponding to the code to be tested; Based on the satisfaction of all the condition sub-items included in each of the condition items, a condition sub-item combination entry corresponding to each of the condition items is determined.

6. The code coverage testing method according to claim 5, characterized in that: The method utilizes the niche genetic algorithm and all the first test cases in the first initial population, takes coverage as an optimization target, and iteratively determines the first total coverage corresponding to all the first test cases, including: S1, injecting all the first test cases into the code to be tested, and running the code to be tested in the DUT test environment to determine the coverage rate corresponding to each of the first test cases in the current iteration round; S2. Determine the individual fitness of each of the first test cases in the current iteration round based on the coverage rate corresponding to each of the first test cases and the historical total coverage rate corresponding to the previous iteration round; S3, using the niche genetic algorithm, based on the coverage entry values ​​corresponding to any two first test cases, updating the individual fitness of each of the first test cases in the current iteration round; S4. Determine a first total coverage rate corresponding to the current iteration round based on the coverage rates corresponding to each of the first test cases in the current iteration round. S5. When the target test parameters corresponding to the first total coverage rate do not meet the first iteration stop condition, generate a third initial population and use the third initial population as the new first initial population; repeat steps S1 to S5 until the iteration is stopped when the target test parameters corresponding to the first total coverage rate meet the first iteration stop condition.

7. A code coverage testing device, characterized in that: include: A first determination module, used to determine at least one conditional sub-item combination entry corresponding to the code to be tested; A first iteration module is used to iteratively determine a first total coverage corresponding to all the first test cases using a niche genetic algorithm and all the first test cases in a first initial population with coverage as an optimization target; Until the target test parameter corresponding to the first total coverage rate satisfies the first iteration stop condition, determining the condition sub-item uncovered items from the condition sub-item combination items; A second determination module, configured to determine a second test case in a second initial population based on the condition sub-item not covering the entry; A second iteration module is used to utilize the niche genetic algorithm and all second test cases in the second initial population, to iteratively determine a second total coverage rate corresponding to all the second test cases with the goal of covering uncovered items of the conditional sub-items, and to stop the iteration when the second total coverage rate reaches a first preset threshold.

8. An electronic device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein: When the processor executes the computer program, the code coverage testing method according to any one of claims 1 to 6 is implemented.

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

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

Citation Information

Cited By

  • Test case management method, electronic equipment and medium

    CN120780611A