Test case generation method and device, electronic equipment, storage medium and program product
By obtaining the source code and code coverage information of the target interface, and using a test case generation model to generate executable test cases for the uncovered code lines, the problem of insufficient code line coverage in automated interface testing is solved, and more comprehensive interface testing is achieved.
Patent Information
- Application Number
- CN202510220369.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2025-02-26
- Publication Date
- 2025-12-16
- Estimated Expiration
- 2045-02-26
AI Technical Summary
In existing automated interface testing, the code coverage of test cases is not high enough, resulting in insufficient interface testing.
By obtaining the source code and code coverage information of the target interface, the unexecuted lines of code and their corresponding functions are identified. The test case generation model is then used to analyze the nearest neighbor test cases of the target and generate test cases that can execute those lines of code.
It improves the code coverage of interface tests, makes automated testing more thorough, and ensures that interface functions are fully tested.
Smart Images

Figure CN120144454B_ABST
Abstract
Description
Technical Field
[0001] This disclosure relates to the field of computer technology, and in particular to a test case generation method, apparatus, electronic device, storage medium, and program product. Background Technology
[0002] In today's internet-driven world, the development of automated API testing is becoming increasingly important. As projects grow larger and iteration speeds accelerate, manual testing is no longer sufficient. Automated API testing can automate execution, reducing manual costs and improving testing efficiency. While automated API testing offers significant advantages, current automated testing suffers from insufficient testing coverage, meaning that test case code coverage is inadequate, preventing comprehensive and thorough testing of API functionality. Summary of the Invention
[0003] To address the aforementioned technical problems, this disclosure provides a test case generation method, apparatus, electronic device, storage medium, and program product.
[0004] A first aspect of this disclosure provides a test case generation method, the method comprising: obtaining target source code of a target interface, the target source code including interface functions of the target interface and at least one function referenced by the interface functions; obtaining initial test cases and code coverage information corresponding to the target interface, the initial test cases including multiple test cases, the code coverage information indicating executed and unexecuted lines of code in the target source code during the execution of the initial test cases; determining, based on the code coverage information, unexecuted target lines of code in the target source code and the target function to which the target lines of code belong, the target function being one of the interface functions and the at least one function; determining, from the initial test cases, a target nearest neighbor test case corresponding to the target lines of code; the target nearest neighbor test case being a test case of the target parent node of the target lines of code in the initial test cases; the target parent node being the executed line of code in the target source code that is the parent node of the target lines of code and is closest to the target lines of code; inputting the target source code, the target lines of code, the target functions, and the target nearest neighbor test case into a test case generation model, and outputting target test cases.
[0005] In some embodiments of this disclosure, the target source code, the target code line, the target function, and the target nearest neighbor test cases are input into a test case generation model, and the target test cases are output. This includes: the test case generation model generating the target test cases that can execute the target code line based on the analysis of the relationship between the target code line belonging to the target function in the target source code and the target parent node, and the analysis of the target nearest neighbor test cases.
[0006] In some embodiments of this disclosure, after inputting the target source code, the target code line, the target function, and the target nearest neighbor test case into the test case generation model and outputting the target test case, the method further includes: executing the target test case and determining whether the target code line is executed; if the target code line is executed, determining the target test case as the test case for the target interface.
[0007] In some embodiments of this disclosure, after executing the target test case and determining whether the target code line has been executed, the method further includes: if the target code line has not been executed, inputting the target source code, the target code line, the target function, the target nearest neighbor test case, and the target test case into the test case generation model, and outputting a first test case; executing the first test case to determine whether the target code line has been executed; and if the target code line has been executed, determining the first test case as the test case for the target interface.
[0008] In some embodiments of this disclosure, before inputting the target source code, the target code line, the target function, and the target nearest neighbor test case into the test case generation model and outputting the target test case, the method further includes: obtaining a plurality of interface input parameter values included in a preset number of test cases in the initial test case; inputting the target source code, the target code line, the target function, and the target nearest neighbor test case into the test case generation model and outputting the target test case includes: inputting the target source code, the target code line, the target function, the target nearest neighbor test case, and the plurality of interface input parameter values into the test case generation model and outputting the target test case, wherein the attributes of the interface input parameter values included in the target test case are at least the same as the attributes of at least one of the plurality of interface input parameter values.
[0009] In some embodiments of this disclosure, the test case generation model is further used to determine the target interface input parameter value as the interface input parameter value of the target test case if the target interface input parameter value among the multiple interface input parameter values can be used as the interface input parameter value of the target test case, and to generate the interface input parameter value of the target test case if none of the multiple interface input parameter values can be used as the interface input parameter value of the target test case.
[0010] In some embodiments of this disclosure, before inputting the target source code, the target line of code, the target function, and the target nearest neighbor test case into the test case generation model and outputting the target test case, the method further includes: acquiring multiple training data, the multiple training data corresponding to multiple interfaces, the training data including a first source code of the corresponding interface, a first line of code in a first function in the first source code, a first nearest neighbor test case corresponding to the first line of code, and a second test case that can execute the first line of code; inputting the first source code, the first function, the first line of code, and the first nearest neighbor test case from the training data into an initial generation model and outputting a third test case that can execute the first line of code; determining the loss value corresponding to the loss function based on the difference between the third test case and the second test case; and adjusting the initial generation model based on the loss value to obtain the test case generation model.
[0011] A second aspect of this disclosure provides a test case generation apparatus, comprising: an acquisition module, configured to acquire target source code of a target interface, the target source code including interface functions of the target interface and at least one function referenced by the interface functions; acquire initial test cases and code coverage information corresponding to the target interface, the initial test cases including multiple test cases, the code coverage information indicating executed and unexecuted lines of code in the target source code during the execution of the initial test cases; and a determination module, configured to determine, based on the code coverage information, the unexecuted target code lines and code coverage information in the target source code. The target function to which the target code line belongs is one of the interface function and the at least one function; the target nearest neighbor test case corresponding to the target code line is determined from the initial test cases; the target nearest neighbor test case is the test case of the target parent node of the target code line in the initial test cases; the target parent node is the code line in the target source code that is the parent node of the target code line and is executed and is closest to the target code line; the output module is used to input the target source code, the target code line, the target function and the target nearest neighbor test case into the test case generation model and output the target test case.
[0012] In some embodiments of this disclosure, the output module is specifically used to generate a target test case that can execute the target code line based on the analysis of the relationship between the target code line belonging to the target function in the target source code and the target parent node, and the analysis of the target nearest neighbor test cases, through the test case generation model.
[0013] In some embodiments of this disclosure, the determining module is further configured to, after inputting the target source code, the target code line, the target function, and the target nearest neighbor test case into the test case generation model and outputting the target test case, execute the target test case and determine whether the target code line is executed; if the target code line is executed, the target test case is determined as the test case of the target interface.
[0014] In some embodiments of this disclosure, the output module is further configured to, after executing the target test case and determining whether the target code line has been executed, input the target source code, the target code line, the target function, the target nearest neighbor test case, and the target test case into the test case generation model and output a first test case if the target code line has not been executed; the determining module is further configured to execute the first test case and determine whether the target code line has been executed; if the target code line has been executed, determine the first test case as the test case for the target interface.
[0015] In some embodiments of this disclosure, the acquisition module is further configured to acquire multiple interface input parameter values included in a preset number of test cases in the initial test case before inputting the target source code, the target code line, the target function, and the target nearest neighbor test case into the test case generation model and outputting the target test case; the output module is specifically configured to input the target source code, the target code line, the target function, the target nearest neighbor test case, and the multiple interface input parameter values into the test case generation model and output the target test case, wherein the attributes of the interface input parameter values included in the target test case are at least the same as the attributes of at least one of the multiple interface input parameter values.
[0016] In some embodiments of this disclosure, the test case generation model is further used to determine the target interface input parameter value as the interface input parameter value of the target test case if the target interface input parameter value among the multiple interface input parameter values can be used as the interface input parameter value of the target test case, and to generate the interface input parameter value of the target test case if none of the multiple interface input parameter values can be used as the interface input parameter value of the target test case.
[0017] In some embodiments of this disclosure, the acquisition module is further configured to acquire multiple training data before inputting the target source code, the target code line, the target function, and the target nearest neighbor test case into the test case generation model and outputting the target test case. The multiple training data correspond to multiple interfaces. The training data includes a first source code corresponding to the interface, a first code line in a first function within the first source code, a first nearest neighbor test case corresponding to the first code line, and a second test case that executes the first code line. The output module is further configured to input the first source code, the first function, the first code line, and the first nearest neighbor test case from the training data into the initial generation model and output a third test case that executes the first code line. The determination module is further configured to determine the loss value corresponding to the loss function based on the difference between the third test case and the second test case. The apparatus further includes an adjustment module, configured to adjust the initial generation model based on the loss value to obtain the test case generation model.
[0018] A third aspect of this disclosure provides an electronic device including a processor, a memory, and a computer program stored in the memory and executable on the processor, wherein the computer program, when executed by the processor, implements the test case generation method as described in the first aspect.
[0019] A fourth aspect of this disclosure provides a computer-readable storage medium storing a computer program that, when executed by a processor, implements the test case generation method as described in the first aspect.
[0020] A fifth aspect of this disclosure provides a computer program product, wherein the computer program product includes a computer program that, when the computer program product is run on a processor, causes the processor to execute the computer program to implement the test case generation method as described in the first aspect.
[0021] A sixth aspect of this disclosure provides a chip including a processor and a communication interface coupled to the processor, the processor being used to execute program instructions to implement the test case generation method as described in the first aspect.
[0022] The technical solution provided in this disclosure has the following advantages compared with the prior art: Obtaining the target source code of a target interface, the target source code including the interface function of the target interface and at least one function referenced by the interface function; obtaining initial test cases and code coverage information corresponding to the target interface, the initial test cases including multiple test cases, the code coverage information indicating the executed and unexecuted lines of code in the target source code during the execution of the initial test cases; based on the code coverage information, determining the unexecuted target lines of code in the target source code and the target function to which the target lines of code belong, the target function being one of the interface function and the at least one function; determining the target nearest neighbor test case corresponding to the target lines of code from the initial test cases; the target nearest neighbor test case being the test case of the target parent node of the target lines of code in the initial test cases; the target parent node being the executed line of code closest to the target lines of code among the parent nodes of the target lines of code in the target source code; inputting the target source code, the target lines of code, the target functions, and the target nearest neighbor test cases into a test case generation model, and outputting target test cases. In this embodiment of the disclosure, based on the code coverage information of the initial test cases corresponding to the target interface, the unexecuted target code lines in the target source code of the target interface and the target functions to which the target code lines belong are determined. Then, the target nearest neighbor test cases corresponding to the target code lines are obtained from the initial test cases. Subsequently, the target source code, the target code lines, the target functions, and the target nearest neighbor test cases are input into the test case generation model, and the target test cases are output. This generates target test cases corresponding to the target code lines not covered by the initial test cases, thereby improving the code line coverage of the test cases of the target interface, making the automated testing of the target interface more thorough, and enabling a more comprehensive and thorough testing of the interface's functionality during interface testing. Attached Figure Description
[0023] The accompanying drawings, which are incorporated in and form a part of this specification, illustrate embodiments consistent with this disclosure and, together with the description, serve to explain the principles of this disclosure.
[0024] To more clearly illustrate the technical solutions in the embodiments of this disclosure or the prior art, the accompanying drawings used in the description of the embodiments or the prior art will be briefly introduced below. Obviously, those skilled in the art can obtain other drawings based on these drawings without creative effort.
[0025] Figure 1 One of the flowcharts illustrating the test case generation method provided in this embodiment of the disclosure;
[0026] Figure 2 A second schematic flowchart of the test case generation method provided in this embodiment of the disclosure;
[0027] Figure 3 The third flowchart illustrates the test case generation method provided in this embodiment of the disclosure.
[0028] Figure 4 A structural block diagram of a test case generation device provided in this disclosure embodiment;
[0029] Figure 5 This is a structural block diagram of an electronic device provided in an embodiment of the present disclosure. Detailed Implementation
[0030] To better understand the above-mentioned objectives, features, and advantages of this disclosure, the solutions disclosed herein will be further described below. It should be noted that, unless otherwise specified, the embodiments and features described herein can be combined with each other.
[0031] Numerous specific details are set forth in the following description in order to provide a full understanding of this disclosure, but this disclosure may also be implemented in other ways different from those described herein; obviously, the embodiments in the specification are only some, and not all, of the embodiments of this disclosure.
[0032] The terms "first," "second," etc., used in this disclosure and in the claims are used to distinguish similar objects and not to describe a specific order or sequence. It should be understood that such use of data can be interchanged where appropriate so that embodiments of this disclosure can be implemented in orders other than those illustrated or described herein, and the objects distinguished by "first," "second," etc., are generally of the same class and the number of objects is not limited; for example, a first object can be one or more. Furthermore, in the specification and claims, "and / or" indicates at least one of the connected objects, and the character " / " generally indicates that the preceding and following objects are in an "or" relationship.
[0033] Currently, there are two main methods to improve API test case code line coverage. The first is to directly record online traffic and obtain the code line coverage of the traffic through playback, ultimately resulting in test cases with code line coverage close to the online coverage. The drawback of this method is that it requires replaying a large amount of online traffic, which is extremely time-consuming, and it cannot achieve code line coverage higher than the online coverage. The second method is to use fuzzing to mutate various seed traffic to obtain code line coverage higher than the online coverage. However, this method has high uncertainty, is very time-consuming, and the mutated traffic cannot specify which lines of code it covers.
[0034] Traditional fuzzing algorithms simply mutate seed traffic using various operators, ignoring crucial information within the target source code. Furthermore, API code frequently involves branching, and when branch conditions include enumeration fields, fuzzing algorithms struggle to generate enumeration fields within a specific range using mutation operators. Additionally, if branch conditions involve combinations of multiple parameters, fuzzing algorithms also struggle to mutate all input parameters simultaneously, significantly increasing the search space. Moreover, in change testing, testers need to test the modified code, requiring traffic that executes to the changed lines. However, since fuzzing algorithms essentially explore the search space, generating traffic that executes to the specified changed lines can be time-consuming and difficult.
[0035] The electronic devices in this disclosure can be mobile electronic devices or non-mobile electronic devices. Mobile electronic devices can be mobile phones, tablets, laptops, PDAs, in-vehicle electronic devices, wearable devices, ultra-mobile personal computers (UMPCs), netbooks, or personal digital assistants (PDAs), etc.; non-mobile electronic devices can be personal computers (PCs), televisions (TVs), ATMs, or self-service machines, etc.; this disclosure does not impose specific limitations.
[0036] The execution subject of the test case generation method provided in this embodiment can be the aforementioned electronic device or a functional module and / or functional entity in the electronic device that can implement the test case generation method, or it can be a server or a functional module and / or functional entity in the server that can implement the video search method. The specific implementation subject can be determined according to actual usage requirements, and this embodiment does not limit it.
[0037] The test case generation method provided in this disclosure will be described in detail below with reference to the accompanying drawings, through specific embodiments and application scenarios.
[0038] like Figure 1 As shown in the figure, this disclosure provides a test case generation method, which may include the following steps 101 to 105.
[0039] 101. Obtain the target source code of the target interface.
[0040] The target source code includes the interface functions of the target interface and at least one function referenced by the interface functions.
[0041] Among these methods, static program analysis can be used to obtain all the source code information involved in the target interface, which means that the target source code can be obtained by accessing the backend interface.
[0042] 102. Obtain the initial test cases and code coverage information corresponding to the target interface.
[0043] The initial test case includes multiple test cases, and the code coverage information is used to indicate the lines of code that are executed and not executed in the target source code during the execution of the initial test case.
[0044] The initial test cases can be obtained or generated in advance, and the specifics can be determined according to the actual situation. No restrictions are imposed here.
[0045] The code coverage information may include covered code portions (i.e., lines of code executed in the target source code during the execution of the initial test case) and uncovered code portions (i.e., lines of code not executed in the target source code during the execution of the initial test case). Code coverage information may also include other feasibility information, which is not limited here.
[0046] Code coverage information can be obtained by executing initial test cases through the execution engine or by executing initial test cases through testing tools. The specific method can be determined based on the actual situation and is not limited here.
[0047] The execution engine and testing tools can simulate the interface program based on the interface source code, and then execute initial test cases based on the simulated interface program to obtain code coverage information.
[0048] 103. Based on the code coverage information, determine the unexecuted target code lines in the target source code and the target function to which the target code lines belong.
[0049] The objective function is one of the interface function and the at least one function.
[0050] In this context, a target line of code is any line of code in the target source code that is not covered by the initial test cases. Specifically, a target line of code is a line of code within the target function of the target source code that is not covered by the initial test cases.
[0051] 104. Determine the target nearest neighbor test case corresponding to the target line of code from the initial test case.
[0052] The nearest neighbor test case is the test case of the target parent node of the target code line in the initial test case; the target parent node is the code line in the target source code that is executed and is closest to the target code line among the parent nodes of the target code line.
[0053] There is only one target parent node, but the nearest neighbor test case can be any test case that covers the target parent node, that is, the nearest neighbor test case can be one or more test cases.
[0054] For example, such as Figure 2 The diagram shows the multi-branch tree distribution of the target source code. Nodes A, B, C, D, E, F, and G each indicate a line of code in the target source code. Assuming node F corresponds to the target line of code, then the parent nodes of node F include nodes C and A. If there are test cases covering node C, then the line of code corresponding to node C is the target's parent node. If the test cases covering node C include one test case, then that one test case is the target's nearest neighbor test case. If the test cases covering node C include multiple test cases, then those multiple test cases are the target's nearest neighbor test cases. If there are no test cases covering node C, but there are test cases covering node A, then the line of code corresponding to node A is the target's parent node, and all test cases covering node A are the target's nearest neighbor test cases.
[0055] 105. Input the target source code, the target line of code, the target function, and the target nearest neighbor test cases into the test case generation model, and output the target test cases.
[0056] In some embodiments of this disclosure, step 105 specifically involves the test case generation model generating a target test case that can execute the target code line based on the analysis of the relationship between the target code line belonging to the target function in the target source code and the target parent node, as well as the analysis of the target nearest neighbor test cases.
[0057] The test case generation model can be a pre-trained machine learning model, and there are no restrictions here.
[0058] In some embodiments of this disclosure, the test case generation model can determine the relationship between the target code line and the target parent node in the target source code obtained by analysis (determining branch conditions), and the input parameters and parameter values in the nearest neighbor test cases of the target (determining input parameters), and deduce the input parameters to be modified and the specific parameter values of the input parameters to be modified step by step.
[0059] In this embodiment of the disclosure, based on the code coverage information of the initial test cases corresponding to the target interface, the unexecuted target code lines in the target source code of the target interface and the target functions to which the target code lines belong are determined. Then, the target nearest neighbor test cases corresponding to the target code lines are obtained from the initial test cases. Subsequently, the target source code, the target code lines, the target functions, and the target nearest neighbor test cases are input into the test case generation model, and the target test cases are output. This generates target test cases corresponding to the target code lines not covered by the initial test cases, thereby improving the code line coverage of the test cases of the target interface, making the automated testing of the target interface more thorough, and enabling a more comprehensive and thorough testing of the interface's functionality during interface testing.
[0060] Compared to directly recording online traffic and traditional fuzzing methods, this embodiment of the disclosure not only improves the code line coverage of automated interface test cases and can generate test cases that cover the specified current code line, but also reduces the generation time of automated interface test cases.
[0061] In some embodiments of this disclosure, after step 105 described above, the test case generation method provided by the embodiments of this disclosure may further include steps 106 and 107 as described below.
[0062] 106. Execute the target test case to determine whether the target line of code has been executed.
[0063] During the execution of the target test case, if it is determined that the target code line is executed, then it is determined that the target test is used to cover the target code line.
[0064] 107. If the target line of code is executed, the target test case shall be identified as the test case for the target interface.
[0065] It is understandable that the target test cases generated by the test case generation model may not be able to cover the target code lines. Therefore, by executing the target test cases, it is determined whether the target test cases can cover the target code lines. Then, if the target test cases can cover the target code lines, the target test cases are determined as the test cases for the target interface, thereby improving the code line coverage of the target interface, making the automated testing of the target interface more thorough, and making the interface testing more comprehensive and sufficient in terms of testing the functionality of the interface.
[0066] In some embodiments of this disclosure, after step 106 described above, the test case generation method provided in the embodiments of this disclosure may further include steps 108 to 110 as described below.
[0067] 108. If the target code line is not executed, input the target source code, the target code line, the target function, the target nearest neighbor test case, and the target test case into the test case generation model, and output the first test case.
[0068] The test case generation model is used to generate a first test case that can execute the target code line and is different from the target test case, based on the analysis of the relationship between the target code line belonging to the target function in the target source code and the target parent node, the analysis of the target nearest neighbor test cases, and the analysis of the target test cases.
[0069] 109. Execute the first test case to determine whether the target line of code is executed.
[0070] 110. If the target line of code is executed, the first test case shall be determined as the test case for the target interface.
[0071] If the target test case cannot cover the target code line, the target test case is also input into the test case generation model. The test case generation model then corrects itself and learns continuously based on error feedback. This increases the probability that the test cases generated by the test case generation model can cover the corresponding code line, thereby improving the code line coverage of the target interface. This makes the automated testing of the target interface more thorough and allows for more comprehensive and complete testing of the interface's functionality.
[0072] In some embodiments of this disclosure, if the first test case still cannot cover the target code line, the first test case can also be used as an input condition to input the test case generation model, and the test case generation model can be corrected to obtain a newly generated test case. The newly generated test case is used as the first test case, and the process of generating test cases that can execute the target code line based on the test case generation model is executed cyclically until a test case covering the target code line is obtained.
[0073] In some embodiments of this disclosure, if the test cases generated by the test case generation model for the target code line still cannot cover the target code line after a preset number of test cases have been generated, the generation of test cases for the target code line can be stopped, a new code line can be determined from the code lines that the initial test cases cannot cover, and test cases for the new code line can be generated based on the test case generation model.
[0074] In some embodiments of this disclosure, combined with Figure 1 ,like Figure 3As shown, before step 105 above, the test case generation method provided in this embodiment may further include step 111 below, and step 105 above can be specifically implemented through step 105a below.
[0075] 111. Obtain the input parameter values of multiple interfaces included in the preset number of test cases in the initial test case.
[0076] The preset quantity can be determined according to the actual situation, and is not limited here.
[0077] 105a. Input the target source code, the target line of code, the target function, the target nearest neighbor test case, and the multiple interface input parameter values into the test case generation model, and output the target test case.
[0078] The test case generation model is used to generate target test cases that can execute the target code line based on the analysis of the relationship between the target code line belonging to the target function in the target source code and the target parent node, the analysis of the target nearest neighbor test cases, and the analysis of the multiple interface input parameter values. The target test cases include interface input parameter values whose attributes are at least the same as the attributes of at least one of the multiple interface input parameter values.
[0079] The attributes of the input parameter value may include at least one of the following: the type of the input parameter value, the format of the input parameter value; other attributes may also be included, which are not limited here.
[0080] The types of input parameter values can include int, long, float, double, char, etc.; the formats of input parameter values can be JSON, XML, etc.; the specifics can be determined according to the actual situation and are not limited here.
[0081] In this embodiment of the disclosure, by inputting multiple interface input parameter values, which are included in a preset number of test cases, into the test case generation model as an input condition, the test case generation model can improve the probability of generating target test cases that can cover the target code line based on learning from the multiple interface input parameter values, thereby improving the efficiency of generating test cases that can cover the specified code line.
[0082] In some embodiments of this disclosure, the test case generation model is further configured to, based on the analysis of the multiple interface input parameter values, determine the target interface input parameter value as the interface input parameter value of the target test case if the target interface input parameter value among the multiple interface input parameter values can be used as the interface input parameter value of the target test case; otherwise, it generates the interface input parameter value of the target test case if none of the multiple interface input parameter values can be used as the interface input parameter value of the target test case. Thus, the test case generation model can quickly generate test cases that cover the target code line based on multiple interface input parameter values.
[0083] In some embodiments of this disclosure, before step 105 above, the test case generation method provided in the embodiments of this disclosure may further include steps 112 to 115 below.
[0084] 112. Obtain multiple training data sets.
[0085] The training data corresponds to multiple interfaces. The training data includes the first source code of the corresponding interface, the first line of code in the first function of the first source code, the first nearest neighbor test case corresponding to the first line of code, and the second test case that can execute the first line of code.
[0086] Among them, multiple training data can correspond to different interfaces (multiple training data correspond one-to-one with multiple interfaces); or some can correspond to one interface and some can correspond to another interface (multiple training data and multiple interfaces can include many-to-one relationships or one-to-one relationships). The specific relationship can be determined according to the actual situation, and no limitation is made here.
[0087] The training data may also include multiple input parameter values of known test cases corresponding to the first source code, and may also include other feasible data, which are not limited here.
[0088] 113. Input the first source code, first function, first line of code, and first nearest neighbor test case from the training data into the initial generation model, and output the third test case that can execute the first line of code.
[0089] In this embodiment of the disclosure, the first source code, the first function, the first line of code, and the first nearest neighbor test case included in the multiple training data are respectively input into the initial generation model, and multiple third test cases are output, with each of the multiple third test cases corresponding to one of the multiple training data.
[0090] 114. Based on the differences between the third test case and the second test case, determine the loss value corresponding to the loss function.
[0091] The loss value is obtained based on the loss function and the differences between the multiple third test cases and the corresponding second test cases in the training data.
[0092] 115. Based on this loss value, adjust the initial generation model to obtain the test case generation model.
[0093] In this embodiment of the disclosure, the initial generation model is trained based on training data corresponding to multiple interfaces. Since the source code information of different interfaces is different, the source code information will play a role in the loss during the model training process. The model will not ignore the source code information and can learn the logic in the source code information, thereby increasing the probability that the test cases generated by the test case generation model can cover the specified code lines, and thus improving the efficiency of generating test cases that can cover the specified code lines.
[0094] Figure 4 This is a structural block diagram of a test case generation device shown in an embodiment of the present disclosure, such as... Figure 4 As shown, the system includes: an acquisition module 401, used to acquire the target source code of the target interface, the target source code including the interface function of the target interface and at least one function referenced by the interface function; acquire initial test cases and code coverage information corresponding to the target interface, the initial test cases including multiple test cases, the code coverage information used to indicate the executed and unexecuted lines of code in the target source code during the execution of the initial test cases; a determination module 402, used to determine, based on the code coverage information, the unexecuted target lines of code in the target source code and the target function to which the target lines of code belong, the target function being one of the interface function and the at least one function; determine the target nearest neighbor test case corresponding to the target lines of code from the initial test cases; the target nearest neighbor test case is the test case of the target parent node of the target lines of code in the initial test cases; the target parent node is the executed line of code in the target source code that is the parent node of the target lines of code and is closest to the target lines of code; and an output module 403, used to input the target source code, the target lines of code, the target functions, and the target nearest neighbor test cases into the test case generation model and output the target test cases.
[0095] In some embodiments of this disclosure, the output module 403 is specifically used to generate a target test case that can execute the target code line based on the analysis of the relationship between the target code line belonging to the target function in the target source code and the target parent node, and the analysis of the target nearest neighbor test cases, through the test case generation model.
[0096] In some embodiments of this disclosure, the determining module 402 is further configured to, after inputting the target source code, the target code line, the target function, and the target nearest neighbor test case into the test case generation model and outputting the target test case, execute the target test case and determine whether the target code line is executed; if the target code line is executed, the target test case is determined as the test case of the target interface.
[0097] In some embodiments of this disclosure, the output module 403 is further configured to, after executing the target test case and determining whether the target code line has been executed, input the target source code, the target code line, the target function, the target nearest neighbor test case, and the target test case into the test case generation model and output the first test case if the target code line has not been executed; the determining module 402 is further configured to execute the first test case and determine whether the target code line has been executed; if the target code line has been executed, determine the first test case as the test case of the target interface.
[0098] In some embodiments of this disclosure, the acquisition module 401 is further configured to acquire multiple interface input parameter values included in a preset number of test cases in the initial test case before inputting the target source code, the target code line, the target function, and the target nearest neighbor test case into the test case generation model and outputting the target test case; the output module 403 is specifically configured to input the target source code, the target code line, the target function, the target nearest neighbor test case, and the multiple interface input parameter values into the test case generation model and output the target test case, wherein the attributes of the interface input parameter values included in the target test case are at least the same as the attributes of at least one of the multiple interface input parameter values.
[0099] In some embodiments of this disclosure, the test case generation model is further used to determine the target interface input parameter value as the interface input parameter value of the target test case if the target interface input parameter value among the multiple interface input parameter values can be used as the interface input parameter value of the target test case, and to generate the interface input parameter value of the target test case if none of the multiple interface input parameter values can be used as the interface input parameter value of the target test case.
[0100] In some embodiments of this disclosure, the acquisition module 401 is further configured to acquire multiple training data before inputting the target source code, the target code line, the target function, and the target nearest neighbor test case into the test case generation model and outputting the target test case. The multiple training data correspond to multiple interfaces. The training data includes the first source code of the corresponding interface, the first code line in the first function of the first source code, the first nearest neighbor test case corresponding to the first code line, and the second test case that can execute the first code line. The output module 403 is further configured to input the first source code, the first function, the first code line, and the first nearest neighbor test case from the training data into the initial generation model and output the third test case that can execute the first code line. The determination module 402 is further configured to determine the loss value corresponding to the loss function based on the difference between the third test case and the second test case. The device further includes an adjustment module, configured to adjust the initial generation model based on the loss value to obtain the test case generation model.
[0101] In this embodiment of the disclosure, each module can implement the test case generation method provided in the above method embodiment and achieve the same technical effect. To avoid repetition, it will not be described again here.
[0102] Figure 5 This is a schematic diagram of the structure of an electronic device provided in an embodiment of the present disclosure. It is used to illustrate the electronic device that implements any test case generation method in the embodiments of the present disclosure and should not be construed as a specific limitation on the embodiments of the present disclosure.
[0103] like Figure 5 As shown, the electronic device 500 may include a processor (e.g., a central processing unit, a graphics processing unit, etc.) 501, which can perform various appropriate actions and processes according to a program stored in a read-only memory (ROM) 502 or a program loaded from a storage device 508 into a random access memory (RAM) 503. The RAM 503 also stores various programs and data required for the operation of the electronic device 500. The processor 501, ROM 502, and RAM 503 are interconnected via a bus 504. An input / output (I / O) interface 505 is also connected to the bus 504.
[0104] Typically, the following devices can be connected to I / O interface 505: input devices 506 including, for example, touchscreens, touchpads, keyboards, mice, cameras, microphones, accelerometers, gyroscopes, etc.; output devices 507 including, for example, liquid crystal displays (LCDs), speakers, vibrators, etc.; storage devices 508 including, for example, magnetic tapes, hard disks, etc.; and communication devices 509. Communication device 509 allows electronic device 500 to communicate wirelessly or wiredly with other devices to exchange data. Although an electronic device 500 with various devices is shown, it should be understood that it is not required to implement or possess all of the devices shown. More or fewer devices may be implemented or possessed alternatively.
[0105] In particular, according to embodiments of this disclosure, the processes described above with reference to the flowcharts can be implemented as computer software programs. For example, embodiments of this disclosure include a computer program product comprising a computer program carried on a non-transitory computer-readable medium, the computer program containing program code for performing the methods shown in the flowcharts. In such embodiments, the computer program can be downloaded and installed from a network via a communication device 509, or installed from a storage device 508, or installed from a ROM 502. When the computer program is executed by the processor 501, it can perform the functions defined in any test case generation method provided in embodiments of this disclosure.
[0106] It should be noted that the computer-readable medium described in this disclosure can be a computer-readable signal medium or a computer-readable storage medium, or any combination thereof. A computer-readable storage medium can be, for example, but not limited to, an electrical, magnetic, optical, electromagnetic, infrared, or semiconductor system, apparatus, or device, or any combination thereof. More specific examples of a computer-readable storage medium may include, but are not limited to: an electrical connection having one or more wires, a portable computer disk, a hard disk, random access memory (RAM), read-only memory (ROM), erasable programmable read-only memory (EPROM or flash memory), optical fiber, portable compact disk read-only memory (CD-ROM), optical storage device, magnetic storage device, or any suitable combination thereof. In this disclosure, a computer-readable storage medium can be any tangible medium containing or storing a program that can be used by or in conjunction with an instruction execution system, apparatus, or device. In this disclosure, a computer-readable signal medium can include a data signal propagated in baseband or as part of a carrier wave, carrying computer-readable program code. Such propagated data signals can take various forms, including but not limited to electromagnetic signals, optical signals, or any suitable combination thereof. A computer-readable signal medium can be any computer-readable medium other than a computer-readable storage medium, which can send, propagate, or transmit a program for use by or in connection with an instruction execution system, apparatus, or device. The program code contained on the computer-readable medium can be transmitted using any suitable medium, including but not limited to: wires, optical fibers, RF (radio frequency), etc., or any suitable combination thereof.
[0107] In some implementations, the client and server can communicate using any currently known or future-developed network protocol such as HTTP (Hypertext Transfer Protocol), and can interconnect with digital data communication (e.g., communication networks) of any form or medium. Examples of communication networks include local area networks (“LANs”), wide area networks (“WANs”), the Internet (e.g., the Internet of Things), and peer-to-peer networks (e.g., ad hoc peer-to-peer networks), as well as any currently known or future-developed networks.
[0108] The aforementioned computer-readable medium may be included in the aforementioned electronic device; or it may exist independently and not assembled into the electronic device.
[0109] The aforementioned computer-readable medium carries one or more programs that, when executed by the electronic device, cause the electronic device to: acquire target source code of a target interface, the target source code including interface functions of the target interface and at least one function referenced by the interface functions; acquire initial test cases and code coverage information corresponding to the target interface, the initial test cases including multiple test cases, the code coverage information indicating executed and unexecuted lines of code in the target source code during the execution of the initial test cases; and, based on the code coverage information, determine the unexecuted lines of code in the target source code. The test case generates a test case model by identifying the target code line and the target function to which the target code line belongs, wherein the target function is one of the interface function and the at least one function; it also determines the target nearest neighbor test case corresponding to the target code line from the initial test cases; the target nearest neighbor test case is the test case of the target parent node of the target code line in the initial test cases; the target parent node is the executed code line that is closest to the target code line among the parent nodes of the target code line in the target source code; and it inputs the target source code, the target code line, the target function, and the target nearest neighbor test case into the test case generation model to output the target test case.
[0110] In embodiments of this disclosure, computer program code for performing the operations of this disclosure can be written in one or more programming languages or a combination thereof. These programming languages include, but are not limited to, object-oriented programming languages such as Java, Smalltalk, and C++, as well as conventional procedural programming languages such as the "C" language or similar programming languages. The program code can be executed entirely on a computer, partially on a computer, as a standalone software package, partially on a computer and partially on a remote computer, or entirely on a remote computer or server. In cases involving remote computers, the remote computer can be connected to the computer via any type of network, including a local area network (LAN) or a wide area network (WAN), or it can be connected to an external computer (e.g., via the Internet using an Internet service provider).
[0111] The flowcharts and block diagrams in the accompanying drawings illustrate the architecture, functionality, and operation of possible implementations of systems, methods, and computer program products according to various embodiments of this disclosure. In this regard, each block in a flowchart or block diagram may represent a module, segment, or portion of code containing one or more executable instructions for implementing a specified logical function. It should also be noted that in some alternative implementations, the functions indicated in the blocks may occur in a different order than those indicated in the drawings. For example, two consecutively indicated blocks may actually be executed substantially in parallel, and they may sometimes be executed in reverse order, depending on the functions involved. It should also be noted that each block in the block diagrams and / or flowcharts, and combinations of blocks in the block diagrams and / or flowcharts, can be implemented using a dedicated hardware-based system that performs the specified function or operation, or using a combination of dedicated hardware and computer instructions.
[0112] The units described in the embodiments of this disclosure can be implemented in software or hardware. The names of the units are not, in some cases, intended to limit the specific unit.
[0113] The functions described above in this document can be performed, at least in part, by one or more hardware logic components. For example, exemplary types of hardware logic components that can be used, without limitation, include: Field Programmable Gate Arrays (FPGAs), Application-Specific Integrated Circuits (ASICs), Application Standard Products (ASSPs), System-on-Chip (SoCs), Complex Programmable Logic Devices (CPLDs), and so on.
[0114] In the context of this disclosure, a computer-readable medium can be a tangible medium that may contain or store a program for use by or in conjunction with an instruction execution system, apparatus, or device. A computer-readable medium can be a computer-readable signal medium or a computer-readable storage medium. A computer-readable medium can be, but is not limited to, electronic, magnetic, optical, electromagnetic, infrared, or semiconductor systems, apparatus, or devices, or any suitable combination of the foregoing. More specific examples of computer-readable storage media include electrical connections based on one or more wires, portable computer disks, hard disks, random access memory (RAM), read-only memory (ROM), erasable programmable read-only memory (EPROM or flash memory), optical fiber, portable compact disk read-only memory (CD-ROM), optical storage devices, magnetic storage devices, or any suitable combination of the foregoing.
[0115] The above description is merely a preferred embodiment of this disclosure and an explanation of the technical principles employed. Those skilled in the art should understand that the scope of this disclosure is not limited to technical solutions formed by specific combinations of the above-described technical features, but should also cover other technical solutions formed by arbitrary combinations of the above-described technical features or their equivalents without departing from the above-described concept. For example, technical solutions formed by substituting the above features with (but not limited to) technical features disclosed in this disclosure that have similar functions.
[0116] Furthermore, while the operations are described in a specific order, this should not be construed as requiring these operations to be performed in the specific order shown or in a sequential order. In certain environments, multitasking and parallel processing may be advantageous. Similarly, while several specific implementation details are included in the above discussion, these should not be construed as limiting the scope of this disclosure. Certain features described in the context of individual embodiments may also be implemented in combination in a single embodiment. Conversely, various features described in the context of a single embodiment may also be implemented individually or in any suitable sub-combination in multiple embodiments.
[0117] Although the subject matter has been described using language specific to structural features and / or methodological logic, it should be understood that the subject matter defined in the appended claims is not necessarily limited to the specific features or actions described above. Rather, the specific features and actions described above are merely illustrative examples of implementing the claims.
Claims
1. A test case generation method, characterized in that, The method includes: Obtain the target source code of the target interface, wherein the target source code includes the interface function of the target interface and at least one function referenced by the interface function; Obtain the initial test cases and code coverage information corresponding to the target interface. The initial test cases include multiple test cases, and the code coverage information is used to indicate the lines of code that are executed and lines of code that are not executed in the target source code during the execution of the initial test cases. Based on the code coverage information, the unexecuted target code line in the target source code and the target function to which the target code line belongs are determined, wherein the target function is one of the interface function and the at least one function; The target nearest neighbor test case corresponding to the target code line is determined from the initial test cases; the target nearest neighbor test case is the test case of the target parent node of the target code line in the initial test cases; the target parent node is the code line in the target source code that is executed and is closest to the target code line among the parent nodes of the target code line. The target source code, the target line of code, the target function, and the target nearest neighbor test cases are input into the test case generation model, and the target test cases are output.
2. The method according to claim 1, characterized in that, The step of inputting the target source code, the target code line, the target function, and the target nearest neighbor test case into the test case generation model, and outputting the target test case, includes: The test case generation model generates target test cases that can execute the target code lines based on the analysis of the relationship between the target code lines belonging to the target function in the target source code and the target parent node, as well as the analysis of the target nearest neighbor test cases.
3. The method according to claim 1, characterized in that, After inputting the target source code, the target line of code, the target function, and the target nearest neighbor test cases into the test case generation model and outputting the target test cases, the method further includes: Execute the target test case to determine whether the target line of code has been executed; When the target line of code is executed, the target test case is determined as the test case for the target interface.
4. The method according to claim 3, characterized in that, After executing the target test case and determining whether the target line of code has been executed, the method further includes: If the target code line is not executed, the target source code, the target code line, the target function, the target nearest neighbor test case, and the target test case are input into the test case generation model, and the first test case is output. Execute the first test case to determine whether the target line of code has been executed; When the target line of code is executed, the first test case is determined as the test case for the target interface.
5. The method according to claim 1, characterized in that, Before inputting the target source code, the target code line, the target function, and the target nearest neighbor test case into the test case generation model and outputting the target test case, the method further includes: Obtain the input parameter values of multiple interfaces included in a preset number of test cases in the initial test cases; The step of inputting the target source code, the target code line, the target function, and the target nearest neighbor test case into the test case generation model, and outputting the target test case, includes: The target source code, the target line of code, the target function, the target nearest neighbor test case, and the multiple interface input parameter values are input into the test case generation model, and the target test case is output. The attributes of the interface input parameter values included in the target test case are at least the same as the attributes of at least one of the multiple interface input parameter values.
6. The method according to claim 5, characterized in that, The test case generation model is also used to determine the target interface input parameter value as the interface input parameter value of the target test case when the target interface input parameter value among the multiple interface input parameter values can be used as the interface input parameter value of the target test case, based on the analysis of the multiple interface input parameter values. If none of the multiple interface input parameter values can be used as interface input parameter values for the target test case, then the interface input parameter values for the target test case are generated.
7. The method according to claim 1, characterized in that, Before inputting the target source code, the target code line, the target function, and the target nearest neighbor test case into the test case generation model and outputting the target test case, the method further includes: Acquire multiple training data, which correspond to multiple interfaces. The training data includes the first source code of the corresponding interface, the first line of code in the first function of the first source code, the first nearest neighbor test case corresponding to the first line of code, and the second test case that can execute the first line of code. Input the first source code, the first function, the first line of code, and the first nearest neighbor test case from the training data into the initial generation model, and output a third test case that can execute the first line of code; Based on the differences between the third test case and the second test case, the loss value corresponding to the loss function is determined; Based on the loss value, the initial generation model is adjusted to obtain the test case generation model.
8. A test case generation device, characterized in that, include: The acquisition module is used to acquire the target source code of the target interface, wherein the target source code includes the interface function of the target interface and at least one function referenced by the interface function; Obtain the initial test cases and code coverage information corresponding to the target interface. The initial test cases include multiple test cases, and the code coverage information is used to indicate the lines of code that are executed and lines of code that are not executed in the target source code during the execution of the initial test cases. The determination module is configured to determine, based on the code coverage information, the unexecuted target code line in the target source code and the target function to which the target code line belongs, wherein the target function is one of the interface function and the at least one function; Determine the target nearest neighbor test case corresponding to the target line of code from the initial test cases; The target nearest neighbor test case is the test case of the target parent node of the target line of code in the initial test case; The target parent node is the line of code in the target source code that is executed and is closest to the target code line, which is the parent node of the target code line. The output module is used to input the target source code, the target code line, the target function, and the target nearest neighbor test case into the test case generation model, and output the target test case.
9. An electronic device, characterized in that, include: A memory and a processor, wherein the memory is used to store a computer program; and the processor is used to execute the test case generation method of any one of claims 1 to 7 when the computer program is invoked.
10. A computer-readable storage medium, characterized in that, It stores a computer program that, when executed by a processor, implements the test case generation method according to any one of claims 1 to 7.
11. A computer program product, characterized in that, The computer program product includes a computer program that, when executed by a processor, implements the test case generation method according to any one of claims 1 to 7.
Citation Information
Patent Citations
Software testing method and device
CN111400164A
Method and device for analyzing program interface and computer equipment
CN113760690A