Method and device for generating unit test file based on AI model and electronic equipment
Through the Maven architecture-based method, the AI model is used to generate unit test files, which solves the problems of poor compatibility and low generation efficiency of existing tools, and realizes cross-platform AI-enabled unit test generation, improving development efficiency.
Patent Information
- Application Number
- CN202411713354.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-11-27
- Publication Date
- 2025-03-18
AI Technical Summary
Existing tools that use AI models to generate unit test code are not compatible with multiple development tools, and there is a problem of relying on a single development tool, and the process of generating unit test code is cumbersome and inefficient.
Based on the Maven architecture, by configuring execution parameters, creating prompt words for unit testing purposes and sending them to the AI model, receiving and generating test content, and automatically generating unit test files, supporting the use across multiple development tools.
It realizes the universality of using AI models to generate unit test files in different development environments, improves the efficiency of unit test generation, and simplifies the operation process of developers.
Smart Images

Figure CN119645851A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of artificial intelligence technology, and in particular to a method, device, electronic device and storage medium for generating a unit test file based on an AI model. Background Art
[0002] In recent years, artificial intelligence (AI) technology has been widely used in many fields, especially in programming. With its powerful data processing and learning capabilities, AI models have been widely used in tasks such as automatic code generation, code optimization, and intelligent debugging. In particular, the advancement of deep learning and natural language processing technologies has made AI gradually become an important assistant for developers in programming work, improving development efficiency and promoting the intelligence of programming tools.
[0003] Among them, the application of AI models in generating unit test code is gradually emerging. However, the current technical solutions for generating unit test code using AI models still have many shortcomings. Most of the existing tools that use AI models to generate unit test code can only generate code for specific development environments or tools, and are not compatible with other development tools. There are problems of relying on a single development tool and lack of cross-platform use. In addition, when using AI models to generate unit test code, developers are required to manually perform multiple steps, which is cumbersome and inefficient. Summary of the invention
[0004] In view of this, an embodiment of the present application provides a method, device, electronic device and storage medium for generating a unit test file based on an AI model, which is used to solve at least one technical problem.
[0005] An embodiment of the present application provides a method for generating a unit test file based on an AI model, and the method is executed based on a Maven architecture, including: configuring execution parameters according to the content under test, the execution parameters including: a tested class and custom instructions in the tested content, the tested class including at least one tested method, and the custom instructions being used to implement personalized requirements; creating a prompt word for the purpose of unit testing according to the execution parameters and sending it to the AI model; receiving a first test content sent by the AI model, and generating at least one second test content based on the first test content; creating a unit test file at a specified location according to the second test content, the unit test file including at least code for unit testing.
[0006] The method as described above, creating prompt words for the purpose of unit testing according to execution parameters and sending them to the AI model includes: collecting basic information of the content under test based on the execution parameters, the basic information including: one or more of the tested class, the tested method, the business rules of the tested method, the dependency information of the tested method and custom instructions; assembling the collected basic information and filling it in the prompt word template; adjusting the natural language expression in the prompt word template according to the key logic in the business rules and adjusting the test coverage in the prompt word template according to the custom instructions; sending the content in the adjusted prompt word template to the AI model.
[0007] As described above, the custom instructions include: requiring the generation of forward and / or reverse test scenarios, requiring the naming method of unit test files, requiring one or more of the first test contents to be designed through equivalence class partitioning, boundary value or cause-effect diagram methods.
[0008] As described above, the method receives the first test content sent by the AI model, and generates at least one second test content based on the first test content, including: splitting the first test content into unit test code segments and unit test code comment segments based on key identifiers; formatting the unit test code segments according to the language standard requirements of the unit test framework, and the formatting includes: checking syntax, correcting non-standard positions and optimizing code, one or more; generating second test content based on the formatted unit test code segments.
[0009] As described above, the method for optimizing the generation of unit test files includes: generating test statistical information at least based on the unit test files, the test statistical information includes one or more of the execution status, the number of files generated, and abnormal information during the test process; analyzing the achievement rate of the unit test based on the test statistical information, the achievement rate includes one or more of the total achievement rate, the grammatical achievement rate, the logical achievement rate and the execution achievement rate; optimizing the method for generating unit test files based on the achievement rate, the optimization content includes: optimizing the grammatical rules and adjusting one or more of the prompt word templates.
[0010] In the method described above, the test statistical information also includes: one or more of user information, tested content information, test framework information, model engine information, time information and token usage.
[0011] In the method as described above, the execution parameters also include: one or more of: a unit test framework, a type of AI model, a model engine, and a unit test file save location.
[0012] According to another aspect of the present application, a device for generating a unit test file based on an AI model is proposed, the device is suitable for a Maven architecture, and includes: a configuration module, used to configure execution parameters according to the content under test, the execution parameters include: a tested class and a custom instruction in the tested content, the tested class includes at least one tested method, and the custom instruction is used to implement personalized requirements; a prompt word creation module, used to create a prompt word for the purpose of unit testing according to the execution parameters and send it to the AI model; a test content generation module, used to receive a first test content sent by the AI model, and generate at least one second test content based on the first test content; a unit test file creation module, used to create a unit test file at a specified location according to the second test content, the unit test file at least including code for unit testing.
[0013] According to another aspect of the present application, an electronic device is proposed, which includes a processor and a memory, wherein a computer program instruction set is stored in the memory, and when the processor executes the computer program instruction set in the memory, the method for generating a unit test file based on an AI model as described above is implemented.
[0014] According to another aspect of the present application, a computer-readable storage medium is proposed, wherein a computer program instruction set is stored on the computer-readable storage medium, and when the computer program instruction set is executed by a processor, the method for generating a unit test file based on an AI model as described above is implemented.
[0015] This method is based on the Maven architecture and can be used across a variety of development tools and environments. It has good versatility, allowing developers to enjoy the convenience of AI empowerment in different development environments. In addition, traditional unit test writing requires manual file creation, code copying, and test case configuration, while the present invention automatically generates test scripts through plug-in tools, and can also generate unit test files for multiple tested methods at the same time, greatly improving the generation efficiency of unit tests. BRIEF DESCRIPTION OF THE DRAWINGS
[0016] The preferred embodiments of the present invention will be further described in detail below with reference to the accompanying drawings, wherein:
[0017] Figure 1 It is a flowchart of a method for generating unit test files based on an AI model according to an embodiment of the present application.
[0018] Figure 2 This is a flow chart of the method for creating prompt words in step S120.
[0019] Figure 3 This is a flow chart of the method for generating the second test content in step S130.
[0020] Figure 4 It is a flowchart of a method for optimizing and generating unit test files according to an embodiment of the present application.
[0021] Figure 5 It is an interactive diagram for generating a unit test file based on an AI model according to an embodiment of the present application.
[0022] Figure 6 A schematic diagram of the structure of a device for generating unit test files based on an AI model according to an embodiment of the present application.
[0023] Figure 7 It is a schematic diagram of the hardware structure principle of an electronic device according to an embodiment of the present application. DETAILED DESCRIPTION
[0024] In order to make the purpose, technical solution and advantages of the embodiments of the present invention clearer, the technical solution in the embodiments of the present invention will be clearly and completely described below in conjunction with the drawings in the embodiments of the present invention. Obviously, the described embodiments are part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.
[0025] In the following detailed description, reference may be made to the various specification drawings that are part of the present application and are used to illustrate specific embodiments of the present application. In the accompanying drawings, similar reference numerals describe substantially similar components in different figures. The various specific embodiments of the present application are described below in sufficient detail so that a person of ordinary skill in the art with relevant knowledge and skills in the art can implement the technical solutions of the present application. It should be understood that other embodiments may also be used or structural, logical or electrical changes may be made to the embodiments of the present application.
[0026] AI model, or artificial intelligence model, is an algorithmic system that simulates human intelligent behavior. It can learn, reason, judge and solve problems. It usually achieves automated decision-making and prediction by training on large amounts of data. The core of AI model lies in its "learning" ability, which can continuously optimize its behavior and judgment by extracting patterns from data.
[0027] Maven: is a project management and automatic build tool (especially for Java programming) provided by the Apache Software Foundation. Based on the concept of Project Object Model (abbreviation: POM), Maven uses a central piece of information to manage the steps of building, reporting, and documentation of a project. Taking advantage of the fact that all Java projects are based on the Maven architecture, the automatic generation of AI-enabled unit test scripts is achieved by developing Maven tools, which can achieve good versatility across multiple development tools.
[0028] Unit testing in this application is part of the software testing process, which refers to checking and verifying the smallest testable units in the software to ensure that these units execute correctly as expected. The smallest test unit usually refers to a single function, method or class in the software. In object-oriented programming, a method of a class is usually regarded as a unit. The main purpose of unit testing is to verify whether each unit of the code correctly implements the intended functions and features and has no defects.
[0029] The class in this application refers to: the basis of object-oriented programming, which is an abstraction of a class of things with common characteristics in real life, equivalent to a template for creating objects.
[0030] A method in a unit test is also called a function, which is a block of code used to complete a specific task.
[0031] Figure 1 1 is a flow chart of a method for generating a unit test file based on an AI model according to an embodiment of the present application. Figure 1 As shown, the method is executed based on the Maven architecture and includes:
[0032] Step S110, configuring execution parameters according to the tested content, the execution parameters including: a tested class and a custom instruction in the tested content, the tested class including at least one tested method, and the custom instruction being used to implement personalized requirements;
[0033] Step S120, creating prompt words for the purpose of unit testing according to the execution parameters and sending them to the AI model;
[0034] Step S130, receiving a first test content sent by the AI model, and generating at least one second test content based on the first test content;
[0035] Step S140: Create a unit test file at a specified location according to the second test content, wherein the unit test file at least includes code for unit testing.
[0036] Maven is a widely used project management and build tool in Java development. Its powerful dependency management and modular design make project development more efficient and standardized. Maven centrally manages project dependencies through pom.xml files. Developers only need to configure the toolkit's groupId, artifactId, and version information to introduce the toolkit without manually downloading and configuring the path. Maven supports single-module and multi-module projects. For multi-module projects, Maven can share dependency configurations through parent-child project structures, significantly reducing duplication of work. Maven resolves version conflicts through dependency tree parsing functions, and allows developers to lock dependency versions to ensure the consistency of dependency packages. In plug-ins developed based on a certain IDE, version conflicts require developers to check them by themselves, which is prone to omissions or misconfigurations. Finally, Maven has a rich plugin ecosystem and product library support (such as Maven Central or private product libraries), which can quickly download and update dependencies.
[0037] From the above, we can see that the Maven-based architecture has at least the following advantages: by using the Jar file package stored in the artifact library, dependency loading can be completed with simple configuration, without worrying about package size or path management issues; it supports single-module and multi-module project architectures, and developers can introduce toolkits into specific sub-modules to meet the needs of different scenarios; Maven's build tools support automated packaging, testing, and deployment, and toolkit upgrades or migrations can be quickly integrated into the continuous integration / continuous delivery (CI / CD) process; Maven displays all dependency levels of the project through a dependency tree, and developers can clearly understand the transitive dependencies and version conflicts of the toolkit.
[0038] In step S110, the execution parameters are configured according to the content under test. The content under test usually refers to the code snippet that the developer needs to perform unit testing on it, such as a class or a method. The execution parameters include the tested class and custom instructions. The tested class refers to a class that contains at least one tested method. The method here refers to the code unit that needs to be tested, which is a non-mandatory item. If it is not filled in, the default is to generate single tests for all methods in the tested class above; if filled in, fill in the business methods in the business class above that need to generate single tests; this application supports multiple tested methods and supports the simultaneous generation of single test scripts for overloaded methods, and can process multiple classes or multiple methods at the same time, greatly improving the processing efficiency. In addition, only two pieces of information are configured to complete the configuration of the content under test, which simplifies the work steps and work content of generating unit test code and improves the test effect.
[0039] Custom instructions can include special requirements for test data, adjustments to test logic, or certain specific test environment configurations. Custom instructions allow users to adjust the generated test scripts according to their actual needs, improving the flexibility and adaptability of the test, such as test coverage, specific test scenarios, etc.
[0040] Optionally, the execution parameters also include: one or more of: unit test framework, AI model type, model engine, and unit test file save location. Among them, the unit test framework, the default value is JUnit4. You can fill in it yourself, for example: JUnit4 or JUnit5, etc. AI model type, the default value is WUKONG, you can fill in other values, such as CHATGPT, ZHIPU. When CHATGPT, ZHIPU is selected, you need to fill in the corresponding apiKey value. Unit test file save location: the default is src / test / java directory. By configuring multiple execution parameters, this application can generate flexible unit test scripts according to different project requirements and adapt to diverse development environments.
[0041] Step S120 involves creating and sending prompts. According to the configured execution parameters, the system will create prompts for the purpose of unit testing. Prompts refer to instructions or descriptions sent to the AI model to guide the AI model to generate test scripts that meet the requirements. The generated prompt contains all the information required to perform unit testing, such as the name of the tested class, the tested method, expected inputs and outputs, etc. Then, this prompt is sent to the AI model, and the AI model will generate test code based on this prompt.
[0042] In step S130, the first test content sent by the AI model is received, and at least one second test content is generated based on the first test content. The first test content is the preliminary test code generated by the AI model according to the prompt word. However, this is usually not enough to constitute a complete unit test. Therefore, the system will further generate the second test content based on the first test content to achieve the purpose of successfully completing the unit test.
[0043] In step S140, a unit test file is created at a specified location according to the second test content. The unit test file generally includes code for unit testing, which can be directly executed by the test framework to verify the correctness of the tested content. The specified location can be a test directory of the project to ensure that the unit test code is separated from the production code and follow the best practices of the Maven architecture.
[0044] This method is based on the Maven architecture and can be used across a variety of development tools and environments. It has good versatility, allowing developers to enjoy the convenience of AI empowerment in different development environments. In addition, traditional unit test writing requires manual file creation, code copying, and test case configuration, while the present invention automatically generates test scripts through plug-in tools, and can also generate unit test files for multiple tested methods at the same time, greatly improving the generation efficiency of unit tests.
[0045] According to one embodiment of the present application, the custom instructions include: requiring the generation of forward and / or reverse test scenarios, requiring the naming method of unit test files, requiring one or more of the first test contents to be designed through equivalence class partitioning, boundary value or causal graph methods.
[0046] Among them, the forward test scenario refers to testing the behavior of the tested method under normal circumstances, that is, inputting expected data and verifying whether the output meets expectations. The reverse test scenario refers to testing the behavior of the tested method under abnormal circumstances, such as inputting illegal data or simulating abnormal situations, and verifying whether the system can handle these situations correctly. The naming method can follow specific naming rules, such as including the name of the tested class, the name of the tested method, and the type of test (forward or reverse). Such a naming method helps developers quickly identify and understand the purpose and content of each unit test file. Equivalence class partitioning is a test design technique that divides the set of input data into equivalence classes, and the data in each equivalence class has the same effect on the output of the tested method. Boundary value analysis is a test design technique that focuses on the boundary conditions of input data, because boundary conditions are often where errors occur. Causal graphs are a test design technique that designs test cases based on the causal relationship between input and output. Through these custom instructions, the present invention can generate more comprehensive unit test content that meets specific needs, thereby improving the coverage and effectiveness of unit tests. However, in the prior art, it is impossible to put forward personalized requirements when requesting to generate unit test code, resulting in problems such as unsatisfactory test results or low test efficiency.
[0047] Figure 2 is a flow chart of the method for creating prompt words in step S120. Figure 2 As shown, the method includes:
[0048] Step S121, collecting basic information of the tested content based on the execution parameters, the basic information including: one or more of the tested class, the tested method, the business rules of the tested method, the dependency information of the tested method and the custom instructions;
[0049] Step S122, assembling the collected basic information and filling it in the prompt word template;
[0050] Step S123, adjusting the natural language expression in the prompt word template according to the key logic in the business rules and adjusting the test coverage in the prompt word template according to the custom instructions;
[0051] Step S124, sending the content in the adjusted prompt word template to the AI model.
[0052] In step S121, the tested class includes the class name, class structure and the module. The tested method includes the name, parameters and return value type of the target method. The business rules are to parse the functional logic of the tested method, and define its business rules including: input and output constraints. Dependency information is to identify other classes, methods or external resources that the method depends on. During the implementation process, the above information is obtained by parsing the program code, reading the configuration file or directly calling the interface. For complex business rules, the model-driven method can be combined to parse the relevant documents.
[0053] In step S122, the collected information is assembled according to a predefined template. For example, the prompt word template is as follows:
[0054] Test target: {method name} method in the tested class {class name}
[0055] Function description: {Business rules}
[0056] Input data: {test parameters}
[0057] Dependencies: {dependency information}
[0058] Generate unit test code that covers all edge cases.
[0059] In step S123, the system extracts the key logic in the business rules into understandable natural language to ensure that the AI model can accurately parse the intent. For example, if the business rules of the method being tested contain complex conditional logic (such as multi-layer nested if-else), the optimization process will break down these logics into simple and clear descriptions; clearly divide the input conditions and output results of the method to facilitate AI to generate targeted test cases; divide complex rules into multiple independent blocks for easy parsing one by one; in view of the language processing characteristics of the AI model, unnecessary technical terms or complex expressions will be converted into easy-to-understand language during optimization; if the generation of unit tests requires context (such as dependent classes, external resources, and execution environment), relevant descriptions will be supplemented during the optimization process so that the AI model can generate more complete tests based on a comprehensive understanding of the background. Example:
[0060] Original business rule:
[0061] If the input value X is positive, it returns the square of X; otherwise, if X is negative and less than -10, it returns 0; otherwise, it returns the absolute value of X.
[0062] Optimized description:
[0063] The method processes input according to the following rules:
[0064] - Returns the square of the input if it is positive;
[0065] - Returns 0 if the input is a negative number less than -10;
[0066] - Otherwise returns the absolute value of the input.
[0067] At the same time, adjust the test coverage of the prompt words according to the custom instructions to achieve personalized requirements. For example: explicitly require covering all possible boundary conditions; add clear instructions to generate test cases related to exception handling. Example:
[0068] Please generate unit tests for the following scenarios:
[0069] - Input values are maximum, minimum and zero;
[0070] -The parameter is Null or empty;
[0071] -Simulate the situation where dependent methods throw exceptions.
[0072] In step S123, the generated prompt words are sent to the target AI model through the API interface, and the unit test code returned by the AI is received.
[0073] This application can effectively reduce the workload of manual analysis of test requirements and improve the efficiency and accuracy of unit testing by optimizing prompt words. By automatically generating prompt words, more potential boundary test cases can be covered, thereby improving the stability and reliability of the system.
[0074] Figure 3 is a flow chart of the method for generating the second test content in step S130. Figure 3 As shown, the method includes:
[0075] S131, splitting the first test content into a unit test code segment and a unit test code comment segment based on the key identifier;
[0076] S132, formatting the unit test code segment according to the language standard requirements of the unit test framework, wherein the formatting includes: checking syntax, correcting irregular positions, and optimizing code, or more thereof;
[0077] S133: Generate second test content based on the formatted unit test code segment.
[0078] In step S131, the unit test code comment segment is used to explain and interpret the code, including a description of the design purpose, logic, and key points of the generated unit test code. The unit test code segment is the unit test code that meets the expected test objectives. The data content is split by predefined key identifiers, for example: the identifiers / *EXPLANATION_START* / and / *EXPLANATION_END* / indicate the start and end positions of the explanation content. The identifiers / *CODE_START* / and / *CODE_END* / indicate the start and end positions of the unit test code segment.
[0079] In step S132, the split unit test code segments are formatted according to the standard requirements of the Java language and unit test framework (such as JUnit, TestNG). Syntax checking is to check the syntax errors of the code through static code checking tools (such as Checkstyle); correcting non-standard positions is to optimize the code format in accordance with the Java language style guide (such as Google Java Style Guide), which includes code indentation adjustment, method and class annotation supplementation, and line break optimization of long lines of code; code optimization is to improve the readability and efficiency of the code, for example: deleting redundant variables or redundant import statements and optimizing the writing of loops and conditional statements.
[0080] In step S133, each formatted unit test code segment generates a second test content. Then, according to the number of second test contents, a predetermined number of unit test files are created at a specified location, and the unit test files at least include code for unit testing. Among them, a unit test file can be created for multiple second test contents for the same tested content, multiple unit test files can be created for multiple second test contents of different tested contents, or a unit test file can be created for multiple second test contents of different tested contents, which can be flexibly set according to the actual application scenario.
[0081] The above describes the implementation methods and advantages of the embodiments of the present application through multiple embodiments. The following describes the specific processing process of the embodiments of the present application in detail with reference to specific examples.
[0082] Figure 4 1 is a flowchart of a method for optimizing and generating unit test files according to an embodiment of the present application. Figure 4 As shown, the method includes:
[0083] Step S410, generating test statistics information at least according to the unit test file, wherein the test statistics information includes one or more of the execution status, the number of files generated, and abnormal information during the test process;
[0084] Step S420, analyzing the achievement rate of the unit test according to the test statistical information, wherein the achievement rate includes one or more of the total achievement rate, the syntax achievement rate, the logic achievement rate and the execution achievement rate;
[0085] Step S430, optimizing the method of generating a unit test file according to the achievement rate, wherein the optimization content includes: optimizing grammar rules and adjusting one or more of prompt word templates.
[0086] In step S410, the execution status includes success and failure. Success means that the file is generated and passes the preliminary syntax check, and failure means that an error occurs during the generation process (such as a syntax error, a format error, etc.). The number of files generated is the number of all unit test files generated. The exception information is the exception information captured during the test process (such as an unhandled exception that occurs during the generation process, an incomplete code segment returned by the model, etc.). The storage method of the statistical information can be: formatted as a log file (such as JSON, CSV) or stored in a database for subsequent analysis.
[0087] In step S420, the achievement rate is an important indicator for measuring the efficiency and effect of generating a unit test plug-in, and can be expressed by the following formula:
[0088] Achievement rate = number of test files that passed verification / total number of test files generated × 100%;
[0089] Among them, the total number of test files generated is the total number of all test files generated in a certain run; the number of test files that passed verification is the number of test files that were successfully generated and met the basic requirements. The syntax achievement rate is the proportion of test files that passed the syntax check; the logic achievement rate is the proportion of files that passed the preliminary logic check (such as basic boundary case coverage); the execution achievement rate is the proportion of test files that run successfully in the real environment. The achievement rate is calculated according to different dimensions to facilitate in-depth analysis.
[0090] In step S430, the problems of this method are analyzed based on the total achievement rate, grammar achievement rate, logic achievement rate and execution achievement rate in the achievement rate, and corresponding optimization solutions are proposed for the problems. For example, if it is found that some test scenarios (such as abnormal input) are not covered, resulting in a low logic achievement rate, the model prompt word template is adjusted to guide the generation of more comprehensive test cases. If the grammar achievement rate is found to be low, the generation strategy of this method is dynamically adjusted to optimize the grammar rules and checking tools.
[0091] According to one embodiment of the present application, the test statistical information also includes: one or more of user information, tested content information, test framework information, model engine information, time information and token usage. The tested content information is used to record the key attributes of the tested content, including the name of the tested class, the name of the tested method, and the input and output descriptions of the tested method. Token usage is used to record the number of tokens used by the AI model in each generation task (such as the total amount of input tokens and output tokens). This application provides a reliable basis for subsequent analysis and optimization by covering multi-dimensional information records. Through information analysis, it helps users improve unit test generation strategies and improve generation efficiency and quality.
[0092] Figure 5 This is an interactive diagram of generating a unit test file based on an AI model according to an embodiment of the present application. Figure 5 As shown: The method of the present application can be a plug-in based on the Maven architecture, which uses the plug-in to generate unit test code for the getEvaluationOption method of the EvaluationOptionService Java class:
[0093] The first step is to introduce dependencies: This plug-in exists in the artifact library in the form of a Jar file package. It is first introduced into its own project as a dependency through the Maven project architecture.
[0094] Step 2: Parameter configuration: Configuration code example:
[0095] <groupid> com.job51.efficiency< / groupid>
[0096] <artifactid> unittest-maven-plugin< / artifactid>
[0097] <version> 0.0.6-SNAPSHOT< / version>
[0098] <configuration>
[0099] <unitframework> JUnit4< / unitframework>
[0100] <aimodel> WUKONG< / aimodel>
[0101] <job51modeltype> QUR< / job51modeltype>
[0102] <bizclass> com.job51.dev.efficiency.performance.service.Evaluation0ptionService< / bizclass>
[0103] <bizmethods>
[0104] <param> getEvaluation0ption
[0105] < / bizmethods>
[0106] <destpackage> com.job51.dev.efficiency.performance.unittest< / destpackage>
[0107] <otherrequirement> It includes forward and reverse test cases, and the generated test class is named after the Java method being tested plus Test, and the code is generated in a Java file.< / otherrequirement>
[0108] From the above code example, we can see that the configured execution parameters are:
[0109] Unit testing framework: Use Junit4;
[0110] AI big model: Use the Wukong big model;
[0111] Large model engine: use Tongyi Qianwen;
[0112] Java class under test: EvaluationOptionService class under the com.job51.dev.efficiency.performance.service path;
[0113] Method under test: getEvaluationOption;
[0114] Unit test file generation location: com.job51.dev.efficiency.performance.unittest path;
[0115] Custom requirements: Includes forward and reverse test cases, and the generated test class is named after the Java method being tested plus Test, and the code is generated in a Java file.
[0116] Step 3: Call the tool: Double-click the patented tool plug-in in the Maven toolbar to execute it.
[0117] Step 4: parse source code: This plug-in will automatically collect all information such as relevant Java classes, methods / or all methods, introduced dependencies, etc.
[0118] Step 5: Assemble prompts: Based on the above information, organize it into prompts centered on unit test case creation.
[0119] Step 6: Call the AI big model: send the prompt word to the AI big model.
[0120] Step 7: Return AI-generated content: Receive the first test content generated by the AI model.
[0121] Step 8. Content parsing: The data content returned by the large model includes explanation information and valid unit test script code snippets. This plug-in will loop through key symbols to identify the content and split the two.
[0122] Step 9. Generate Java files: Format the script code segment data in a manner that complies with the Java language standard. Finally, write the corresponding code segment into a corresponding number of single test scripts and save them in batches in the specified path location.
[0123] Step 10: Generate test statistics: Generate test statistics while implementing step 9. Based on the test statistics, the plug-in can be optimized and improved.
[0124] This plug-in can achieve good versatility across multiple development tools, can support generating single test cases for multiple tested methods at the same time, and can directly generate Java source code files, which is very convenient; further, when configuring execution parameters, it provides multiple AI models for users to choose from, and at least only two fields can be configured, which is simple to use. Finally, it provides rich configuration details to meet user customization requirements.
[0125] Corresponding to the method embodiment of the present application, the present application also provides a device for generating a unit test file based on an AI model, such as Figure 6 As shown, the device 100 for generating a unit test file includes:
[0126] Configuration module 110, configured to configure execution parameters according to the tested content, the execution parameters including: a tested class and a custom instruction in the tested content, the tested class including at least one tested method, the custom instruction being used to implement personalized requirements;
[0127] A prompt word creation module 120, for creating prompt words for the purpose of unit testing according to execution parameters and sending them to the AI model;
[0128] A test content generating module 130 is configured to receive a first test content sent by the AI model, and generate at least one second test content based on the first test content;
[0129] The unit test file creation module 140 is used to create a unit test file at a specified location according to the second test content, and the unit test file at least includes code for unit testing.
[0130] Figure 7 It is a schematic diagram of the hardware structure principle of an electronic device according to an embodiment of the present application. The electronic device can be implemented as a server or other various terminal devices, such as a desktop personal computer, a tablet computer, a laptop computer, a mobile phone, etc., including a processor 601 and a memory 602. The memory 602 stores a program instruction set. When the processor 601 executes the program instruction set on the memory 602, any of the aforementioned recommendation information generation methods is implemented.
[0131] Specifically, the processor 601 may include a central processing unit (CPU), or an application specific integrated circuit (ASIC), or may be configured to implement one or more integrated circuits of the embodiment of the present invention.
[0132] The memory 602 may include a large capacity memory for data or instructions. By way of example and not limitation, the memory 602 may include a hard disk drive (HDD), a floppy disk drive, a flash memory, an optical disk, a magneto-optical disk, a magnetic tape, or a universal serial bus (USB) drive or a combination of two or more of these. In appropriate cases, the memory 602 may include a removable or non-removable (or fixed) medium. In appropriate cases, the memory 602 may be inside or outside the integrated gateway disaster recovery device. In a specific embodiment, the memory 602 is a non-volatile solid-state memory.
[0133] The memory may include a read-only memory (ROM), a random access memory (RAM), a magnetic disk storage medium device, an optical storage medium device, a flash memory device, an electrical, optical or other physical / tangible memory storage device. Therefore, generally, the memory includes one or more tangible (non-transitory) computer-readable storage media (e.g., a memory device) encoded with software including computer executable instructions, and when the software is executed (e.g., by one or more processors), it is operable to perform the recommendation information generation method provided by the present invention.
[0134] In one example, the electronic device may further include a communication interface 603 and a bus 604. The processor 601, the memory 602, and the communication interface 603 are connected through the bus 604 and complete the communication between each other. The communication interface 603 is mainly used to realize the communication between the modules, devices, units and / or devices in the embodiment of the present invention. The bus 604 includes hardware, software or both, and couples the components of the online data traffic billing device to each other. For example, but not limitation, the bus may include an accelerated graphics port (AGP) or other graphics bus, an enhanced industrial standard architecture (EISA) bus, a front-end bus (FSB), a hypertransport (HT) interconnect, an industrial standard architecture (ISA) bus, an infinite bandwidth interconnect, a low pin count (LPC) bus, a memory bus, a micro channel architecture (MCA) bus, a peripheral component interconnect (PCI) bus, a PCI-Express (PCI-X) bus, a serial advanced technology attachment (SATA) bus, a video electronics standard association local (VLB) bus or other suitable buses or a combination of two or more of these. Where appropriate, the bus 604 may include one or more buses. Although embodiments of the present invention describe and illustrate a particular bus, the present invention contemplates any suitable bus or interconnect.
[0135] The present invention also provides a computer-readable storage medium having computer program instructions stored thereon, and the computer program instructions can be executed by a processor to implement any of the methods for generating unit test files based on an AI model in the aforementioned embodiments. The computer-readable storage medium can be any medium that can tangibly contain or store computer-executable instructions for use by or in combination with an instruction execution system, device, and equipment. The storage medium can be a transient computer-readable storage medium or a non-transient computer-readable storage medium. Non-transient computer-readable storage media may include, but are not limited to, magnetic storage devices, optical storage devices, and / or semiconductor storage devices. Examples of such storage devices include, for example, magnetic disks, optical disks based on CD, DVD, or Blu-ray technology, and persistent solid-state memories such as flash memory, solid-state drives, and the like.
[0136] The present invention also provides a computer program product, which includes a set of computer program instructions, and when the computer program instruction set is executed by a processor, it implements any of the methods for generating unit test files based on an AI model in the aforementioned embodiments. The computer program product includes but is not limited to application installation packages published on websites and application stores, application plug-ins, and small programs that can be run in certain applications.
[0137] It should be clear that the present invention is not limited to the specific configuration and processing described above and shown in the figures. For the sake of simplicity, a detailed description of the known method is omitted here. In the above embodiments, several specific steps are described and shown as examples. However, the method process of the present invention is not limited to the specific steps described and shown, and those skilled in the art can make various changes, modifications and additions, or change the order between the steps after understanding the spirit of the present invention.
[0138] The above embodiments are only used to illustrate the present invention, but not to limit the present invention. Ordinary technicians in the relevant technical field can make various changes and modifications without departing from the scope of the present invention. Therefore, all equivalent technical solutions should also fall within the scope of the present invention.< / configuration>
Claims
1. A method for generating unit test files based on an AI model, characterized in that: The method is performed based on the Maven architecture and includes: Configure execution parameters according to the tested content, the execution parameters including: a tested class and a custom instruction in the tested content, the tested class including at least one tested method, and the custom instruction used to implement personalized requirements; Create prompt words for unit testing purposes based on execution parameters and send them to the AI model; Receive a first test content sent by the AI model, and generate at least one second test content based on the first test content; A unit test file is created at a designated location according to the second test content, wherein the unit test file at least includes code for unit testing.
2. The method according to claim 1, characterized in that Creating prompts for unit testing purposes based on execution parameters and sending them to the AI model includes: Collecting basic information of the tested content based on the execution parameters, the basic information including: one or more of the tested class, the tested method, the business rules of the tested method, the dependency information of the tested method, and the custom instructions; Assemble the collected basic information and fill it in the prompt word template; Adjusting the natural language expression in the prompt word template according to the key logic in the business rules and adjusting the test coverage in the prompt word template according to the custom instructions; The content in the adjusted prompt word template is sent to the AI model.
3. The method according to claim 1, characterized in that The custom instructions include: requiring the generation of forward and / or reverse test scenarios, requiring the naming method of unit test files, requiring the design of the first test content through equivalence class division, boundary value or cause-effect diagram method, or one or more of the following.
4. The method according to claim 1, characterized in that: Receiving a first test content sent by the AI model, and generating at least one second test content based on the first test content includes: Splitting the first test content into a unit test code segment and a unit test code comment segment based on the key identifier; Formatting the unit test code segment according to the language standard requirements of the unit test framework, wherein the formatting includes: checking syntax, correcting irregular positions, and optimizing code. The second test content is generated based on the formatted unit test code segment.
5. The method according to claim 1, characterized in that Methods for optimizing generated unit test files include: Generate test statistics information at least according to the unit test files, wherein the test statistics information includes one or more of execution status, number of files generated, and abnormal information during the test process; Analyzing the achievement rate of the unit test according to the test statistical information, wherein the achievement rate includes one or more of the total achievement rate, the grammatical achievement rate, the logical achievement rate, and the execution achievement rate; According to the method for generating a unit test file by optimizing the achievement rate, the optimization content includes: optimizing grammar rules and adjusting one or more of prompt word templates.
6. The method according to claim 5, characterized in that The test statistical information also includes: one or more of user information, tested content information, test framework information, model engine information, time information and token usage.
7. The method according to any one of claims 1 to 6, characterized in that: The execution parameters also include: one or more of: a unit test framework, a type of AI model, a model engine, and a unit test file save location.
8. A device for generating unit test files based on an AI model, characterized in that: The device is applicable to the Maven architecture and includes: A configuration module, configured to configure execution parameters according to the tested content, wherein the execution parameters include: a tested class and a custom instruction in the tested content, wherein the tested class includes at least one tested method, and the custom instruction is used to realize personalized requirements; A prompt word creation module is used to create prompt words for unit testing purposes according to execution parameters and send them to the AI model; A test content generation module, configured to receive a first test content sent by the AI model, and generate at least one second test content based on the first test content; A unit test file creation module is used to create a unit test file at a specified location according to the second test content, and the unit test file at least includes code for unit testing.
9. An electronic device, characterized in that: It includes a processor and a memory, wherein a computer program instruction set is stored in the memory, and when the processor executes the computer program instruction set in the memory, the method for generating a unit test file based on an AI model as described in any one of claims 1 to 7 is implemented.
10. A computer-readable storage medium, characterized in that: in, The computer-readable storage medium stores a computer program instruction set, and when the computer program instruction set is executed by the processor, the method for generating a unit test file based on an AI model as described in any one of claims 1-7 is implemented.