Test code generation method and device, equipment, storage medium and product

By extracting contextual information from the code under test using an abstract syntax tree and combining it with a large model, the problem of existing tools being unable to automatically adjust the format and structure of test code is solved, thus achieving efficient test code generation.

CN118916266APending Publication Date: 2024-11-08CHINA MERCHANTS BANK
View PDF 0 Cites 3 Cited by

Patent Information

Application Number
CN202410929047.X
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-07-11
Publication Date
2024-11-08

AI Technical Summary

Technical Problem

Existing tools lack intelligence when generating unit test code, and cannot automatically adjust the format and structure of the test code. Users need to frequently modify prompts and writing style, resulting in low efficiency.

Method used

By extracting contextual information about variable types and calling methods from the code under test based on an abstract syntax tree, and combining it with preset test scenarios and coding style hints, a large model is used to generate test code that meets specific requirements.

Benefits of technology

It enables automatic extraction of key contextual information, avoiding frequent manual modification of prompts by users and significantly improving the efficiency of test code generation.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN118916266A_ABST
    Figure CN118916266A_ABST
Patent Text Reader

Abstract

The invention discloses a test code generation method and device, equipment, a storage medium and a product, and relates to the technical field of computers.The test code generation method comprises the steps that on the basis of an abstract syntax tree, context extraction is conducted on a variable type and a calling mode of a to-be-tested code, and key context information is obtained; the key context information, a preset test scene prompt word and a preset coding style prompt word are sent to a large model, a test code output by the large model is received, the large model carries out test scene analysis on the key context information according to the test scene prompt word and the coding style prompt word, and the test code output by the large model is obtained. And outputting the corresponding test code. According to the method, the cue word and the key context information extracted by the abstract syntax tree are input into the large model, and the test code meeting the specific coding style and the test scene requirement is generated by the large model, so that the workload of generating the test code is obviously reduced, and the working efficiency is obviously improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the field of computer technology, and in particular to a test code generation method, apparatus, device, storage medium and product. Background Art

[0002] In the software development process, unit testing is a key link to ensure code quality and functional correctness. The importance of unit testing is reflected in the ability to detect and fix errors early, reduce subsequent maintenance costs, and improve software reliability. However, the traditional way of writing unit tests usually requires developers to manually write test cases based on specific business logic and application scenarios. This process is not only time-consuming but also prone to errors. Developers need to conduct a detailed analysis of each functional point and write corresponding test code, which is particularly challenging in large projects. With the development of artificial intelligence and machine learning technology, the use of large model technology to assist or automatically generate test code has become a research hotspot.

[0003] In existing technologies, some plug-ins or tools can complete the writing of unit tests through large models. However, these tools usually lack sufficient intelligence and may miss some unit test scenarios. At the same time, these tools cannot automatically adjust the format and structure of the test code according to different coding styles. For this reason, users need to frequently manually modify the prompt words to adjust the unit test scenarios of the large model, and manually adjust the code writing style of the output test code. This increases the user's workload and reduces work efficiency.

[0004] The above contents are only used to assist in understanding the technical solution of the present application and do not constitute an admission that the above contents are prior art. Summary of the invention

[0005] The main purpose of this application is to provide a test code generation method, device, equipment, storage medium and product, aiming to solve the technical problem that users need to frequently manually modify prompt words to adjust the unit test scenarios of large models and manually adjust the code writing style of the output test code.

[0006] To achieve the above purpose, the present application proposes a test code generation method, the method comprising:

[0007] Based on the abstract syntax tree, the variable type and calling method of the test code are context extracted to obtain key context information;

[0008] The key context information, preset test scenario prompt words and preset coding style prompt words are sent to the big model, and the test code output by the big model is received, wherein the big model performs a test scenario analysis on the key context information according to the test scenario prompt words and the coding style prompt words, and outputs the corresponding test code.

[0009] In some feasible embodiments, the step of extracting the context of the variable type and calling method of the code to be tested based on the abstract syntax tree to obtain key context information includes:

[0010] Parsing the code to be tested, constructing an abstract syntax tree of the code to be tested, and obtaining the variable type of the code to be tested through the abstract syntax tree;

[0011] Traversing the variable types of the code to be tested, and checking whether the variable types are set in the code to be tested;

[0012] If the variable type is set in the code to be tested, then reading the type information of the field of the variable type and the system configuration information as the context information of the variable type;

[0013] Scan the code to be tested to obtain a calling method of the code to be tested, obtain the code of the calling method through an abstract syntax tree, and use the code of the calling method as context information of the calling method;

[0014] The context information of the variable type and the context information of the calling mode are combined to obtain key context information.

[0015] In some feasible embodiments, after the step of parsing the calling mode through the abstract syntax tree, the step further includes:

[0016] Obtaining a calling method of the code to be tested;

[0017] The calling method of the code to be tested is compared with a preset calling method library. If the calling method of the code to be tested exists in the calling method library, the dynamic prompt word corresponding to the calling method of the code to be tested in the calling method library is obtained.

