Test case generation method, system, terminal device and storage medium

Through the combination of regular expressions and differential evolution algorithms, test cases with high coverage are generated, which solves the problems of low test case generation efficiency and low coverage in the existing technology, and improves the accuracy of software testing.

CN113672503BActive Publication Date: 2024-08-27CHINA MOBILEHANGZHOUINFORMATION TECH CO LTD +1
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202110886345.1
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2021-08-03
Publication Date
2024-08-27
Estimated Expiration
2041-08-03

AI Technical Summary

Technical Problem

Existing automated test case generation solutions cannot achieve mass production, and the generated test cases have low coverage of the software under test, resulting in poor effectiveness.

Method used

Test cases are generated by setting sentence templates based on regular expressions, constructing a fitness function to calculate the fitness value of the test cases, and using a differential evolution algorithm to iteratively find the optimal solution to improve the coverage of test cases.

Benefits of technology

It achieves efficient batch test case generation for the software under test, significantly improves test case coverage and test accuracy, and ensures the effectiveness of generated test cases.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN113672503B
    Figure CN113672503B
Patent Text Reader

Abstract

The present invention discloses a method, system, terminal device and computer storage medium for generating test cases. The test case generation method generates test cases according to a preset sentence template, wherein the sentence template is generated based on a regular expression rule setting; constructs a fitness function that meets the test coverage criterion, and calculates the fitness value of the test case for the tested software program according to the fitness function; and iteratively searches for the optimal solution according to the fitness value to determine the optimal test case in the test case. The traditional automated test case generation method, the present invention sets a sentence template based on a regular expression, so that the sentence template can be used to automatically generate test cases in batches. In addition, it can also improve the coverage rate of the test case for the tested software program, thereby ensuring the effectiveness of the generated test case.
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 method and system for generating a test case and a terminal device. Background Art

[0002] With the advancement of modern technology and the rapid development of computers, various computer software have become necessities in life. As a result, people's requirements for the quality of software used in daily life have gradually increased.

[0003] Software testing is an important technical means to test software quality, and designing test cases is an important part and core difficulty of software testing. Designing automatic generation of test cases can greatly improve the efficiency of software testing and save a lot of manpower and material resources. Therefore, the research topic of automatic generation of test cases has important theoretical value and practical significance.

[0004] At present, the automatic generation of test cases usually needs to consider the automatic generation of test case sets and the effectiveness of the generated test cases. Although there are some algorithm-based automatic test case generation solutions, the existing solutions cannot achieve mass production of test cases, and the generated test cases have low coverage for the software under test, resulting in poor effectiveness. Summary of the invention

[0005] The main purpose of the present invention is to provide a test case generation method, system, terminal device and computer storage medium, aiming to solve the technical problems that the existing solutions cannot achieve mass production of test cases and the generated test cases have low coverage for the tested software, resulting in poor effectiveness.

[0006] To achieve the above object, the present invention provides a method for generating a test case, the method comprising:

[0007] Generate a test case according to a preset sentence template, wherein the sentence template is generated based on a regular expression rule setting;

[0008] Constructing a fitness function that meets the test coverage criteria, and calculating the fitness value of the test case for the tested software program according to the fitness function;

[0009] An optimal solution is iteratively sought according to the fitness value to determine the optimal test case among the test cases.

[0010] Preferably, the sentence template includes a main sentence template and a clause template, and the step of generating a test case according to the preset sentence template includes:

[0011] Calling the main sentence template to match the string sentence pattern that conforms to the regular expression rule to generate a main sentence test case; and,

[0012] Calling the clause template to match a string sentence pattern that conforms to the regular expression rule to generate a clause test case;

[0013] The main clause test cases and the sub-clause test cases in the same scenario are arranged and cross-combined to form a multi-round test case set.

[0014] Preferably, before the step of arranging and cross-combining the main clause test cases and the sub-clause test cases in the same scenario to form a multi-round test case set, the method further includes:

[0015] Calculating the cosine similarity between two of the main clause test cases or between two of the sub-clause test cases;

[0016] Determining whether the cosine similarity is greater than or equal to a preset threshold;

[0017] If yes, after removing any case from the main clause test case or the subclause test case, continue to calculate the cosine similarity until it is determined that the cosine similarity is less than the preset threshold.

[0018] Preferably, the step of constructing a fitness function that meets the test coverage criterion includes:

[0019] Obtaining a branch node of the software program under test;

[0020] A fitness function that meets the test coverage criterion is constructed based on the branch nodes in combination with a preset branch distance function.

[0021] Preferably, the step of calculating the fitness value of the test case for the tested software program according to the fitness function comprises:

[0022] Inputting the test case into the software program under test to perform a test run, so as to collect all the path coverage information of the test case for the software program under test;

[0023] The fitness function is called to calculate the fitness value of the test case for the software program under test based on the path coverage information.

[0024] Preferably, the step of iteratively searching for the optimal solution according to the fitness value to determine the optimal test case among the test cases includes:

[0025] The preset differential evolution algorithm is called to iteratively search for an optimal solution according to the fitness value, so as to determine an optimal test case in the test case that covers all branches of the software program under test.

[0026] Preferably, the step of calling a preset differential evolution algorithm to iteratively search for an optimal solution according to the fitness value comprises:

[0027] Calling the differential evolution algorithm to perform optimization operations on the test case to obtain an optimized and updated optimal solution;

