Unit test case generation method and device, electronic equipment and storage medium

CN117421235BActive Publication Date: 2026-10-30BEIJING BAIDU NETCOM SCI & TECH CO LTD
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202311464790.4
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2023-11-06
Publication Date
2026-10-30
Estimated Expiration
2043-11-06

AI Technical Summary

Benefits of technology

[0020]应当理解,本部分所描述的内容并非旨在标识本申请的实施例的关键或重要特征,也不用于限制本申请的范围。本申请的其它特征将通过以下的说明书而变得容易理解。

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN117421235B_ABST
    Figure CN117421235B_ABST
Patent Text Reader

Abstract

The application discloses a unit test case generation method and device, electronic equipment and a storage medium, relates to the technical field of computers, and in particular to the field of artificial intelligence such as deep learning and large models. The specific scheme is: obtaining a to-be-tested method and context information of the to-be-tested method; generating a first unit test case for correctly calling the to-be-tested method according to the to-be-tested method and the context information; supplementing the first unit test case with a test case for a normal condition according to the to-be-tested method and the context information, to generate a second unit test case; and supplementing the second unit test case with a test case for an abnormal condition, to generate a third unit test case. In this way, the unit test case for correctly calling the to-be-tested method is generated first, then the unit test case is supplemented with the test case for the normal condition, and then the test case for the normal condition is supplemented with the test case for the abnormal condition, so that the unit test case containing the normal condition and the abnormal condition is gradually generated, and the quality of the unit test case is improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This application relates to the field of computer technology, and in particular to the field of artificial intelligence such as deep learning and large models, specifically to a method, apparatus, electronic device and storage medium for generating unit test cases. Background Technology

[0002] Unit testing refers to the inspection and verification of the smallest testable unit in software. In the field of white-box testing, unit testing is an indispensable step, allowing hidden problems in the code to be discovered as early as possible before integration testing. This stage is the one with the lowest cost of resolving problems. Summary of the Invention

[0003] This application provides a method, apparatus, electronic device, and storage medium for generating unit test cases. The specific solution is as follows:

[0004] According to one aspect of this application, a method for generating unit test cases is provided, comprising:

[0005] Obtain the method under test and its context information;

[0006] Based on the method under test and the context information, generate the first unit test case that correctly calls the method under test:

[0007] Based on the method to be tested and the context information, supplement the test cases of the first unit test cases with test cases of normal conditions, and generate the test cases of the second unit test cases.

[0008] Add test cases for abnormal situations to the test cases of the second unit, and generate test cases for the third unit.

[0009] According to another aspect of this application, a unit test case generation apparatus is provided, comprising:

[0010] The acquisition module is used to acquire the method under test and its context information.

[0011] The first generation module is used to generate the first unit test case that correctly calls the method under test based on the method under test and the context information.

[0012] The second generation module is used to supplement the test cases of the first unit test cases with test cases of normal conditions based on the method to be tested and context information, and generate the test cases of the second unit test cases.

[0013] The third generation module is used to supplement the second unit test cases with test cases for abnormal situations, and generate the third unit test cases.

[0014] According to another aspect of this application, an electronic device is provided, comprising:

[0015] At least one processor; and

[0016] A memory communicatively connected to the at least one processor; wherein,

[0017] The memory stores instructions that can be executed by the at least one processor, which, when executed by the at least one processor, enables the at least one processor to perform the method described in the above embodiments.

[0018] According to another aspect of this application, a non-transitory computer-readable storage medium storing computer instructions is provided, wherein the computer instructions are used to cause the computer to perform the method described in the above embodiments.

[0019] According to another aspect of this application, a computer program product is provided, including a computer program that, when executed by a processor, implements the steps of the method described in the above embodiments.

[0020] It should be understood that the description in this section is not intended to identify key or essential features of the embodiments of this application, nor is it intended to limit the scope of this application. Other features of this application will become readily apparent from the following description. Attached Figure Description

[0021] The accompanying drawings are provided for a better understanding of this solution and do not constitute a limitation of this application. Wherein:

[0022] Figure 1 A flowchart illustrating a method for generating unit test cases according to an embodiment of this application;

[0023] Figure 2 A flowchart illustrating a method for generating unit test cases according to another embodiment of this application;

[0024] Figure 3 A flowchart illustrating a method for generating unit test cases according to another embodiment of this application;

[0025] Figure 4 A flowchart illustrating a method for generating unit test cases according to another embodiment of this application;

[0026] Figure 5 A flowchart illustrating a method for generating unit test cases according to another embodiment of this application;

[0027] Figure 6 This application provides a schematic diagram of a process for generating unit test cases in multiple rounds based on a large model thinking chain, as illustrated in an embodiment of the present application.

[0028] Figure 7 A schematic diagram of the structure of a unit test case generation device provided in an embodiment of this application;

[0029] Figure 8 This is a block diagram of an electronic device used to implement the unit test case generation method of the embodiments of this application. Detailed Implementation