[0018] In some feasible embodiments, the steps of scanning the code to be tested, obtaining a calling method of the code to be tested, obtaining a code of the calling method through an abstract syntax tree, and using the code of the calling method as context information of the calling method include:

[0019] Scan the code to be tested to obtain a calling method of the code to be tested;

[0020] Parsing the calling method through the abstract syntax tree;

[0021] If the project path has a variable type of a scope node, the calling method is the enrichment method, and the code of the enrichment method obtained through the abstract syntax tree is the context information of the method class;

[0022] If there is a specific naming method in the project path, the corresponding calling method is identified through a regular expression, and the code corresponding to the calling method is obtained through the abstract syntax tree as the context information of the method class.

[0023] In some feasible embodiments, the step of sending the key context information, the preset test scenario prompt words and the preset coding style prompt words to the big model and receiving the test code output by the big model includes:

[0024] Sending the key context information and preset test scenario prompt words to the big model;

[0025] Receiving the unit test scenario output by the large model, and sending the unit test scenario and preset coding style prompt words to the large model, wherein the large model performs a test scenario analysis on the key context information according to the test scenario prompt words, and outputs the unit test scenario;

[0026] The test code output by the large model is received, wherein the large model modifies the coding style of the unit test scenario according to the coding style prompt word and outputs the test code.

[0027] In some feasible embodiments, the step of receiving the unit test scenario output by the large model and sending the unit test scenario and preset coding style prompt words to the large model includes:

[0028] Receiving the unit test scenario returned by the large model;

[0029] Splitting the unit test scenario to obtain multiple secondary unit test scenarios;

[0030] Display all of the sub-unit test scenarios;

[0031] Evaluate all of the sub-unit test scenarios to obtain a target sub-unit test scenario;

[0032] The target secondary unit test scenario and the preset coding style prompt words are sent to the large model.

[0033] In addition, to achieve the above purpose, the present application also proposes a test code generation device, the test code generation device comprising:

[0034] The extraction module is used to extract the context of the variable type and calling method of the test code based on the abstract syntax tree to obtain key context information;

[0035] A generation module is used to send the key context information, preset test scenario prompts and preset coding style prompts to the big model, and receive the test code output by the big model, wherein the big model performs a test scenario analysis on the key context information according to the test scenario prompts and the coding style prompts, and outputs the corresponding test code.

[0036] In addition, to achieve the above-mentioned purpose, the present application also proposes a test code generation device, which includes: a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the computer program is configured to implement the steps of the test code generation method described above.

[0037] In addition, to achieve the above objectives, the present application also proposes a storage medium, which is a computer-readable storage medium, and a computer program is stored on the storage medium. When the computer program is executed by a processor, the steps of the test code generation method described above are implemented.

[0038] In addition, to achieve the above-mentioned purpose, the present application also provides a computer program product, which includes a computer program, and when the computer program is executed by a processor, the steps of the test code generation method described above are implemented.

[0039] One or more technical solutions proposed in this application have at least the following technical effects:

[0040] This application provides a solution, which extracts key context information into a large model by inputting user-configured test scenario prompt words and coding style prompt words and an abstract syntax tree, and generates test code that meets the requirements of specific coding styles and test scenarios by the large model. This method realizes the automatic extraction of key context information, avoids the effect of users manually inputting prompt words frequently, and significantly improves the efficiency of test code generation. BRIEF DESCRIPTION OF THE DRAWINGS

[0041] The accompanying drawings, which are incorporated in and constitute a part of this specification, illustrate embodiments consistent with the present application and, together with the description, serve to explain the principles of the present application.

[0042] In order to more clearly illustrate the embodiments of the present application or the technical solutions in the prior art, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, for ordinary technicians in this field, other drawings can be obtained based on these drawings without paying any creative labor.

[0043] Figure 1 A flowchart of the test code generation method provided for this application;

[0044] Figure 2 A flowchart of the first embodiment of the test code generation method of the present application is provided;

[0045] Figure 3 A brief flowchart of the test code generation method provided in Example 1 of the present application;

[0046] Figure 4 This is a schematic diagram of the module structure of the test code generation device according to the embodiment of the present application;

[0047] Figure 5 Schematic diagram of the device structure of the hardware operating environment involved in the test code generation method in the embodiment of the present application.

[0048] The purpose, features and advantages of this application will be further described in conjunction with the embodiments and with reference to the accompanying drawings. DETAILED DESCRIPTION

[0049] It should be understood that the specific embodiments described herein are only used to explain the technical solutions of the present application and are not used to limit the present application.

[0050] In order to better understand the technical solution of the present application, a detailed description will be given below in conjunction with the accompanying drawings and specific implementation methods.

[0051] The main solution of the embodiment of the present application is: based on the abstract syntax tree, the variable type and calling method of the test code are context extracted to obtain key context information; the key context information, preset test scenario prompt words and preset coding style prompt words are sent to the big model, and the test code output by the big model is received, wherein the big model performs test scenario analysis on the key context information according to the test scenario prompt words and the coding style prompt words, and outputs the corresponding test code.