[0028] If it is determined that the fitness value of the test case reaches a preset target error, outputting the optimal solution;

[0029] If it is determined that the fitness value does not reach the preset target error, the optimization operation is re-performed on the test case to obtain a new optimal solution and calculate a new fitness value, until the fitness value reaches the preset target error and the new optimal solution is output.

[0030] In addition, to achieve the above object, the present invention also provides a test case generation system, the test case generation system comprising:

[0031] A test case generation module, used to generate a test case according to a preset sentence template, wherein the sentence template is generated based on a regular expression rule setting;

[0032] A fitness calculation module, used to construct a fitness function that meets the test coverage criteria, and calculate the fitness value of the test case for the tested software program according to the fitness function;

[0033] The optimization module is used to iteratively search for the optimal solution according to the fitness value to determine the optimal test case among the test cases.

[0034] Among them, each functional module of the test case generation system of the present invention implements the steps of the test case generation method described above during operation.

[0035] In addition, to achieve the above-mentioned purpose, the present invention also provides a terminal device, which includes: a memory, a processor, and a test case generation program stored in the memory and runnable on the processor, and the test case generation program, when executed by the processor, implements the steps of the test case generation method as described above.

[0036] In addition, to achieve the above-mentioned purpose, the present invention also provides a computer storage medium, on which a test case generation program is stored, and when the test case generation program is executed by a processor, the steps of the test case generation method as described above are implemented.

[0037] In addition, to achieve the above-mentioned purpose, the present invention also provides a computer program product, which includes a computer program, and when the computer program is executed by a processor, the steps of the test case generation method described above are implemented.

[0038] The present invention provides a test case generation method, system, terminal device, computer storage medium and computer program product, which generate test cases according to preset sentence templates, wherein the sentence templates are generated based on regular expression rule settings; construct a fitness function that meets the test coverage criteria, and calculate the fitness value of the test case for the tested software program according to the fitness function; and iteratively search for the optimal solution according to the fitness value to determine the optimal test case among the test cases.

[0039] In the process of testing a software program under test to automatically generate test cases, the present invention first uses a sentence template that is pre-generated based on a regular expression setting to automatically generate test cases in batches, then further constructs a fitness function that meets the test coverage criteria for testing the software program under test, and calculates the fitness value of the generated test case for the software program under test based on the fitness function, and finally, iterates based on the fitness value to find the optimal solution, thereby determining the optimal test case among the generated test cases that can fully cover the software program under test.

[0040] Compared with the traditional method of generating automated test cases, the present invention sets a sentence template based on a regular expression, so that the sentence template can be used to automatically generate batches of test cases. In addition, the present invention also calculates the fitness value of the generated test case for the software program under test by constructing a fitness function, and then performs an iterative optimization operation based on the fitness value to determine the optimal test case among the generated test cases. In this way, the coverage of the test case for the software program under test can be greatly improved, thereby ensuring the effectiveness of the generated test case for the software program under test, and further improving the test accuracy of the software program under test. BRIEF DESCRIPTION OF THE DRAWINGS

[0041] Figure 1 A schematic diagram of the device structure of the hardware operating environment of the terminal device involved in the embodiment of the present invention;

[0042] Figure 2 A schematic diagram of a flow chart of an embodiment of a method for generating a test case of the present invention;

[0043] Figure 3 A schematic diagram of an application scenario involved in an embodiment of a method for generating a test case of the present invention;

[0044] Figure 4A sentence model involved in an embodiment of a method for generating a test case of the present invention;

[0045] Figure 5 A multi-round dialogue test case involved in an embodiment of a method for generating a test case of the present invention;

[0046] Figure 6 A schematic diagram of an application flow involved in an embodiment of a method for generating a test case of the present invention;

[0047] Figure 7 A schematic diagram of functional modules of an embodiment of a system for generating test cases of the present invention.

[0048] The realization of the purpose, functional features and advantages of the present invention will be further explained in conjunction with embodiments and with reference to the accompanying drawings. DETAILED DESCRIPTION

[0049] It should be understood that the specific embodiments described herein are only used to explain the present invention, and are not used to limit the present invention.

[0050] Reference Figure 1 , Figure 1 The present invention is a schematic diagram of the device structure of the hardware operating environment of the terminal device involved in the embodiment of the present invention.

[0051] The terminal device of the embodiment of the present invention may be a terminal device loaded with a software program to be tested, or the terminal device of the embodiment of the present invention may also be a terminal device specifically used for testing the software program and configured to automatically generate test cases, and the device may be a smart phone, a PC (Personal Computer), a tablet computer, a portable computer, etc.

[0052] like Figure 1 As shown, the terminal device may include: a processor 1001, such as a CPU, a communication bus 1002, a user interface 1003, a network interface 1004, and a memory 1005. Among them, the communication bus 1002 is used to realize the connection and communication between these components. The user interface 1003 may include a display screen (Display), an input unit such as a keyboard (Keyboard), and the optional user interface 1003 may also include a standard wired interface and a wireless interface. The network interface 1004 may optionally include a standard wired interface and a wireless interface (such as a Wi-Fi interface). The memory 1005 may be a high-speed RAM memory, or a stable memory (non-volatile memory), such as a disk memory. The memory 1005 may also be a storage device independent of the aforementioned processor 1001.

[0053] Those skilled in the art will understand that Figure 1The terminal device structure shown in the figure does not constitute a limitation on the terminal device, and may include more or fewer components than shown in the figure, or combine certain components, or arrange the components differently.