[0030] The following description, in conjunction with the accompanying drawings, illustrates exemplary embodiments of this application, including various details to aid understanding. These should be considered merely exemplary. Therefore, those skilled in the art will recognize that various changes and modifications can be made to the embodiments described herein without departing from the scope and spirit of this application. Similarly, for clarity and brevity, descriptions of well-known functions and structures are omitted in the following description.

[0031] The following description, with reference to the accompanying drawings, details a method, apparatus, electronic device, and storage medium for generating unit test cases according to embodiments of this application.

[0032] Figure 1 This is a flowchart illustrating a method for generating unit test cases according to an embodiment of this application.

[0033] The unit test case generation method of this application embodiment can be executed by the unit test case generation device of this application embodiment. The device can be configured in an electronic device to generate high-quality unit test cases.

[0034] Among them, electronic devices can be any device with computing capabilities, such as personal computers, mobile terminals, servers, etc. Mobile terminals can be hardware devices with various operating systems, touch screens and / or displays, such as in-vehicle devices, mobile phones, tablets, personal digital assistants, wearable devices, etc.

[0035] like Figure 1 As shown, the method for generating unit test cases includes:

[0036] Step 101: Obtain the method under test and its context information.

[0037] In this application, a syntax tree can be parsed on the code file to be tested, the method to be tested can be extracted from the code file to be tested, and the context information associated with the method to be tested can be mined based on the class to which the method to be tested belongs, the data type of the input parameter, the data type of the output parameter, etc., so that unit testing can be performed at the method level.

[0038] The method to be tested can also be understood as the function to be tested. The method to be tested extracted from the code file to be tested can be one or more, and there is no limitation on this.

[0039] In this application, the context information of the method under test may include, but is not limited to, the name of the method under test, the class to which the method under test belongs, the name of the input parameter of the method under test, the parameter type of the input parameter, the name of the output parameter of the method under test, and the parameter type of the output parameter.

[0040] Step 102: Generate the first unit test case that correctly calls the method under test based on the method under test and the context information.

[0041] The first unit test case can be a unit test case that correctly and simply calls the method under test.

[0042] In this application, the method under test and context information can be analyzed to obtain the characteristic information of the method under test, and the first unit test case that correctly calls the method under test can be generated based on the characteristic information of the method under test, the test framework, etc.

[0043] The characteristic information of the method under test may include the class to which the method belongs, the number of input parameters, the parameter types of the input parameters, the number of output parameters, and the parameter types of the output parameters. Input parameters can refer to the input parameters themselves.

[0044] The testing framework can be determined according to actual needs, and this application does not impose any restrictions on it.

[0045] Step 103: Based on the method to be tested and the context information, supplement the first unit test cases with test cases under normal conditions to generate the second unit test cases.

[0046] In this application, the test method and context information can be analyzed to determine the internal business logic of the test method, i.e., the implementation logic. Based on the implementation logic, test cases for normal conditions can be added to the first unit test cases to generate the second unit test cases.

[0047] The implementation logic can include the input parameters, expected output, boundary conditions, and abnormal situations of the method under test.

[0048] In this application, the test cases under normal conditions are mainly to verify that the method under test can correctly perform the expected function under normal input conditions, such as verifying whether the input data meets the requirements, and verifying whether the return value of the method under test is correct under given parameters.

[0049] In this application, test cases for normal situations can be added to the first unit test cases to obtain the second unit test cases, or the first unit test cases and the second unit test cases can be independent, for example, each normal situation corresponds to one unit test case.

[0050] In this application, test scenarios that can normally call the method under test can be added to the test cases of the first unit to verify whether the correct output is generated under normal input conditions, thereby realizing the testing of normal conditions.

[0051] Step 104: Add test cases for abnormal situations to the second unit test cases to generate the third unit test cases.

[0052] In this application, test cases for abnormal situations can be added to the second unit test cases according to the implementation logic of the method under test, so as to generate the third unit test cases.

[0053] Among them, the test cases for abnormal situations mainly verify that the method under test can correctly handle and respond to abnormal inputs. For example, verify whether the method under test will output appropriate exception or error information when there is no input, negative input, null pointer, or invalid data, and verify whether the method under test is stable under boundary conditions.

[0054] In this application, test cases for abnormal situations can be added to the second unit test cases that include normal cases. Alternatively, the second unit test cases and the third unit test cases can be independent, such as one unit test case corresponding to each normal case or each abnormal case. In other words, the relationship between a method under test and unit test cases can be one-to-one or one-to-many.

[0055] In this application, the test framework used when generating unit test cases may differ, and the correspondence between the method under test and the unit test cases may also differ.

[0056] In this embodiment, a first unit test case that correctly calls the method under test is generated based on the method under test and its context information. Then, based on the method under test and the context information, test cases for normal scenarios are added to the first unit test case to generate a second unit test case containing normal scenarios. Finally, test cases for abnormal scenarios are added to the second unit test case, resulting in a third unit test case containing both normal and abnormal scenarios. Therefore, by first generating unit test cases that correctly call the method under test based on the method under test and its context information, then adding test cases for normal scenarios to these unit test cases, and finally adding test cases for abnormal scenarios to the test cases containing normal scenarios, the quality of unit test cases is improved.