[0052] In this embodiment, for the convenience of description, the following description is made with a computer as the execution subject.

[0053] In existing technologies, some plug-ins or tools can complete the writing of unit tests through large models. However, these tools usually lack sufficient intelligence and may miss some unit test scenarios. At the same time, these tools cannot automatically adjust the format and structure of the test code according to different coding styles. For this reason, users need to frequently manually modify the prompt words to adjust the unit test scenarios of large models, and manually adjust the code writing style of the test code output by the large model each time. This increases the workload of users and reduces work efficiency.

[0054] This application provides a solution, which extracts key context information into a large model by inputting user-configured test scenario prompt words and coding style prompt words and an abstract syntax tree, and generates test code that meets the requirements of specific coding styles and test scenarios by the large model. This method realizes the automatic extraction of key context information, avoids the effect of users manually inputting prompt words frequently, and significantly improves the efficiency of test code generation.

[0055] Reference Figure 2 , Figure 2 A flowchart of the first embodiment of the test code generation method of the present application is provided.

[0056] In a feasible embodiment, the test code generation method of the present application is applied to a test code generation software plug-in. By installing the test code generation software plug-in, the user inputs or selects preset test scenario prompt words and coding style prompt words, uses the abstract syntax tree to scan the user-specified code method to be tested, obtains key context information, inputs the key context information and test scenario prompt words into the big model, receives the unit test scenario data generated by the big model, splits the unit test scenario data into multiple unit test scenarios, displays all unit test scenarios, receives the user's selection instructions, inputs the unit test scenarios and coding style prompt words corresponding to the user's selection instructions into the big model, receives the test code generated by the big model, displays the test code, and significantly improves the test code generation efficiency.

[0057] It can be seen from the above embodiments that the present application adopts the method of inputting preset prompt words and key context information extracted through the abstract syntax tree into the big model to obtain unit test scenario data, split and display the unit test scenario data, receive user selection instructions, and input the unit test scenarios and coding style prompt words corresponding to the user selection instructions into the big model to obtain the test code generated by the big model. This overcomes the technical problem that users need to frequently manually modify the prompt words to adjust the unit test scenarios of the big model, and manually adjust the code writing style of the test code output by the big model each time, thereby achieving the technical effect of avoiding users from manually inputting prompt words frequently, and significantly improving the efficiency of test code generation.

[0058] It should be noted that the execution subject of this embodiment can be a computing service device with data processing, network communication and program running functions, such as a tablet computer, a personal computer, a mobile phone, etc., or an electronic device capable of realizing the above functions, etc. The following takes a computer as an example to illustrate this embodiment and the following embodiments.

[0059] Based on this, the present application embodiment provides a test code generation method, referring to Figure 1 , Figure 1 This is a flowchart of the first embodiment of the test code generation method of this application.

[0060] In this embodiment, the test code generation method includes steps S10 to S20:

[0061] Step S10, based on the abstract syntax tree, context extraction is performed on the variable type and calling method of the code to be tested to obtain key context information;

[0062] It should be noted that the abstract syntax tree is a tree representation of the abstract syntax structure of the source code, the variable type is the data type used when defining method parameters, local variables or return values, the calling method is to start and use the business logic processing method provided by the service layer in the software architecture, and the key context information is the variable type and calling method of the code to be tested extracted through the context to obtain the code block of the limited class and the code block of the limited method.

[0063] It should also be noted that the context extraction is to perform field extraction on the variable type and to perform code extraction on the calling method.

[0064] Based on the abstract syntax tree, field extraction is performed on the variable type of the code to be tested, and code extraction is performed on the calling method of the code to be tested to obtain key context information, thereby solving the technical problem of incomplete context extraction information of the code to be tested.

[0065] In a feasible implementation, step S10 may include steps A11 to A15:

[0066] Step A11, using Javaparser to parse the code to be tested and construct an abstract syntax tree;

[0067] It should be noted that the Javaparser is an open source source code parser and operation library, the abstract syntax tree is a tree representation of the abstract syntax structure of the source code, and the code to be tested is the method code of the service layer.

[0068] The method code of the service layer is parsed by the Javaparser to generate an abstract syntax tree.

[0069] Step A12, traversing the types of variables in the code to be tested, if the types are set in the project path, using the Javaparser to read the type information and system configuration information of the class fields based on the abstract syntax tree, and saving the type information and system configuration information of the class fields as context information of the variable type;

[0070] It should be noted that the context information of the variable type includes field type information and system configuration information. The field type information includes field name and field type, and the system configuration information includes name field and value field.

[0071] By scanning the variable types in the code to be tested, the existing set types are obtained. Based on the abstract syntax tree, the Javaparser is used to read the type information and system configuration information of the class fields, and the type information and system configuration information of the class fields are saved as context information of the variable type. If the variable set in the type is a variable of other classes, the type information and system configuration information of the fields in other classes will be recursively read.