[0054] like Figure 1 As shown, the memory 1005 as a computer storage medium may include an operating system, a network communication module, a user interface module, and a test case generation program.

[0055] exist Figure 1 In the terminal shown, the network interface 1004 is mainly used to connect to the backend server and perform data communication with the backend server; the user interface 1003 is mainly used to connect to the client and perform data communication with the client; and the processor 1001 can be used to call the test case generation program stored in the memory 1005 and perform the following steps:

[0056] Generate a test case according to a preset sentence template, wherein the sentence template is generated based on a regular expression rule setting;

[0057] Constructing a fitness function that meets the test coverage criteria, and calculating the fitness value of the test case for the tested software program according to the fitness function;

[0058] An optimal solution is iteratively sought according to the fitness value to determine the optimal test case among the test cases.

[0059] Preferably, the sentence template includes a main sentence template and a clause template. The processor 1001 can be used to call the test case generation program stored in the memory 1005, and further perform the following steps:

[0060] Calling the main sentence template to match the string sentence pattern that conforms to the regular expression rule to generate a main sentence test case; and,

[0061] Calling the clause template to match a string sentence pattern that conforms to the regular expression rule to generate a clause test case;

[0062] The main clause test cases and the sub-clause test cases in the same scenario are arranged and cross-combined to form a multi-round test case set.

[0063] Preferably, the processor 1001 can be used to call the test case generation program stored in the memory 1005, and before performing the arrangement and cross-combination of the main clause test case and the clause test case in the same scenario to form a multi-round test case set, further perform the following steps:

[0064] Calculating the cosine similarity between two of the main clause test cases or between two of the sub-clause test cases;

[0065] Determining whether the cosine similarity is greater than or equal to a preset threshold;

[0066] If yes, after removing any case from the main clause test case or the subclause test case, continue to calculate the cosine similarity until it is determined that the cosine similarity is less than the preset threshold.

[0067] Preferably, the processor 1001 can be used to call the test case generation program stored in the memory 1005, and further perform the following steps:

[0068] Obtaining a branch node of the software program under test;

[0069] A fitness function that meets the test coverage criterion is constructed based on the branch nodes in combination with a preset branch distance function.

[0070] Preferably, the processor 1001 can be used to call the test case generation program stored in the memory 1005, and further perform the following steps:

[0071] Inputting the test case into the software program under test to perform a test run, so as to collect all the path coverage information of the test case for the software program under test;

[0072] The fitness function is called to calculate the fitness value of the test case for the software program under test based on the path coverage information.

[0073] Preferably, the processor 1001 can be used to call the test case generation program stored in the memory 1005, and further perform the following steps:

[0074] The preset differential evolution algorithm is called to iteratively search for an optimal solution according to the fitness value, so as to determine an optimal test case in the test case that covers all branches of the software program under test.

[0075] Preferably, the differential evolution algorithm is called to perform optimization operations on the test case to obtain an optimized and updated optimal solution;

[0076] If it is determined that the fitness value of the test case reaches a preset target error, outputting the optimal solution;

[0077] If it is determined that the fitness value does not reach the preset target error, the optimization operation is re-performed on the test case to obtain a new optimal solution and calculate a new fitness value, until the fitness value reaches the preset target error and the new optimal solution is output.

[0078] Based on the above hardware structure, various embodiments of the method for generating a test case of the present invention are proposed.

[0079] It should be noted that with the advancement of modern technology and the rapid development of computers, various computer software has become a necessity in life. As a result, people's requirements for the quality of software used in daily life have gradually become higher. Software testing is an important technical means to test software quality, and designing test cases is an important link and core difficulty in software testing. Designing automatic generation of test cases will greatly improve the efficiency of software testing and save a lot of manpower and material resources. Therefore, the research topic of automatic generation of test cases has important theoretical value and practical significance.

[0080] The automated test case generation scheme generally considers two aspects: first, how to automatically generate the test case set; second, the effectiveness of the test case. Generally speaking, the higher the coverage of the test case, the better the test case. So how to improve the effectiveness of the automated test case set through algorithms.

[0081] However, although there are some algorithm-based automated test case generation solutions, the existing solutions cannot achieve mass production of test cases, and the generated test cases have low coverage for the software under test, resulting in poor effectiveness.

[0082] In view of this, the present invention proposes a method for generating test cases, standardizes the design method for automatic test case generation, firstly, based on a case template generated based on regular expressions, batch test cases are generated, and multi-scenario and multi-round dialogue cases are constructed; then, how to automatically generate the optimal test cases is transformed into a function optimization problem by constructing a fitness function; finally, on the basis of the search-based test case generation framework, the coverage criterion is selected as the path branch coverage, that is, firstly, a particle swarm is randomly selected as the input parameter of the test function, and the particle swarm is iteratively updated by the differential evolution algorithm (and for ease of understanding and explanation, the DE algorithm is used as a substitute for description, where the full name of DE is Differential Evolution) until the termination condition is reached to output the optimal test case. The purpose of this method is to optimize the path branch coverage of the test case and realize the adaptive adjustment of the path. The present invention proposes a method for generating test cases, which can automatically generate effective test cases (including single round, multi-round, and multi-scenario interactions) in batches, automatically cover code program branches, and help to quickly and accurately find the optimal test case. The test efficiency is high, and the test case coverage is complete after the algorithm optimization, which improves the test accuracy.