[0057] Figure 2 This is a flowchart illustrating a method for generating unit test cases according to another embodiment of this application.

[0058] like Figure 2 As shown, the method for generating unit test cases includes:

[0059] Step 201: Obtain the method under test and its context information.

[0060] In this application, step 201 can be implemented in any of the embodiments of this application, and there is no limitation on it, nor will it be described in detail here.

[0061] Step 202: Obtain the first prompt information.

[0062] In this application, the first prompt information can be used to instruct the large model on the feature analysis task of the method under test and the generation task of unit test cases that correctly call the method under test. The feature analysis task can refer to the task of analyzing the feature information of the method under test, and the generation task can refer to the task of generating unit test cases that correctly call the method under test.

[0063] For example, the first prompt message is: "Analyze the class, input parameter characteristics, and output parameter characteristics of the method under test, including method A1 and its context information, and use test framework a1 to generate unit test cases that correctly and simply call method A1." Here, the location of "method A1" in the first prompt message can be the code of method A1.

[0064] In this application, the first prompt information may be obtained based on the user's input in the front-end interface of the large model, such as the user inputting the method to be tested, the context information of the method to be tested, and the first prompt information, or the first prompt information may be generated based on a preset prompt template, and there is no limitation on this.

[0065] Step 203: Input the method to be tested, context information, and first prompt information into the large model for processing to generate the first unit test case.

[0066] In this application, a large model can be used to analyze the context information of the method under test and the method under test according to the feature analysis task, obtain the feature information of the method under test, and process the feature information of the method under test according to the task of generating unit test cases that correctly call the method under test, thereby generating the first unit test case that correctly calls the method under test.

[0067] For example, the large model can process the feature information of the method under test and the default test framework according to the generation task to generate the first unit test case. That is, based on the generation task, the first unit test case is generated using the default test framework according to the feature information of the method under test.

[0068] The explanation of the feature information and the first unit test cases can be found in the above embodiments, and therefore will not be repeated here.

[0069] In this application, the test framework used to generate unit test cases can also be determined by the user according to actual needs. For example, the first prompt information can also include the target test framework. A large model can be used to analyze the method under test and the surrounding information according to the feature analysis task, obtain the feature information of the method under test, and process the feature information of the method under test and the target test framework according to the generation task to generate the first unit test case.

[0070] Therefore, the large model can be guided by the first prompt information to analyze the context information of the method under test and the method under test, obtain the feature information of the method under test, and guide the large model to generate the first unit test case that correctly calls the method under test based on the feature information using the target test framework, thereby improving the quality of the first unit test case.

[0071] Step 204: Based on the method to be tested and the context information, supplement the first unit test cases with test cases under normal conditions to generate the second unit test cases.

[0072] Step 205: Add test cases for abnormal situations to the second unit test cases to generate the third unit test cases.

[0073] In this application, steps 204-205 can be implemented in any of the embodiments of this application, and there is no limitation on this, nor will it be described in detail here.

[0074] In this embodiment, by obtaining the first prompt information, the large model processes the method under test, context information, and the first prompt information to generate a first unit test case. Thus, based on the code generation capability of the large model, the first prompt information instructs the large model to complete the feature analysis task and the generation of unit test cases that correctly call the method under test. This results in unit test cases where the method under test can be correctly called, improving the quality of the unit test cases.

[0075] Figure 3 This is a flowchart illustrating a method for generating unit test cases according to another embodiment of this application.

[0076] like Figure 3 As shown, the method for generating unit test cases includes:

[0077] Step 301: Obtain the method under test and its context information.

[0078] Step 302: Generate the first unit test case that correctly calls the method under test based on the method under test and the context information.

[0079] In this application, steps 301-302 can be implemented in any of the embodiments of this application, and there is no limitation on this, nor will it be described in detail here.

[0080] Step 303: Obtain the second prompt information.

[0081] In this application, the second prompt information can be used to instruct the large model on the implementation logic analysis task of the method under test and the first supplementary task of unit test cases under normal conditions.

[0082] The implementation logic analysis task can refer to the task of analyzing the implementation logic of the method under test, and the first supplementary task can refer to the task of supplementing the first unit test cases with unit tests under normal conditions. The explanation of the implementation logic of the method under test can be found in the above embodiments, and therefore will not be repeated here.

[0083] For example, the second prompt message is: "Analyze the input parameters, expected output, boundary conditions, and abnormal conditions of the method under test, including method A1 and its context information, and supplement the first unit test case with test cases for normal conditions." Here, "method A1" in the second prompt message can be the code for method A1, and "first unit test case" can be the code for the first unit test case.

[0084] In this application, the second prompt information may be obtained based on the user's input in the front-end interface of the large model, such as the user inputting the method to be tested, the context information of the method to be tested, and the second prompt information, or the second prompt information may be generated based on a preset prompt template, and there is no limitation on this.

[0085] Step 304: Input the method to be tested, context information, and the first unit test cases into the large model for processing to obtain the second unit test cases.