[0072] Step A13, scanning the code to be tested to obtain a calling method of the code to be tested, and based on the abstract syntax tree, using Javaparser to obtain code corresponding to the calling method as context information of the calling method;

[0073] It should be noted that the context information of the calling method is the code corresponding to the calling method.

[0074] By scanning the code to be tested, if the project path has a variable type of a scope node, the calling method is the enriched method. Based on the abstract syntax tree, Javaparser is used to obtain the context information of the method class whose code is the enriched method; if the project path has a specific naming method, the corresponding calling method is identified by a regular expression. Based on the abstract syntax tree, Javaparser is used to obtain the context information of the method class whose code corresponds to the calling method.

[0075] Step A14, comparing the calling method of the code to be tested with a preset calling method library, if the calling method of the code to be tested exists in the calling method library, obtaining the dynamic prompt word corresponding to the calling method of the code to be tested from the calling method library;

[0076] It should be noted that the dynamic prompt words are description statements or code blocks used to limit and supplement the test codes generated corresponding to various calling modes of the code to be tested.

[0077] The calling method of the code to be tested is compared with a preset calling method library. If the calling method of the code to be tested exists in the preset calling method library, the dynamic prompt word corresponding to the calling method of the code to be tested is obtained from the preset calling method library.

[0078] Step A15: Combining the context information of the variable type, the context information of the method class and the dynamic prompt word into key context information.

[0079] It should be noted that the key context information is the code block of the restricted class and the code block of the restricted method obtained by extracting the variable type and calling method of the code to be tested through the context.

[0080] By combining the context information of the variable type, the context information of the method class and the dynamic prompt word, key context information serving as input content of the large model is obtained.

[0081] In this implementation, the method code of the service layer is parsed by the Javaparser to generate an abstract syntax tree. By scanning the variable types in the code to be tested, the types that are set are obtained. Based on the abstract syntax tree, the Javaparser is used to read the type information and system configuration information of the fields of the class, and the type information and system configuration information of the fields of the class are saved as context information of the variable type. If the variable set in the type is a variable of another class, the type information and system configuration information of the fields in the other class will be recursively read. By scanning the code to be tested, if the variable type of the scope node exists in the project path, the calling method is the enrichment method. Based on the abstract syntax tree, the Javaparser is used to obtain the enrichment method. The code is the context information of the method class; if there is a specific naming method in the project path, the corresponding calling method is identified by regular expression, and based on the abstract syntax tree, Javaparser is used to obtain the context information of the code corresponding to the calling method as the method class, and the calling method of the code to be tested is compared with the preset calling method library. If the calling method of the code to be tested exists in the preset calling method library, the dynamic prompt word corresponding to the calling method of the code to be tested is obtained from the preset calling method library, and the context information of the variable type, the context information of the method class and the dynamic prompt word are combined to obtain the key context information as the input content of the large model, which solves the technical problem of incomplete context extraction information of the code to be tested.

[0082] The above is only a feasible implementation of step S10 provided in this embodiment, and this embodiment does not specifically limit the specific implementation of step S10.

[0083] Step S20, sending the key context information, preset test scenario prompt words and preset coding style prompt words to the big model, receiving the test code output by the big model, wherein the big model performs a test scenario analysis on the key context information according to the test scenario prompt words and the coding style prompt words, and outputs the corresponding test code.

[0084] It should be noted that the test scenario prompt words are sentences used to inspire the big model to output test scenario codes, and the coding style prompt words are sentences used to help the big model understand and generate test codes with the coding style required by the user. The big model here refers to the artificial intelligence model used to automatically generate unit test codes.

[0085] By sending the key context information, preset test scenario prompt words and preset coding style prompt words to the big model, receiving the test code output by the big model, and obtaining the test code for the code to be tested, the technical problem that the user needs to frequently manually modify the prompt words to adjust the unit test scenario of the big model and manually adjust the code writing style of the output unit test code is solved.

[0086] In a feasible implementation, step S20 may include steps A21 to A24:

[0087] Step A21, obtaining test scenario prompt words and coding style prompt words;

[0088] It should be noted that the test scenario prompt words are sentences used to inspire the large model to output test scenario codes, and the coding style prompt words are sentences used to help the large model understand and generate test codes with the coding style required by the user.

[0089] By generating two input boxes for the general test scenario prompt words and the coding style prompt words, when a save signal is received, the configured prompt words in the two input boxes are stored, and after the user inputs the prompt words or the user selects the preset prompt words, the general test scenario prompt words and the coding style prompt words corresponding to the code to be tested are determined.

[0090] Step A22, sending the key context information and preset test scenario prompt words to the big model, receiving the unit test scenario output by the big model, splitting the data into multiple secondary unit test scenarios, and displaying all the secondary unit test scenarios;

[0091] It should be noted that the big model here refers to an artificial intelligence model used to automatically generate unit test code, and the unit test scenario is a test scenario code statement output by the big model after performing a test scenario analysis on the key context information based on the test scenario prompt words.

[0092] It should be noted that the secondary unit test scenario is a specific test scenario obtained by splitting the unit test scenario.

