Test case generation method and device
By automatically obtaining the target parameters and data dictionary of the API, generating and selecting candidate test cases, the problem of time-consuming and labor-intensive writing of API test cases is solved, and fast response and efficient test case generation is achieved, ensuring the comprehensiveness and accuracy of the API.
Patent Information
- Application Number
- CN202510477377.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-04-16
- Publication Date
- 2025-07-25
AI Technical Summary
The test cases of APIs in the prior art need to be written manually, which is time-consuming and labor-intensive, and information cannot be obtained in time when API changes, resulting in inefficient testing and errors in non-system problems.
By automatically obtaining target parameters and their data dictionary when monitoring API updates, multiple candidate test cases are generated, and target test cases are selected using the overlay code information to achieve automated processing and rapid response.
Reduces manual intervention, shortens the test cycle, improves the comprehensiveness and accuracy of the test, and ensures that the various functions and boundary conditions of the API are fully verified and potential problems or vulnerabilities are found.
Smart Images

Figure CN120371704A_ABST
Abstract
Description
Technical Field
[0001] The present disclosure relates to the field of computer technologies, and in particular, to a method and apparatus for generating test cases. Background Art
[0002] With the rise of microservice architectures and cloud computing technologies, the invocation of Application Programming Interfaces (APIs) between different software systems and services has become a key way to achieve data exchange, function integration, and business collaboration. To ensure that these APIs can work stably, return data correctly, and meet performance, security, and compatibility requirements under various conditions, developers and test teams need to design detailed test cases for each API.
[0003] In related technologies, test cases for APIs need to be written manually. Manual writing of test cases is not only time-consuming and laborious, but also when the API changes, the testing party cannot timely obtain information about the API changes, and a large number of error reports of non-system problems will occur when executing the test cases, reducing the test efficiency. Summary of the Invention
[0004] The present disclosure provides a method and apparatus for generating test cases to at least solve one of the technical problems in related technologies to some extent. The technical solutions of the present disclosure are as follows:
[0005] According to a first aspect of an embodiment of the present disclosure, a method for generating test cases is provided, including: in response to detecting an update of an Application Programming Interface (API), obtaining at least one target parameter associated with the API and a data dictionary of each of the target parameters; where the data dictionary is used to describe detailed information of the corresponding target parameter; generating a plurality of candidate test cases according to the at least one target parameter and the corresponding data dictionary; performing simulation testing on the API using each of the candidate test cases to obtain coverage code information of each of the candidate test cases; and determining a target test case for testing the API from the plurality of candidate test cases based on the coverage code information of each of the candidate test cases.
[0006] According to a second aspect of the embodiments of the present disclosure, there is provided a test case generation device, including: an acquisition module, configured to obtain at least one target parameter associated with the API and a data dictionary of each of the target parameters in response to detecting an update of an application programming interface (API); wherein the data dictionary is used to describe detailed information of the corresponding target parameter; a generation module, configured to generate a plurality of candidate test cases for testing the API according to the at least one target parameter and the data dictionary of each of the target parameters; a simulation module, configured to perform a simulation test on the API by using each of the candidate test cases to obtain coverage code information of each of the candidate test cases; and a determination module, configured to determine a target test case for testing the API from the plurality of candidate test cases based on the coverage code information of each of the candidate test cases.
[0007] According to a third aspect of the embodiments of the present disclosure, there is provided an electronic device, including: a processor; and a memory for storing executable instructions of the processor; wherein the processor is configured to execute the instructions to implement the test case generation method as described in the first aspect embodiment of the present disclosure.
[0008] According to a fourth aspect of the embodiments of the present disclosure, there is provided a computer-readable storage medium, when instructions in the computer-readable storage medium are executed by a processor of an electronic device, enabling the electronic device to execute the test case generation method as described in the first aspect embodiment of the present disclosure.
[0009] According to a fifth aspect of the embodiments of the present disclosure, there is provided a computer program product, including: a computer program, which when executed by a processor, implements the test case generation method as described in the first aspect embodiment of the present disclosure.
[0010] The technical solutions provided by the embodiments of the present disclosure at least bring the following beneficial effects:
[0011] In this technical solution, when it is detected that the API is updated, the target parameters associated with the API and their data dictionaries are automatically obtained, and multiple candidate test cases are generated for simulation testing. Finally, the target test cases are determined based on the covered code information. Thus, automation is achieved, reducing the need for manual intervention. Especially when facing frequently updated APIs, it can quickly respond to changes and automatically generate relevant test cases, greatly shortening the test cycle. Secondly, generating candidate test cases based on a detailed data dictionary ensures the comprehensiveness and accuracy of the test, helping to discover potential problems or vulnerabilities. In addition, using the covered code information to select the target test cases finally used for testing not only optimizes the use of test resources but also ensures that all functions and boundary conditions of the API are fully verified. Among them, when generating multiple candidate test cases based on at least one target parameter and the corresponding data dictionary, initial test cases are generated based on the orthogonal combination rules generated from at least one target parameter and the data dictionaries of each target parameter, achieving that the test cases cover all possible parameter combinations and reducing potential defects caused by missing important combinations. Furthermore, through at least one round of iterative process, the initial test cases are optimized to obtain multiple candidate test cases, achieving the removal of invalid and redundant test cases, thereby generating more effective and targeted test cases.
[0012] It should be understood that the above general description and the following detailed description are only exemplary and explanatory, and cannot limit the present disclosure. BRIEF DESCRIPTION OF THE DRAWINGS
[0013] The drawings herein are incorporated into the specification and constitute a part of this specification, showing embodiments consistent with the present disclosure, and are used together with the specification to explain the principles of the present disclosure, and do not constitute an improper limitation of the present disclosure.
[0014] Figure 1 is a schematic flowchart of a method for generating test cases shown in the first embodiment of the present disclosure;
[0015] Figure 2 is a schematic flowchart of a method for generating test cases shown in the second embodiment of the present disclosure;
[0016] Figure 3 is a schematic flowchart of a method for generating test cases shown in the third embodiment of the present disclosure;
[0017] Figure 4 is a schematic flowchart of a method for generating test cases shown in the fourth embodiment of the present disclosure;
[0018] Figure 5 is a schematic diagram of the principle of a method for generating test cases shown in the embodiments of the present disclosure;
[0019] Figure 6 It is a schematic diagram of the principle for determining the update of the API shown in the embodiments of the present disclosure;
[0020] Figure 7 It is a schematic structural diagram of a test case generation device shown in the fifth embodiment of the present disclosure;
[0021] Figure 8 It is a schematic structural diagram of an electronic device shown in an exemplary embodiment of the present disclosure. Detailed implementation manners
[0022] In order to enable those of ordinary skill in the art to better understand the technical solutions of the present disclosure, the technical solutions in the embodiments of the present disclosure will be clearly and completely described below with reference to the accompanying drawings.
[0023] It should be noted that the terms "first", "second", etc. in the specification and claims of the present disclosure and the above-mentioned drawings are used to distinguish similar objects, and do not necessarily need to describe a specific order or sequence. It should be understood that such used data can be interchanged under appropriate circumstances so that the embodiments of the present disclosure described herein can be implemented in an order other than those illustrated or described herein. The implementation manners described in the following exemplary embodiments do not represent all implementation manners consistent with the present disclosure. On the contrary, they are merely examples of devices and methods consistent with some aspects of the present disclosure as detailed in the appended claims.
[0024] It should be noted that in the technical solutions of the present disclosure, the collection, storage, use, processing, transmission, provision, and disclosure of the user's personal information and other processing are all carried out on the premise of obtaining the user's consent, and all comply with the provisions of relevant laws and regulations and do not violate public order and good customs.
[0025] The following describes the test case generation method and device of the embodiments of the present disclosure with reference to the drawings.
[0026] Figure 1 It is a schematic flowchart of the test case generation method shown in the first embodiment of the present disclosure. Among them, it should be noted that the execution subject of the embodiments of the present disclosure can be a test case generation device, and this test case generation device can be applied to any electronic device with computing capabilities so that the electronic device can execute the test case generation function.
[0027] As Figure 1 shown, the test case generation method includes the following steps:
[0028] Step 101, in response to monitoring that the application programming interface (API) is updated, obtain at least one target parameter associated with the API and the data dictionary of each target parameter.
[0029] Among them, the data dictionary is used to describe the detailed information of the corresponding target parameters.
[0030] In order to adjust the corresponding test cases in a timely manner when the API is updated, as a possible implementation, monitor the API interface. When it is detected that the API is updated (such as, upgraded, new functions added, or bugs fixed, etc.), generate the corresponding test cases based on the updated API.
[0031] In the embodiments of the present disclosure, compare the APIs in the API management platform with the APIs in the test platform. When the APIs in the API management platform are different from those in the test platform, it is determined that the APIs in the API management platform are updated (such as, added, modified). Furthermore, according to the API documentation or other API-related metadata sources, determine the target parameters of the API and the data dictionary of each target parameter. It should be noted that when an API in the API management platform is deleted, directly delete the test cases of this API in the test platform.
[0032] It should be noted that the data dictionary is a document or structured data set used to describe the detailed information of the corresponding target parameters. The data dictionary provides all the necessary information about the parameters to facilitate the understanding and correct use of these parameters. For example, the data dictionary includes: parameter name, parameter type, allowed values, default range, constraints, etc.
[0033] Step 102: Generate multiple candidate test cases according to at least one target parameter and the corresponding data dictionary.
[0034] In order to improve the comprehensiveness of API testing, as a possible implementation, generate multiple candidate test cases based on all possible input situations of at least one target parameter.
[0035] In the embodiments of the present disclosure, since the data dictionary describes the detailed information of the corresponding target parameters, multiple candidate test cases are generated based on at least one target parameter and the data dictionary of each target parameter.
[0036] Step 103: Use each candidate test case to perform a simulation test on the API to obtain the coverage code information of each candidate test case.
[0037] In order to accurately obtain the coverage code information of each candidate test case, in the embodiments of the present disclosure, perform a simulated call on the API, and during the execution process, record the executed code and the unexecuted code, so as to determine the coverage code information of the candidate test case (such as, the specific code details covered, the coverage rate, etc.) based on the executed code and the unexecuted code.
[0038] Step 104: Based on the covered code information of each candidate test case, determine the target test case for testing the API from multiple candidate test cases.
[0039] To remove redundant test cases and improve testing efficiency, in the embodiments of the present disclosure, test cases that can cover more code paths are selected by analyzing the covered code information of each candidate test case, and test cases that repeatedly cover the same code path are avoided.
[0040] In summary, when it is monitored that the API is updated, by automatically obtaining the target parameters associated with the API and their data dictionaries, generating multiple candidate test cases for simulation testing, and finally determining the target test case according to the covered code information, thus, automated processing is achieved, reducing the need for manual intervention. Especially when facing frequently updated APIs, rapid response to changes and automatic generation of relevant test cases are realized, greatly shortening the testing cycle; secondly, generating candidate test cases based on a detailed data dictionary ensures the comprehensiveness and accuracy of testing, helping to discover potential problems or vulnerabilities; in addition, using the covered code information to select the target test case finally used for testing not only optimizes the use of testing resources but also ensures that all functions and boundary conditions of the API are fully verified, improving the user testing experience.
[0041] To clearly illustrate how multiple candidate test cases are generated according to at least one target parameter and the corresponding data dictionary in the above embodiments, the present disclosure proposes another method for generating test cases.
[0042] Figure 2 It is a schematic flowchart of the method for generating test cases shown in the second embodiment of the present disclosure.
[0043] As Figure 2 shown, the method for generating test cases includes the following steps:
[0044] Step 201: In response to monitoring that the application programming interface (API) is updated, obtain at least one target parameter associated with the API and the data dictionary of each target parameter.
[0045] Wherein, the data dictionary is used to describe the detailed information of the corresponding target parameter.
[0046] Step 202: Generate multiple orthogonal combination rules according to at least one target parameter and the data dictionary of each target parameter.
[0047] To improve the comprehensiveness and accuracy of test cases, as a possible implementation manner, orthogonal combination rules are generated based on all possible situations of each target parameter in the test.
[0048] In an embodiment of the present disclosure, according to at least one target parameter and the data dictionary of each target parameter, determine the number of value takings of each target parameter; according to the number of value takings corresponding to each target parameter and the data dictionary, determine the value set of each target parameter; based on the value sets of each target parameter, generate multiple orthogonal combination rules.
[0049] That is to say, first, according to the target parameter and its data dictionary, determine the number of value takings of each target parameter; then, based on these numbers of value takings and the detailed information of the data dictionary, determine a specific value set for each target parameter, and this value set contains all possible values of this parameter; finally, based on these value sets of the target parameters, use the method of orthogonal design to generate multiple orthogonal combination rules.
[0050] Step 203, create multiple test data based on multiple orthogonal combination rules.
[0051] Furthermore, create test data one by one according to multiple orthogonal combination rules; for example, for each orthogonal combination rule, combine the target parameter and its value takings therein to form a complete test data.
[0052] Step 204, generate multiple candidate test cases according to multiple test data.
[0053] In order to remove invalid and redundant test cases, in an embodiment of the present disclosure, generate multiple initial test cases based on multiple test data, and generate multiple candidate test cases based on multiple initial test cases.
[0054] Step 205, perform a simulation test on the API using each candidate test case to obtain the covered code information of each candidate test case.
[0055] Step 206, based on the covered code information of each candidate test case, determine the target test cases for testing the API from multiple candidate test cases.
[0056] It should be noted that the execution processes of Step 201, Steps 205 to 206 can be implemented in any one of the embodiments of the present disclosure respectively. The embodiments of the present disclosure do not make any limitations thereto and will not be elaborated further.
[0057] In summary, by generating multiple orthogonal combination rules according to at least one target parameter and the data dictionary of each target parameter; creating multiple test data based on multiple orthogonal combination rules; generating multiple candidate test cases according to multiple test data, thus ensuring that the key business logics and edge cases are fully verified, improving the comprehensiveness and efficiency of testing, and thereby effectively discovering possible problems of the software under different parameter combinations, and enhancing the efficiency and reliability of software development.
[0058] To clearly illustrate how multiple candidate test cases are generated based on multiple test data in the above embodiments, the present disclosure proposes another method for generating test cases.
[0059] Figure 3 It is a schematic flowchart of the method for generating test cases shown in the third embodiment of the present disclosure.
[0060] As Figure 3 shown, the method for generating test cases includes the following steps:
[0061] Step 301, in response to detecting an update to the application programming interface API, obtain at least one target parameter associated with the API and the data dictionary of each target parameter.
[0062] Wherein, the data dictionary is used to describe the detailed information of the corresponding target parameter.
[0063] Step 302, generate multiple orthogonal combination rules according to at least one target parameter and the data dictionary of each target parameter.
[0064] Step 303, create multiple test data based on the multiple orthogonal combination rules.
[0065] Step 304, sequentially fill each test data into the input parameters of the API to obtain multiple initial test cases.
[0066] To improve the comprehensiveness of API testing, as a possible implementation, each test data is sequentially filled into the input parameters of the API to obtain multiple initial test cases.
[0067] In the embodiments of the present disclosure, for any test data, it is first necessary to format it according to the input requirements of the API and fill it into the corresponding input parameters of the API to obtain an initial test case.
[0068] Step 305, generate multiple candidate test cases based on the multiple initial test cases.
[0069] To remove invalid and redundant test cases, as a possible implementation, based on multiple rounds of iteration processes, the generated test cases are screened and processed to obtain candidate test cases.
[0070] In the embodiments of the present disclosure, multiple initial test cases are randomly selected and an initial population is generated; at least one round of iteration process is performed on the initial population to obtain the updated population output in each round of iteration; based on the updated populations output in each round of iteration, multiple candidate test cases are selected.
[0071] First, randomly select from multiple initial test cases and combine the selected initial test cases into an initial population. Furthermore, perform at least one round of iterative optimization on this initial population. In each round of iteration, update the test cases in the population (such as parameter mutation, parameter crossover, etc.) to obtain an updated population. Finally, select candidate test cases from the updated populations output in each round of iteration.
[0072] For example, for the updated population output in any round of iteration, obtain the fitness of each updated test case in the updated population output in any round of iteration. According to the fitness of each updated test case in the updated population output in any round of iteration, determine candidate test cases from each updated test case in the updated population output in any round of iteration. For example, select the updated test case with the highest fitness from each updated test case in the updated population output in each round of iteration as the candidate test case. It should be noted that the fitness of the updated test case is used to indicate the test value of the corresponding updated test case, and there is a positive correlation between the fitness of the updated test case and the test value.
[0073] Among them, for the first round of iteration process in at least one round of iteration process, the generation steps of the updated population output in the first round of iteration process are as follows:
[0074] (1) Determine the fitness of each initial test case in the initial population according to multiple positive evaluation indicators of each initial test case in the initial population. Among them, the fitness of the initial test case is used to indicate the test value of the corresponding initial test case.
[0075] In the embodiments of the present disclosure, the positive evaluation indicator refers to a positive standard reflecting the quality or value of the test case. The positive evaluation indicators include but are not limited to: coverage rate (covering parameter boundary values, key combination ratio), effectiveness (whether the parameters meet the type and constraints), uniqueness (the difference degree from other test cases in the population), etc. Based on multiple positive evaluation indicators of each initial test case in the initial population, calculate the fitness of each initial test case in the initial population. For example, assign a weight to each positive evaluation indicator of each initial test case to reflect its importance relative to the overall fitness. Then, calculate the fitness score of the corresponding initial test case by means of weighted summation. It should be noted that the fitness of the initial test case is used to indicate the test value of the corresponding initial test case.
[0076] (2) Determine the extraction probability of each initial test case in the initial population according to each fitness. Based on the extraction probability of each initial test case in the initial population, extract multiple initial test cases from the initial population to generate an intermediate population in the first round of iteration process.
[0077] In order to improve the pertinence and effectiveness of test cases, in the embodiments of the present disclosure, the extraction probability of each initial test case is determined according to its fitness. For example, the fitness of the i-th initial test case in the initial population is F i , and the total fitness of all initial test cases in the initial population is F total , then the extraction probability p i of the i-th initial test case = F i / F total . Furthermore, based on the probabilities of all initial test cases in the initial population, multiple test cases are extracted from the initial population to generate an intermediate population in the first-round iteration process. For example, the initial test cases with high extraction probabilities are selected to generate the intermediate population in the first-round iteration process. It should be noted that the higher the fitness of an initial test case, the greater its extraction probability.
[0078] (3) Update the parameters of the initial test cases in the intermediate population of the first-round iteration process to obtain the updated population output by the first-round iteration process.
[0079] In the embodiments of the present disclosure, the parameters of the initial test cases in the intermediate population are updated to generate an updated population. For example, the parameters to be processed are determined, that is, which parameters will participate in the subsequent crossover and mutation operations. Then, the crossover and mutation operations are performed on the parameters to be processed. Among them, the crossover operation refers to exchanging the parameters between two or more initial test cases; the mutation operation refers to changing the parameter values in a single initial test case to generate new test cases.
[0080] For the non-first-round iteration process of at least one iteration process, the steps for generating the updated population output by the non-first-round iteration process are as follows:
[0081] (1) Determine the fitness of each updated test case in the updated population output by the previous iteration process of the non-first-round iteration process according to multiple positive evaluation indicators of each updated test case in the updated population output by the previous iteration process of the non-first-round iteration process;
[0082] In the embodiments of the present disclosure, in the non-first-round iteration process, the updated population generated by the previous iteration process of the non-first-round iteration process is obtained. Then, according to multiple positive evaluation indicators of each updated test case in the updated population output by the previous iteration process, the fitness of each updated test case is determined. Among them, the multiple positive evaluation indicators include but are not limited to: coverage rate, effectiveness, uniqueness, etc.
[0083] (2) Determine the extraction probability of each updated test case in the updated population output by the previous iteration process according to each fitness;
[0084] Furthermore, based on the fitness of each updated test case, the extraction probability of each updated test case is calculated.
[0085] (3) Based on the extraction probabilities of the updated test cases in the updated population output from the previous iteration process, extract multiple updated test cases from the updated population output from the previous iteration process to generate an intermediate population for non-first-round iteration processes;
[0086] In the embodiments of the present disclosure, based on the updated population output from the previous iteration process, extract multiple test cases therefrom according to the extraction probability of each updated test case; among them, the higher the fitness of the updated test case, the greater the probability of being extracted; furthermore, select a group of representative test cases from the updated population to form an intermediate population for non-first-round iteration processes.
[0087] (4) Update the parameters of the updated test cases in the intermediate population for non-first-round iteration processes to obtain the updated population output from non-first-round iteration processes.
[0088] In the embodiments of the present disclosure, during the non-first-round iteration process, further adjust and optimize the parameters of the updated test cases in the intermediate population, for example, by performing operations such as crossover and mutation, modify the parameter values or combinations of these test cases to remove invalid and redundant test cases.
[0089] Step 306, use each candidate test case to perform a simulation test on the API to obtain the covered code information of each candidate test case.
[0090] Step 307, based on the covered code information of each candidate test case, determine the target test cases for testing the API from multiple candidate test cases.
[0091] It should be noted that the execution processes of steps 301 to 303 and steps 306 to 307 can be implemented in any one of the embodiments of the present disclosure respectively. The embodiments of the present disclosure do not make any limitations thereto and will not be elaborated further.
[0092] In summary, by sequentially filling each test data into the input parameters of the API to obtain multiple initial test cases; based on the multiple initial test cases, generate multiple candidate test cases. Thus, different test data are used to simulate the usage scenarios of the test, covering a wide range of input conditions and boundary situations, thereby achieving that the candidate test cases cover multiple usage scenarios of the API, improving the comprehensiveness and accuracy of the test, and at the same time removing invalid and redundant test cases, improving the effectiveness of the test.
[0093] To clearly illustrate how the above embodiments determine at least one target test case from multiple candidate test cases based on the covered code information of each candidate test case, the present disclosure proposes another method for generating test cases.
[0094] Figure 4It is a schematic flowchart of a method for generating test cases shown in the fourth embodiment of the present disclosure.
[0095] As Figure 4 shown, the method for generating test cases includes the following steps:
[0096] Step 401, in response to detecting an update of an application programming interface (API), obtain at least one target parameter associated with the API and the data dictionary of each target parameter.
[0097] Wherein, the data dictionary is used to describe the detailed information of the corresponding target parameter.
[0098] Step 402, generate a plurality of candidate test cases according to at least one target parameter and the corresponding data dictionary.
[0099] Step 403, perform a simulation test on the API using each candidate test case to obtain the coverage code information of each candidate test case.
[0100] Step 404, in response to the existence of a first test case among a plurality of candidate test cases, use the first test case as the target test case; wherein, the coverage code information of the first test case is different from that of other test cases except the first test case among the plurality of candidate test cases; and / or, in response to the existence of a second test case among the plurality of candidate test cases, use the second test case as the target test case; wherein, the coverage code information of the second test case includes the coverage code information of at least one other test case except the first test case.
[0101] To improve the effectiveness and accuracy of testing, as an example, among a plurality of candidate test cases, if there is a first test case and the coverage code information of the first test case is different from that of all other candidate test cases, then use the first test case as the target test case. It should be noted that since each test case will cover a part of the program code during execution, and different test cases may cover different code regions, the coverage code information of the first test case does not overlap with that of other test cases. Using the first test case as the target test case can ensure a more comprehensive testing process and discover more potential errors or defects.
[0102] As another example, among multiple candidate test cases, if there is a second test case whose covered code information includes the covered code information of other candidate test cases (except the second test case itself), it means that the second test case can trigger and verify the logic of more program codes during execution. Therefore, the second test case is used as the target test case. It should be noted that since the second test case has a wider coverage range, it is more likely to reveal potential problems or defects in the program code. Using the second test case as the target test case helps to comprehensively verify the correctness and stability of the program code.
[0103] Based on any embodiment of the present disclosure, as Figure 5 shown, in practical applications, the test case generation method mainly includes three stages:
[0104] The first stage:
[0105] 1. As Figure 6 shown, in response to monitoring that the application programming interface (API) is updated (such as, added or modified), obtain at least one target parameter associated with the API and the data dictionary of each target parameter;
[0106] 2. According to at least one target parameter and the corresponding data dictionary, combine with the orthogonal combination rule to generate test data;
[0107] 3. Based on the test data, generate initial test cases and compress the initial test cases to determine candidate test cases; for example, perform at least one round of iteration process on the initial population in the initial test cases to obtain the updated populations output in each round of iteration process, and based on the updated populations output in each round of iteration, select multiple candidate test cases;
[0108] The second stage:
[0109] Use each candidate test case to perform a simulation test on the API to obtain the covered code information of each candidate test case; where the covered code information includes the coverage rate and the covered data detail information, and the covered data detail information includes the covered code.
[0110] The third stage:
[0111] For any candidate test case, determine whether there is a difference between this candidate test case and other candidate test cases. If there is a difference, then use this candidate test case as the target test case.
[0112] For example, among multiple candidate test cases, if there is a first test case whose covered code information is different from that of all other candidate test cases, the first test case is used as the target test case; and / or, among multiple candidate test cases, if there is a second test case whose covered code information includes the covered code information of other candidate test cases (except the second test case itself), the second test case is used as the target test case.
[0113] Corresponding to the test case generation method provided in the above embodiment, the present disclosure also provides a test case generation device. Since the test case generation device provided in the embodiments of the present disclosure corresponds to the test case generation method provided in the above embodiment, the implementation manners of the test case generation method are also applicable to the test case generation device provided in the embodiments of the present disclosure and will not be described in detail in the embodiments of the present disclosure.
[0114] Figure 7 It is a schematic structural diagram of the test case generation device shown in the fifth embodiment of the present disclosure.
[0115] As Figure 7 shown, the test case generation device 700 includes: an acquisition module 710, a generation module 720, a simulation module 730, and a determination module 740.
[0116] Among them, the acquisition module 710 is configured to, in response to detecting an update of the application programming interface API, acquire at least one target parameter associated with the API and the data dictionary of each target parameter; wherein the data dictionary is used to describe the detailed information of the corresponding target parameter; the generation module 720 is configured to generate multiple candidate test cases for testing the API according to the at least one target parameter and the data dictionary of each target parameter; the simulation module 730 is configured to perform a simulation test on the API using each candidate test case to obtain the covered code information of each candidate test case; the determination module 740 is configured to determine a target test case for testing the API from multiple candidate test cases based on the covered code information of each candidate test case.
[0117] As a possible implementation manner of the embodiments of the present disclosure, the generation module 720 is configured to generate multiple orthogonal combination rules according to at least one target parameter and the data dictionary of each target parameter; create multiple test data based on the multiple orthogonal combination rules; and generate multiple candidate test cases according to the multiple test data.
[0118] As a possible implementation manner of an embodiment of the present disclosure, the generation module 720 is configured to sequentially fill each test data into the input parameters of the API to obtain a plurality of initial test cases; and generate a plurality of candidate test cases according to the plurality of initial test cases.
[0119] As a possible implementation manner of an embodiment of the present disclosure, the generation module 720 is configured to randomly select a plurality of initial test cases and generate an initial population; perform at least one round of iterative process on the initial population to obtain an updated population output in each round of iteration; and select a plurality of candidate test cases based on the updated populations output in each round of iteration.
[0120] As a possible implementation manner of an embodiment of the present disclosure, the first round of the at least one round of iterative process includes: determining the fitness of each initial test case in the initial population according to a plurality of positive evaluation indexes of each initial test case in the initial population; wherein the fitness of the initial test case is used to indicate the test value of the corresponding initial test case; determining the extraction probability of each initial test case in the initial population according to each fitness; extracting a plurality of initial test cases from the initial population based on the extraction probability of each initial test case in the initial population to generate an intermediate population of the first round of the iterative process; and updating the parameters of the initial test cases in the intermediate population of the first round of the iterative process to obtain the updated population output in the first round of the iterative process.
[0121] As a possible implementation manner of an embodiment of the present disclosure, a non-first-round iterative process of the at least one round of iterative process includes: determining the fitness of each updated test case in the updated population output in the previous iterative process of the non-first-round iterative process according to a plurality of positive evaluation indexes of each updated test case in the updated population output in the previous iterative process of the non-first-round iterative process; determining the extraction probability of each updated test case in the updated population output in the previous iterative process according to each of the fitnesses; extracting a plurality of updated test cases from the updated population output in the previous iterative process based on the extraction probability of each updated test case in the updated population output in the previous iterative process to generate an intermediate population of the non-first-round iterative process; and updating the parameters of the updated test cases in the intermediate population of the non-first-round iterative process to obtain the updated population output in the non-first-round iterative process.
[0122] As a possible implementation manner of an embodiment of the present disclosure, the generation module 720 is configured to obtain the fitness of each updated test case in the updated population output in any round of iteration for any round of iteration; and determine candidate test cases from each updated test case in the updated population output in any round of iteration according to the fitness of each updated test case in the updated population output in any round of iteration.
[0123] As a possible implementation manner of the embodiments of the present disclosure, the generation module 720 is configured to determine the number of value takings of each target parameter according to at least one target parameter and the data dictionary of each target parameter; determine the value set of each target parameter according to the number of value takings corresponding to each target parameter and the data dictionary; and generate a plurality of orthogonal combination rules based on the value sets of each target parameter.
[0124] As a possible implementation manner of the embodiments of the present disclosure, the determination module 740 is configured to perform at least one of the following: in response to the existence of a first test case among a plurality of candidate test cases, use the first test case as the target test case, where the first test case has different covered code information from other test cases except the first test case among the plurality of candidate test cases; in response to the existence of a second test case among the plurality of candidate test cases, use the second test case as the target test case, where the covered code information of the second test case includes the covered code information of other test cases except the second test case among the plurality of candidate tests.
[0125] The test case generation device according to the embodiments of the present disclosure, when detecting an update of the API, automatically obtains the target parameters associated with the API and their data dictionaries, generates a plurality of candidate test cases for simulation testing, and finally determines the target test case according to the covered code information. Thus, automatic processing is realized, the need for manual intervention is reduced. Especially when facing frequently updated APIs, it can quickly respond to changes and automatically generate relevant test cases, greatly shortening the test cycle. Secondly, generating candidate test cases based on a detailed data dictionary ensures the comprehensiveness and accuracy of the test, and helps to discover potential problems or vulnerabilities. In addition, using the covered code information to select the target test case finally used for testing not only optimizes the use of test resources, but also ensures that all functions and boundary conditions of the API are fully verified.
[0126] In an exemplary embodiment, an electronic device is further proposed.
[0127] Wherein, the electronic device includes:
[0128] A processor;
[0129] A memory for storing instructions executable by the processor;
[0130] Wherein, the processor is configured to execute instructions to implement the test case generation method proposed in any of the foregoing embodiments.
[0131] As an example, Figure 8 is a schematic structural diagram of an electronic device 800 shown in an exemplary embodiment of the present disclosure. As Figure 8 shown, the above-mentioned electronic device 800 may further include:
[0132] A memory 810 and a processor 820, a bus 830 connecting different components (including the memory 810 and the processor 820), the memory 810 stores a computer program, and when the processor 820 executes the program, the method for generating a test case described in the embodiments of the present disclosure is implemented.
[0133] The bus 830 represents one or more of several types of bus architectures, including a memory bus or a memory controller, a peripheral bus, a graphics acceleration port, a processor, or a local bus using any bus architecture in a variety of bus architectures. By way of example, these architectures include, but are not limited to, Industry Standard Architecture (ISA) bus, Micro Channel Architecture (MAC) bus, Enhanced ISA bus, Video Electronics Standards Association (VESA) local bus, and Peripheral Component Interconnect (PCI) bus.
[0134] The electronic device 800 typically includes a variety of electronically device-readable media. These media can be any available media that can be accessed by the electronic device 800, including volatile and non-volatile media, removable and non-removable media.
[0135] The memory 810 may further include a computer system-readable medium in the form of volatile memory, such as random access memory (RAM) 840 and / or cache memory 850. The server 800 may further include other removable / non-removable, volatile / non-volatile computer system storage media. By way of example only, a storage system 860 may be used to read and write non-removable, non-volatile magnetic media ( Figure 8 not shown, commonly referred to as a "hard disk drive"). Although Figure 8 not shown in the figure, a disk drive for reading and writing a removable non-volatile disk (such as a "floppy disk") and an optical disk drive for reading and writing a removable non-volatile optical disk (such as a CD-ROM, DVD-ROM, or other optical media) may be provided. In these cases, each drive may be connected to the bus 830 through one or more data media interfaces. The memory 810 may include at least one program product having a set (e.g., at least one) of program modules that are configured to perform the functions of the embodiments of the present disclosure.
[0136] A program / utility 880 having a set (at least one) of program modules 870 may be stored, for example, in the memory 810. Such program modules 870 include, but are not limited to, an operating system, one or more application programs, other program modules, and program data. Each or some combination of these examples may include the implementation of a network environment. The program modules 870 generally perform the functions and / or methods in the embodiments described in the present disclosure.
[0137] The electronic device 800 can also communicate with one or more external devices 890 (such as a keyboard, a pointing device, a display 891, etc.), and can also communicate with one or more devices that enable a user to interact with the electronic device 800, and / or communicate with any device that enables the electronic device 800 to communicate with one or more other computing devices (such as a network card, a modem, etc.). Such communication can be carried out through the input / output (I / O) interface 892. Moreover, the electronic device 800 can also communicate with one or more networks (such as a local area network (LAN), a wide area network (WAN), and / or a public network, such as the Internet) through the network adapter 893. As shown in the figure, the network adapter 893 communicates with other modules of the electronic device 800 through the bus 830. It should be understood that although not shown in the figure, other hardware and / or software modules can be used in combination with the electronic device 800, including but not limited to: microcode, device drivers, redundant processing units, external disk drive arrays, RAID systems, tape drives, and data backup storage systems, etc.
[0138] The processor 820 executes various functional applications and data processing by running the programs stored in the memory 810.
[0139] It should be noted that for the implementation process and technical principle of the electronic device in this embodiment, refer to the foregoing explanation of the method for generating test cases of the embodiments of the present disclosure, and details will not be repeated here.
[0140] In an exemplary embodiment, a computer-readable storage medium including instructions is also provided, such as a memory including instructions. The above instructions can be executed by the processor of the electronic device to complete the method for generating test cases proposed in any of the foregoing embodiments. Optionally, the computer-readable storage medium can be a ROM, a random access memory (RAM), a CD-ROM, a magnetic tape, a floppy disk, and an optical data storage device, etc.
[0141] In an exemplary embodiment, a computer program product is also provided, including a computer program / instructions, characterized in that when the computer program / instructions are executed by the processor, the method for generating test cases proposed in any of the foregoing embodiments is implemented.
[0142] Those skilled in the art will readily think of other embodiments of the present disclosure after considering the specification and practicing the invention disclosed herein. The present disclosure is intended to cover any variations, uses, or adaptations of the present disclosure, which follow the general principles of the present disclosure and include common general knowledge or conventional technical means in the technical field not disclosed in the present disclosure. The specification and embodiments are only regarded as exemplary, and the true scope and spirit of the present disclosure are pointed out by the following claims.
[0143] It should be understood that the present disclosure is not limited to the exact structures described above and shown in the drawings, and various modifications and changes can be made without departing from its scope. The scope of the present disclosure is limited only by the appended claims.
Claims
1. A method for generating test cases, characterized in that Including: In response to detecting an update of an Application Programming Interface (API), obtaining at least one target parameter associated with the API and a data dictionary for each of the target parameters; wherein the data dictionary is used to describe detailed information of the corresponding target parameter; Generating a plurality of candidate test cases according to the at least one target parameter and the corresponding data dictionary; Performing a simulation test on the API using each of the candidate test cases to obtain coverage code information of each of the candidate test cases; Based on the coverage code information of each of the candidate test cases, determining a target test case for testing the API from the plurality of candidate test cases.
2. The method according to claim 1, characterized in that, The generating a plurality of candidate test cases according to the at least one target parameter and the corresponding data dictionary includes: Generating a plurality of orthogonal combination rules according to the at least one target parameter and the data dictionary for each of the target parameters; Creating a plurality of test data based on the plurality of orthogonal combination rules; Generating the plurality of candidate test cases according to the plurality of test data.
3. The method according to claim 2, characterized in that, The generating the plurality of candidate test cases according to the plurality of test data includes: Successively filling each of the test data into the input parameters of the API to obtain a plurality of initial test cases; Generating a plurality of candidate test cases based on the plurality of initial test cases.
4. The method according to claim 3, wherein The generating a plurality of candidate test cases based on the plurality of initial test cases includes: Randomly selecting a plurality of the initial test cases and generating an initial population; Performing at least one round of iterative process on the initial population to obtain an updated population output in each round of iteration; Selecting the plurality of candidate test cases based on the updated population output in each round of iteration.
5. The method according to claim 4, wherein The first round of the at least one round of iterative process includes: Determining the fitness of each initial test case in the initial population according to a plurality of positive evaluation indicators of each initial test case in the initial population; wherein the fitness of the initial test case is used to indicate the test value of the corresponding initial test case; Determining the extraction probability of each initial test case in the initial population according to each of the fitness values; Based on the extraction probability of each initial test case in the initial population, extracting a plurality of initial test cases from the initial population to generate an intermediate population in the first round of the iterative process; Updating the parameters of the initial test cases in the intermediate population of the first round of the iterative process to obtain the updated population output in the first round of the iterative process.
6. The method according to claim 4, characterized in that, The non-first round of the at least one round of iterative process includes: Determining the fitness of each updated test case in the updated population output in the previous iterative process of the non-first round of the iterative process according to a plurality of positive evaluation indicators of each updated test case in the updated population output in the previous iterative process of the non-first round of the iterative process; Determining the extraction probability of each updated test case in the updated population output in the previous iterative process according to each of the fitness values; Based on the extraction probabilities of the updated test cases in the updated population output from the previous iteration process, multiple updated test cases are extracted from the updated population output from the previous iteration process to generate the intermediate population in the non-first-round iteration process; The parameters of the updated test cases in the intermediate population in the non-first-round iteration process are updated to obtain the updated population output in the non-first-round iteration process.
7. The method according to claim 4, wherein The selection of the multiple candidate test cases based on the updated populations output in each round of iteration includes: For the updated population output in any round of iteration, obtain the fitness of each updated test case in the updated population output in the any round of iteration; According to the fitness of each updated test case in the updated population output in the any round of iteration, determine the candidate test cases from each updated test case in the updated population output in the any round of iteration.
8. The method according to claim 2, characterized in that, The generation of multiple orthogonal combination rules according to the at least one target parameter and the data dictionary of each target parameter includes: According to the at least one target parameter and the data dictionary of each target parameter, determine the number of value takings of each target parameter; According to the number of value takings and the data dictionary corresponding to each target parameter, determine the value set of each target parameter; Based on the value sets of each target parameter, generate the multiple orthogonal combination rules.
9. The method according to claim 1, characterized in that, The determination of at least one target test case from the multiple candidate test cases based on the coverage code information of each candidate test case includes at least one of the following: In response to the existence of a first test case among the multiple candidate test cases, use the first test case as the target test case; wherein, the coverage code information of the first test case is different from that of the other test cases among the multiple candidate test cases except the first test case; In response to the existence of a second test case among the multiple candidate test cases, use the second test case as the target test case; wherein, the coverage code information of the second test case includes the coverage code information of the other test cases among the multiple candidates except the second test case.
10. A test case generation device, characterized in that, Includes: An acquisition module, configured to, in response to detecting an update of an application programming interface API, acquire at least one target parameter associated with the API and the data dictionary of each target parameter; wherein, the data dictionary is used to describe the detailed information of the corresponding target parameter; A generation module, configured to generate multiple candidate test cases for testing the API according to the at least one target parameter and the data dictionary of each target parameter; A simulation module, configured to perform a simulation test on the API using each candidate test case to obtain the coverage code information of each candidate test case; A determination module, configured to determine a target test case for testing the API from the multiple candidate test cases based on the coverage code information of each candidate test case.