[0086] In this application, a large model can be used to analyze the method under test and context information based on the implementation logic analysis task to determine the implementation logic of the method under test. That is, the large model performs the implementation logic analysis task on the method under test and context information to determine the implementation logic of the method under test, and processes the implementation logic and the first unit test case according to the first supplementary task to generate the second unit test case.

[0087] Therefore, the second prompt information can guide the large model to analyze the implementation logic of the method under test and the method under test, obtain the implementation logic of the method under test, and guide the large model to generate a second unit test case containing normal cases based on the implementation logic and the first unit test case, thereby improving the quality of the second unit test case.

[0088] Step 305: Add test cases for abnormal situations to the second unit test cases to generate the third unit test cases.

[0089] In this application, step 305 can be implemented in any of the embodiments of this application, and there is no limitation on it, nor will it be described in detail here.

[0090] In this embodiment, by obtaining the second prompt information, the large model processes the method under test, context information, and the second prompt information to generate a second unit test case. Thus, based on the code generation capability of the large model, the second prompt information instructs the large model to complete the first supplementary task of implementing the logical analysis task and test cases for normal situations, thereby obtaining unit test cases that include normal situations and improving the quality of the unit test cases.

[0091] Figure 4 This is a flowchart illustrating a method for generating unit test cases according to another embodiment of this application.

[0092] like Figure 4 As shown, the method for generating unit test cases includes:

[0093] Step 401: Obtain the method under test and its context information.

[0094] Step 402: Generate the first unit test case that correctly calls the method under test based on the method under test and the context information.

[0095] Step 403: Based on the method to be tested and the context information, supplement the first unit test cases with test cases under normal conditions to generate the second unit test cases.

[0096] In this application, steps 401-403 can be implemented in any of the embodiments of this application, and there is no limitation on this, nor will it be described in detail here.

[0097] Step 404: Obtain the third prompt information.

[0098] In this application, the third prompt information can be used to instruct the large model to perform a second supplementary task for unit test cases of abnormal conditions, wherein the second supplementary task can refer to the task of supplementing the second unit test cases with test cases of abnormal conditions.

[0099] For example, the third prompt message is "Add test cases for abnormal situations to the second unit test cases." Here, "second unit test cases" in the second prompt message can be the code for the second unit test cases.

[0100] In this application, the third prompt information may be obtained based on the user's input in the front-end interface of the large model, such as the user inputting the second unit test case and the third prompt information, or the third prompt information may be generated based on a preset prompt template, and there is no limitation on this.

[0101] Step 405: Input the second unit test cases and the third prompt information into the large model for processing to generate the third unit test cases.

[0102] In this application, the second unit test case and the third prompt information can be input into the large model. If the large model has already analyzed the implementation logic of the method under test, it can obtain and directly use that implementation logic. Based on the second supplementary task, the large model processes the implementation logic and the second unit test case to generate the third unit test case. Therefore, the previously analyzed implementation logic of the method under test can be directly used to supplement the second unit test case with test cases for abnormal situations, saving resources and improving the efficiency of unit test case generation.

[0103] Optionally, the third prompt can also instruct the large model to perform an implementation logic analysis task for the method under test. If the large model has not previously analyzed the implementation logic of the method under test, it can analyze the context information of the method under test and the method under test based on the implementation logic analysis task to determine the implementation logic of the method under test. Then, based on the second supplementary task, it can process the implementation logic and the second unit test cases to generate the third unit test cases. Thus, the third prompt can guide the large model to analyze the implementation logic of the method under test and guide it to supplement the second unit test cases with test cases for abnormal situations based on the implementation logic, thereby obtaining the third unit test cases and improving the quality of the third unit test cases.

[0104] Optionally, users can input the method to be tested, the context information of the method to be tested, the second unit test case, and the third prompt information into the front-end interface of the large model. They can first determine whether the implementation logic of the method to be tested has been analyzed. If the implementation logic of the method to be tested has been analyzed, the implementation logic can be used directly to supplement the second unit test case with test cases for abnormal situations, thus obtaining the third unit test case.

[0105] In this embodiment, by obtaining the third prompt information, the third unit test case and the third prompt information are processed using a large model to generate the third unit test case. Therefore, based on the code generation capability of the large model, the third prompt information instructs the large model to complete the second supplementary task of test cases for abnormal situations, thereby obtaining unit test cases that include both normal and abnormal situations, thus improving the quality of the unit test cases.

[0106] Figure 5 This is a flowchart illustrating a method for generating unit test cases according to another embodiment of this application.

[0107] like Figure 5 As shown, the method for generating unit test cases includes:

[0108] Step 501: Obtain the method under test and its context information.

[0109] Step 502: Obtain the first prompt information.

[0110] Step 503: Input the method to be tested, context information, and first prompt information into the large model for processing to generate the first unit test case.

[0111] Step 504: Obtain the second prompt information.

[0112] Step 505: Input the method to be tested, context information, and the first unit test case into the large model for processing to obtain the second unit test case.

[0113] Step 506: Obtain the third prompt information.

[0114] Step 507: Input the second unit test cases and the third prompt information into the large model for processing to generate the third unit test cases.