[0093] By sending the key context information and preset test scenario prompt words to the big model, the receiving big model performs test scenario analysis on the key context information according to the test scenario prompt words, outputs test scenario code statements, splits the test scenario code statements into multiple secondary unit test scenario statements, and displays all secondary unit test scenarios in the display box.

[0094] Step A23, evaluate each of the secondary unit test scenarios to obtain the effectiveness and efficiency of each of the secondary unit test scenarios; analyze the effectiveness and efficiency of the secondary unit test scenarios to generate a quantitative scoring result for each of the secondary unit test scenarios; based on the quantitative scoring result and according to preset conditions, determine the optimal secondary unit test scenario, and send the optimal unit test scenario and preset coding style prompt words to the large model;

[0095] It should be noted that the effectiveness refers to the validity and correctness of the test scenario; the efficiency refers to the resource utilization during the test execution process; the quantitative scoring result is the conversion of the analyzed effectiveness and efficiency data into a numerical percentage or ten-point scoring result; the preset conditions are the evaluation conditions set in advance by the user.

[0096] By evaluating each of the sub-unit test scenarios, the evaluation results of the effectiveness, correctness and resource utilization during the execution of each of the sub-unit test scenarios are obtained; the evaluation results of the effectiveness, correctness and resource utilization during the execution of the sub-unit test scenarios are analyzed, and the data of the effectiveness, correctness and resource utilization during the execution of each of the sub-unit test scenarios obtained by analysis are converted into numerical scoring results on a percentage or ten-point scale; based on the scoring results, according to the judgment conditions set in advance by the user, the optimal sub-unit test scenario is determined, and the optimal unit test scenario and the preset coding style prompt words are sent to the large model.

[0097] Step A24, receiving and displaying the test code output by the large model.

[0098] In this embodiment, by generating two input boxes for the general test scenario prompt word and the coding style prompt word, when a save signal is received, the configured prompt words in the two input boxes are stored, and after the user inputs the prompt word or the user selects the preset prompt word, the general test scenario prompt word and the coding style prompt word corresponding to the code to be tested are determined; the key context information and the preset test scenario prompt word are sent to the big model, and the big model is received, and the test scenario analysis of the key context information is performed according to the test scenario prompt word, and the test scenario code statement output is split into multiple secondary unit test scenario statements, and all the secondary unit test scenarios are displayed in the display box; each of the secondary unit test scenarios is evaluated to obtain the validity, The evaluation results of the validity, correctness and resource utilization during the execution process of the secondary unit test scenarios are analyzed, and the data of the validity, correctness and resource utilization during the execution process obtained from the analysis of each secondary unit test scenario are converted into numerical scoring results on a percentage or ten-point scale; based on the scoring results, according to the judgment conditions set in advance by the user, the optimal secondary unit test scenario is determined, and the optimal unit test scenario and preset coding style prompt words are sent to the large model; the test code output by the large model is received and displayed, which solves the technical problem that the user needs to frequently manually modify the prompt words to adjust the unit test scenario of the large model, and manually adjust the code writing style of the output unit test code.

[0099] The above is only a feasible implementation of step S20 provided in this embodiment, and this embodiment does not specifically limit the specific implementation of step S20.

[0100] This embodiment provides a test code generation method, which uses the Javaparser to parse the method code of the service layer to generate an abstract syntax tree, and obtains the type of the setting by scanning the variable type in the code to be tested. Based on the abstract syntax tree, the Javaparser is used to read the type information and system configuration information of the field of the class, and the type information and system configuration information of the field of the class are saved as context information of the variable type. If the variable set in the type is a variable of other classes, the type information and system configuration information of the fields in other classes will be recursively read. By scanning the code to be tested, if the variable type of the scope node exists in the project path, the calling method is the enrichment method. , based on the abstract syntax tree, use Javaparser to obtain the context information of the method class of the code of the hyperlinked method; if there is a specific naming method in the project path, identify the corresponding calling method through regular expressions, and based on the abstract syntax tree, use Javaparser to obtain the context information of the method class corresponding to the calling method, and compare the calling method of the code to be tested with the preset calling method library. If the calling method of the code to be tested exists in the preset calling method library, obtain the dynamic prompt word corresponding to the calling method of the code to be tested from the preset calling method library, and compare the context information of the variable type, the context information of the method class and the dynamic prompt word Combine to obtain key context information as the input content of the large model, by generating two input boxes for the general test scenario prompt word and the coding style prompt word, when a save signal is received, store the configured prompt words in the two input boxes, and after the user inputs the prompt word or the user selects the preset prompt word, determine the general test scenario prompt word and the coding style prompt word corresponding to the code to be tested; send the key context information and the preset test scenario prompt word to the large model, receive the large model, perform test scenario analysis on the key context information according to the test scenario prompt word, output the test scenario code statement, split the test scenario code statement into multiple secondary unit test scenario statements, and display the entire statement in a display box The method comprises the following steps: evaluating each of the secondary unit test scenarios to obtain the evaluation results of the effectiveness, correctness and resource utilization of each of the secondary unit test scenarios during execution; analyzing the evaluation results of the effectiveness, correctness and resource utilization of the secondary unit test scenarios during execution, generating data of the effectiveness, correctness and resource utilization of each of the secondary unit test scenarios obtained through analysis and converting them into numerical scoring results based on a percentage or ten-point scale; based on the scoring results, determining the optimal secondary unit test scenario according to the judgment conditions set in advance by the user, and sending the optimal unit test scenario and the preset coding style prompt words to the large model;The test code output by the large model is received and displayed, which solves the technical problem that users need to frequently manually modify the prompt words to adjust the unit test scenario of the large model and manually adjust the code writing style of the output unit test code. This method realizes the automatic extraction of key context information, avoids the effect of users manually inputting prompt words frequently, and significantly improves the efficiency of test code generation. ;