[0083] In view of the above phenomenon and technical concept, various embodiments of a method for generating a test case provided by the present invention are proposed.

[0084] Please refer to Figure 2 , Figure 2The figure is a flow chart of a first embodiment of a method for generating a test case of the present invention. In this embodiment, the method for generating a test case is applied to the above-mentioned terminal device. The method for generating a test case includes:

[0085] Step S10, generating a test case according to a preset sentence template, wherein the sentence template is generated based on a regular expression rule setting;

[0086] The terminal device calls the sentence template that is pre-generated based on the regular expression rule settings, and automatically generates batches of test cases by randomly matching keywords.

[0087] It should be noted that, in the present embodiment, the number of sentence templates is greater than or equal to one, and, when there are multiple sentence templates, each sentence template is classified according to the scenario. In addition, each sentence template is composed of multiple keywords-keys, and each keyword-key corresponds to multiple value values. In this way, the terminal device can specifically use each sentence template to generate a random match of multiple character strings composed of keywords-keys, and fill the value values ​​into the corresponding keyword-key slots, so as to automatically and batch generate test cases for testing the software program under test.

[0088] Step S20, constructing a fitness function that meets the test coverage criteria, and calculating the fitness value of the test case for the tested software program according to the fitness function;

[0089] After automatically generating batches of test cases, the terminal device further constructs a fitness function that meets the test coverage criteria, and then uses the fitness function to calculate the fitness value of the software program under test for the test cases input into the software program under test for test operation.

[0090] Specifically, for example, in this embodiment, the terminal device inputs the test case for the test run of the software program under test as the input of the constructed fitness function that meets the test coverage criteria, first screens and outputs the valid test cases in the test case, and further initializes the search algorithm to use the particle swarm obtained by optimizing the valid test case after each iteration of the DE algorithm as the input parameter of the software program under test, and then, by starting to run the test operation on the software program under test, the path coverage information of the valid test case for the software program under test is collected, and then the fitness function uses the path coverage information to calculate the fitness value of the test case currently used as the input parameter.

[0091] It should be noted that, in this embodiment, the test case input into the tested software program for test running may be only one, or may be a test case set consisting of multiple test cases.

[0092] Step S30, iteratively searching for an optimal solution according to the fitness value to determine the optimal test case among the test cases.

[0093] After the terminal device uses the fitness function to calculate the fitness value of the test case for the software program under test, it iterates according to the fitness value to find the optimal solution, thereby determining the optimal test case among the generated test cases that can fully cover the software program under test.

[0094] Specifically, for example, in this embodiment, after the terminal device calculates the fitness value of the test case currently used as an input parameter by calling the fitness function and using the test case's path coverage information for the software program under test, it further continues to adaptively iterate according to the quality of the fitness value through a search algorithm to find a better solution, and finally determines that the iterative process meets the termination condition of the DE algorithm, the optimal test case that can fully cover the software program under test can be obtained.

[0095] It should be noted that, in this embodiment, the terminal device iterates through the fitness value, so that the current test case can continuously approach the optimal position during each iteration of the DE algorithm. In this way, the optimal test case or the final test case set that can cover all branches of the software program under test can be found by performing optimization operations through loop iteration.

[0096] The embodiment of the present invention provides a method for generating test cases, which automatically generates batches of test cases by calling a sentence template that is pre-generated based on a regular expression rule setting through a terminal device, and by randomly matching keywords; after automatically generating batches of test cases, the terminal device further constructs a fitness function that meets the test coverage criterion, and then uses the fitness function to calculate the fitness value of the test case input into the software program under test for test operation; after the terminal device uses the fitness function to calculate the fitness value of the test case for the software program under test, it iterates according to the fitness value to find the optimal solution, thereby determining the optimal test case among the generated test cases that can fully cover the software program under test.

[0097] Compared with the traditional method of generating automated test cases, the present invention sets a sentence template based on a regular expression, so that the sentence template can be used to automatically generate batches of test cases. In addition, the present invention also calculates the fitness value of the generated test case for the software program under test by constructing a fitness function, and then performs an iterative optimization operation based on the fitness value to determine the optimal test case among the generated test cases. In this way, the coverage of the test case for the software program under test can be greatly improved, thereby ensuring the effectiveness of the generated test case for the software program under test, and further improving the test accuracy of the software program under test.

[0098] Further, based on the above first embodiment, a second embodiment of the method for generating a test case of the present invention is proposed. The main difference between this embodiment and the above first embodiment is that, in this embodiment, the sentence template includes a main sentence template and a clause template, and step S10 in the above first embodiment may include:

[0099] Step S101, calling the main sentence template to match the string sentence pattern that conforms to the regular expression rule to generate a main sentence test case;

[0100] Step S102, calling the clause template to match the string sentence pattern that conforms to the regular expression rule to generate a clause test case;

[0101] The terminal device generates a sentence template based on a regular expression in advance, and then, by calling the main sentence template in the sentence template, matches the keywords owned by the main sentence template to obtain a string sentence that conforms to the regular expression rule as a main sentence test case. In addition, the terminal device calls the clause template in the generated sentence template, and matches the keywords owned by the clause template to obtain a string sentence that conforms to the regular expression rule as a clause test case.