[0115] For a detailed explanation of steps 501-507 in this application, please refer to the above embodiments, and therefore will not be repeated here.

[0116] In this embodiment, based on the code generation capability of a large model, a series of intermediate reasoning steps are generated by using multi-round prompting interaction, thereby generating higher quality unit test cases. This ensures that the generated unit test cases are readable, can correctly call the method under test, and generate rich test scenarios.

[0117] Large models perform well in solving general problems, but they may not be ideal for testing domains that require a certain level of reasoning. This is especially true for unit test case generation, where the quality requirements for unit test cases generated from large models are very high. Ideally, the generated unit test cases should be directly usable, free of syntax errors, and contain a rich set of test scenario examples and correct assertions.

[0118] Based on this, this application allows for fine-tuning of a large-scale model, enabling it to learn the correspondence between the features of the method under test and the unit test methods. The specific process involves: categorizing the codebase in an open-source codebase by language; using different syntax tree parsing rules for different languages ​​to extract pairs of methods under test and their corresponding unit test methods; cleaning and filtering the extracted unit test methods to distinguish test frameworks and ensure assertions; performing context mining on the cleaned data for the methods under test; and assembling the data of the methods under test, unit test methods, and their context information for fine-tuning the large-scale model. The resulting large-scale model possesses specialized unit test case generation capabilities and can be used as a basis for generating unit test cases. Here, the unit test method can be understood as a unit test case.

[0119] To facilitate understanding of the above embodiments, the following is in conjunction with... Figure 6 To explain, Figure 6 This is a schematic diagram illustrating a process for generating unit test cases in multiple rounds based on a large model thinking chain, as provided in an embodiment of this application.

[0120] like Figure 6 As shown, the first part of the operation involves parsing the file to be tested to obtain the method to be tested and its context information.

[0121] The second part, the large-scale model's thought process, involves the first step of framework generation, which includes analysis of the method call to be tested and generation of unit test case frameworks. The analysis of the method call to be tested involves inputting the method to be tested and its context information into the large-scale model to perform feature analysis, including analyzing the class, input parameters, and output parameters of the method. The generation of the unit test case framework allows the large-scale model to generate correct and simple unit test cases that call the method to be tested, based on the analyzed class, input parameters, and output parameters, using a specified test framework. Therefore, the result of the first step in framework generation is unit test cases that simply call the method to be tested.

[0122] The second step, supplementing normal test cases, includes analyzing the expected behavior of the method under test and generating test cases for normal conditions. Expected behavior analysis involves the large model analyzing the implementation logic of the method under test, including input parameters, expected output, boundary conditions, and exceptions. Normal condition test case generation expands upon the unit test cases generated in the first step, generating test cases for normal conditions. Specifically, based on the implementation logic of the method under test, the unit test cases generated in the first step are supplemented with test scenarios that allow the method under test to be called normally, verifying whether the correct output is produced under normal input conditions. Therefore, the result of the second step, supplementing normal test cases, is the generation of unit test cases with normal conditions.

[0123] The third step, supplementing abnormal test cases, includes generating abnormal situation test cases. This step expands upon the unit test cases generated in the second step. Specifically, based on the implementation logic of the method under test, it adds test cases that can call the method under test in abnormal situations, such as empty input, negative input, null pointer exceptions, etc. This ensures that the method under test can generate appropriate exceptions or error messages. Thus, the result of the third step, which is the final generated result, is a set of test cases with both normal and abnormal conditions.

[0124] Since the availability of test cases for abnormal situations generated by a large model is usually lower than that of test cases for normal situations, test cases for abnormal situations can be supplemented based on the second test case.

[0125] In some embodiments, unit test cases can be generated based on algorithms, such as genetic algorithms or feedback-guided random algorithms. This method mainly involves analyzing the original text to obtain the execution path and generate unit test cases. However, this method lacks a deep understanding of the program, resulting in poor readability of the generated unit test cases and limitations in assertion scope.

[0126] In some embodiments, the method under test and context information can be passed to a large model to generate unit test cases all at once. However, the unit test methods obtained in this way have many syntax errors, incomplete test scenario construction, and incorrect assertions. If developers modify the generated unit test cases, the cost is relatively high.

[0127] The multi-round unit test case generation method in this application effectively inspires the large model to generate a thought process similar to that of developers. It uses multi-round prompts and interactions to generate a series of intermediate reasoning steps, thereby generating higher-quality unit test cases with higher readability and more accurate assertions. First, the large model analyzes and extracts the main features of the method under test, including its class, the data types of input and output parameters. This allows the model to extract these error-prone features, enabling the generated unit test cases to more correctly invoke the method under test. Then, the large model analyzes the implementation logic of the method under test, including the method's input parameters, expected output, boundary conditions, and abnormal situations. The key to this step is ensuring a clear understanding of the expected behavior of the method under test, allowing the large model to understand the construction logic of normal and abnormal scenarios, making the generated test cases and assertions more consistent with the internal logic of the method under test.

[0128] To implement the above embodiments, this application also proposes a unit test case generation device. Figure 7 This is a schematic diagram of the structure of a unit test case generation device provided in an embodiment of this application.

