Code generation method, code generation model and code modification model training method
By analyzing the syntax structure of the target program and searching for unit dependencies, high-quality unit test code is generated, which solves the problems of low quality and low efficiency of unit test code in the existing technology, and achieves more accurate and efficient unit tests.
Patent Information
- Application Number
- CN202411472302.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-10-21
- Publication Date
- 2025-01-17
- Estimated Expiration
- 2044-10-21
AI Technical Summary
The unit test code generated by the prior art is of low quality and low efficiency, with many errors, failing to fully utilize the potential of large models and not combining with integrated development environments (IDEs).
By parsing the target program with syntax structure, filtering the information type required for unit testing to obtain alternative code information, and searching the object code information based on the unit dependency relationship of the unit to be tested to generate high-quality unit test code.
Improve the generation quality and efficiency of unit test code, reduce the error rate, and ensure the accuracy and overall testing efficiency of unit test code.
Smart Images

Figure CN118981317B_ABST
Abstract
Description
Technical Field
[0001] The embodiments of this specification relate to the field of computer technology, and in particular to a code generation method, a code generation model, and a training method for a code modification model. Background Art
[0002] With the rapid development of computer technology, various software have been widely developed.
[0003] During the software development process, it is necessary to perform unit testing on the software, that is, to check and verify the smallest program logic unit (such as function) in the software to ensure the quality of the software. At present, a unit test code can be written for the unit to be tested in the software using a computer, and the unit to be tested can be tested by running the unit test code.
[0004] However, there are many errors in the unit test code generated by computers at present, and the quality of the generated unit test code still needs to be improved. Summary of the invention
[0005] In view of this, an embodiment of this specification provides a code generation method that can improve the quality of unit test code generation. One or more embodiments of this specification also involve a code generation model training method, a code modification model training method, a task platform, a computing device, a computer-readable storage medium, and a computer program product. The code generation method includes:
[0006] For a unit to be tested in a target program, based on the unit dependency of the unit to be tested, target code information is retrieved from candidate code information; wherein the candidate code information is obtained by screening auxiliary information based on a target information type, the auxiliary information is obtained by parsing a grammatical structure of a code file of the target program, and the target information type is the type of information used for unit testing;
[0007] Based on the target code information, a unit test code of the unit to be tested is generated.
[0008] In one embodiment of the present specification, the grammatical structure of the code file of the target program is parsed, and the information type required for the unit test is screened to obtain the candidate code information. When the unit to be tested in the target program is unit tested, the target code information is retrieved from these candidate code information based on its unit dependency, and the unit test code is generated based on this. In this way, it can be ensured that the retrieved target code information is relatively detailed and all of them are the information required for the unit test. The unit test code generated based on these code information can be relatively accurate, with fewer errors, and the generation quality of the unit test code is guaranteed to be high. BRIEF DESCRIPTION OF THE DRAWINGS
[0009] Figure 1 This is an application scenario architecture diagram of a code generation method provided in an embodiment of this specification;
[0010] Figure 2 is a flow chart of a code generation method provided in an embodiment of this specification;
[0011] Figure 3 It is a flowchart of a method for training a code generation model provided in an embodiment of this specification;
[0012] Figure 4 It is a flowchart of a method for training a code modification model provided in an embodiment of this specification;
[0013] Figure 5 This is a simplified block diagram of a code repository parsing module for performing code parsing provided in an embodiment of this specification;
[0014] Figure 6 This is a simple block diagram of code parsing of a unit test generation module provided in an embodiment of this specification;
[0015] Figure 7 is a flowchart of another code generation method provided in an embodiment of this specification;
[0016] Figure 8 is a flowchart of another code generation method provided in an embodiment of this specification;
[0017] Fig. 9 It is a structural diagram of a task platform provided in an embodiment of this specification;
[0018] Fig.10 It is a structural diagram of a code generating device provided in an embodiment of this specification;
[0019] Fig.11 It is a structural diagram of a training device for a code generation model provided in an embodiment of this specification;
[0020] Fig.12 It is a structural diagram of a training device for a code modification model provided in an embodiment of this specification;
[0021] Fig.13 is a schematic diagram of the structure of another code generating device provided in an embodiment of this specification;
[0022] Fig.14 It is a structural block diagram of a computing device provided in an embodiment of this specification. DETAILED DESCRIPTION
[0023] Many specific details are described in the following description to facilitate a full understanding of this specification. However, this specification can be implemented in many other ways than those described herein, and those skilled in the art can make similar generalizations without violating the connotation of this specification, so this specification is not limited to the specific implementation disclosed below.
[0024] The terms used in one or more embodiments of this specification are only for the purpose of describing specific embodiments, and are not intended to limit one or more embodiments of this specification. The singular forms of "a" and "the" used in one or more embodiments of this specification and the appended claims are also intended to include plural forms, unless the context clearly indicates other meanings. It should also be understood that the term "and / or" used in one or more embodiments of this specification refers to and includes any or all possible combinations of one or more associated listed programs. It should be understood that although the terms first, second, etc. may be used to describe various information in one or more embodiments of this specification, these information should not be limited to these terms. These terms are only used to distinguish the same type of information from each other. For example, without departing from the scope of one or more embodiments of this specification, the first may also be referred to as the second, and similarly, the second may also be referred to as the first. Depending on the context, the word "if" as used herein can be interpreted as "at the time of..." or "when..." or "in response to determination".
[0025] In addition, it should be noted that the user information (including but not limited to user device information, user personal information, etc.) and data (including but not limited to data used for analysis, stored data, displayed data, etc.) involved in one or more embodiments of this specification are all information and data authorized by the user or fully authorized by all parties, and the collection, use and processing of relevant data must comply with the relevant laws, regulations and standards of the relevant countries and regions, and provide corresponding operation entrances for users to choose to authorize or refuse.
[0026] In one or more embodiments of this specification, a large model refers to a deep learning model with large-scale model parameters, which usually contains hundreds of millions, tens of billions, hundreds of billions, trillions, or even more than 10 trillion model parameters. A large model can also be called a foundation model / foundation model. The large model is pre-trained with large-scale unlabeled corpus to produce a pre-trained model with more than 100 million parameters. This model can adapt to a wide range of downstream tasks, and the model has good generalization ability, such as a large-scale language model (LLM, Large Language Model), a multi-modal pre-training model, etc. When the large model is actually applied, only a small number of samples are needed to fine-tune the pre-trained model and it can be applied to different tasks. The large model can be widely used in natural language processing (NLP), computer vision and other fields, and can be specifically applied to computer vision tasks such as visual question answering (VQA), image description (IC, Image Caption), image generation, and other tasks in the field of computer vision, as well as text-based sentiment classification, text summary generation, machine translation and other natural language processing tasks. The main application scenarios of the large model include digital assistants, intelligent robots, search, online education, office software, e-commerce, intelligent design, etc. The models involved in the embodiments of this specification include but are not limited to the above-mentioned large models, and can be any type of machine learning model. For example, it can be a convolutional neural network (CNN) model, a Transformer model, etc.
[0027] Unit testing (simply called single testing) is a very important part of the software development process and an important step in ensuring software quality. It plays an important role in improving software quality, reducing development costs, accelerating delivery, and ensuring code maintainability in enterprise production. Unit testing refers to the inspection and verification of the smallest program logic unit (such as a function) in the software. It is necessary to write corresponding unit test code for the logic unit and then run the unit test code to test the logic unit and detect whether the implementation logic of the logic unit is correct and whether it can run normally in various operating environments. If the test results indicate that there is a problem with the logic unit, the logic unit is adjusted according to the test results to ensure the overall quality of the software.
[0028] However, writing unit test code (also referred to as writing unit tests) is an extremely time-consuming and labor-intensive task. Before developers write unit test code for a unit under test in a program, they need to clarify the complex dependencies between the codes of the various logical units in the program and simulate the dependencies in the unit test. For example, the unit under test has a dependency relationship with the logical unit corresponding to its input parameter (i.e., input parameter), the unit under test has a dependency relationship with the database it needs to connect to, and the logical unit has a dependency relationship with the web page it needs to access during operation. In addition, when designing the input information of the unit test code, developers must fully consider the handling of various boundary conditions and abnormal conditions. This places high demands on developers, resulting in a high threshold for writing unit test code, and because there is more information to consider, it is difficult for developers to consider it comprehensively, resulting in poor quality of the written unit test code, such as possible code errors, and low efficiency in generating unit test code.
[0029] At present, there are software products that use large models to generate unit test code. However, the unit test code generated by these products still has many errors, and many generated unit test codes have basic compilation errors. In addition, these products only tell the model part of the information in the program to get the generated unit test code once, and do not fully combine with the integrated development environment (IDE) to release the potential of large models. Therefore, the quality and efficiency of unit test code generation still need to be improved. IDE is a comprehensive software application that provides a complete set of tools and functions to facilitate software developers to write, test, debug and manage code.
[0030] In this specification, a code generation method is provided, which can improve the quality and efficiency of unit test code generation. One or more embodiments of this specification also involve a code generation model training method, a code modification model training method, a task platform, a computing device, a computer-readable storage medium and a computer program product, which are described in detail one by one in the following embodiments.
[0031] The code generation method provided in the embodiments of this specification can use a large model to generate unit test code. Considering that the model parameters of the large model are huge and the computing resources of the mobile terminal are limited, the code generation method provided in the embodiments of this specification can be applied to Figure 1 The application scenarios shown are not limited to these. Figure 1 This is an application scenario architecture diagram of a code generation method provided in an embodiment of this specification. Figure 1In the application scenario shown, the large model is deployed in the server 10, and the server 10 can be connected to one or more client devices 20 through a local area network connection, a wide area network connection, an Internet connection, or other types of data networks. The client device 20 here may include but is not limited to: a smart phone, a tablet computer, a laptop computer, a PDA, a personal computer, a smart home device, a vehicle-mounted device, etc. The client device 20 can interact with the user through a graphical user interface to implement the call of the large model, and then implement the code generation method provided in the embodiment of this specification. If the computing resources of the client device are large enough, the large model can also be directly deployed on the client device, and the client device directly uses the large model to implement the code generation method provided in the embodiment of this specification.
[0032] Figure 2 is a flowchart of a code generation method provided in an embodiment of the present specification. The method can be applied to a code generation device. For example, the code generation device can be Figure 1 The server 10 in the embodiment includes the following steps 202 and 204.
[0033] Step 202: for the unit to be tested in the target program, based on the unit dependency of the unit to be tested, retrieve the target code information from the alternative code information; wherein the alternative code information is obtained by screening auxiliary information based on the target information type, the auxiliary information is obtained by parsing the grammatical structure of the code file of the target program, and the target information type is the type of information used for unit testing.
[0034] The code generation device can generate corresponding unit test codes for the logic units in various software projects, and the software project can be an enterprise-level software development project. For example, the project can be a shopping mall software, an instant messaging software, or an office software. A software project can correspond to a code warehouse, which includes all the codes of the software project and the corresponding project logic information. For example, it includes the display of the front-end page, the reception and feedback of information, the processing of the back-end, the user login, the connection to the database, and the administrator operation. Each code can be stored in different files respectively, and the codes are connected based on the corresponding project logic.
[0035] The target program in the embodiment of this specification can be a software project, or it can also be a program corresponding to some functional modules in a software project. The unit to be tested in the embodiment of this specification can be any logical unit in the target program, such as a function, or it can also be a class. A class includes at least one constructor, and also includes some member variables and other information. The embodiment of this specification is subsequently introduced by taking the unit to be tested as a function as an example. The target program can be written directly on the code generation device, or it can be written on other devices and then transmitted to the code generation device.
[0036] In an embodiment of the present specification, the unit to be tested can be specified by a user based on a client device, and the user triggers the client device to send a unit test request for the unit to be tested to the code generation device, so as to transmit the information of the unit to be tested to the code generation device based on the unit test request. The user here can be a software developer or tester. The user can open any code file of the target program on his client device, select the code corresponding to the unit to be tested (such as a function) therein, or select the function name of a function in the code file, or directly enter the identifier of a function in the target program, and then click the control for generating unit test code to trigger the client device to transmit the information of the unit to be tested to the code generation device.
[0037] In some implementations, the unit to be tested may also be determined by the code generation device itself, such as the code generation device sequentially determines each logic unit in the target program as a unit to be tested, and then generates corresponding unit test code for each logic unit. Alternatively, the logic units in the target program that meet the conditions to be met may be determined as units to be tested according to the set conditions to be met by the unit to be tested.
[0038] There are dependencies between the various logical units in the target program. For example, a logical unit has a dependency relationship with the logical unit corresponding to its input parameter (i.e., input parameter), and the input parameter of one logical unit is the output parameter of another logical unit; in two logical units with dependencies, the execution mode of one logical unit is determined by the information of the other logical unit; a database can also be used as a logical unit, and a logical unit has a dependency relationship with the database it needs to connect to; a web page can also correspond to a logical unit in the program, and a logical unit has a dependency relationship with the web page it needs to access during operation; there is a dependency relationship between a function and the class to which it belongs.
[0039] In the embodiments of this specification, the dependency relationship between each logic unit and other logic units is referred to as the unit dependency relationship of the logic unit. For any logic unit in the target program, the code generation device can obtain its unit dependency relationship, and the unit dependency relationship can indicate other logic units required for the execution of the logic unit. In some methods, specific information of other logic units can also be specifically indicated. The unit dependency relationship can be obtained by the code generation device by analyzing the target program as a whole. Optionally, the analysis process can be combined with some information such as descriptions or comments entered by developers for the target program.
[0040] After determining the unit to be tested in the target program, the code generation device can obtain its unit dependency. In the embodiment of the present specification, the code generation device determines the target code information required to generate the unit test code for the unit to be tested based on the unit dependency, so as to ensure that the rich code information associated with the unit to be tested is obtained, and the quality of the unit test code generation is good. In the embodiment of the present specification, the code generation device retrieves the target code information from the candidate code information corresponding to the target program based on the unit dependency of the unit to be tested. For example, based on the unit dependency of the unit to be tested, the information of the logical unit indicated by the unit dependency and the information of the unit to be tested can be retrieved from the candidate code information to obtain the target code information. The target code information may include all the code information associated with the unit to be tested in the candidate code information, which may include the code information of the logical unit in the candidate code information that has a dependency relationship with the unit to be tested, and this information is equivalent to the context information of the unit to be tested. The target code information may also include the code information of the unit to be tested itself in the candidate code information. The code generation device may call a symbol search tool to search in the candidate code information according to the input search requirements (such as the information of the unit to be tested and its unit dependency).
[0041] The alternative code information corresponding to the target program may be generated before the unit test code is generated. The embodiments of this specification take the generation of the alternative code information by the code generation device as an example. In some implementations, the alternative code information is generated by a device other than the code generation device, and the code generation device can directly obtain the generated alternative code information and search it; or the search conditions can be sent to the other device, and the other device searches based on the search conditions and returns the search results to the code generation device. Optionally, the alternative code information can be stored in the same location as the code file of the target program.
[0042] For example, the code generation device can obtain auxiliary information by parsing the grammatical structure of the code file of the target program, and then filter the auxiliary information based on the target information type to obtain the alternative code information corresponding to the target program. The target information type can be obtained based on the information analysis used for unit testing, and the target information type is the type of information used for unit testing. The information used for unit testing here may include the information used in the history of generating unit test codes for various logical units. In the embodiments of this specification, the information used for unit testing for a variety of different logical units can be input into the code generation device, so that the code generation device analyzes and obtains the target information type applicable to various logical units. Alternatively, the target information type can also be obtained by the developer based on historical experience in unit testing.
[0043] Taking the example of a logic unit that has been historically unit tested including a first function, and the input of the first function is the output of the second function, and generating unit test code for the first function based on its own function code and the function code of the second function, the information used in the aforementioned unit test includes the function code of the first function and the code of the second function. Taking the example of a logic unit that has been historically unit tested including a third function, and generating unit test code for the second function based on its own function code and the code contained in the class to which the third function belongs, the information used in the aforementioned unit test includes: the function code of the third function and the code contained in the class to which the third function belongs. By analyzing the information used for these unit tests, it can be obtained that the target information types include: information about the function itself, information about the input function of the function, and information about the class to which the function belongs.
[0044] The code generation device performs grammatical structure analysis on the code file of the target program, so as to convert the content of the code file into code information suitable for retrieval, such as converting the content of the code file into the correspondence between various information types and specific information, and indexing the various types of information obtained by the analysis. For example, after performing grammatical structure analysis on a code file, the name of the class and the corresponding content, the name of the function in each class and the corresponding content can be obtained, and for each function, the correspondence between its input parameter, output parameter, function body and other information types and specific content can be obtained.
[0045] The code generation device performs grammatical structure analysis on each code file of the target program, and can obtain auxiliary information corresponding to the code file. Based on the target information type, the auxiliary information corresponding to each code file can be screened, and then the code information screened from the auxiliary information corresponding to each code file is integrated to obtain the candidate code information corresponding to the target program as a whole. Afterwards, the code generation device retrieves the target code information that satisfies the unit dependency in the candidate code information based on the unit dependency of the unit under test. The code generation device can retrieve the code information of the functions and classes associated with the unit under test in the candidate code information corresponding to the target program to obtain the target code information.
[0046] In some implementations, the code generation device may also retrieve some auxiliary code information in the code file of the target program, and generate unit test code based on the target code information and in combination with the auxiliary code information, such as obtaining information of an import file required by the unit under test in the code file of the target program.
[0047] Step 204: Generate unit test code for the unit to be tested based on the target code information.
[0048] After determining the target code information corresponding to the code under test, the code generation device can generate corresponding unit test code for the unit under test based on the target code information and in accordance with the generation rules of the unit test code. In the embodiment of this specification, the code generation device can use a large model to generate the unit test code. If the code generation device generates the unit test code based on the unit test request sent by the client device, the generated unit test code can also be sent to the client device for the client device to run the unit test code to test the unit under test. In some embodiments, the code generation device can also directly run the unit test code to test the unit under test, and then send the test result of the unit under test to the client device, and the user of the client device can directly adjust the unit under test based on the test result.
[0049] Since the target code information obtained by the code generation device is relatively detailed and all of it is information required for unit testing, the unit test code generated based on this code information can be relatively accurate and contain fewer errors, which can ensure that the generated quality of the unit test code is high, and accordingly, the overall test efficiency and accuracy of the target program can be ensured to be high, which is conducive to accelerating the development speed of the target program and improving the quality of the target program.
[0050] In some implementations, before retrieving the target code information from the alternative code information based on the unit dependency of the unit to be tested in the target program in step 202, the code generation method provided in the embodiment of this specification may also include a process of generating alternative code information corresponding to the target program. The process may include: obtaining a target program including at least one code file; performing grammatical structure analysis on each code file in the target program to obtain auxiliary information; filtering the auxiliary information based on the target information type; and obtaining alternative code information based on the information obtained by filtering. The steps in this process can refer to the above-mentioned introduction to step 202, and no additional description is given here for the above-mentioned content.
[0051] For example, the client device can send the target program to the code generation device, and the code generation device accordingly obtains the target program, which may include at least one code file, and each code file may include codes for implementing different functions. The code generation device performs grammatical structure analysis on each code file in the received target program, and obtains auxiliary information corresponding to each code file. The auxiliary information corresponding to each code file can indicate the grammatical structure of the code file, and may include composition structure information of various grammatical components in the code file. Such as including composition structures such as comments, variable declarations, and functions.
[0052] The code generation device can also obtain the target information type, such as the target information type is pre-set in the code generation device by the staff, or the code generation device can also obtain it by analyzing the information by itself. Based on the target information type, the code generation device screens the auxiliary information corresponding to each code file of the target program, removes the information in the auxiliary information that is not related to the unit test, and obtains the alternative code information corresponding to the target program. Afterwards, the alternative code information can also be stored, such as storing the alternative code information as an information library. Through this information screening, it is possible to reduce the storage space occupied by useless information on the basis of ensuring that the information related to the unit test is obtained, and to ensure the efficiency of subsequent information retrieval.
[0053] In some embodiments, for each code file in the target program, the code generation device can perform an abstract syntax tree (AST) analysis to obtain auxiliary information, which includes an abstract syntax tree for each code file. Abstract syntax tree analysis is the process of converting source code into a tree structure that can represent the grammatical structure of the source code. Each node in the AST tree represents a part of the source code, such as a variable, an operator, a control flow statement, etc.
[0054] In some implementations, the target information type may include information types of function dimension and information types of class dimension. Accordingly, the above-mentioned screening of auxiliary information based on the target information type may include: screening the auxiliary information based on the information types of function dimension and information types of class dimension included in the target information type.
[0055] For example, the information types of the function dimension include at least one of a function index, a function signature, a function input parameter, a function output parameter, a function body, and a function annotation. The information types of the class dimension include at least one of a class index, a class member variable, a class constructor, a class function signature, and a parent class index. The code generation device can retain the code information of these information types in the auxiliary information and eliminate other information to obtain alternative code information. The code information of these information types retained after screening can be determined as alternative code information. Since the alternative code information is information of a specified information type and can reflect some component structures of the code file, the alternative code information can be considered as structured information.
[0056] Optionally, for different types of functions or classes in a program, the information types based on which the information is screened may not be completely the same. For example, for an instantiated function, its function body may not be obtained, but for a static function, its function body may be obtained.
[0057] In some implementations, the candidate code information may be structured information that meets certain format requirements. The above-mentioned step of obtaining the candidate code information based on the screened information may include: arranging the screened information according to the class dimension and the function dimension to obtain structured candidate code information. For example, the information of the class dimension in the screened information is arranged according to the format requirements of the class dimension, and / or the information of the function dimension in the screened information is arranged according to the format requirements of the function dimension to obtain structured candidate code information; wherein the structured information is information that meets the format requirements.
[0058] In the embodiments of this specification, the format requirements of the class dimension and the function dimension are both to store information in the form of key-value pairs. In some embodiments, the format requirements of the class dimension and the format requirements of the function dimension may also be different, which is not limited here. The code generation device can set corresponding index paths for the information of the class dimension and the information of the function dimension, respectively, and arrange and store each information in the form of a key-value pair. For example, for a type of information in the class dimension, the key is the class constructor, and the value is function A and its specific content. The code generation device can also arrange the obtained key-value pairs once according to the relationship between each class and each function, so that the key-value pairs with associated classes and functions are arranged to similar positions. The code generation device can also arrange in other ways, such as arranging information of classes and functions with the same characteristics or used to implement similar functions to similar positions. After information screening and this arrangement, the structured alternative code information corresponding to the target program can be obtained.
[0059] In the embodiments of the present specification, the alternative code information is made into structured information. In this way, when information retrieval is performed in the alternative code information, it can be ensured that the information required for generating the unit test code can be retrieved more quickly and comprehensively, and the probability of retrieving erroneous information can be reduced, which can correspondingly improve the generation efficiency and quality of the unit test code.
[0060] In some implementations, the candidate code information generated for the target program can be updated synchronously with the target program. Accordingly, the code generation method provided in the embodiment of this specification also includes: when the target program is updated, performing grammatical structure analysis on the updated code file in the target program to obtain update auxiliary information; based on the target information type, filtering the update auxiliary information, and updating the candidate code information based on the filtered information.
[0061] For example, after the developer updates the target program, the updated code file can be sent to the code generation device using the client device. The code generation device can parse the received code again and filter the obtained update auxiliary information based on the target information type. The code generation device can replace the code information corresponding to the code file in the original candidate code information with the filtered information to update the candidate code information. The code generation device can also arrange the filtered information into structured information in a set manner and then replace it.
[0062] Optionally, the client device may also send the updated target program as a whole to the code generating device, or only send the updated code portion in the target program to the code generating device, so that the code generating device updates the candidate code information based on the received code. For example, the code generating device may regenerate new candidate code information based on the updated target program and delete the original candidate code information. Alternatively, the code generating device replaces the corresponding part of the code information in the original candidate code information based on the updated code portion to update the candidate code information.
[0063] In the embodiment of the present specification, the code generation device can use the code generation model to generate the unit test code of the unit to be tested. Accordingly, the above step 204 may include: inputting the target code information into the code generation model to obtain the unit test code for the unit to be tested output by the code generation model; wherein the code generation model is obtained based on the sample code information and unit test code training corresponding to multiple first sample units, and the sample code information corresponding to the first sample unit includes: the code information of the unit having a dependency relationship with the first sample unit in the reference code information corresponding to the first sample program to which the first sample unit belongs; the reference code information corresponding to the first sample program is obtained by screening the reference information based on the target information type, and the reference information is obtained by parsing the grammatical structure of the code file of the first sample program. In order to distinguish the sample programs, sample units, training samples and training labels corresponding to other models, the sample programs, sample units, training samples and training labels corresponding to the code generation model in the embodiment of the present specification are referred to as the first sample program, the first sample unit, the first training sample and the first training label.
[0064] In an embodiment of the present specification, the code generation device can train a corresponding large model (such as a code generation model) for a unit test scenario, so as to use the large model to perform reasoning based on the target code information retrieved for the unit to be tested, and generate unit test code for the unit to be tested. The training data used by the code generation model can correspond to the target code information. By way of example, the training data of the code generation model includes sample code information and unit test code corresponding to a plurality of first sample units, each first sample unit corresponds to at least one sample code information, and each sample code information can correspond to a section of unit test code. The first sample unit can be a logic unit that has been unit tested historically, and the unit test code corresponding to the first sample unit can be the correct unit test code used when it performs unit testing.
[0065] The method of obtaining the sample code information of any first sample unit is similar to the method of obtaining the target code information of the unit to be tested, and they can be referenced to each other. For example, the code file of the first sample program to which the first sample unit belongs is parsed in advance to obtain reference information corresponding to each code file, and these reference information constitute the reference information corresponding to the first sample program. Afterwards, the reference information can be screened based on the target information type to obtain the reference code information corresponding to the first sample program. The target information type (such as the first target information type) can be the same as the target information type (such as the second target information type) based on which the auxiliary information of the target program is screened. Alternatively, the first target information type can be different from the second target information type, such as the first target information type is obtained by optimizing and adjusting on the basis of the second target information type. For each first sample unit, the code information associated with the first sample unit can be searched in the reference code information corresponding to the first sample program as the sample code information corresponding to the first sample unit. For example, based on the unit dependency relationship of the first sample unit, the code information of the unit having a dependency relationship with the first sample unit is searched in the reference code information. The sample code information also includes the code information of the first sample unit itself in the reference code information.
[0066] Before executing the code generation method provided in the embodiments of this specification, a code generation model training method may be executed first to train the code generation model, and then the trained code generation model may be used to execute the code generation method. Figure 3 This is a flowchart of a method for training a code generation model provided in one embodiment of the present specification. The training method can be applied to a model training device, and the model training device can be the same as the code generation device for executing the code generation method described above, or can be different. In the case where the model training device is different from the code generation device, after the model training device completes training the code generation model, the code generation device calls the code generation model to execute the code generation method described above. In the case where the model training device is the same as the code generation device, the following code generation model training method belongs to the code generation method executed by the code generation device, and is executed before the above step 202. Figure 3 As shown, the training method of the code generation model includes the following steps 302 to 306. The training method can be cross-referenced with the above introduction on the code generation model.
[0067] Step 302: Obtain a first training sample and a first training label, wherein the first training sample includes sample code information corresponding to a first sample unit in a first sample program, and the sample code information includes: code information of a unit having a dependency relationship with the first sample unit in reference code information corresponding to the first sample program; the reference code information is obtained by screening reference information based on a target information type, the reference information is obtained by performing grammatical structure analysis on each code file in the first sample program, the target information type is the type of information used for unit testing, and the first training label is the unit test code of the first sample unit.
[0068] Among them, the first training sample is the data supported by the code generation model for processing, including the sample code information corresponding to the first sample unit that has obtained the correct unit test code. For the method of obtaining the sample code information, please refer to the above-mentioned related introduction. The first training label corresponding to the first training sample is the correct processing result that should be obtained by executing the training task for the first training sample, which is used to determine the prediction loss of the initial code generation model for the training task, thereby adjusting the model parameters of the initial code generation model.
[0069] Step 304: input the first training sample into the initial code generation model to predict the reference unit test code corresponding to the first sample unit.
[0070] The model training device can construct an initial code generation model based on the logic of generating unit test code. Afterwards, each first training sample obtained can be input into the initial code generation model so that the code generation model can perform reasoning based on each first training sample and predict the reference unit test code corresponding to the first sample unit.
[0071] Step 306: Based on the comparison result between the reference unit test code and the unit test code in the first training tag, adjust the initial code generation model to obtain a trained code generation model.
[0072] For each reference unit test code output by the initial code generation model, it can be compared with the corresponding first training label, and the parameters of the initial code generation model can be adjusted based on the comparison result. Then, the operation step of obtaining the first training data (i.e., step 302) is returned to perform the prediction, comparison, and parameter adjustment process multiple times until the training stop condition is reached, and a trained code generation model is obtained.
[0073] Specifically, the loss value can be calculated based on the prediction result (i.e., the reference unit test code) and the first training label, and the model parameters of the initial code generation model can be adjusted based on the back propagation of the loss value, and then the step of obtaining training data can be returned to continue to select sample code information to train the initial code generation model until the training stop condition is reached, the training is completed, and a code generation model that can be put into use is obtained. The training stop condition can be that the loss value is less than the loss value threshold, or the number of iterations reaches the number threshold, or the loss value threshold and the number threshold can be combined to determine whether the training stop condition is reached.
[0074] The loss value threshold is the critical value of the loss value. When the loss value is greater than or equal to the loss value threshold, it means that there is still a certain deviation between the prediction result of the initial code generation model and the actual result, and the model parameters of the initial code generation model still need to be adjusted. At this time, it is determined that the training stop condition has not been met. When the loss value is less than the loss value threshold, it means that the prediction result of the initial code generation model is close enough to the actual result, and the training can be stopped. At this time, it can be determined that the training stop condition has been met. The value of the loss value threshold is selected according to the actual situation.
[0075] In addition, the number threshold is set according to the actual situation. When the number of iterations reaches the number threshold, it means that the initial code generation model has been trained enough times. At this time, the prediction result of the initial code generation model is close to the actual result, and the training can be stopped.
[0076] The training method of the code generation model provided by the embodiment of this specification is applied, and the sample code information based on it includes the code information of the unit having a dependency relationship with the first sample unit, and the code information is retrieved from the reference code information obtained by parsing the syntax structure of the code file of the first sample program and screening based on the target information type. In this way, it can be ensured that the sample code information is relatively detailed and all of them are the information required for the test of the first sample unit. The code generation model can generate relatively accurate unit test code based on these sample code information, which can ensure that the generation quality of the unit test code is high.
[0077] In some embodiments, after generating unit test code for the unit to be tested, it is also possible to detect whether there are errors in the unit test code, and if there are errors in the unit test code, adjust the unit test code to ensure that the correct unit test code is finally obtained. For example, the code generation device can call a compilation tool to compile the generated unit test code and detect whether there are compilation errors in the unit test code. In the absence of compilation errors, the code generation device can also run the unit test code to detect whether there are runtime errors in the unit test code. The code generation device can modify the unit test code based on the detected error information using a large model (such as a code modification model). Accordingly, after the above step 204 generates the unit test code for the unit to be tested based on the target code information, the code generation method provided in the embodiment of this specification also includes the following steps s2 to s8.
[0078] Step s2: compile the unit test code to obtain a compilation result.
[0079] The code generation device can call a compiler tool to compile the unit test code. If the compilation result indicates that there is no compilation error, the obtained unit test code can be directly determined as the final unit test code and returned to the client device. If the compilation result indicates that there is a compilation error, subsequent steps can be performed to modify the code.
[0080] In the case where there is an error in the compilation process of the unit test code, the compilation result may include error information, which may indicate the location of the error in the code and the cause of the error, such as the cause may include the lack of certain parameters, certain information mismatch or logical inconsistency, etc. In order to facilitate the distinction from other error information, the error information indicating that there is an error in the compilation process is referred to as the first error information in the embodiments of this specification.
[0081] In some implementations, the unit test code in step s2 may be a unit test code directly uploaded by the client device, and the client device may upload the unit test code to request the code generation device to detect and modify it. In this manner, steps s2 to s8 may be directly executed independently without relying on the aforementioned steps 202 and 204.
[0082] Step s4: when the compilation result includes first error information, searching for first supplementary information in the alternative code information corresponding to the target program based on the first error information; wherein the first error information indicates that there is an error in the compilation process of the unit test code.
[0083] The code generation device searches for the code information required to solve the corresponding error in the alternative code information corresponding to the target program based on the first error message. In the embodiment of this specification, the code information searched is referred to as the first supplementary information. For example, if the first error message indicates that parameter A is missing, the code generation device can search for code information related to parameter A in the alternative code information. If the first error message indicates that two operation logics in the unit test code do not match, the code information related to the two operation logics can be searched again in the alternative code information. The code generation device can call a symbol retrieval tool to search for the first supplementary information based on the first error message.
[0084] Step s6: calling the code modification model to process the unit test code of the unit to be tested, the first error information and the first supplementary information, and updating the unit test code.
[0085] In the embodiments of this specification, the code generation device can modify the originally generated unit test code using the code modification model to fix the original compilation error. The code modification model can directly modify the unit test code and output the updated unit test code. In some embodiments, the code modification model can only output the modification method for the original unit test code, and the code generation device then modifies the unit test code based on the modification method. For example, the code generation device can input the unit test code, the first error message and the first supplementary information into the code modification model, so that the code modification model processes the unit test code, the first error message and the first supplementary information of the unit to be tested, and obtains the modification method for the unit test code output by the code modification model. After that, the code generation device calls the file modification tool to modify the unit test code based on the modification method to update the unit test code. For example, the modification method can indicate which lines in the unit test code are modified, replace the specified code in the unit test code with the corresponding target code, delete which code, or add which code and the like.
[0086] In the embodiments of the present specification, the code modification model is trained based on multiple training samples and corresponding training labels. Each training sample includes a sample test code of a sample unit and corresponding sample error information and sample supplementary information, and the sample test code is a unit test code for the sample unit. The training label is a modification method for the sample test code. The acquisition and processing methods of the sample test code and the unit test code can be similar, the acquisition and processing methods of the sample error information and the first error information can be similar, and the acquisition and processing methods of the sample supplementary information and the first supplementary information can be similar.
[0087] The sample test code can be generated using a code generation model, or it can be manually written by a developer. The sample error information indicates that there is an error in the compilation process of the sample test code, and can also indicate the specific error situation. The sample supplementary information is obtained by searching the reference code information corresponding to the sample program to which the sample unit belongs based on the sample error information. The reference code information corresponding to the sample program is obtained by screening the reference information based on the target information type. The reference information is obtained by parsing the grammatical structure of each code file in the sample program. For information on how to obtain the reference code information, please refer to the aforementioned introduction to the alternative code information, which will not be elaborated here.
[0088] Step s8: Based on the updated unit test code, re-compile the unit test code to obtain the compilation result and subsequent steps until it is determined that the first stop modification condition is met; wherein the first stop modification condition includes: the compilation result does not include the first error message, and / or the number of times the unit test code is updated exceeds a threshold number.
[0089] After obtaining the updated unit test code, the code generation device can re-execute step s2 and subsequent steps to further detect whether the updated unit test code still has compilation errors. If there are still compilation errors, continue to modify the unit test code, and determine whether the first stop modification condition is met after each modification to obtain the target unit test code that meets the first stop modification condition. The first stop modification condition can be that there are no compilation errors in the unit test code, or the number of times the unit test code is updated exceeds a number threshold. For example, the number threshold can be 3 times, 4 times or other times. When the number of times the unit test code is updated exceeds the number threshold and there are still compilation errors, the compilation result and the current unit test code can be fed back to the user, so that the user can manually determine again whether it is necessary to continue code modification.
[0090] Before modifying the unit test code (ie, step s6), a code modification model training method may be executed to train the code modification model, and then the trained code modification model may be used to modify the code. Figure 4It is a flowchart of a training method for a code modification model provided in an embodiment of the present specification. The training method can be applied to a model training device, and the model training device can be the same as the code generation device for executing the code generation method mentioned above, or can also be different. In the case where the model training device is different from the code generation device, after the code modification model is trained by the model training device, the code generation device calls the code modification model again to execute the above-mentioned code modification process. In the case where the model training device is the same as the code generation device, the following training method for the code modification model belongs to the code generation method executed by the code generation device, and is executed at any time before the above-mentioned step s6, such as before step 202. In the embodiments of the present specification, the training devices for the code generation model and the code modification model can be the same or different, and are not limited here.
[0091] like Figure 4 As shown, the training method of the code modification model includes the following steps 402 to 406. The training method can be cross-referenced with the aforementioned introduction to the use of the code modification model, and can also appropriately refer to the aforementioned introduction to the code generation model. In order to distinguish the sample programs, sample units, training samples, and training labels corresponding to the aforementioned code generation model, the sample programs, sample units, training samples, and training labels corresponding to the code modification model in the embodiments of this specification are referred to as second sample programs, second sample units, second training samples, and second training labels.
[0092] Step 402: obtain a second training sample and a second training label, wherein the second training sample includes a sample test code, sample error information, and sample supplementary information of a second sample unit in a second sample program, the sample error information indicates that there is an error in the compilation process of the second sample test code, the sample supplementary information is obtained by searching in reference code information corresponding to the second sample program based on the sample error information, the reference code information corresponding to the second sample program is obtained by screening reference information based on a target information type, the reference information is obtained by performing grammatical structure analysis on each code file in the second sample program, and the second training label is a modification method for the sample test code.
[0093] Among them, the second training sample is the data supported by the code modification model, including the sample test code that has obtained the correct modification method, and also includes the sample error information and sample supplementary information corresponding to the sample test code. For the method of obtaining the sample test code, please refer to the above-mentioned related introduction. The sample error information includes the compilation error information for the sample test code. In some cases, the sample error information may also include other error information of the sample test code. The staff can pre-set the correspondence between various error information and supplementary information, and then the computer can search for the sample supplementary information in the reference code information based on the sample error information according to the correspondence.
[0094] The second training label corresponding to the second training sample is the correct processing result that should be obtained by executing the training task for the second training sample, and is used to determine the prediction loss of the initial code modification model for the training task, thereby adjusting the model parameters of the initial code modification model.
[0095] Step 404: input the second training sample into the initial code modification model to predict a reference modification method corresponding to the second training sample.
[0096] The model training device can construct an initial code modification model based on the logic of modifying the code errors. Afterwards, each second training sample obtained can be input into the initial code modification model so that the code modification model can perform reasoning based on each second training sample and predict the reference modification method corresponding to the second sample unit.
[0097] Step 406: Based on the comparison result between the reference modification method and the modification method in the second training label, adjust the initial code modification model to obtain a trained code modification model.
[0098] The reference modification method for each sample test code output by the initial code modification model can be compared with the second training label (i.e., the correct modification method) corresponding to the sample test code, and the parameters of the initial code modification model can be adjusted based on the comparison result. Then return to the operation step of obtaining the second training data (i.e., step 402), and perform the prediction, comparison, and parameter adjustment process multiple times until the training stop condition is reached to obtain a trained code modification model. The model can be adjusted based on the loss value. The training stop condition can be that the loss value is less than the loss value threshold, or it can be that the number of iterations reaches the number threshold, or it can be determined whether the training stop condition is reached by combining the loss value threshold and the number threshold.
[0099] In the embodiment of this specification, in addition to detecting whether the generated unit test code has compilation errors and modifying the unit test code based on the compilation errors, the code generation device can also detect whether the unit test code has errors in other aspects and make corresponding adjustments to ensure the overall accuracy of the unit test code. For example, the code generation device can also detect whether there are runtime errors in the unit test code when there are no compilation errors. Accordingly, after step s8, the code generation method provided in the embodiment of this specification can also include the following steps s10 to s16.
[0100] Step s10, run the target unit test code to obtain the running result; wherein, there is no error in the compilation process of the target unit test code.
[0101] The target unit test code may be a unit test code without compilation errors obtained after the compilation errors are repaired in the aforementioned steps s4 to s8. If the unit test code obtained in step 204 is determined to have no compilation errors after step s2, the unit test code may be directly determined as the target unit test code to execute step s10. The target unit test code after compilation may be executed in step s10. For example, the code generation device may call a code execution tool to execute the compiled target unit test code.
[0102] In some implementations, the target unit test code may also be a unit test code uploaded by the client device without compilation errors. In this manner, steps s10 to s16 may be directly executed independently without relying on the aforementioned steps 202, 204 and s2.
[0103] If there is no problem in the running process, the running result may include the output information obtained by the target unit test code processing the input information. If there is a problem in the running process, the running result also includes the second error information such as the location of the running error and the possible cause of the error.
[0104] Step s12: when the running result includes second error information, searching for second supplementary information in the alternative code information corresponding to the target program based on the second error information; wherein the second error information indicates that there is an error in the running process of the target unit test code.
[0105] The way in which the code generating device searches for the second supplementary information based on the second error information can refer to the way in which the first supplementary information is searched in the aforementioned step s4, which will not be described in detail here.
[0106] Step s14: calling the code modification model to process the target unit test code, the second error information and the second supplementary information, and updating the target unit test code.
[0107] The code modification model here can be the same model as the code modification model called when performing code modification based on compilation errors. For details, please refer to the relevant introduction mentioned above. The process of the code generation device calling the code modification model for processing can refer to the relevant introduction in step s6, which will not be repeated here. The code modification model called in step s14 can also be different from the code modification model called in step s6. In this case, a corresponding code modification model can be retrained for the runtime error. The training method can refer to the training method of the code modification model for coding errors mentioned above, which will not be repeated here.
[0108] Step s16: based on the updated target unit test code, re-execute the target unit test code to obtain the running result and subsequent steps until it is determined that the second stop modification condition is met; wherein the second stop modification condition includes: there is no error in the running result, and / or the number of times the target unit test code is updated exceeds a threshold number.
[0109] After obtaining the updated target unit test code, the code generation device can determine whether the second stop modification condition is met, and when it is not met, re-execute step s10 and subsequent steps to further detect whether the updated target unit test code still has a runtime error. If there is still a runtime error, continue to modify the target unit test code until it is determined that the second stop modification condition is met. The second stop modification condition can be that the operation result indicates that there is no runtime error, or the number of times the target unit test code is updated is greater than the number threshold. When the number of times the target unit test code is updated exceeds the number threshold and there is still a compilation error, the operation result and the current target unit test code can be fed back to the user, so that the user can manually determine again whether it is necessary to continue code modification.
[0110] When the code generation device determines that the second stop modification condition is satisfied, the current target unit test code can be determined as the unit test code finally generated for the unit under test, and then the unit test code can be fed back to the client device as needed, or stored, or used for other purposes.
[0111] In some implementations, the code generation device can also display some information about the model operation process when calling the code modification model for processing. For example, it can display which corresponding tool the model called at each time, the specific results returned by the tool, and the reason for calling the tool; it can also display the processing logic based on each step when the model processes the code. Based on this information, it is convenient for the staff to determine whether the model is running normally.
[0112] In the embodiments of this specification, the code generation device can help developers greatly improve the efficiency and quality of writing unit tests by leveraging the unit test generation capability of the large model, thereby further improving the quality of software products developed by the enterprise. In addition, the code generation model and the code modification model in the embodiments of this specification are both trained based on the code information obtained by parsing and screening the code files of the program, ensuring that more detailed information that is more in line with unit testing is used for model training, which can reduce the illusion phenomenon of the model and ensure that the unit test code generated by the model is of high quality.
[0113] In the embodiment of this specification, the code generation device can use a unit test automatic generation system to implement the aforementioned process of generating unit test code for the unit to be tested. The system is generally introduced below. The system is a system based on a large model and includes multiple agents. The system is divided into two modules, namely a code warehouse parsing module and a unit test generation module. The aforementioned process of generating alternative code information can be implemented using the code warehouse parsing module, and the aforementioned process of generating unit test code and modifying it can be implemented based on the unit test generation module.
[0114] The code repository parsing module is responsible for completing the parsing of the entire code repository (such as the software project to which the corresponding target program belongs), and keeping the parsing data updated in sync with the code repository update, while storing the parsed structured information (that is, the aforementioned candidate code information) in a local file. The parsed structured information is used as the input of the unit test generation module. Figure 5 This is a simplified block diagram of a code repository parsing module for performing code parsing provided in an embodiment of this specification. Figure 5 As shown, the code repository parsing module includes a code parsing agent, which can call the environment tool (such as AST parsing tool) in the code generation device to parse all code files in the code repository uploaded by the user, and filter, reorganize and aggregate the parsed information according to the target information type, so as to store the structured code information in the form of key-value pairs at the two levels of function and class. The structured code information can be stored locally in the user in text form.
[0115] The structured information stored at the function level may include function index, function signature, function input parameter, function output parameter, function body, function annotation, etc. The structured information stored at the class level may include class index, class member variables, class constructor, class function signature, parent class index, etc.
[0116] Figure 6 This is a simple block diagram of a unit test generation module for code parsing provided in an embodiment of this specification. Figure 6As shown, the unit test generation module may include a code generation agent and a code error repair agent. When a user submits a unit test generation requirement, the code generation agent first generates a temporary unit test file (such as the unit test code obtained in step 204), and then the code error repair agent repairs the errors in the temporary unit test file to obtain the final unit test file.
[0117] The unit test generation module will call the symbol search tool, compiler tool and file modification tool during execution. The symbol search tool will search for structured code information based on the input search requirements (for example, searching for a function or class) and return the matching results. The compiler tool will compile the specified file and return the compilation result of the file. The file modification tool will create or delete a file based on the input requirements, or modify the content of the specified file based on the input modification requirements.
[0118] like Figure 6 As shown, the user can submit a unit test generation requirement to the code generation agent, which can indicate a function to be tested selected by the user. The code generation agent can first call the symbol retrieval tool according to the function to be tested selected by the user, retrieve all code information related to the function to be tested in the structured code information, and then integrate the user's requirements with the retrieved information, request the code generation model to perform reasoning, and obtain the unit test code generated by the model. Then call the file modification tool to create a temporary unit test file in the user's code repository, and write the unit test code returned by the model into the file.
[0119] The code error repair agent will call the compiler tool to compile the temporary unit test file generated by the code generation agent to obtain the compilation result. If the compilation result indicates that there is no compilation error, the unit test file will be directly returned to the user; if the compilation result indicates that there is a compilation error, the code error repair agent will call the symbol search tool to retrieve the supplementary code information for repairing the error based on the compilation error information. Then the compilation error information, the unit test code with errors and the supplementary code information are compiled and integrated, and the code modification model is requested to perform reasoning to obtain error repair suggestions. Then the code error repair agent will call the file modification tool to apply the error repair suggestions to the unit test code file with errors and modify it. Repeat the above process until there is no additional compilation error information or the maximum number of retries (such as 3) is reached, and then return the final unit test code file to the user.
[0120] In the embodiments of this specification, the code in the code repository is parsed and reorganized and stored in a structured code information format through AST parsing technology. When the initial unit test is generated, the code generation agent searches the structured code information to fully obtain the valid information related to the tested function. On this basis, the code generation model can be used to generate high-quality unit test code and reduce the errors in the unit test code. After obtaining the initially generated unit test code, the code error repair agent interacts with the compiler tool and the symbol retrieval tool in the IDE environment to repair the errors in the unit test code in the form of multiple rounds of interactive modification, which can break through the upper limit of the traditional large model that only generates the unit test code once, and ensure that the final unit test code is of high quality.
[0121] Figure 7 is a flowchart of another code generation method provided in an embodiment of this specification. The method can be applied to a code generation device. For example, the code generation device can be Figure 1 Server 10 in. Figure 7 The interaction scenario between the code generation apparatus and the front-end device is introduced, and the front-end device can be the aforementioned client device. Figure 7 The method provided can be used with the previous Figure 2 The provided methods are referenced to each other. The method specifically includes the following steps 702 and 708 .
[0122] Step 702: Receive a code generation request uploaded by a front-end device for a unit under test in a target program.
[0123] The code generation request is a unit test code generation request for the unit under test, which is also the aforementioned unit test requirement. The way in which the front-end device generates the code generation request can refer to the relevant introduction of the user specifying the unit under test in the aforementioned step 202.
[0124] Step 704, in response to the code generation request, based on the unit dependency of the unit to be tested, the target code information is retrieved from the alternative code information; wherein the alternative code information is obtained by screening the auxiliary information based on the target information type, the auxiliary information is obtained by parsing the grammatical structure of each code file in the target program, and the target information type is the type of information used for unit testing.
[0125] For step 704, reference may be made to the aforementioned description of step 202, and no further elaboration is given here.
[0126] Step 706: Input the target code information into the code generation model to obtain the unit test code output by the code generation model for the unit to be tested.
[0127] For step 704, reference may be made to the aforementioned description of step 204, and no further elaboration is given here.
[0128] Step 708: Send the unit test code to the front-end device, so that the front-end device can run the unit test code to test the unit to be tested.
[0129] For example, after generating the unit test code, the code generation device may send the unit test code to the front-end device. The front-end device may have a unit test code display area for displaying the received unit test code. Optionally, the unit test code may also be directly stored in the front-end device in the form of a file, and the file is opened to display the specific content of the unit test code when triggered by the user.
[0130] In some embodiments, after step 708, the user can determine whether the received unit test code meets his needs and whether it can effectively test the unit under test. If the user believes that the unit test code does not meet his needs after running the unit test code for the unit under test, he can send problem feedback information for the unit test code to the code generation device, and the problem feedback information can carry specific description information that the user believes does not meet the needs. When the code generation device receives the problem feedback information sent by the front-end device for the unit test code, the code generation model is adjusted based on the problem feedback information to enhance the ability of the code generation model. The code generation device can re-execute the code generation method based on the adjusted code generation model based on the user's instructions to regenerate the unit test code of the unit under test and feedback it to the user.
[0131] It should be noted that Figure 7 Take the example that the code generation device directly generates the unit test code and then sends the unit test code to the front-end device. Figure 2 Corresponding to the introduction, after step 706, the code generation device can execute the aforementioned steps s2 to s8 to detect whether there are compilation errors in the unit test code. When there are compilation errors, the code is modified before being sent to the front-end device. In some embodiments, the code generation device can also execute steps s10 to s16 to detect whether there are runtime errors in the unit test code without compilation errors. When there are runtime errors, the code is modified before being sent to the front-end device. In this way, the accuracy and quality of the unit test code sent to the front-end device can be improved, the user experience of obtaining the unit test code from the code generation device can be improved, and the user's excessive adjustment of the code can be reduced.
[0132] In the embodiment of the present specification, the code generation device performs grammatical structure analysis on the code file of the target program, and screens based on the information type required for the unit test to obtain the candidate code information. In response to the code generation request sent by the front-end device, when the unit test is performed on the unit to be tested in the target program, the target code information is retrieved from these candidate code information based on its unit dependency, and the unit test code is generated based on this and fed back to the front-end device. In this way, it can be ensured that the retrieved target code information is relatively detailed and is all the information required for the unit test. The unit test code generated based on these code information can be relatively accurate and have fewer errors, ensuring that the front-end user obtains a higher quality unit test code, reducing the user's excessive adjustment of the unit test code, reducing labor costs, and ensuring that the test effect of the unit to be tested is relatively accurate.
[0133] Figure 8 This is a flowchart of another code generation method provided in an embodiment of this specification, which is applied to a task platform and specifically includes the following steps 802 and 804.
[0134] Step 802: Receive a model request sent by a terminal device, wherein the model request includes at least one of a scene identifier of a target scene, scene input data of the target scene, and a target model specification parameter.
[0135] It should be noted that the task platform is an online platform that provides model services, allowing developers, enterprises or individual users to submit tasks, obtain resources or implement specific functions through API calls, web interfaces or other interactive methods. In the embodiments of this specification, the task platform is a cloud service platform that can provide deep learning models, allowing external applications (such as e-commerce platforms, navigation applications, social applications, film and television production applications, and online education platforms, etc.) to access and use. The task platform ensures efficient and accurate response to requests from terminal devices by managing model training, updating, optimization, load balancing, scheduling and reasoning. The terminal device is a terminal device that requests to obtain the target code generation model.
[0136] In actual implementation, a model request is a request sent by a terminal device to a task platform to obtain or use a specific model service, and the model request may include a scene identifier of a target scene, scene input data of a target scene, and at least one of target model specification parameters. In an embodiment of this specification, the model request may be a request for a code generation model and / or a code modification model.
[0137] Among them, the target scenario is the specific environment or situation where the target code generation model will be applied, which describes the purpose and expected effect of the model service. The scenario identifier of the target scenario is the identification information of the target scenario used for identification, which is used to assist in quickly locating the model related to the specific scenario, for example, "unit testing", "code writing", "video question and answer", "video navigation", "video monitoring", "video quality evaluation", "object positioning", "product positioning and search", etc. The scene input data of the target scenario can be the input data used to train the initial code generation model in the target scenario to adapt to the target scenario, such as the scene input data in the video question and answer scenario can be sample code with answers. In addition, the model specification parameters are attribute specification parameters that define the model architecture, performance or configuration, such as model model, model type, model size, etc. The model specification parameters are used to determine the model that adapts to user needs.
[0138] For example, on the task platform client of the terminal device, the user selects the scene identifier of the target scene as "unit test", selects the required model size as: 32GB, uploads the scene input data of the target scene, and the terminal device generates a model request and sends it to the task platform. The task platform receives the model request sent by the terminal device to the task platform, where the model request includes the scene identifier of the target scene, the scene input data of the target scene, and the target model specification parameters.
[0139] In the embodiments of the present specification, a user can independently select a scene identifier of a target scene, scene input data of a target scene, and at least one item of target model specification parameters on a terminal device to trigger a model request. The task platform can receive a model request for a unit test sent by the terminal device, and provide reference information for selecting a target code generation model based on the scene identifier of the target scene, scene input data of the target scene, and at least one item of target model specification parameters, which is applicable to a wider range of scenarios.
[0140] Step 804: Based on the model request, determine a corresponding code generation model and / or code modification model from at least one model, wherein the code generation model is trained based on the above-mentioned training method for the code generation model, and the code modification model is trained based on the above-mentioned training method for the code modification model, and the code generation model and / or code modification model are used to generate unit test code.
[0141] In the embodiments of the present specification, a model library may be provided, and the module library may store at least one code generation model and code modification model adapted to different scenarios, as well as multiple code generation models and code modification models with different model specification parameters. Based on the model request, the corresponding code generation model and / or code modification model is determined from at least one model. An optional method is: based on the model request, the corresponding code generation model and / or code modification model is searched from at least one model included in the model library, and the model library may store multiple code generation models and code modification models with different model specification parameters; another optional method is: based on the model request, the code generation model and / or code modification model is obtained by training; another optional method is: based on the model request, the code generation model and / or code modification model is constructed, which is not limited here.
[0142] In an implementation of step 804, the determining of a corresponding code generation model and / or code modification model from at least one model based on the model request may include:
[0143] Based on at least one item of information in the model request, searching the model library for a code generation model and / or a code modification model corresponding to the at least one item of information; wherein the scene identifier of the target scene and the scene input data of the target scene both correspond to a model adapted to the target scene, and the target model specification parameters correspond to a model having the same model specification parameters as the target model specification parameters;
[0144] In a case where at least one item of information includes scenario input data of a target scenario, the found code generation model and / or code modification model is trained based on the scenario input data of the target scenario to obtain a trained code generation model and / or code modification model.
[0145] Regarding the implementation of step 804, the task platform can determine the required code generation model and / or code modification model by searching for models from the model library for the scenario identifier of the target scenario and the target model specification parameters. The required code generation model and / or code modification model is determined by searching for models from the model library for the scenario input data of the target scenario and continuing to train the found models.
[0146] In the case where the model request includes only one piece of information, if the model request includes the scenario identifier of the target scenario, step 804 may include: based on the scenario identifier of the target scenario, searching for a code generation model and / or a code modification model adapted to the target scenario from a model library, wherein the model library stores at least one code generation model and code modification model adapted to different scenarios. If the model request includes the scenario input data of the target scenario, step 804 may include: determining an initial code generation model and / or an initial code modification model adapted to the target scenario from at least one model; based on the scenario input data of the target scenario, training the initial code generation model and / or the initial code modification model to obtain a trained code generation model and / or code modification model. If the model request includes model specification parameters, step 804 may include: based on the model specification parameters, searching for a corresponding code generation model and / or a code modification model from a model library, wherein the model library stores a plurality of code generation models and code modification models with different model specification parameters.
[0147] When the model request includes two or three items of information including the scene identification of the target scene, the scene input data of the target scene, and the target model specification parameters, the task platform can search the model library for a model that satisfies the two or three items of information at the same time to determine the required model. If the model request includes the scene identification of the target scene and the target model specification parameters, the task platform can search the model library for a model corresponding to the target model specification parameters that are suitable for the target scene. If the model request includes the scene identification of the target scene and the scene input data of the target scene, the task platform can first search the model library for a model that is suitable for the target scene, and then train the model based on the scene input data of the target scene to obtain the required model. If the model request includes the target model specification parameters and the scene input data of the target scene, the task platform can first search the model library for a model of the target model specification parameters, and then train the model based on the scene input data of the target scene to obtain the required model.
[0148] For the case where the model request includes the scene identification of the target scene, the scene input data of the target scene, and the target model specification parameters, the task platform can first search for at least one pre-trained code generation model from the model library based on the scene identification of the target scene, and then screen and obtain a code generation model of corresponding size from at least one code generation model based on the target model specification parameters, and then train the code generation model of corresponding size based on the scene input data of the target scene to obtain a code generation model suitable for user needs.
[0149] The code generation model obtained by the task platform is based on Figure 3 The code modification model obtained by training the above code generation model provided is based on Figure 4 The above code provided is trained by modifying the training method of the model. For specific methods, please refer to the content of the above model training, which will not be repeated here.
[0150] In the embodiments of this specification, based on scenario requirements, the code generation model and / or code modification model suitable for the scenario is accurately found through scenario identification, and then these models are used to generate unit test code, which can ensure that the obtained unit test code is more accurate and fits the scenario, thereby improving user experience and code generation quality.
[0151] Optionally, the unit test code required for unit testing of code in different scenarios may be different. As an example, the task platform can provide code generation models and code modification models for multiple scenarios, and can provide corresponding code generation models and code modification models based on the model request for unit testing sent by the terminal device, so as to generate accurate and effective unit test code for the logical units in the program in the corresponding field.
[0152] In the embodiment of this specification, after step 804, the task platform may also deploy a code generation model, and build a code generation interface based on the code generation model, so that the terminal device schedules the code generation model through the code generation interface to generate unit test code for the unit to be tested in the target program;
[0153] And / or, deploy a code modification model, and build a code modification interface based on the code modification model, so that the terminal device schedules the code modification model through the code modification interface to modify the unit test code of the unit under test in the target program.
[0154] Corresponding to the above method embodiment, this specification also provides a task platform embodiment, Fig. 9 Schematic diagram of a task platform provided in one embodiment of this specification. Fig. 9 As shown, the task platform 900 includes: a request interface 902 and a response unit 904;
[0155] A request interface 902, used to receive a model request for unit testing sent by a terminal device, wherein the model request includes at least one of a scene identifier of a target scene, scene input data of the target scene, and a model specification parameter;
[0156] A response unit 904 is used to determine a corresponding code generation model and / or code modification model from at least one model based on a model request, wherein the code generation model is trained based on the training method of the above-mentioned code generation model, and the code modification model is trained based on the training method of the above-mentioned code modification model.
[0157] Optionally, the model library stores at least one code generation model and code modification model adapted to different scenarios, and a plurality of code generation models and code modification models with different model specification parameters; the response unit 904 is used to:
[0158] Based on at least one item of information in the model request, searching in the model library for a code generation model and / or a code modification model corresponding to at least one item of information; wherein the scene identifier of the target scene and the scene input data of the target scene both correspond to a model adapted to the target scene, and the target model specification parameters correspond to a model having the same model specification parameters as the target model specification parameters;
[0159] In a case where at least one item of information includes scenario input data of a target scenario, the found code generation model and / or code modification model is trained based on the scenario input data of the target scenario to obtain a trained code generation model and / or code modification model.
[0160] Optionally, the task platform also includes a code generation interface, and the code processing interface is constructed based on the target code generation model; the code generation interface is used for scheduling by the terminal device to generate unit test code based on the code information associated with the unit under test in the target program. Optionally, the task platform also includes a code modification interface, and the code modification interface is constructed based on the code modification model; the code modification interface is used for scheduling by the terminal device to modify the unit test code.
[0161] The above is a schematic scheme of a task platform of this embodiment. It should be noted that the technical scheme of the task platform and the technical scheme of the above code generation method belong to the same concept, and the details of the technical scheme of the task platform that are not described in detail can be found in the description of the technical scheme of the above code generation method.
[0162] Corresponding to the above-mentioned code generation method embodiment, this specification also provides a code generation device embodiment, Fig.10 Schematic diagram of a code generation device provided in an embodiment of this specification. Fig.10 As shown, the code generating device comprises:
[0163] A retrieval module 1002 is used to retrieve target code information from candidate code information for a unit to be tested in a target program based on unit dependencies of the unit to be tested; wherein the candidate code information is obtained by screening auxiliary information based on a target information type, the auxiliary information is obtained by parsing a grammatical structure of a code file of the target program, and the target information type is a type of information used for unit testing;
[0164] The code generation module 1004 is used to generate unit test code for the unit to be tested based on the target code information.
[0165] Optionally, the code generating device further includes:
[0166] A first acquisition module is used to acquire a target program including at least one code file before retrieving target code information from candidate code information for a unit to be tested in the target program based on a unit dependency relationship of the unit to be tested;
[0167] The first parsing module is used to parse the syntax structure of each code file in the target program to obtain auxiliary information;
[0168] A first screening module, used for screening auxiliary information based on target information type;
[0169] The determination module is used to obtain candidate code information based on the information obtained by screening.
[0170] Optionally, the first screening module is used to screen the auxiliary information based on the information type of the function dimension and the information type of the class dimension included in the target information type.
[0171] Optionally, the determination module is used to: arrange the information of the class dimension in the information obtained by filtering according to the format requirements of the class dimension, and / or arrange the information of the function dimension in the information obtained by filtering according to the format requirements of the function dimension, to obtain structured alternative code information; wherein the structured information is information that meets the format requirements.
[0172] Optionally, the code generating device further includes:
[0173] The second parsing module is used to parse the grammatical structure of the updated code file in the target program to obtain update auxiliary information when the target program is updated;
[0174] The second screening module is used to screen the update auxiliary information based on the target information type, and update the candidate code information based on the screened information.
[0175] Optionally, the retrieval module 1002 is used to retrieve information of the logic unit indicated by the unit dependency relationship and the information of the unit to be tested from the candidate code information based on the unit dependency relationship of the unit to be tested, and obtain the target code information.
[0176] Optionally, the code generation module is used to: input the target code information into the code generation model to obtain the unit test code for the unit to be tested output by the code generation model; wherein the code generation model is trained based on the sample code information and unit test code corresponding to multiple first sample units, and the sample code information includes: the code information of the unit having a dependency relationship with the first sample unit in the reference code information corresponding to the first sample program to which the first sample unit belongs; the reference code information corresponding to the first sample program is obtained by screening the reference information based on the target information type, and the reference information is obtained by performing a grammatical structure analysis on the code file of the first sample program.
[0177] Optionally, the code generating device further includes:
[0178] A second acquisition module is used to acquire a first training sample and a first training label before inputting the target code information into the code generation model to obtain the unit test code for the unit to be tested output by the code generation model, wherein the first training sample includes sample code information corresponding to the first sample unit in the first sample program, and the first training label is the unit test code of the first sample unit;
[0179] A first prediction module, used to input the first training sample into the initial code generation model to predict the reference unit test code corresponding to the first sample unit;
[0180] The first adjustment module is used to adjust the initial code generation model based on the comparison result between the reference unit test code and the unit test code in the first training tag to obtain a trained code generation model.
[0181] Optionally, the code generating device further includes:
[0182] A compiling module, for generating unit test code of the unit to be tested based on the target code information, and then compiling the unit test code to obtain a compilation result;
[0183] A first search module, configured to search for first supplementary information in the candidate code information based on the first error information when the compilation result includes first error information; wherein the first error information indicates that an error exists in the compilation process of the unit test code;
[0184] A first calling module is used to call the code modification model to process the unit test code, the first error information and the first supplementary information, and update the unit test code;
[0185] The first execution module is used to re-compile the unit test code based on the updated unit test code to obtain the compilation result and subsequent steps until it is determined that the first stop modification condition is met; wherein the first stop modification condition includes: the compilation result does not include the first error information, and / or the number of times the unit test code is updated exceeds a threshold number.
[0186] Optionally, the first calling module is used to: input the unit test code, the first error information and the first supplementary information into the code modification model to obtain a modification method for the unit test code output by the code modification model; and call a file modification tool to update the unit test code.
[0187] Optionally, the code generating device further includes:
[0188] A running module is used to run the target unit test code and obtain the running result; wherein there is no error in the compilation process of the target unit test code;
[0189] A second search module is used to search for second supplementary information in the candidate code information based on the second error information when the running result includes second error information; wherein the second error information indicates that there is an error in the running process of the target unit test code;
[0190] A second calling module is used to call the code modification model to process the target unit test code, the second error information and the second supplementary information, and update the target unit test code;
[0191] The second execution module is used to re-execute the target unit test code based on the updated target unit test code to obtain the running result and subsequent steps until it is determined that the second stop modification condition is met; wherein the second stop modification condition includes: there is no error in the running result, and / or the number of times the target unit test code is updated exceeds a threshold number.
[0192] Optionally, the code generating device further includes:
[0193] a third acquisition module, for acquiring a second training sample and a second training label before calling the code modification model to process the unit test code, the first error information and the first supplementary information and updating the unit test code, wherein the second training sample includes the sample test code, the sample error information and the sample supplementary information of the second sample unit in the second sample program, the sample error information indicates that there is an error in the compilation process of the second sample test code, the sample supplementary information is obtained by searching in the reference code information corresponding to the second sample program based on the sample error information, the reference code information corresponding to the second sample program is obtained by screening the reference information based on the target information type, the reference information is obtained by parsing the grammatical structure of each code file in the second sample program, and the second training label is a modification method for the sample test code;
[0194] A second prediction module, used for inputting the second training sample into the initial code modification model to predict a reference modification method corresponding to the second training sample;
[0195] The second adjustment module is used to adjust the initial code modification model based on the comparison result between the reference modification method and the modification method in the second training label to obtain the trained code modification model.
[0196] Corresponding to the above method embodiment, this specification also provides a training device embodiment of a code generation model, Fig.11 1 is a schematic diagram of a structure of a code generation model training device provided in an embodiment of this specification. Fig.11 As shown, the device comprises:
[0197] The first acquisition module 1102 is used to acquire training samples and training labels, wherein the training samples include sample code information corresponding to the sample unit in the sample program, and the sample code information includes: code information of units having a dependency relationship with the sample unit in the reference code information corresponding to the sample program; the reference code information is obtained by screening the reference information based on the target information type, the reference information is obtained by parsing the grammatical structure of each code file in the sample program, the target information type is the type of information used for unit testing, and the training label is the unit test code of the sample unit;
[0198] The first prediction module 1104 is used to input the training sample into the initial code generation model to predict the reference unit test code corresponding to the sample unit;
[0199] The first adjustment module 1106 is used to adjust the initial code generation model based on the comparison result between the reference unit test code and the unit test code in the training tag to obtain a trained code generation model.
[0200] Corresponding to the above method embodiment, this specification also provides a training device embodiment of a code modification model, Fig.12 1 is a schematic diagram of a structure of a code modification model training device provided in an embodiment of this specification. Fig.12 As shown, the device comprises:
[0201] The second acquisition module 1202 is used to acquire training samples and training labels, wherein the training samples include sample test codes, sample error information and sample supplementary information of the sample unit, the sample error information indicates that there is an error in the compilation process of the sample test code, the sample supplementary information is obtained by searching in the reference code information corresponding to the sample program to which the sample unit belongs based on the sample error information, the reference code information corresponding to the sample program is obtained by screening the reference information based on the target information type, the reference information is obtained by parsing the grammatical structure of each code file in the sample program, the target information type is the type of information used for unit testing, and the training label is a modification method for the sample test code;
[0202] The second prediction module 1204 is used to predict the reference modification mode corresponding to the training sample by using the initial code modification model;
[0203] The second adjustment module 1206 is used to adjust the initial code modification model based on the comparison result between the reference modification method and the modification method in the training label to obtain a trained code modification model; wherein the code modification model is used to modify the unit test code with problems.
[0204] The above-mentioned training device for the code generation model and the training device for the code modification model belong to the same concept as the technical solution of the above-mentioned training method for the code generation model, and the technical solution of the training device can refer to the description of the technical solution of the above-mentioned training method. The first acquisition module 1102, the first prediction module 1104 and the first adjustment module 1106 in the above-mentioned training device for the code generation model can refer to the relevant introduction of the second acquisition module, the first prediction module and the first adjustment module in the code generation device, respectively. The second acquisition module 1202, the second prediction module 1204 and the second adjustment module 1206 in the above-mentioned training device for the code modification model can refer to the relevant introduction of the third acquisition module, the second prediction module and the second adjustment module in the code generation device, respectively.
[0205] Corresponding to the above method embodiment, this specification also provides another code generation device embodiment, Fig.13 Schematic diagram of another code generation device provided in an embodiment of this specification. Fig.13 As shown, the code generating device comprises:
[0206] The receiving module 1302 is used to receive a code generation request uploaded by a front-end device for a unit under test in a target program;
[0207] A retrieval module 1304 is used to retrieve target code information from candidate code information based on the unit dependency of the unit to be tested in response to the code generation request; wherein the candidate code information is obtained by screening auxiliary information based on the target information type, the auxiliary information is obtained by parsing the syntax structure of each code file in the target program, and the target information type is the type of information used for unit testing;
[0208] An input module 1306 is used to input the target code information into the code generation model to obtain the unit test code output by the code generation model for the unit to be tested;
[0209] The sending module 1308 is used to send the unit test code to the front-end device, so that the front-end device can run the unit test code to test the unit to be tested.
[0210] Optionally, the code generation apparatus further includes: an adjustment module for adjusting the code generation model based on the problem feedback information sent by the front-end device for the unit test code after sending the unit test code to the front-end device and upon receiving the problem feedback information sent by the front-end device for the unit test code.
[0211] Fig.1414 shows a block diagram of a computing device provided according to an embodiment of the present specification. The components of the computing device 1400 include, but are not limited to, a memory 1410 and a processor 1420. The processor 1420 is connected to the memory 1410 via a bus 1430, and a database 1450 is used to store data. The computing device 1400 also includes an access device 1440, which enables the computing device 1400 to communicate via one or more networks 1460. Examples of these networks include a public switched telephone network (PSTN), a local area network (LAN), a wide area network (WAN), a personal area network (PAN), or a combination of communication networks such as the Internet. The access device 1440 may include one or more of any type of network interface, wired or wireless (e.g., a network interface card (NIC)), such as an IEEE802.11 wireless local area network (WLAN) wireless interface, a Worldwide Interoperability for Microwave Access (Wi-MAX) interface, an Ethernet interface, a Universal Serial Bus (USB) interface, a cellular network interface, a Bluetooth interface, and a Near Field Communication (NFC).
[0212] In one embodiment of the present specification, the above components of the computing device 1400 and Fig.14 Other components not shown in the figure may also be connected to each other, for example, via a bus. It should be understood that Fig.14 The computing device structure block diagram shown is only for the purpose of illustration, and is not intended to limit the scope of this specification. Those skilled in the art can add or replace other components as needed.
[0213] The computing device 1400 may be any type of stationary or mobile computing device, including a mobile computer or mobile computing device (e.g., a tablet computer, a personal digital assistant, a laptop computer, a notebook computer, a netbook, etc.), a mobile phone (e.g., a smart phone), a wearable computing device (e.g., a smart watch, smart glasses, etc.), or other types of mobile devices, or a stationary computing device such as a desktop computer or a personal computer (PC). The computing device 1400 may also be a mobile or stationary server. The memory 1410 is used to store computer programs / instructions, and the processor 1420 is used to execute the computer programs / instructions stored in the memory 1410. When the computer program / instructions are executed by the processor, the steps of the above-mentioned code generation method, the code generation model training method, and the code modification model training method are implemented.
[0214] The above is a schematic scheme of a computing device of this embodiment. It should be noted that the technical scheme of the computing device and the technical schemes of the above-mentioned code generation method, code generation model training method, and code modification model training method belong to the same concept, and the details not described in detail in the technical scheme of the computing device can be referred to the description of the technical schemes of the above-mentioned code generation method, code generation model training method, and code modification model training method.
[0215] An embodiment of this specification also provides a computer-readable storage medium, which stores a computer program / instruction, and when the computer program / instruction is executed by a processor, it implements the steps of the above-mentioned code generation method, the training method of the code generation model, and the training method of the code modification model. The above is a schematic scheme of a computer-readable storage medium of this embodiment. It should be noted that the technical scheme of the storage medium and the technical scheme of the above-mentioned code generation method, the training method of the code generation model, and the training method of the code modification model belong to the same concept. The details not described in detail in the technical scheme of the storage medium can be found in the description of the technical scheme of the above-mentioned code generation method, the training method of the code generation model, and the training method of the code modification model.
[0216] An embodiment of this specification also provides a computer program product, including a computer program / instruction, which implements the steps of the above-mentioned code generation method, the training method of the code generation model, and the training method of the code modification model when executed by the processor. The above is a schematic scheme of a computer program product of this embodiment. It should be noted that the technical scheme of the computer program product and the technical scheme of the above-mentioned code generation method, the training method of the code generation model, and the training method of the code modification model belong to the same concept. The details not described in detail in the technical scheme of the computer program product can be found in the description of the technical scheme of the above-mentioned code generation method, the training method of the code generation model, and the training method of the code modification model.
[0217] The above is a description of a specific embodiment of the specification. Other embodiments are within the scope of the appended claims. In some cases, the actions or steps recorded in the claims can be performed in an order different from that in the embodiments and still achieve the desired results. In addition, the processes depicted in the drawings do not necessarily require the specific order or continuous order shown to achieve the desired results. In some embodiments, multitasking and parallel processing are also possible or may be advantageous.
[0218] Computer programs / instructions include computer program codes, which may be in source code form, object code form, executable files or some intermediate forms, etc. Computer readable media may include: any entity or device capable of carrying computer program codes, recording media, USB flash drives, mobile hard disks, magnetic disks, optical disks, computer memories, read-only memories (ROM), random access memories (RAM), electric carrier signals, telecommunication signals, and software distribution media, etc. It should be noted that the contents of computer readable media may be appropriately increased or decreased according to the requirements of patent practice. For example, in some regions, according to patent practice, computer readable media do not include electric carrier signals and telecommunication signals.
[0219] It should be noted that, for the aforementioned method embodiments, for the sake of simplicity of description, they are all expressed as a series of action combinations, but those skilled in the art should be aware that the embodiments of this specification are not limited by the described order of actions, because according to the embodiments of this specification, certain steps can be performed in other orders or simultaneously. Secondly, those skilled in the art should also be aware that the embodiments described in the specification are all preferred embodiments, and the actions and modules involved are not necessarily required by the embodiments of this specification. In the above embodiments, the description of each embodiment has its own emphasis. For parts that are not described in detail in a certain embodiment, please refer to the relevant description of other embodiments.
[0220] The preferred embodiments of this specification disclosed above are only used to help explain this specification. The optional embodiments do not describe all the details in detail, nor do they limit the invention to only the specific implementation methods described. Obviously, many modifications and changes can be made according to the content of the embodiments of this specification. This specification selects and specifically describes these embodiments in order to better explain the principles and practical applications of the embodiments of this specification, so that technicians in the relevant technical field can well understand and use this specification. This specification is only limited by the claims and their full scope and equivalents.
Claims
1. A code generation method, comprising: For a unit to be tested in a target program, based on the unit dependency of the unit to be tested, target code information is retrieved from candidate code information; wherein the candidate code information is structured information that meets format requirements and is obtained by screening auxiliary information based on a target information type; the auxiliary information is obtained by parsing the grammatical structure of a code file of the target program, the target information type is the type of information used for unit testing, and the target information type includes an information type of a function dimension and an information type of a class dimension; the format requirements include setting corresponding index paths for information of a class dimension and information of a function dimension, respectively, and arranging and storing each information in the form of a key-value pair; Based on the target code information, a unit test code of the unit to be tested is generated.
2. The method according to claim 1, before retrieving the target code information from the candidate code information for the unit to be tested in the target program based on the unit dependency relationship of the unit to be tested, further comprising: Acquire the target program including at least one code file; For each code file in the target program, grammatical structure analysis is performed to obtain the auxiliary information; Based on the target information type, the auxiliary information is screened; Based on the information obtained by screening, the candidate code information is obtained.
3. The method according to claim 2, wherein obtaining the candidate code information based on the information obtained by screening comprises: The information of the class dimension in the filtered information is arranged according to the format requirements of the class dimension, and / or the information of the function dimension in the filtered information is arranged according to the format requirements of the function dimension, so as to obtain the structured candidate code information.
4. The method according to claim 2, further comprising: When the target program is updated, performing grammatical structure analysis on the updated code file in the target program to obtain update auxiliary information; Based on the target information type, the update auxiliary information is screened, and the candidate code information is updated based on the screened information.
5. The method according to any one of claims 1 to 4, wherein the step of retrieving target code information from candidate code information based on the unit dependency of the unit under test comprises: Based on the unit dependency of the unit to be tested, information of the logic unit indicated by the unit dependency and information of the unit to be tested are retrieved from the candidate code information to obtain target code information.
6. The method according to any one of claims 1 to 4, wherein generating the unit test code of the unit under test based on the target code information comprises: Inputting the target code information into a code generation model to obtain a unit test code output by the code generation model for the unit to be tested; Among them, the code generation model is obtained through training based on sample code information and unit test code corresponding to multiple first sample units, and the sample code information includes: code information of a unit having a dependency relationship with the first sample unit in the reference code information corresponding to the first sample program to which the first sample unit belongs; the reference code information corresponding to the first sample program is structured information that meets the format requirements and is obtained by screening the reference information based on the target information type; the reference information is obtained by performing grammatical structure analysis on the code file of the first sample program.
7. The method according to claim 6, before inputting the target code information into a code generation model to obtain the unit test code output by the code generation model for the unit under test, further comprising: Acquire a first training sample and a first training label, wherein the first training sample includes sample code information corresponding to a first sample unit in a first sample program, and the first training label is a unit test code of the first sample unit; Inputting the first training sample into an initial code generation model to predict a reference unit test code corresponding to the first sample unit; Based on the comparison result between the reference unit test code and the unit test code in the first training tag, the initial code generation model is adjusted to obtain a trained code generation model.
8. The method according to any one of claims 1 to 4, after generating the unit test code of the unit under test based on the target code information, further comprising: Compiling the unit test code to obtain a compilation result; In the case where the compilation result includes first error information, searching for first supplementary information in the candidate code information based on the first error information; wherein the first error information indicates that there is an error in the compilation process for the unit test code; Calling a code modification model to process the unit test code, the first error information, and the first supplementary information, and updating the unit test code; Based on the updated unit test code, re-execute the compilation of the unit test code to obtain the compilation result and subsequent steps, until it is determined that the first stop modification condition is met; wherein the first stop modification condition includes: the compilation result does not include the first error information, and / or the number of times the unit test code is updated exceeds a threshold number of times.
9. The method according to claim 8, wherein the calling code modification model processes the unit test code, the first error information and the first supplementary information to update the unit test code, comprising: Inputting the unit test code, the first error information and the first supplementary information into the code modification model to obtain a modification method for the unit test code output by the code modification model; A file modification tool is called to update the unit test code based on the modification method.
10. The method according to claim 8, further comprising: Run the target unit test code to obtain a running result; wherein there are no errors in the compilation process of the target unit test code; In the case where the running result includes second error information, searching for second supplementary information in the candidate code information based on the second error information; wherein the second error information indicates that there is an error in the running process of the target unit test code; Calling a code modification model to process the target unit test code, the second error information and the second supplementary information, and updating the target unit test code; Based on the updated target unit test code, re-execute the running target unit test code to obtain the running result and subsequent steps until it is determined that the second stop modification condition is met; wherein the second stop modification condition includes: there is no error in the running result, and / or the number of times the target unit test code is updated exceeds a threshold number.
11. The method according to claim 8, wherein the calling code modification model processes the unit test code, the first error information and the first supplementary information, and before updating the unit test code, further comprises: Obtain a second training sample and a second training label, wherein the second training sample includes a sample test code, sample error information, and sample supplementary information of a second sample unit in a second sample program, the sample error information indicates that an error exists in the compilation process of the sample test code, the sample supplementary information is obtained by searching in reference code information corresponding to the second sample program based on the sample error information, the reference code information corresponding to the second sample program is structured information that meets the format requirements and is obtained by screening reference information based on the target information type; the reference information is obtained by performing grammatical structure analysis on each code file in the second sample program, and the second training label is a modification method for the sample test code; Inputting the second training sample into an initial code modification model to predict a reference modification method corresponding to the second training sample; Based on the comparison result between the reference modification method and the modification method in the second training label, the initial code modification model is adjusted to obtain a trained code modification model.
12. A code generation method, comprising: Receiving a code generation request uploaded by a front-end device for a unit under test in a target program; In response to the code generation request, based on the unit dependency of the unit to be tested, the target code information is retrieved from the candidate code information; wherein the candidate code information is structured information that meets the format requirements and is obtained by screening the auxiliary information based on the target information type; the auxiliary information is obtained by parsing the syntax structure of each code file in the target program, the target information type is the type of information used for unit testing, and the target information type includes the information type of the function dimension and the information type of the class dimension; the format requirements include setting corresponding index paths for the information of the class dimension and the information of the function dimension, respectively, and arranging and storing each information in the form of key-value pairs; Inputting the target code information into a code generation model to obtain a unit test code output by the code generation model for the unit to be tested; The unit test code is sent to the front-end device, so that the front-end device runs the unit test code to test the unit to be tested.
13. The method according to claim 12, after sending the unit test code to the front-end device, the method further comprises: When problem feedback information sent by the front-end device for the unit test code is received, the code generation model is adjusted based on the problem feedback information.
14. A method for training a code generation model, comprising: Acquire training samples and training labels, wherein the training samples include sample code information corresponding to sample units in a sample program, and the sample code information includes: code information of units having a dependency relationship with the sample units in reference code information corresponding to the sample program; the reference code information is structured information that meets format requirements and is obtained by screening reference information based on a target information type; the reference information is obtained by parsing the syntax structure of each code file in the sample program, and the target information type is the type of information used for unit testing, and the target information type includes information types of function dimensions and information types of class dimensions; the format requirements include setting corresponding index paths for information of class dimensions and information of function dimensions, respectively, and arranging and storing each information in a key-value pair manner, and the training label is the unit test code of the sample unit; Input the training sample into an initial code generation model to predict the reference unit test code corresponding to the sample unit; Based on the comparison result between the reference unit test code and the unit test code in the training tag, the initial code generation model is adjusted to obtain a trained code generation model.
15. A method for training a code modification model, comprising: Acquire training samples and training labels, wherein the training samples include sample test code, sample error information and sample supplementary information of a sample unit, wherein the sample error information indicates that there is an error in the compilation process of the sample test code, and the sample supplementary information is obtained by searching in reference code information corresponding to a sample program to which the sample unit belongs based on the sample error information, and the reference code information corresponding to the sample program is structured information that meets format requirements and is obtained by screening reference information based on a target information type; the reference information is obtained by performing grammatical structure parsing on each code file in the sample program, the target information type is the type of information used for unit testing, and the target information type includes information types of function dimensions and information types of class dimensions; the format requirements include setting corresponding index paths for information of class dimensions and information of function dimensions, respectively, and arranging and storing each information in the form of key-value pairs, and the training label is a modification method for the sample test code; Using the initial code modification model, predicting a reference modification method corresponding to the training sample; Based on the comparison result between the reference modification method and the modification method in the training label, the initial code modification model is adjusted to obtain a trained code modification model; wherein the code modification model is used to modify the unit test code with problems.
16. A code generation method, applied to a task platform, comprising: Receiving a model request sent by a terminal device, wherein the model request includes at least one of a scene identifier of a target scene, scene input data of the target scene, and a target model specification parameter; Based on the model request, a corresponding code generation model and / or code modification model is determined from at least one model; wherein the code generation model is obtained by training based on the training method as described in claim 14, and the code modification model is obtained by training based on the training method as described in claim 15, and the code generation model and / or the code modification model is used to generate unit test code.
17. The method according to claim 16, wherein the model library stores at least one code generation model and code modification model adapted to different scenarios, and a plurality of code generation models and code modification models with different model specification parameters; and the determining the corresponding code generation model and / or code modification model from at least one model based on the model request comprises: Based on the at least one item of information in the model request, searching the model library for a code generation model and / or a code modification model corresponding to the at least one item of information; wherein the scene identifier of the target scene and the scene input data of the target scene both correspond to a model adapted to the target scene, and the target model specification parameters correspond to a model having the same model specification parameters as the target model specification parameters; In a case where the at least one item of information includes scenario input data of the target scenario, the found code generation model and / or code modification model is trained based on the scenario input data of the target scenario to obtain a trained code generation model and / or code modification model.
18. The method according to claim 16 or 17, after determining a corresponding code generation model and / or code modification model from at least one model based on the model request, further comprising: Deploy the code generation model, and construct a code generation interface based on the code generation model, so that the terminal device schedules the code generation model through the code generation interface to generate unit test code for the unit to be tested in the target program; And / or, deploy the code modification model, and construct a code modification interface based on the code modification model, so that the terminal device schedules the code modification model through the code modification interface to modify the unit test code of the unit to be tested in the target program.
19. A task platform, comprising a request interface and a response unit; The request interface is used to receive a model request for unit testing sent by a terminal device, wherein: The model request includes at least one of a scene identifier of a target scene, scene input data of the target scene, and a model specification parameter; The response unit is used to determine a corresponding code generation model and / or code modification model from at least one model based on the model request, wherein the code generation model is trained based on the training method as described in claim 14, and the code modification model is trained based on the training method as described in claim 15.
20. The task platform according to claim 19, further comprising a code generation interface, wherein the code generation interface is constructed based on the code generation model; the code generation interface is used for scheduling by the terminal device to generate unit test code based on code information associated with the unit to be tested in the target program; And / or, it also includes a code modification interface, which is constructed based on a code modification model; the code modification interface is used for the terminal device to schedule and modify the unit test code.
21. A computing device comprising: Memory and processor; The memory is used to store computer programs / instructions, and the processor is used to execute the computer programs / instructions. When the computer programs / instructions are executed by the processor, the steps of the method described in any one of claims 1 to 18 are implemented.
22. A computer-readable storage medium storing a computer program / instruction, wherein the computer program / instruction, when executed by a processor, implements the steps of the method according to any one of claims 1 to 18.
23. A computer program product, comprising a computer program / instructions, which, when executed by a processor, implement the steps of the method according to any one of claims 1 to 18.
Citation Information
Patent Citations
Test processing method and system
CN118051429A
Code generation method and device based on cloud service
CN118092923A
Method and device for automatically generating unit test based on thinking chain prompt
CN118503125A
Code processing method and code repair testing method
CN118760612A