[0102] It should be noted that in this embodiment, when the terminal device sets multiple sentence templates, each sentence template is classified according to the scenario, and each sentence model is divided into a main sentence template and a clause template. The value values ​​corresponding to the keywords-key owned by the main sentence template and the clause template are also classified according to the scenario.

[0103] Specifically, for example, in this embodiment, please refer to Figure 4 The sentence model shown (in this embodiment and other embodiments described herein, the sentence model is the sentence template mentioned in the method for generating a test case of the present invention), assuming that the sentence template generated by the terminal device based on the regular expression is , the sentence template Include the main clause template: , and the main clause template By the keyword-key value in the main clause template string: The key value is: They correspond to the following values: In addition, the sentence template Also includes clause templates: , similarly, the clause template: By keyword-key value in clause template string: The key value is: They correspond to the following values: .

[0104] The terminal device calls each main sentence template: Based on the main clause template Keyword-key value in string: Randomly match multiple strings and their respective keyword-key values: The slot is filled with the corresponding value: , thereby automatically and batch-generating main clause test cases.

[0105] In addition, the terminal device calls each clause template: Based on the clause template Keyword-key value in string: Randomly match multiple strings and their respective keyword-key values: The slot is filled with the corresponding value: , thereby automatically and batch-generating clause test cases.

[0106] Step S103, arranging and cross-combining the main clause test cases and the sub-clause test cases in the same scenario to form a multi-round test case set.

[0107] After the terminal device generates batches of main clause test cases and sub-clause test cases by randomly matching by calling the main clause template and the sub-clause template respectively, it further arranges and cross-combines the main clause test cases and sub-clause test cases classified into the same scenario, thereby forming a multi-round test case set for testing the software program under test.