[0129] like Figure 7 As shown, the unit test case generation device 700 includes:

[0130] The acquisition module 710 is used to acquire the method under test and its context information.

[0131] The first generation module 720 is used to generate the first unit test case that correctly calls the method under test based on the method under test and context information.

[0132] The second generation module 730 is used to supplement the test cases of the first unit test cases with test cases of normal conditions according to the method to be tested and the context information, and generate the second unit test cases.

[0133] The third generation module 740 is used to supplement the second unit test cases with test cases for abnormal situations, and generate the third unit test cases.

[0134] Optionally, the first generation module 720 is used for:

[0135] Obtain the first prompt information; wherein, the first prompt information is used to instruct the large model on the feature analysis task of the method under test and the generation task of unit test cases that correctly call the method under test;

[0136] The method to be tested, context information, and initial prompt information are input into the large model for processing to generate the first unit test case.

[0137] Optionally, the first generation module 720 is used for:

[0138] A large model is used to analyze the method under test and its context based on the feature analysis task, thereby obtaining the feature information of the method under test.

[0139] Based on the generated task, the feature information and target test framework are processed to generate the first unit test cases.

[0140] Optionally, the second generation module 730 is used for:

[0141] Obtain the second prompt information; wherein, the second prompt information is used to indicate the first supplementary task of the large model for the implementation logic analysis task of the method under test and the unit test cases under normal conditions;

[0142] The method to be tested, context information, and the first unit test cases are input into the large model for processing to obtain the second unit test cases.

[0143] Optionally, the second generation module 730 is used for:

[0144] A large model is used to analyze the implementation logic of the method under test and the context information to determine the implementation logic of the method under test.

[0145] Based on the first supplementary task, the implementation logic and the first unit test case are processed to generate the second unit test case.

[0146] Optionally, the third generation module 740 is used for:

[0147] Obtain the third prompt information; wherein, the third prompt information is used to instruct the large model on the second supplementary task of unit test cases for abnormal situations;

[0148] The second unit test cases and the third prompt information are input into the large model for processing to generate the third unit test cases.

[0149] Optionally, the third generation module 740 is used for:

[0150] Obtain the implementation logic of the method to be tested;

[0151] The large model is used to process the implementation logic and the second unit test cases based on the second supplementary task, and generate the third unit test cases.

[0152] Optionally, the third generation module 740 is used for:

[0153] A large model is used to analyze the implementation logic of the method under test and the context information to determine the implementation logic of the method under test.

[0154] Based on the second supplementary task, the implementation logic and the second unit test cases are processed to generate the third unit test cases.

[0155] It should be noted that the explanation of the aforementioned unit test case generation method embodiment also applies to the unit test case generation device of this embodiment, so it will not be repeated here.

[0156] In this embodiment, a first unit test case that correctly calls the method under test is generated based on the method under test and its context information. Then, based on the method under test and the context information, test cases for normal scenarios are added to the first unit test case to generate a second unit test case containing normal scenarios. Finally, test cases for abnormal scenarios are added to the second unit test case, resulting in a third unit test case containing both normal and abnormal scenarios. Therefore, by first generating unit test cases that correctly call the method under test based on the method under test and its context information, then adding test cases for normal scenarios to these unit test cases, and finally adding test cases for abnormal scenarios to the test cases containing normal scenarios, the quality of unit test cases is improved.

[0157] According to embodiments of this application, this application also provides an electronic device, a readable storage medium, and a computer program product.

[0158] Figure 8 A schematic block diagram of an example electronic device 800 that can be used to implement embodiments of this application is shown. The electronic device is intended to represent various forms of digital computers, such as laptop computers, desktop computers, workstations, personal digital assistants, servers, blade servers, mainframe computers, and other suitable computers. The electronic device may also represent various forms of mobile devices, such as personal digital processors, cellular phones, smartphones, wearable devices, and other similar computing devices. The components shown herein, their connections and relationships, and their functions are merely illustrative and are not intended to limit the implementation of the application described and / or claimed herein.

[0159] like Figure 8 As shown, device 800 includes a computing unit 801, which can perform various appropriate actions and processes based on a computer program stored in ROM (Read-Only Memory) 802 or a computer program loaded from storage unit 808 into RAM (Random Access Memory) 803. RAM 803 can also store various programs and data required for the operation of device 800. The computing unit 801, ROM 802, and RAM 803 are interconnected via bus 804. I / O (Input / Output) interface 805 is also connected to bus 804.

[0160] Multiple components in device 800 are connected to I / O interface 805, including: input unit 806, such as keyboard, mouse, etc.; output unit 807, such as various types of monitors, speakers, etc.; storage unit 808, such as disk, optical disk, etc.; and communication unit 809, such as network card, modem, wireless transceiver, etc. Communication unit 809 allows device 800 to exchange information / data with other devices through computer networks such as the Internet and / or various telecommunications networks.