[0101] For example, to help understand the implementation process of the test code generation method obtained by combining this embodiment with the above-mentioned embodiment 1, please refer to Figure 3 , Figure 3 A brief flowchart of a test code generation method is provided, specifically:

[0102] Install the test code generation plug-in, and the user enters the test scenario prompt word and coding style prompt word in the prompt word input box;

[0103] Select the code corresponding to the business logic (i.e. the service layer method) as the code to be tested, and click the GENRATE TEST option on the Genrate menu;

[0104] The plug-in traverses the types of variables in the code to be tested, reads the field information of the set class through the parser, and saves the type information of the field of the class and the system configuration information as the context information of the variable type; obtains the test method of the code to be tested by matching the specific node type with the specific naming method, reads the code corresponding to the test method through the parser, and saves the code corresponding to the test method as the context information of the calling method; combines the context information of the variable type and the context information of the calling method to obtain the key context information;

[0105] Send the key context information and the test scenario prompt words to the big model, and receive the unit test scenario returned by the big model;

[0106] The unit test scenario is split to obtain multiple sub-unit test scenarios, and all the sub-unit test scenarios are displayed in a side display box of the IDEA editor;

[0107] Receive a command specified by a user, and send the secondary unit test scenario and the coding style prompt word corresponding to the specified command to the large model;

[0108] The unit test code returned by the large model is received, and the unit test code returned by the large model is displayed in a side display box of the IDEA editor.

[0109] It should be noted that the above examples are only used to understand the present application and do not constitute a limitation on the test code generation method of the present application. More simple transformations based on this technical concept are all within the scope of protection of the present application.

[0110] This application also provides a test code generation device, please refer to Figure 4 , the test code generating device comprises:

[0111] The extraction module is used to extract the context of the variable type and calling method of the test code based on the abstract syntax tree to obtain key context information;

[0112] A generation module is used to send the key context information, preset test scenario prompts and preset coding style prompts to the big model, and receive the test code output by the big model, wherein the big model performs a test scenario analysis on the key context information according to the test scenario prompts and the coding style prompts, and outputs the corresponding test code.

[0113] The test code generation device provided by the present application adopts the test code generation method in the above embodiment, which can solve the technical problem that users need to frequently manually modify the prompt words to adjust the unit test scenario of the large model, and manually adjust the code writing style of the test code output by the large model each time. Compared with the prior art, the beneficial effects of the test code generation device provided by the present application are the same as the beneficial effects of the test code generation method provided by the above embodiment, and the other technical features in the test code generation device are the same as the features disclosed in the above embodiment method, which will not be repeated here.

[0114] The present application provides a test code generation device, which includes: at least one processor; and a memory communicatively connected to the at least one processor; wherein the memory stores instructions executable by the at least one processor, and the instructions are executed by the at least one processor so that the at least one processor can execute the test code generation method in the above-mentioned embodiment 1.

[0115] Reference below Figure 5, which shows a schematic diagram of the structure of a test code generating device suitable for implementing an embodiment of the present application. The test code generating device in the embodiment of the present application may include but is not limited to mobile terminals such as mobile phones, laptop computers, digital broadcast receivers, PDAs (Personal Digital Assistants), PADs (Portable Application Descriptions), PMPs (Portable Media Players), vehicle-mounted terminals (such as vehicle-mounted navigation terminals), etc., and fixed terminals such as digital TVs, desktop computers, etc. Figure 5 The test code generation device shown is merely an example and should not bring any limitation to the functions and scope of use of the embodiments of the present application.