[0108] It should be noted that, in this embodiment, please refer to Figure 5 Assume that the main sentence test case set generated by the terminal device is represented as { , ,... , }, the clause test case set is represented as { , , ..., Then, the terminal device forms a multi-round test case set by arranging and cross-combining the main clause test case set and the clause test case set under the same scenario, which can be expressed as { , , ..., }.

[0109] Furthermore, in a feasible embodiment, before the above step S103, the method for generating a test case of the present invention may further include:

[0110] Step S104, calculating the cosine similarity between two of the main clause test cases or between two of the sub-clause test cases;

[0111] Before arranging and cross-combining the main clause test cases and the sub-clause test cases classified into the same scenario, the terminal device also pre-calculates the cosine similarity between the two main clause test cases and the cosine similarity between the two sub-clause test cases for the main clause test cases and the sub-clause test cases classified into the same scenario.

[0112] Specifically, for example, in this embodiment, it is assumed that the terminal device calls the main sentence template Automatically and batch-generate n main clause test cases, and the n main clause test cases are in the form of a test case set { , , ..., In this way, the terminal device calculates the cosine similarity of the test cases in the test case set in pairs. =sim( , )、 = sim( , )......

[0113] It should be noted that, in this embodiment, the terminal device performs cosine similarity calculation for two test cases as follows:

[0114]

[0115] Similarly, the terminal device uses the same process as above to calculate the cosine similarity between each pair of automatically generated clause test cases.

[0116] Step S105, determining whether the cosine similarity is greater than or equal to a preset threshold;

[0117] Step S106: If yes, after removing any test case from the main clause test case or the subclause test case, continue to calculate the cosine similarity until it is determined that the cosine similarity is less than the preset threshold.

[0118] After calculating the cosine similarity between two main clause test cases and the cosine similarity between two clause test cases, the terminal device further compares the cosine similarity with a preset threshold to determine whether the cosine similarity is greater than or equal to the preset threshold. Thus, when it is determined through comparison that the cosine similarity is greater than or equal to the preset threshold, any test case is removed from each main clause test case or each clause test case. Then, the terminal device continues to calculate the cosine similarity between two main clause test cases and the cosine similarity between two clause test cases from the remaining main clause test cases or each clause test cases after the test case is removed, so as to further compare the cosine similarity with the preset threshold to determine whether it is greater than or equal to the preset threshold, and repeat this cycle until it is determined that the calculated cosine similarity is less than the preset threshold.

[0119] Specifically, for example, in this embodiment, assuming that the preset threshold is t, if the threshold t is set, if the terminal device calculates the cosine similarity of the two main sentence test users >t, then the terminal device arbitrarily removes all the main clause test cases automatically generated or , then the terminal device further calculates all the main clause test cases to remove or After that, the cosine similarity between the remaining main sentence test cases is , and in the >t, further remove any of the remaining main clause test cases or , and so on, until the cosine similarity sim of all the main sentence test case sets is less than the threshold t, then a new main sentence test case set is formed { , ,... , }.

[0120] In addition, similarly, if the terminal device calculates the cosine similarity of the two clauses testing the user >t, then the terminal device is arbitrarily removed from all clause test cases automatically generated or , then the terminal device further calculates the entire clause test case to remove or After that, the cosine similarity between the remaining clause test cases is , and in the >t, further remove any of the remaining main clause test cases or , and so on, until the cosine similarity sim of each pair of cases in all clause test case sets is less than the threshold t, then a new clause test case set { , , ..., }.

[0121] In this embodiment, compared with the traditional method of generating automated test cases, the present invention uses regular expression template matching technology to generate all string sentences that meet the rules through template matching. The string sentence expression consists of several keys, and the key maps several values. The value is automatically and randomly filled with Chinese phrases and Chinese keywords, so that batches of test cases can be generated. In addition, the present invention classifies sentence templates according to scenes, main sentences, and clauses. The main sentence case set and the clause case set also use cosine similarity to achieve case deduplication. The deduplicated main sentence case set and the clause case set are arranged and combined to construct an efficient multi-round, multi-scenario interactive text dialogue test case set.

[0122] Further, based on the above first embodiment, a third embodiment of the method for generating test cases of the present invention is proposed. The main difference between this embodiment and the above first embodiment is that in this embodiment, in step S20 in the above first embodiment, the step of "constructing a fitness function that meets the test coverage criterion" may include:

[0123] Step S201, obtaining a branch node of the software program under test;

[0124] In the process of constructing a fitness function that meets the test coverage criterion, the terminal device first obtains all branch nodes of the software program under test that currently needs to be tested.

[0125] Specifically, for example, in this embodiment, assuming that the current software program under test has N branches, and the input test cases have a total of m dimensions, the terminal device obtains corresponding interface information after statically analyzing the software program under test, thereby determining all N branch nodes of the software program under test.

[0126] Step S202: constructing a fitness function that meets the test coverage criterion according to the branch nodes in combination with a preset branch distance function.

[0127] After obtaining all the branch nodes of the software program under test, the terminal device further constructs a fitness function that meets the test coverage criterion based on the branch nodes in combination with a preset branch distance function.

[0128] It should be noted that, in this embodiment, the preset branch distance function can be specifically , … .

[0129] Specifically, for example, in this embodiment, after determining all N branch nodes of the software program under test, the terminal device performs an insertion operation before each branch node in the software program under test, and adds a branch distance function corresponding to the current branch. , … , and then construct the fitness function by superimposing all branch functions: .

[0130] It should be noted that, in this embodiment, when =0, it indicates that the current test case has achieved full coverage of all branches of the tested software program, that is, the coverage rate reaches 100%. In this way, in order to normalize the result value, when the coverage rate ranges between 0-100, the fitness function constructed by the terminal device can be specifically expressed as follows:

[0131] ( = ).

[0132] Further, in a feasible embodiment, in the above step S20, the step of "calculating the fitness value of the test case for the tested software program according to the fitness function" may include:

[0133] Step S203, inputting the test case into the software program under test to perform a test run, so as to collect all the path coverage information of the test case for the software program under test;

[0134] When the terminal device calculates the fitness value of the test case for the software program under test, it first inputs the generated test case into the software program under test for test running. In this way, during the process of the software program under test running based on the test case, the test case collects all path coverage information for the software program under test.

[0135] Step S204: calling the fitness function to calculate the fitness value of the test case for the software program under test based on the path coverage information.

[0136] After collecting all the path coverage information of the test case for the software program under test, the terminal device further calls the pre-constructed fitness function to use the path coverage information for calculation to obtain the fitness value of the test case for the software program under test.

[0137] Further, in a feasible embodiment, step S30 in the first embodiment may include:

[0138] Step S301, calling a preset differential evolution algorithm to iteratively search for an optimal solution according to the fitness value, so as to determine an optimal test case in the test case that covers all branches of the software program under test.

[0139] After the terminal device uses the fitness function to calculate the fitness value of the test case for the software program under test, it calls the preset differential evolution algorithm to iterate according to the fitness value to find the optimal solution, thereby determining the optimal test case among the generated test cases that can fully cover the software program under test.

[0140] It should be noted that, in the present embodiment, the terminal device initializes the input parameters of the program interface of the software program under test by calling the preset differential evolution algorithm - DE algorithm, and further passes in the test case data to schedule the interface program for test operation. After that, the terminal device collects the path coverage information of the test case for all the software program under test. Finally, the terminal device further analyzes and determines whether the test case has reached the optimal position based on the call to the DE algorithm using the path coverage information. If so, the test case is outputted. Otherwise, the optimization process of the test case is continued to be iterated.

[0141] Furthermore, in a feasible embodiment, the above step S301 may include:

[0142] Step S3011, calling the differential evolution algorithm to perform an optimization operation on the test case to obtain an optimized and updated optimal solution;

[0143] Step S3012: if it is determined that the fitness value of the test case reaches a preset target error, outputting the optimal solution;

[0144] Step S3013, if it is determined that the fitness value does not reach the preset target error, re-optimization operation is performed on the test case to obtain a new optimal solution and a new fitness value is calculated, until the fitness value reaches the preset target error and the new optimal solution is output.

[0145] Specifically, for example, in this embodiment, please refer to Figure 6The application process shown. The terminal device performs initialization operation by calling the DE algorithm to initialize the DE particle swarm control parameters: population size NP, scaling factor F and hybridization probability CR, thereby randomly generating an initial population S. Then, the terminal device evaluates the initial population S by calling a pre-constructed fitness function, that is, the fitness value of each individual in the initial population S is calculated based on the fitness function. Then, the terminal device uses the DE algorithm to perform individual optimization operations on the initial population S one by one, and performs mutation, hybridization, and selection operations, and updates the individual optimal solution: And the group optimal solution: Finally, the terminal device determines whether the calculated fitness value reaches the preset target error, that is, whether the fitness value reaches: the difference between the optimal solutions of the algorithm for K consecutive iterations is less than or equal to a certain accuracy value If it is judged that it has not been reached, the terminal device performs chaotic search and turns to the process of evaluating the initial population S. Otherwise, the terminal device outputs the optimal value of the group to the best set of test cases.

[0146] In this embodiment, the terminal effectively improves the problem of the fitness function extreme value falling into the local optimum by calling the DE algorithm and introducing the chaos algorithm in the DE algorithm, so as to obtain the test case set with the best coverage for the tested software program. In addition, based on the introduction of the chaos algorithm, the overall convergence speed is also accelerated, thereby reducing the time for generating automated test cases.

[0147] Furthermore, based on the first embodiment, the second embodiment and the third embodiment of the method for generating a test case of the present invention, an optimal embodiment of the method for generating a test case of the present invention is proposed.

[0148] In this embodiment, please refer to Figure 3 The application scenario shown. The terminal device of the embodiment of the present invention realizes automatic batch generation of test cases through the use case module, the driver module and the algorithm module, and improves the coverage rate of the test cases for the tested software program, thereby ensuring the effectiveness of the generated test cases for the tested software program, and further improving the test accuracy of the tested software program.

[0149] Specifically, for example, in this embodiment, through the use case module: set a sentence template generated based on regular expression rules, each sentence template consists of multiple keywords keys, each key corresponds to multiple value values, each sentence template generates a random match of multiple strings consisting of keys, and fills the corresponding key slots with the value values, so as to generate batch use cases. In addition, through the driver module: the test driver module first takes the output of the algorithm module as input, and after running the program, collects the coverage information of the test case and automatically feeds it back to the algorithm module. And, through the algorithm module: according to the coverage information fed back by the driver module, schedule the fitness function, obtain the fitness value of the test case and judge it. This module is the key to the overall framework of the test case automatic generation algorithm. In each iteration of the algorithm, the current use case is continuously approached to the optimal position and executed in a loop until the optimal test case set covering all branches of the program is found.

[0150] Furthermore, the present invention also provides a test case generation system, the test case generation system includes a client. Figure 7 , Figure 7 FIG. 1 is a functional module diagram of an embodiment of a test case generation system of the present invention. Figure 7 As shown, the test case generation system of the present invention includes:

[0151] A test case generation module 10, for generating a test case according to a preset sentence template, wherein the sentence template is generated based on a regular expression rule setting;

[0152] The fitness calculation module 20 is used to construct a fitness function that meets the test coverage criteria, and calculate the fitness value of the test case for the tested software program according to the fitness function;

[0153] The optimization module 30 is used to iteratively search for an optimal solution according to the fitness value to determine the optimal test case among the test cases.

[0154] Furthermore, the sentence template includes a main sentence template and a clause template, and the use case generation module 10 includes:

[0155] A first generating unit is used to call the main sentence template to match the string sentence pattern that conforms to the regular expression rule to generate a main sentence test case; and

[0156] A second generating unit is used to call the clause template to match the string sentence pattern that conforms to the regular expression rule to generate a clause test case;

[0157] The multi-round test case combination unit is used to arrange and cross-combine the main clause test cases and the sub-clause test cases in the same scenario to form a multi-round test case set.

[0158] Furthermore, the use case generation module 10 further includes:

[0159] A calculation unit, used for calculating the cosine similarity between two main clause test cases or between two subclause test cases;

[0160] A determination unit, used to determine whether the cosine similarity is greater than or equal to a preset threshold;

[0161] A deduplication unit is used to continue calculating the cosine similarity after removing any case from the main clause test case or the subclause test case until it is determined that the cosine similarity is less than the preset threshold.

[0162] Furthermore, the fitness calculation module 20 includes:

[0163] An acquisition unit, used for acquiring a branch node of the software program under test;

[0164] The first calculation unit is used to construct a fitness function that meets the test coverage criterion according to the branch node in combination with a preset branch distance function.

[0165] Furthermore, the fitness calculation module 20 further includes:

[0166] A quasi-testing unit, used for inputting the test case into the software program under test to perform a test run, so as to collect all the path coverage information of the test case for the software program under test;

[0167] The second calculation unit is used to call the fitness function to calculate the fitness value of the test case for the tested software program based on the path coverage information.

[0168] Furthermore, the optimization module 30 is also used to call a preset differential evolution algorithm to iteratively search for an optimal solution according to the fitness value, so as to determine the optimal test case in the test case that covers all branches of the software program under test.

[0169] Furthermore, the optimization module 30 includes:

[0170] An optimization unit, used for calling the differential evolution algorithm to perform optimization operations on the test case to obtain an optimized and updated optimal solution;

[0171] A first optimization unit, configured to output the optimal solution if it is determined that the fitness value of the test case reaches a preset target error;

[0172] The second optimization unit is used to re-optimize the test case to obtain a new optimal solution and calculate a new fitness value if it is determined that the fitness value has not reached the preset target error, and output the new optimal solution after the fitness value reaches the preset target error.

[0173] Among them, the functional implementation of each module in the above-mentioned test case generation system corresponds to the various steps in the above-mentioned test case generation method embodiment, and its functions and implementation processes are no longer repeated here one by one.

[0174] The present invention also provides a computer storage medium on which a test case generation program is stored. When the test case generation program is executed by a processor, the steps of the test case generation method described in any of the above embodiments are implemented.

[0175] The specific embodiments of the computer storage medium of the present invention are basically the same as the embodiments of the test case generation method described above, and will not be described in detail here.

[0176] The present invention also provides a computer program product, which includes a computer program. When the computer program is executed by a processor, the steps of the test case generation method described in any of the above embodiments are implemented.

[0177] The specific embodiments of the computer storage medium of the present invention are basically the same as the embodiments of the test case generation method described above, and will not be described in detail here.

[0178] It should be noted that, in this article, the terms "include", "comprises" or any other variations thereof are intended to cover non-exclusive inclusion, so that a process, method, article or system including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, article or system. In the absence of further restrictions, an element defined by the sentence "comprises a ..." does not exclude the existence of other identical elements in the process, method, article or system including the element.

[0179] The serial numbers of the above embodiments of the present invention are only for description and do not represent the advantages or disadvantages of the embodiments.

[0180] 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 invention is essentially 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, magnetic disk, optical disk) as described above, and includes a number of instructions for a terminal device (which can be a mobile phone, a computer, a server, or a network device, etc.) to execute the methods described in each embodiment of the present invention.

[0181] The above are only preferred embodiments of the present invention, and are not intended to limit the patent scope of the present invention. Any equivalent structure or equivalent process transformation made using the contents of the present invention specification and drawings, or directly or indirectly applied in other related technical fields, are also included in the patent protection scope of the present invention.

Claims

1. A method for generating test cases, It is characterized in that The test case generation method comprises: Generating test cases by randomly matching keywords according to preset sentence templates, wherein the sentence templates are generated based on regular expression rule settings, wherein each sentence template consists of multiple keywords, and each keyword corresponds to multiple values; Constructing a fitness function that meets the test coverage criteria, and calculating the fitness value of the test case for the tested software program according to the fitness function; Iteratively searching for an optimal solution according to the fitness value to determine the optimal test case among the test cases; The step of generating test cases by randomly matching keywords according to a preset sentence template includes: Use each sentence template to randomly match multiple strings consisting of keyword keys, and fill multiple value values ​​corresponding to the keyword keys into the slots where the keyword keys are located to generate test cases.

2. The method for generating a test case according to claim 1, It is characterized in that The sentence template includes a main sentence template and a clause template. The step of generating a test case according to the preset sentence template also includes: Calling the main sentence template to match the string sentence pattern that conforms to the regular expression rule to generate a main sentence test case; and, Calling the clause template to match a string sentence pattern that conforms to the regular expression rule to generate a clause test case; The main clause test cases and the sub-clause test cases in the same scenario are arranged and cross-combined to form a multi-round test case set.

3. The method for generating a test case according to claim 2, It is characterized in that Before the step of arranging and cross-combining the main clause test cases and the sub-clause test cases in the same scenario to form a multi-round test case set, the method further includes: Calculating the cosine similarity between two of the main clause test cases or between two of the sub-clause test cases; Determining whether the cosine similarity is greater than or equal to a preset threshold; If yes, after removing any case from the main clause test case or the subclause test case, continue to calculate the cosine similarity until it is determined that the cosine similarity is less than the preset threshold.

4. The method for generating a test case according to claim 1, It is characterized in that The step of constructing a fitness function that meets the test coverage criterion includes: Obtaining a branch node of the software program under test; A fitness function that meets the test coverage criterion is constructed based on the branch nodes in combination with a preset branch distance function.

5. The method for generating a test case according to claim 1 or 4, It is characterized in that The step of calculating the fitness value of the test case for the tested software program according to the fitness function comprises: Inputting the test case into the software program under test to perform a test run, so as to collect all the path coverage information of the test case for the software program under test; The fitness function is called to calculate the fitness value of the test case for the software program under test based on the path coverage information.

6. The method for generating a test case according to claim 1, It is characterized in that The step of iteratively searching for the optimal solution according to the fitness value to determine the optimal test case among the test cases comprises: The preset differential evolution algorithm is called to iteratively search for an optimal solution according to the fitness value, so as to determine an optimal test case in the test case that covers all branches of the software program under test.

7. The method for generating a test case according to claim 6, It is characterized in that The step of calling a preset differential evolution algorithm to iteratively search for an optimal solution according to the fitness value includes: Calling the differential evolution algorithm to perform optimization operations on the test case to obtain an optimized and updated optimal solution; If it is determined that the fitness value of the test case reaches a preset target error, outputting the optimal solution; If it is determined that the fitness value does not reach the preset target error, the optimization operation is re-performed on the test case to obtain a new optimal solution and calculate a new fitness value, until the fitness value reaches the preset target error and the new optimal solution is output.

8. A test case generation system, It is characterized in that The test case generation system comprises: A test case generation module is used to generate test cases by randomly matching keywords according to a preset sentence template, wherein the sentence template is generated based on a regular expression rule setting, wherein each sentence template consists of multiple keywords, and each keyword corresponds to multiple values; A fitness calculation module, used to construct a fitness function that meets the test coverage criteria, and calculate the fitness value of the test case for the tested software program according to the fitness function; An optimization module, used for iteratively searching for an optimal solution according to the fitness value, so as to determine an optimal test case among the test cases; Among them, the use case generation module is also used to use each sentence template to randomly match multiple character strings composed of keyword keys, and fill multiple value values ​​corresponding to the keyword keys into the slots where the keyword keys are located, so as to generate test cases.

9. A terminal device, It is characterized in that The terminal device includes: a memory, a processor, and a test case generation program stored in the memory and executable on the processor. When the test case generation program is executed by the processor, the steps of the test case generation method described in any one of claims 1 to 7 are implemented.

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