[0161] The computing unit 801 can be various general-purpose and / or special-purpose processing components with processing and computing capabilities. Some examples of the computing unit 801 include, but are not limited to, CPUs (Central Processing Units), GPUs (Graphics Processing Units), various special-purpose AI (Artificial Intelligence) computing chips, various computing units running machine learning model algorithms, DSPs (Digital Signal Processors), and any suitable processor, controller, microcontroller, etc. The computing unit 801 performs the various methods and processes described above, such as the method for generating unit test cases. For example, in some embodiments, the method for generating unit test cases may be implemented as a computer software program tangibly contained in a machine-readable medium, such as storage unit 808. In some embodiments, part or all of the computer program may be loaded and / or installed on device 800 via ROM 802 and / or communication unit 809. When the computer program is loaded into RAM 803 and executed by the computing unit 801, one or more steps of the method for generating unit test cases described above may be performed. Alternatively, in other embodiments, the computing unit 801 may be configured, by any other suitable means (e.g., by means of firmware), to execute a method for generating unit test cases.

[0162] Various implementations of the systems and techniques described above herein can be implemented in digital electronic circuit systems, integrated circuit systems, FPGAs (Field Programmable Gate Arrays), ASICs (Application-Specific Integrated Circuits), ASSPs (Application-Specific Standard Products), SOCs (System-on-Chips), CPLDs (Complex Programmable Logic Devices), computer hardware, firmware, software, and / or combinations thereof. These various implementations may include implementations in one or more computer programs that can be executed and / or interpreted on a programmable system including at least one programmable processor, which may be a dedicated or general-purpose programmable processor, capable of receiving data and instructions from a storage system, at least one input device, and at least one output device, and transmitting data and instructions to the storage system, the at least one input device, and the at least one output device.

[0163] The program code used to implement the methods of this application may be written in any combination of one or more programming languages. This program code may be provided to a processor or controller of a general-purpose computer, special-purpose computer, or other programmable data processing device, such that when executed by the processor or controller, the functions / operations specified in the flowcharts and / or block diagrams are implemented. The program code may be executed entirely on a machine, partially on a machine, as a standalone software package partially on a machine and partially on a remote machine, or entirely on a remote machine or server.

[0164] In the context of this application, a machine-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 machine-readable medium can be a machine-readable signal medium or a machine-readable storage medium. A machine-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 machine-readable storage media include electrical connections based on one or more wires, portable computer disks, hard disks, RAM, ROM, EPROM (Electrically Programmable Read-Only Memory) or flash memory, optical fiber, CD-ROM (Compact Disc Read-Only Memory), optical storage devices, magnetic storage devices, or any suitable combination of the foregoing.

[0165] To provide interaction with a user, the systems and techniques described herein can be implemented on a computer having: a display device for displaying information to the user (e.g., a CRT (Cathode-Ray Tube) or LCD (Liquid Crystal Display) monitor); and a keyboard and pointing device (e.g., a mouse or trackball) through which the user provides input to the computer. Other types of devices can also be used to provide interaction with the user; for example, feedback provided to the user can be any form of sensory feedback (e.g., visual feedback, auditory feedback, or tactile feedback); and input from the user can be received in any form (including sound input, voice input, or tactile input).

[0166] The systems and technologies described herein can be implemented in computing systems that include backend components (e.g., as data servers), or middleware components (e.g., application servers), or frontend components (e.g., user computers with graphical user interfaces or web browsers through which users can interact with implementations of the systems and technologies described herein), or any combination of such backend, middleware, or frontend components. The components of the system can be interconnected via digital data communication (e.g., communication networks) of any form or medium. Examples of communication networks include LANs (Local Area Networks), WANs (Wide Area Networks), the Internet, and blockchain networks.

[0167] Computer systems can include clients and servers. Clients and servers are generally geographically separated and typically interact via communication networks. The client-server relationship is established by computer programs running on the respective computers and having a client-server relationship with each other. A server can be a cloud server, also known as a cloud computing server or cloud host, a hosting product within the cloud computing service system that addresses the shortcomings of traditional physical hosts and VPS (Virtual Private Server) services, such as high management difficulty and weak business scalability. Servers can also be servers for distributed systems or servers integrated with blockchain technology.

[0168] According to embodiments of this application, this application also provides a computer program product that, when an instruction processor in the computer program product is executed, performs the unit test case generation method proposed in the above embodiments of this application.

[0169] It should be understood that the various forms of processes shown above can be used to rearrange, add, or delete steps. For example, the steps described in this application can be executed in parallel, sequentially, or in different orders, as long as the desired result of the technical solution disclosed in this application can be achieved, and this is not limited herein.

[0170] The specific embodiments described above do not constitute a limitation on the scope of protection of this application. Those skilled in the art should understand that various modifications, combinations, sub-combinations, and substitutions can be made according to design requirements and other factors. Any modifications, equivalent substitutions, and improvements made within the spirit and principles of this application should be included within the scope of protection of this application.

Claims