[0116] like Figure 5 As shown, the test code generation device may include a processing device 1001 (e.g., a central processing unit, a graphics processor, etc.), which can perform various appropriate actions and processes according to a program stored in a read-only memory (ROM: Read Only Memory) 1002 or a program loaded from a storage device 1003 to a random access memory (RAM: Random Access Memory) 1004. In RAM1004, various programs and data required for the operation of the test code generation device are also stored. The processing device 1001, ROM1002, and RAM1004 are connected to each other through a bus 1005. An input / output (I / O) interface 1006 is also connected to the bus. Generally, the following systems can be connected to the I / O interface 1006: an input device 1007 including, for example, a touch screen, a touch pad, a keyboard, a mouse, an image sensor, a microphone, an accelerometer, a gyroscope, etc.; an output device 1008 including, for example, a liquid crystal display (LCD: Liquid Crystal Display), a speaker, a vibrator, etc.; a storage device 1003 including, for example, a magnetic tape, a hard disk, etc.; and a communication device 1009. The communication device 1009 can allow the test code generation device to communicate wirelessly or wired with other devices to exchange data. Although the test code generation device with various systems is shown in the figure, it should be understood that it is not required to implement or have all the systems shown. More or fewer systems can be implemented or provided alternatively.

[0117] In particular, according to the embodiments disclosed in the present application, 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 application 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 a network through a communication device, or installed from a storage device 1003, or installed from a ROM 1002. When the computer program is executed by the processing device 1001, the above-mentioned functions defined in the method of the embodiment disclosed in the present application are executed.

[0118] The test code generation device provided by the present application adopts the test code generation method in the above embodiment, which can solve the technical problem that users need to frequently manually modify the prompt words to adjust the unit test scenario of the large model, and manually adjust the code writing style of the test code output by the large model each time. Compared with the prior art, the beneficial effects of the test code generation device provided by the present application are the same as the beneficial effects of the test code generation method provided by the above embodiment, and the other technical features in the test code generation device are the same as the features disclosed in the method of the previous embodiment, which will not be repeated here.

[0119] It should be understood that the various parts disclosed in this application can be implemented by hardware, software, firmware or a combination thereof. In the description of the above embodiments, specific features, structures, materials or characteristics can be combined in any one or more embodiments or examples in a suitable manner.

[0120] The above is only a specific implementation of the present application, but the protection scope of the present application is not limited thereto. Any technician familiar with the technical field can easily think of changes or substitutions within the technical scope disclosed in the present application, which should be included in the protection scope of the present application. Therefore, the protection scope of the present application should be based on the protection scope of the claims.

[0121] The present application provides a computer-readable storage medium having computer-readable program instructions (ie, computer programs) stored thereon, and the computer-readable program instructions are used to execute the test code generation method in the above-mentioned embodiment.

[0122] The computer-readable storage medium provided in the present application may be, for example, a USB flash drive, but is not limited to electrical, magnetic, optical, electromagnetic, infrared, or semiconductor systems, systems or devices, or any combination of the above. More specific examples of computer-readable storage media may include, but are not limited to: an electrical connection with one or more wires, a portable computer disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the above. In this embodiment, the computer-readable storage medium may be any tangible medium containing or storing a program that can be used by or in combination with an instruction execution system, system or device. The program code contained on the computer-readable storage medium may be transmitted using any appropriate medium, including but not limited to: wires, optical cables, RF (Radio Frequency), etc., or any suitable combination of the above.

[0123] The computer-readable storage medium may be included in the test code generating device; or may exist independently without being assembled into the test code generating device.

[0124] The above-mentioned computer-readable storage medium carries one or more programs. When the above-mentioned one or more programs are executed by the test code generation device, the test code generation device: based on the abstract syntax tree, performs context extraction on the variable type and calling method of the test code to obtain key context information; sends the key context information, preset test scenario prompt words and preset coding style prompt words to the big model, and receives the test code output by the big model, wherein the big model performs test scenario analysis on the key context information according to the test scenario prompt words and the coding style prompt words, and outputs the corresponding test code.

[0125] Computer program code for performing the operations of the present application may be written in one or more programming languages ​​or a combination thereof, including object-oriented programming languages ​​such as Java, Smalltalk, C++, and conventional procedural programming languages ​​such as "C" or similar programming languages. The program code may be executed entirely on the user's computer, partially on the user's computer, as a separate software package, partially on the user's computer and partially on a remote computer, or entirely on a remote computer or server. In the case of a remote computer, the remote computer may be connected to the user's computer through any type of network, including a local area network (LAN) or a wide area network (WAN), or may be connected to an external computer (e.g., via the Internet using an Internet service provider).

[0126] 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 application. 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 a specified function or operation, or can be implemented with a combination of dedicated hardware and computer instructions.

[0127] The modules involved in the embodiments described in this application may be implemented by software or hardware, wherein the name of the module does not constitute a limitation on the unit itself in some cases.

[0128] The readable storage medium provided by the present application is a computer-readable storage medium, which stores computer-readable program instructions (i.e., computer programs) for executing the above-mentioned test code generation method, and can solve the technical problem that users need to frequently manually modify prompt words to adjust the unit test scenarios of large models, and manually adjust the code writing style of the test code output by the large model each time. Compared with the prior art, the beneficial effects of the computer-readable storage medium provided by the present application are the same as the beneficial effects of the test code generation method provided by the above-mentioned embodiment, which will not be repeated here.

[0129] The present application also provides a computer program product, including a computer program, which implements the steps of the test code generation method as described above when executed by a processor.

