Java unit test code generation method and system based on IDEA plug-in
By analyzing and fine-tuning the Java unit test code generated by the IDEA plug-in, a more comprehensive test case is generated, which solves the problem of insufficient test coverage and quality in the existing technology, and achieves more efficient test coverage and development efficiency.
Patent Information
- Application Number
- CN202411602737.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-11-11
- Publication Date
- 2025-02-07
AI Technical Summary
When generating Java unit test code, the existing IDEA plug-in uses unique models and single test cases, resulting in the failure to meet the test coverage and quality standards, which has a large unqualification.
By obtaining the object code for analysis, extracting the class methods and simulated use case parameters in the dependency package, and integrating the processing to obtain the code analysis results. Enter the code analysis results into the pre-trained model for fine-tuning, and obtain the fine-tuned large model. Then enter the test code into the fine-tuning large model to generate unit test code.
Improve test coverage, fully cover business logic and exceptions, enhance test reliability, reduce the workload of manually writing test cases, reduce development costs, and improve development efficiency.
Smart Images

Figure CN119396736A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of computer technology, and in particular to a Java unit test code generation method and system based on IDEA plug-in Background Art
[0002] The interaction between each IDEA plug-in and the development environment depends on preset interfaces and protocols. Although certain code standards and test frameworks (such as JUnit and TestNG) are used to ensure code quality, the model used is unique and the test cases generated are single, resulting in substandard test coverage and reliability.
[0003] At present, the mainstream technical solutions for plug-in generation unit test are:
[0004] 1. Tongyi Lingma: An AI coding assistant based on the Tongyi big model launched by Alibaba. It uses Tongyi Qianwen (CodeQwen) to provide code to AI for intelligent unit test code generation by selecting the code with the mouse.
[0005] 2. Baidu Comate: A coding assistant developed by Baidu. By clicking the navigation bar, you can provide code to the AI to intelligently generate unit test code.
[0006] When implementing unit testing, different IDEA plug-ins use different testing strategies and frameworks. The plug-ins do not follow best practices (that is, the generated unit test code does not fully cover multiple business situations and does not cover all possible situations), and the models used are not fine-tuned and trained. As a result, the test coverage and quality cannot meet the standards, and there is a high degree of non-compliance. Summary of the invention
[0007] The object of the present invention is to provide a Java unit test code generation method and system based on IDEA plug-in.
[0008] In order to solve the above technical problems, the present invention provides a Java unit test code generation method based on IDEA plug-in, which is characterized by comprising the following steps:
[0009] Get the object code;
[0010] Analyze the target code and obtain the code analysis result;
[0011] Input the code analysis results into the pre-trained model for training to obtain a fine-tuned large model;
[0012] Get the code to be tested;
[0013] Input the code to be tested into the fine-tuned large model to obtain the unit test code.
[0014] Preferably, analyzing the target code to obtain a code analysis result specifically includes the following steps:
[0015] Extract the class methods contained in the external dependency packages and internal custom dependency packages that the target code depends on in the current project;
[0016] Take the target code as the main body and the extracted code as the dependency, and perform code structure type analysis on the dependent code.
[0017] Extract two pieces of data from the data table in the relational database as the simulation case parameters that need to be passed to the test case;
[0018] Read the code structure data in the data table and assign the corresponding data type so that the simulation parameters can be filled in reasonably;
[0019] The target code, the external dependency packages that the target code depends on in the current project, the class methods contained in the internal custom dependency packages, and the simulation use case parameters are integrated and processed to obtain the code analysis results.
[0020] Preferably, the code analysis results are input into the pre-trained model for training to obtain a fine-tuned large model, which specifically includes the following steps:
[0021] Select a pre-trained model;
[0022] Based on the code analysis results, build vertical field question-answer pairs;
[0023] Select the fine-tuning framework, fine-tuning method, and fine-tuning formula;
[0024] The question-answer pairs are input into the pre-trained model, and the pre-trained model is fine-tuned according to the fine-tuning framework, fine-tuning method and fine-tuning formula to obtain a fine-tuned large model.
[0025] Preferably, the pre-trained model performs business scenario recognition, and the business scenarios include normal business processes and abnormal situations.
[0026] Preferably, the method further comprises the following steps:
[0027] Evaluate the fine-tuned large model.
[0028] Preferably, the fine-tuning framework is a DeepSpeed framework;
[0029] The fine-tuning method is the LoRA method, which adjusts the weight of the model by introducing a low-rank matrix;
[0030] The fine-tuning formula is: calculate a new weight matrix W' as the product of the original weight matrix W and two low-rank matrices A and B, W'=W+AB; wherein A and B are parameters learned during the fine-tuning process.
[0031] Preferably, the code to be tested is input into the fine-tuned large model to obtain the unit test code, which specifically includes the following steps:
[0032] Perform deep analysis on the code to be tested to obtain the branch structure;
[0033] By performing combined analysis on the branch structure, the execution path is obtained;
[0034] Generate input data according to the execution path;
[0035] Feed the input data into the fine-tuned model and generate the unit test code.
[0036] Preferably, the code to be tested is deeply parsed to obtain a branch structure, which specifically includes the following steps:
[0037] The code under test is processed through static analysis technology, and all involved conditional judgments, logical branch points and possible paths are extracted as branch structures.
[0038] The present invention also provides a Java unit test code generation system based on IDEA plug-in, comprising:
[0039] A first acquisition module, used for acquiring target code;
[0040] A code analysis module is used to analyze the target code and obtain code analysis results;
[0041] The model fine-tuning module is used to input the code analysis results into the pre-trained model for training to obtain a fine-tuned large model;
[0042] The second acquisition module is used to acquire the code to be tested;
[0043] The multi-branch test case generation module is used to input the code to be tested into the fine-tuned large model to obtain the unit test code.
[0044] Compared with the prior art, the present invention has the following beneficial effects:
[0045] 1. Improve test coverage:
[0046] By intelligently generating multi-scenario test cases, the test coverage of the code is improved.
[0047] 2. Enhance test reliability:
[0048] Comprehensive coverage of business logic and exception situations enhances the reliability of testing.
[0049] 3. Reduce development costs:
[0050] It reduces the workload of manually writing test cases and reduces development costs.
[0051] 4. Improve development efficiency:
[0052] Rapidly generate test cases and shorten the time of writing test codes. BRIEF DESCRIPTION OF THE DRAWINGS
[0053] The specific implementation modes of the present invention are further described in detail below with reference to the accompanying drawings.
[0054] Figure 1 It is a flow chart of a method for generating Java unit test code based on IDEA plug-in of the present invention;
[0055] Figure 2 It is a flowchart of code analysis;
[0056] Figure 3 It is a flowchart of scene recognition. DETAILED DESCRIPTION
[0057] Many specific details are set forth in the following description to facilitate a full understanding of the present invention. However, the present invention can be implemented in many other ways than those described herein, and those skilled in the art can make similar generalizations without violating the connotation of the present invention, so the present invention is not limited to the specific implementation disclosed below.
[0058] The terms used in one or more embodiments of this specification are only for the purpose of describing specific embodiments, and are not intended to limit one or more embodiments of this specification. The singular forms of "a", "said" and "the" used in one or more embodiments of this specification and the appended claims are also intended to include plural forms, unless the context clearly indicates other meanings. It should also be understood that the term "and / or" used in one or more embodiments of this specification refers to and includes any or all possible combinations of one or more associated listed items.
[0059] It should be understood that although the terms first, second, etc. may be used to describe various information in one or more embodiments of this specification, this information should not be limited to these terms. These terms are only used to distinguish the same type of information from each other. For example, without departing from the scope of one or more embodiments of this specification, the first may also be referred to as the second, and similarly, the second may also be referred to as the first. Depending on the context, the word "if" as used herein may be interpreted as "at the time of" or "when" or "in response to determining".
[0060] When generating unit test code, the existing IDEA plug-in has the problem of using only one model, single test case, and insufficient coverage. The model used by the existing technology has not been fine-tuned for specific business scenarios, resulting in the generated test code being unable to fully cover various business logics and abnormal situations.
[0061] The present invention is further described in detail below in conjunction with the accompanying drawings:
[0062] In order to better illustrate the technical effect of the present invention, the present invention provides the following specific embodiments to illustrate the above technical process:
[0063] Example 1: The IDEA plug-in based on deep learning of the present invention is used to intelligently generate unit test code covering multiple scenarios. The plug-in is implemented by the following steps:
[0064] 1. Code analysis: In the process of code analysis of program code, first obtain the target code (a method code block) for which unit tests need to be generated, extract the external dependency packages that the target code depends on in the current project, and the class methods contained in the internal custom dependency packages, take the target code as the main body, and the extracted code as the dependency, and perform code structure type analysis on the dependent code. If an entity class is involved, extract two pieces of data from the data table in the relational database as the simulation case parameters that the test case needs to pass, then read the code structure data in the data table, assign the corresponding data type so that the simulation parameters are reasonably filled, and finally integrate the whole according to the process analysis task of the analysis task to obtain the code analysis result. The code analysis result includes the target code, the external dependency packages that the target code depends on in the current project, the class methods contained in the internal custom dependency packages, and the simulation case parameters; the process implementation is as follows Figure 2 As shown:
[0065] The simulation use case parameters can be used to set specific simulation parameters to test the behavior of the code in different situations, including normal processes, boundary conditions, abnormal situations, etc. This helps to fully cover various execution paths of the code;
[0066] Code analysis results,The fine-tuned model can comb clearer logical perception through target code,dependencies, so that it can accurately control the state of the test environment and the,corresponding business scenarios, and improve the work efficiency of the model.
[0067] 2. Scenario recognition: Based on business logic, identify possible business scenarios and abnormal situations.
[0068] A: Normal business process
[0069] Generally valid input: It is stored in the database table and is judged as normal business data. A set of data extracted from the data table in the relational database is used as the simulation case parameters that need to be passed by the test case to constitute the valid input. Another set of data is tested repeatedly to ensure the stability of the unit test operation.
[0070] Boundary value: Determine the valid and invalid range of input values. For example, the acceptable valid range of a field is 0 to 120. Within the valid value range, it is determined to be normal boundary value data. Use 0 and 120 as two sets of boundary value simulation data parameters to verify whether the code compiles well for boundary value processing. Check whether there are error messages, exceptions thrown, or incorrect output.
[0071] B: Abnormal situation
[0072] General invalid input: For example, for a field that requires an email address, invalid input may include special characters, empty strings, or non-existent domain names. When invalid data is entered, check whether the system rejects these inputs and provides appropriate error messages and ensure that the system can handle invalid input gracefully without crashing or producing unpredictable behavior.
[0073] Empty value input: For non-empty parameter fields, if an empty value is entered, it is considered an invalid empty value input. When invalid data is entered, check that the system handles empty values in accordance with business rules, for example, through validation, default values, or error messages.
[0074] Data type input determination: Determine the data type of each field or parameter and the current return value type. In the case of unequal data types, it is determined as an incorrect data type input. Enter an invalid data type and check whether the system can detect the mismatched incorrect data type. The process is implemented as follows: Figure 3 shown.
[0075] 3. Model fine-tuning
[0076] Fine-tuning is the process of further training a pre-trained large deep learning model using a new, task-specific dataset. The goal is to enable the model to quickly adapt to new tasks or domains without having to train from scratch. Fine-tuning the CodeLlama-7B model to generate unit test code for a specific vertical domain requires the following steps:
[0077] In the process of fine-tuning a large model to generate unit test code, the following steps are usually followed:
[0078] (1) Select a pre-trained model: Take the base model Codellama-7B as an example. This is a powerful pre-trained model that can be used as a starting point for fine-tuning.
[0079] (2) Data preparation: Construct question-answer pairs (QA pairs) in vertical fields, where Q represents the code at the original method granularity (target code) and A represents the corresponding unit test code (the external dependency packages that the target code in the code analysis results depends on in the current project, the class methods and simulation case parameters contained in the internal custom dependency packages, etc.). These data pairs will be used to fine-tune the model so that it can understand the code structure and test case writing methods in specific fields.
[0080] (3) Fine-tuning framework selection: Use the DeepSpeed framework for fine-tuning, which supports efficient training of large-scale models and can handle the memory and computing requirements of large models.
[0081] (4) Model structure: During the fine-tuning process, some technologies are used to adjust the model structure, such as the LoRA (Low-Rank Adaptation) method, which is a parameter-efficient fine-tuning method that adjusts the model weights by introducing a low-rank matrix instead of directly modifying the original weights. This time, LoRA technology is used to fine-tune some parameters.
[0082] (5) Calculation formula: During the fine-tuning process, some specific calculation formulas are used to adjust the model parameters. For example, when using LoRA, the new weight matrix W' is calculated as the product of the original weight matrix W and two low-rank matrices A and B, W' = W + AB; where A and B are the parameters learned during the fine-tuning process.
[0083] (6) Fine-tuning: During fine-tuning, attention should be paid to data quality, diversity, and quantity. High-quality data is critical to model performance. It is also necessary to ensure data diversity to cover different test scenarios. In addition, the right amount of data can help the model generalize better.
[0084] (7) Model evaluation: After fine-tuning is completed, the performance of the model needs to be evaluated on the validation set to ensure that it can generate high-quality unit test code, including code correctness, coverage, and consistency with the original code.
[0085] (8) Deployment and application: Once the model performs well on the validation set, the fine-tuned model can be deployed and applied in the actual development environment to automatically generate unit test code.
[0086] Through these steps, a general large model can be fine-tuned into a code generation model for a specific domain, improving the automation level of unit testing and code quality.
[0087] 4. Multi-branch test case generation:
[0088] Based on the fine-tuned model, the logical branches in the code are automatically analyzed, and test cases covering various business scenarios are generated based on different branch conditions. The specific process is as follows:
[0089] (1) Code analysis
[0090] First, the code to be tested is deeply analyzed, especially the possible branch structures in the code (such as if-else, switch-case, etc.). Through static analysis technology, the tool can extract all the involved conditional judgments, logical branch points, and possible paths. These branch points may be determined based on input parameters, database status, external interface responses, or other environmental conditions.
[0091] In this example, the logic branching is controlled by two conditions:
[0092] order.TotalAmount>1000
[0093] order.CustomerType=="VIP"
[0094] (2) Condition combination analysis
[0095] By analyzing the conditions of each branch in combination, the tool can calculate all possible execution paths. For example, in the above code, there are three possible logical paths:
[0096] order.TotalAmount>1000&&order.CustomerType=="VIP"
[0097] order.TotalAmount>1000&&order.CustomerType! ="VIP"
[0098] order.TotalAmount<=1000
[0099] (3) Test data generation
[0100] After identifying all possible logical branches, the tool automatically generates input data corresponding to these branches to ensure that each branch can be effectively tested. Through the generation algorithm based on the fine-tuned model, the tool can automatically adjust the value of the input data according to the conditions to generate test cases covering each logical branch. The following are different input data generated for the sample code:
[0101] Test case 1:
[0102] Input: order.TotalAmount=1200, order.CustomerType="VIP"
[0103] Expected Output: "Apply VIP Discount"
[0104] Test case 2:
[0105] Input: order.TotalAmount=1200, order.CustomerType="Regular"
[0106] Expected Output: "Apply Regular Discount"
[0107] Test case 3:
[0108] Input: order.TotalAmount=800, order.CustomerType="VIP"
[0109] Expected Output: "No Discount"
[0110] In this way, the generated test cases will be used as test data for the unit test code, thereby generating unit test code that is more in line with the code logic and business scenarios.
[0111] 5. User Interaction: Provides a user interface that allows developers to manually add test cases and then modify them, including a clear workspace where test cases are displayed. To improve readability, test cases are presented in strips in cards.
[0112] The overall architecture diagram is as follows: Figure 1 As shown:
[0113] The code insertion operation is an on-demand operation. Users can create a new unit test code file by clicking the "New Unit Test" button on the user interface. The code created by the file will be automatically inserted into the IDEA editor. Users can modify any details of the unit test at any time in the editor, such as manually changing the parameters of the code when the parameters of the generated test code do not match.
[0114] However, other machine learning algorithms, such as genetic algorithms and decision trees, can also be considered for test case generation and optimization. In addition, this plug-in can be integrated with other IDEA plug-ins, such as code quality analysis plug-ins, version control plug-ins, etc., to provide more comprehensive development support.
[0115] In the several embodiments provided by the present invention, it should be understood that the disclosed devices and methods can be implemented in other ways. For example, the device embodiments described above are only schematic, for example, the division of the modules, modules or units is only a logical function division, and there may be other division methods in actual implementation, for example, multiple units, modules or components can be combined or integrated into another device, or some features can be ignored or not executed.
[0116] The units may or may not be physically separated, and the components displayed as units may be one physical unit or multiple physical units, that is, they may be located in one place or distributed in multiple different places. Some or all of the units may be selected according to actual needs to achieve the purpose of the present embodiment.
[0117] In addition, each functional unit in each embodiment of the present invention may be integrated into one processing unit, or each unit may exist physically separately, or two or more units may be integrated into one unit. The above-mentioned integrated unit may be implemented in the form of hardware or in the form of software functional units.
[0118] In particular, according to the embodiments disclosed in the present invention, the process described above with reference to the flowchart can be implemented as a computer software program. For example, the embodiments disclosed in the present invention include a computer program product, which includes a computer program carried on a computer-readable medium, and the computer program includes a program code for executing the method shown in the flowchart. In such an embodiment, the computer program can be downloaded and installed from the network through the communication part, and / or installed from a removable medium. When the computer program is executed by the central processing unit (CPU), the above-mentioned functions defined in the method of the present invention are executed. It should be noted that the above-mentioned computer-readable medium of the present invention can be a computer-readable signal medium or a computer-readable storage medium or any combination of the above two. The computer-readable storage medium can be, for example, but not limited to, a system, device or device of an electrical, magnetic, optical, electromagnetic, infrared segment, or semiconductor, or any combination of the above.
[0119] The flow chart and block diagram in the accompanying drawings illustrate the possible architecture, function and operation of the system, method and computer program product according to various embodiments of the present invention. In this regard, each square box in the flow chart or block diagram can represent a module, a program segment or a part of a code, and the module, the program segment or a part of the code contains one or more executable instructions for realizing the specified logical function. It should also be noted that in some alternative implementations, the functions marked in the square box can also occur in a sequence different from that marked in the accompanying drawings. For example, two square boxes represented in succession can actually be executed substantially in parallel, and they can sometimes be executed in the opposite order, depending on the functions involved. It should also be noted that each square box in the block diagram and / or flow chart, and the combination of the square boxes in the block diagram and / or flow chart can be implemented with a dedicated hardware-based system that performs the specified function or operation, or can be implemented with a combination of dedicated hardware and computer instructions.
[0120] The above is only a specific embodiment of the present invention, but the protection scope of the present invention is not limited thereto. Any changes or substitutions within the technical scope disclosed by the present invention should be included in the protection scope of the present invention. Therefore, the protection scope of the present invention should be based on the protection scope of the claims.
Claims
1. A Java unit test code generation method based on IDEA plug-in, characterized in that: The following steps are involved: Get the object code; Analyze the target code and obtain the code analysis result; Input the code analysis results into the pre-trained model for training to obtain a fine-tuned large model; Get the code to be tested; Input the code to be tested into the fine-tuned large model to obtain the unit test code.
2. The method for generating Java unit test code based on IDEA plug-in according to claim 1, characterized in that: Analyze the target code to obtain the code analysis results, which specifically includes the following steps: Extract the class methods contained in the external dependency packages and internal custom dependency packages that the target code depends on in the current project; Take the target code as the main body and the extracted code as the dependency, and perform code structure type analysis on the dependent code. Extract two pieces of data from the data table in the relational database as the simulation case parameters that need to be passed to the test case; Read the code structure data in the data table and assign the corresponding data type so that the simulation parameters can be filled in reasonably; The target code, the external dependency packages that the target code depends on in the current project, the class methods contained in the internal custom dependency packages, and the simulation use case parameters are integrated and processed to obtain the code analysis results.
3. The method for generating Java unit test code based on IDEA plug-in according to claim 2, characterized in that: The code analysis results are input into the pre-trained model for training to obtain a fine-tuned large model. The specific steps include: Select a pre-trained model; Based on the code analysis results, build vertical field question-answer pairs; Select the fine-tuning framework, fine-tuning method, and fine-tuning formula; The question-answer pairs are input into the pre-trained model, and the pre-trained model is fine-tuned according to the fine-tuning framework, fine-tuning method and fine-tuning formula to obtain a fine-tuned large model.
4. The method for generating Java unit test code based on IDEA plug-in according to claim 3, characterized in that: The pre-trained model performs business scenario recognition, which includes normal business processes and abnormal situations.
5. The method for generating Java unit test code based on IDEA plug-in according to claim 4, characterized in that: The following steps are also included: Evaluate the fine-tuned large model.
6. The method for generating Java unit test code based on IDEA plug-in according to claim 5, characterized in that: The fine-tuning framework is the DeepSpeed framework; The fine-tuning method is the LoRA method, which adjusts the weight of the model by introducing a low-rank matrix; The fine-tuning formula is: calculate a new weight matrix W' as the product of the original weight matrix W and two low-rank matrices A and B, W'=W+AB; wherein A and B are parameters learned during the fine-tuning process.
7. The method for generating Java unit test code based on IDEA plug-in according to claim 6, characterized in that: Input the code to be tested into the fine-tuned large model to obtain the unit test code, which specifically includes the following steps: Perform deep analysis on the code to be tested to obtain the branch structure; By performing combined analysis on the branch structure, the execution path is obtained; Generate input data according to the execution path; Feed the input data into the fine-tuned model and generate the unit test code.
8. The method for generating Java unit test code based on IDEA plug-in according to claim 7, characterized in that: Perform a deep analysis of the code to be tested to obtain the branch structure, which includes the following steps: The code to be tested is processed through static analysis technology, and all involved conditional judgments, logical branch points and possible paths are extracted as branch structures.
9. A Java unit test code generation system based on IDEA plug-in, used to implement the Java unit test code generation method based on IDEA plug-in as claimed in any one of claims 1 to 8, characterized in that: include: A first acquisition module, used for acquiring target code; A code analysis module is used to analyze the target code and obtain code analysis results; The model fine-tuning module is used to input the code analysis results into the pre-trained model for training to obtain a fine-tuned large model; The second acquisition module is used to acquire the code to be tested; The multi-branch test case generation module is used to input the code to be tested into the fine-tuned large model to obtain the unit test code.
Citation Information
Cited By
Training data generation method and device, equipment and medium
CN120029604A