1. A method for generating unit test cases, comprising: Obtain the method under test and its context information; Based on the method under test and the context information, a first unit test case for correctly invoking the method under test is generated: wherein, a first prompt information is obtained, the first prompt information is used to instruct the large model on the feature analysis task of the method under test and the generation task of generating unit test cases for correctly invoking the method under test; the method under test, the context information and the first prompt information are input into the large model for processing to generate the first unit test case; Based on the method under test and the context information, test cases for normal conditions are supplemented to the first unit test cases to generate second unit test cases; wherein, second prompt information is obtained, the second prompt information is used to instruct the large model to perform the first supplementary task of the implementation logic analysis task of the method under test and the unit test cases for normal conditions; the method under test, the context information and the first unit test cases are input into the large model for processing to obtain the second unit test cases; Supplement the second unit test case with test cases for abnormal situations to generate a third unit test case; wherein, obtain a third prompt message, the third prompt message is used to instruct the large model to perform a second supplementary task for the unit test cases for abnormal situations; input the second unit test case and the third prompt message into the large model for processing to generate the third unit test case.

2. The method as described in claim 1, wherein, The first prompt information also includes the target testing framework. The step of inputting the method to be tested, the context information, and the first prompt information into a large model for processing to generate the first unit test case includes: The large model is used to analyze the method under test and the context according to the feature analysis task, and to obtain the feature information of the method under test. Based on the generated task, the feature information and the target test framework are processed to generate the first unit test case.

3. The method as described in claim 1, wherein, The step of inputting the method under test, the context information, and the first unit test case into a large model for processing to obtain the second unit test case includes: The large model is used to analyze the method under test and the context information according to the implementation logic analysis task, and the implementation logic of the method under test is determined. Based on the first supplementary task, the implementation logic and the first unit test case are processed to generate the second unit test case.

4. The method of claim 1, wherein, The step of inputting the second unit test case and the third prompt information into the large model for processing to generate the third unit test case includes: Obtain the implementation logic of the method under test; The large model is used to process the implementation logic and the second unit test case according to the second supplementary task, and the third unit test case is generated.

5. The method of claim 1, wherein, The third prompt information is also used to instruct the large model to perform implementation logic analysis tasks on the method under test. The step of inputting the second unit test case and the third prompt information into the large model for processing to generate the third unit test case includes: The large model is used to analyze the method under test and the context information according to the implementation logic analysis task, and the implementation logic of the method under test is determined. Based on the second supplementary task, the implementation logic and the second unit test case are processed to generate the third unit test case.

6. A unit test case generation apparatus, comprising: The acquisition module is used to acquire the method under test and the context information of the method under test; The first generation module is used to generate a first unit test case that correctly calls the method under test based on the method under test and the context information: wherein, a first prompt information is obtained, the first prompt information is used to instruct the large model on the feature analysis task of the method under test and the generation task of the unit test case that correctly calls the method under test; the method under test, the context information and the first prompt information are input into the large model for processing to generate the first unit test case; The second generation module is used to supplement the first unit test cases with normal test cases according to the method under test and the context information, and generate the second unit test cases; wherein, it obtains second prompt information, which is used to instruct the large model to perform the first supplementary task of the implementation logic analysis task of the method under test and the unit test cases of normal case; and inputs the method under test, the context information and the first unit test cases into the large model for processing to obtain the second unit test cases; The third generation module is used to supplement the second unit test cases with test cases for abnormal situations, and generate the third unit test cases; wherein, it obtains third prompt information, which is used to instruct the large model to perform a second supplementary task for the unit test cases for abnormal situations; and inputs the second unit test cases and the third prompt information into the large model for processing to generate the third unit test cases.

7. The apparatus of claim 6, wherein, The first generation module is used for: The large model is used to analyze the method under test and the context according to the feature analysis task, and to obtain the feature information of the method under test. Based on the generated task, the feature information and the target test framework are processed to generate the first unit test case.

8. The apparatus of claim 6, wherein, The second generation module is used for: The large model is used to analyze the method under test and the context information according to the implementation logic analysis task, and the implementation logic of the method under test is determined. Based on the first supplementary task, the implementation logic and the first unit test case are processed to generate the second unit test case.

9. The apparatus of claim 6, wherein, The third generation module is used for: Obtain the implementation logic of the method under test; The large model is used to process the implementation logic and the second unit test case according to the second supplementary task, and the third unit test case is generated.

10. The apparatus of claim 6, wherein, The third generation module is used for: The large model is used to analyze the method under test and the context information according to the implementation logic analysis task, and the implementation logic of the method under test is determined. Based on the second supplementary task, the implementation logic and the second unit test case are processed to generate the third unit test case.

11. An electronic device, comprising: At least one processor; as well as A memory communicatively connected to the at least one processor; wherein, The memory stores instructions that can be executed by the at least one processor to enable the at least one processor to perform the method of any one of claims 1-5.

12. A non-transitory computer-readable storage medium storing computer instructions, wherein, The computer instructions are used to cause the computer to perform the method according to any one of claims 1-5.

13. A computer program product comprising a computer program that, when executed by a processor, implements the steps of the method according to any one of claims 1-5.

Citation Information

Patent Citations

  • Abnormal unit test automatic generation method based on genetic algorithm and log analysis

    CN111221741A

  • Test case and test case rule generation methods and devices, chip test method and device, equipment and medium

    CN111752839A