[0130] The computer program product provided by the present application can solve the technical problem that users need to frequently manually modify prompt words to adjust the unit test scenario of a large model, and manually adjust the code writing style of the test code output by the large model each time. Compared with the prior art, the beneficial effects of the computer program product provided by the present application are the same as the beneficial effects of the test code generation method provided by the above embodiment, which will not be repeated here.

[0131] The above descriptions are only some embodiments of the present application, and are not intended to limit the patent scope of the present application. All equivalent structural changes made using the contents of the present application specification and drawings under the technical concept of the present application, or direct / indirect applications in other related technical fields are included in the patent protection scope of the present application.

Claims

1. A test code generation method, It is characterized in that The method includes: Based on the abstract syntax tree, the variable type and calling method of the test code are context extracted to obtain key context information; The key context information, preset test scenario prompt words and preset coding style prompt words are sent to the big model, and the test code output by the big model is received, wherein the big model performs a test scenario analysis on the key context information according to the test scenario prompt words and the coding style prompt words, and outputs the corresponding test code.

2. The method according to claim 1, It is characterized in that The step of extracting the context of the variable type and calling method of the code to be tested based on the abstract syntax tree to obtain key context information includes: Parsing the code to be tested, constructing an abstract syntax tree of the code to be tested, and obtaining the variable type of the code to be tested through the abstract syntax tree; Traversing the variable types of the code to be tested, and checking whether the variable types are set in the code to be tested; If the variable type is set in the code to be tested, then reading the type information of the field of the variable type and the system configuration information as the context information of the variable type; Scan the code to be tested to obtain a calling method of the code to be tested, obtain the code of the calling method through an abstract syntax tree, and use the code of the calling method as context information of the calling method; The context information of the variable type and the context information of the calling mode are combined to obtain key context information.

3. The method according to claim 2, It is characterized in that The step of scanning the code to be tested, obtaining a calling method of the code to be tested, obtaining a code of the calling method through an abstract syntax tree, and using the code of the calling method as context information of the calling method includes: Scan the code to be tested to obtain a calling method of the code to be tested; Parsing the calling method through the abstract syntax tree; If the project path has a variable type of a scope node, the calling method is the enrichment method, and the code of the enrichment method obtained through the abstract syntax tree is the context information of the method class; If there is a specific naming method in the project path, the corresponding calling method is identified through a regular expression, and the code corresponding to the calling method is obtained through the abstract syntax tree as the context information of the method class.

4. The method according to claim 3, It is characterized in that After the step of parsing the calling mode through the abstract syntax tree, the following step is further included: Obtaining a calling method of the code to be tested; The calling method of the code to be tested is compared with a preset calling method library. If the calling method of the code to be tested exists in the calling method library, the dynamic prompt word corresponding to the calling method of the code to be tested in the calling method library is obtained.

5. The method according to claim 1, It is characterized in that The step of sending the key context information, the preset test scenario prompt words and the preset coding style prompt words to the large model and receiving the test code output by the large model comprises: Sending the key context information and preset test scenario prompt words to the big model; Receiving the unit test scenario output by the large model, and sending the unit test scenario and a preset coding style prompt word to the large model, wherein the large model performs a test scenario analysis on the key context information according to the test scenario prompt word, and outputs the unit test scenario; The test code output by the large model is received, wherein the large model modifies the coding style of the unit test scenario according to the coding style prompt word and outputs the test code.

6. The method according to claim 5, It is characterized in that The step of receiving the unit test scenario output by the large model and sending the unit test scenario and the preset coding style prompt words to the large model includes: Receiving the unit test scenario returned by the large model; Splitting the unit test scenario to obtain multiple secondary unit test scenarios; Display all of the sub-unit test scenarios; Evaluate all of the sub-unit test scenarios to obtain a target sub-unit test scenario; The target secondary unit test scenario and the preset coding style prompt words are sent to the large model.

7. A test code generating device, It is characterized in that The test code generating device comprises: The extraction module is used to extract the context of the variable type and calling method of the test code based on the abstract syntax tree to obtain key context information; A generation module is used to send the key context information, preset test scenario prompts and preset coding style prompts to the big model, and receive the test code output by the big model, wherein the big model performs a test scenario analysis on the key context information according to the test scenario prompts and the coding style prompts, and outputs the corresponding test code.

8. A test code generating device, It is characterized in that The device comprises: a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the computer program is configured to implement the steps of the test code generation method according to any one of claims 1 to 6.

9. A storage medium, It is characterized in that The storage medium is a computer-readable storage medium, and a computer program is stored on the storage medium. When the computer program is executed by a processor, the steps of the test code generation method according to any one of claims 1 to 6 are implemented.

10. A computer program product, It is characterized in that The computer program product comprises a computer program, and when the computer program is executed by a processor, the steps of the test code generation method according to any one of claims 1 to 6 are implemented.

Citation Information

Cited By

  • End-to-end automatic test code generation system and method

    CN122152716A

  • End-to-end automated test code generation system and method

    CN122152716B

  • Test code generation method and related product

    WO2026144771A1