Code generation method, computing device, electronic device and storage medium

By parsing changes in the code base and generating target test code, the problem of inefficiency of code testing is solved, and more efficient test coverage and automation is achieved.

CN119883939AInactive Publication Date: 2025-04-25ALIBABA (CHINA) CO LTD
View PDF 5 Cites 0 Cited by

Patent Information

Application Number
CN202510387157.2
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-03-28
Publication Date
2025-04-25
Estimated Expiration
Not applicable · inactive patent

AI Technical Summary

Technical Problem

The code test writing process is highly dependent on manual labor, resulting in inefficient testing, and existing automation tools are difficult to capture the deep logic and boundary conditions of the code, resulting in low test coverage.

Method used

By monitoring changes in the code base, the target source code is parsed, its syntax structure information is obtained, and the target test code is generated based on this to integrate into the main branch of the source code.

Benefits of technology

It realizes more efficient test code generation, ensuring that the test code can accurately cover all parts of the source code, including boundary conditions and exception handling, replacing the process of manually writing test code.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119883939A_ABST
    Figure CN119883939A_ABST
Patent Text Reader

Abstract

The invention discloses a code generation method, computing equipment, electronic equipment and a storage medium, and relates to the field of large model technologies and software engineering. The method comprises the steps that in response to monitoring that a target source code in a code library is changed, the target source code is analyzed, grammar structure information of the target source code is obtained, and the grammar structure information comprises an internal structure and an external interface of the target source code; generating a target test code corresponding to the target source code based on the target source code and the grammar structure information; and integrating the test code to the main branch corresponding to the target source code. The technical problem that the source code testing efficiency is low is solved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the fields of large model technology and software engineering, and more specifically, to a code generation method, a computing device, an electronic device and a storage medium. Background Art

[0002] In the life cycle of software development, code testing plays a vital role. Code testing can effectively detect and prevent code defects and improve software quality.

[0003] However, the code test writing process is highly dependent on manual work. Developers need to spend a lot of time and energy to understand the code logic and build test cases. In addition, the code changes frequently. After each code update, the test code needs to be adjusted to ensure the synchronization of the test code with the source code. Therefore, the efficiency of source code testing is relatively low. Even if rule-based automated testing tools and code analyzers are applied, it is difficult to capture the deep logic and boundary conditions of the code, resulting in low coverage of the generated test code, and the efficiency of source code testing is still difficult to improve.

[0004] To address the above-mentioned problems, no effective solution has been proposed yet. Summary of the invention

[0005] The embodiments of the present application provide a code generation method, a computing device, an electronic device, and a storage medium to at least solve the technical problem of low efficiency in source code testing.

[0006] According to one aspect of an embodiment of the present application, a code generation method is provided, comprising: in response to monitoring a change in a target source code in a code base, parsing the target source code to obtain grammatical structure information of the target source code, wherein the grammatical structure information includes an internal structure and an external interface of the target source code; based on the target source code and the grammatical structure information, generating a target test code corresponding to the target source code; and integrating the target test code into a main branch corresponding to the target source code.

[0007] According to another aspect of an embodiment of the present application, a code generation method is also provided, including: obtaining source code in a code library by calling a first interface, wherein the first interface includes a first parameter, and the parameter value of the first parameter includes the source code; in response to a change in a target source code in the source code, parsing the target source code to obtain grammatical structure information of the target source code, wherein the grammatical structure information includes an internal structure and an external interface of the target source code; based on the target source code and the grammatical structure information, generating a target test code corresponding to the target source code; integrating the target test code into a main branch corresponding to the target source code; outputting the main branch by calling a second interface, wherein the second interface includes a second parameter, and the parameter value of the second parameter includes the main branch.

[0008] According to another aspect of an embodiment of the present application, a code generation method is also provided, including: in response to a code submission instruction acting on an operation interface, monitoring whether the source code in the code library has changed; in response to monitoring that the target source code has changed, parsing the target source code to obtain grammatical structure information of the target source code, wherein the grammatical structure information includes the internal structure and external interface of the target source code; based on the target source code and the grammatical structure information, generating a target test code corresponding to the target source code, and displaying the target test code on the operation interface; in response to a code confirmation instruction acting on the operation interface, integrating the target test code into the main branch corresponding to the target source code.

[0009] According to another aspect of the embodiments of the present application, a computing device is further provided, including: a memory storing an executable program; and a processor for running the program, wherein the method in each embodiment of the present application is executed when the program is running.

[0010] According to another aspect of the embodiments of the present application, an electronic device is also provided, including: a memory storing an executable program; a processor connected to the memory via a bus and used to run the program, wherein the method of each embodiment of the present application is executed when the program is running.

[0011] According to another aspect of an embodiment of the present application, a computer-readable storage medium is also provided, the computer-readable storage medium including a stored executable program, wherein when the executable program is running, the device where the computer-readable storage medium is located is controlled to execute the methods in each embodiment of the present application.

[0012] According to another aspect of the embodiments of the present application, a computer program product is also provided, including a computer program, and when the computer program is executed by a processor, the method in each embodiment of the present application is implemented.

[0013] According to another aspect of an embodiment of the present application, a computer program product is also provided, including a non-volatile computer-readable storage medium, wherein the non-volatile computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the method in each embodiment of the present application is implemented.

[0014] According to another aspect of the embodiments of the present application, a computer program is also provided, and when the computer program is executed by a processor, the methods in the various embodiments of the present application are implemented.

[0015] In an embodiment of the present application, in response to monitoring changes in the target source code in the code base, the target source code is parsed to obtain the grammatical structure information of the target source code; based on the target source code and the grammatical structure information, the target test code corresponding to the target source code is generated; and the test code is integrated into the main branch corresponding to the target source code. It is easy to notice that after monitoring the changes in the code base, the source code parsing and test code generation process is started, and the specific logic of the target source code and the interaction method with the external environment can be obtained by parsing the target source code and obtaining the grammatical structure information of the target source code. The target test code is generated based on the target source code and the grammatical structure information, ensuring that the target test code can more accurately target each part of the target source code, covering more boundary conditions and exception handling, while replacing the process of developers manually writing test code, improving the generation efficiency of the target test code, thereby achieving the technical effect of improving the efficiency of source code testing, and then solving the technical problem of low source code testing efficiency.

[0016] It is easy to notice that the above general description and the following detailed description are only for the purpose of exemplifying and explaining the present application, and do not constitute a limitation of the present application. BRIEF DESCRIPTION OF THE DRAWINGS

[0017] The drawings described herein are used to provide a further understanding of the present application and constitute a part of the present application. The illustrative embodiments of the present application and their descriptions are used to explain the present application and do not constitute an improper limitation on the present application. In the drawings:

[0018] Figure 1 is a schematic diagram of an application scenario of a code generation method according to an embodiment of the present application;

[0019] Figure 2 is a flow chart of a code generation method according to an embodiment of the present application;

[0020] Figure 3 is a schematic diagram of an optional generation of sub-test codes according to an embodiment of the present application;

[0021] Figure 4 is an optional flow chart of generating unit test code according to an embodiment of the present application;

[0022] Figure 5 is a schematic diagram of an optional generation of unit test code according to an embodiment of the present application;

[0023] Figure 6 is a flowchart of another code generation method according to an embodiment of the present application;

[0024] Figure 7 is a flowchart of another code generation method according to an embodiment of the present application;

[0025] Figure 8 is a structural block diagram of a computing device according to an embodiment of the present application;

[0026] Fig. 9 It is a structural block diagram of an electronic device according to an embodiment of the present application. DETAILED DESCRIPTION

[0027] In order to enable those skilled in the art to better understand the solution of the present application, the technical solution in the embodiments of the present application will be clearly and completely described below in conjunction with the drawings in the embodiments of the present application. Obviously, the described embodiments are only part of the embodiments of the present application, not all of the embodiments. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in this field without creative work should fall within the scope of protection of the present application.

[0028] It should be noted that the terms "first", "second", etc. in the specification and claims of the present application and the above-mentioned drawings are used to distinguish similar objects, and are not necessarily used to describe a specific order or sequence. It should be understood that the data used in this way can be interchangeable where appropriate, so that the embodiments of the present application described herein can be implemented in an order other than those illustrated or described herein. In addition, the terms "including" and "having" and any of their variations are intended to cover non-exclusive inclusions, for example, a process, method, system, product or device comprising a series of steps or units is not necessarily limited to those steps or units clearly listed, but may include other steps or units that are not clearly listed or inherent to these processes, methods, products or devices.

[0029] The technical solution provided in this application is mainly implemented using large-scale model technology. The large model here refers to a deep learning model with large-scale model parameters, which can usually contain hundreds of millions, tens of billions, hundreds of billions, trillions or even more than 10 trillion model parameters. The 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 large-scale language model (LLM), multi-modal pre-training model, etc.

[0030] It should be noted that when the large model is actually applied, the pre-trained model can be fine-tuned through a small number of samples, so that the large model can be applied to different tasks. For example, the large model can be widely used in natural language processing (NLP), computer vision, speech processing and other fields. Specifically, it can be applied to computer vision tasks such as visual question answering (VQA), image caption (IC), image generation, etc. It can also be widely used in natural language processing tasks such as text-based sentiment classification, text summary generation, machine translation, etc. Therefore, the main application scenarios of the large model include but are not limited to digital assistants, intelligent robots, search, online education, office software, e-commerce, intelligent design, etc. In the embodiment of the present application, data processing through a large language model in a code parsing scenario is taken as an example for explanation,

[0031] First, some nouns or terms that appear in the description of the embodiments of the present application are subject to the following explanations:

[0032] Unit testing: refers to the inspection and verification of the smallest program logic unit in the software.

[0033] AST (Abstract Syntax Tree): Abstract syntax tree is an abstract representation of the grammatical structure of the source code, which represents the grammatical structure of the programming language in a tree form.

[0034] IDE (Integrated Development Environment): An integrated development environment 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.

[0035] CI (Continuous Integration): Continuous integration is a software development practice that aims to ensure the quality and stability of code by frequently merging code changes into a shared main branch and automatically running builds and tests.

[0036] prompt: prompt word, which is the context information and task requirement information input into the model.

[0037] According to an embodiment of the present application, a code generation method is provided. It should be noted that the steps shown in the flowchart of the accompanying drawings can be executed in a computer system such as a set of computer executable instructions, and although a logical order is shown in the flowchart, in some cases, the steps shown or described can be executed in an order different from that shown here.

[0038] Considering the huge number of model parameters of the large model and the limited computing resources of the mobile terminal, the above method provided in the embodiment of the present application can be applied to Figure 1 The application scenarios shown are not limited to these. Figure 1 In 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, thereby implementing the method provided in the embodiment of the present application.

[0039] In an embodiment of the present application, a system composed of a client device and a server can perform the following steps: the client device executes uploading the target source code to the code library. The server executes, in response to monitoring the change of the target source code in the code library, parsing the target source code to obtain the grammatical structure information of the target source code; based on the target source code and the grammatical structure information, generating the target test code corresponding to the target source code; integrating the test code into the main branch corresponding to the target source code.

[0040] It should be noted that, with the rapid development of high-performance computing units, in other application scenarios, the above method provided in the embodiment of the present application can also be applied to the model all-in-one machine. In an optional embodiment, a plurality of models are built into the model all-in-one machine, and the user can choose to adjust a model as needed to obtain the user's own model, so that the high-performance computing unit built into the model all-in-one machine can directly call the adjusted model to execute the above method provided in the embodiment of the present application. In another optional embodiment, a trained model is built into the large model all-in-one machine, so that the high-performance computing unit built into the model all-in-one machine can directly call the model to execute the above method provided in the embodiment of the present application.

[0041] Furthermore, when users need to train their own models, they can also upload their own data sets through the client, which are sent by the client to the server, so that the server can adjust the pre-trained model with the data set to obtain the user's own model and then deploy it to the production environment. In order to facilitate users' needs for model adjustment, the server can provide complete adjustment tools, development frameworks and processes, and can support multiple adjustment strategies, so that the adjusted model can better adapt to applications in different fields and achieve a high degree of customization.

[0042] Under the above operating environment, this application provides Figure 2The code generation method shown. Figure 2 is a flow chart of a code generation method according to an embodiment of the present application. Figure 2 As shown, the method may include the following steps:

[0043] Step S202 , in response to detecting a change in the target source code in the code base, the target source code is parsed to obtain grammatical structure information of the target source code, wherein the grammatical structure information includes an internal structure and an external interface of the target source code.

[0044] The code repository in the above steps is a system for storing and managing the source code of software projects. It is used to record the various source code versions in the software development process and the differences between each source code version.

[0045] The target source code in the above steps is the source code portion for which test code needs to be generated.

[0046] The grammatical structure information in the above steps refers to the structured representation of the target source code, which can be represented in the form of AST. The grammatical structure information can reflect the grammatical composition, function call relationship, variable type and scope of the target source code. The grammatical structure information can be represented in the form of AST.

[0047] The internal structure in the above steps is the structure inside the target source code, which may include but is not limited to the definition and relationship of functions, classes, variables, and the control flow of the code, etc. The control flow of the code may include conditional structures, loops, etc., but is not limited thereto.

[0048] The external interface in the above steps is the interaction point between the target source code and other external modules or libraries, which may include but is not limited to called external functions, dependent classes and interfaces, etc.

[0049] In an optional embodiment, the CI system can detect whether the target source code in the code base has changed. When the target source code in the code base has a version update or new content is added, the CI system can detect the change of the target source code. After detecting that the target source code has changed, the acquisition and parsing of the target source code is triggered.

[0050] The target source code in the code base can be read by an automated tool, and the grammatical structure of the target source code can be parsed to generate AST, thereby obtaining grammatical structure information. The automated tool can be ESLint (a static code analyzer) or SonarQube (a code quality management tool), but is not limited thereto.

[0051] In another optional embodiment, Webhooks (a mechanism for implementing real-time communication and event-driven) can be configured in the code base, and changes in the target source code in the code base can be monitored through Webhooks. Then a real-time syntax analysis service is built, and notifications from Webhooks are received through the real-time syntax analysis service. The real-time syntax analysis service can be customized or built using an externally provided code analysis API (Application Programming Interface).

[0052] When the target source code in the code base changes, Webhooks sends a notification to the real-time syntax analysis service, which extracts the updated information of the target source code from the notification and uses the built-in parser or calls an external code analysis API to perform syntax analysis on the target source code to generate AST, thereby obtaining syntax structure information.

[0053] Step S204: generating a target test code corresponding to the target source code based on the target source code and the grammatical structure information.

[0054] The target test code in the above steps is the test code generated for the target source code, which is used to verify the functional correctness, boundary conditions and exception handling of the target source code.

[0055] In an optional embodiment, the function logic, parameter types, call relationships, etc. reflected in the grammatical structure information can be analyzed to obtain analysis results. Then, based on the target source code and the analysis results, test cases are designed through natural language processing and code generation technology, simulation objects that simulate external interfaces are created, and logic codes that check whether the output meets expectations are written, thereby obtaining target test code.

[0056] In another optional embodiment, an RL (Reinforcement Learning) method can be used to guide the generation of the target test code. Specifically, an RL Agent (Reinforcement Learning Agent) can be created, and the grammatical structure information of the target source code can be used to construct the state space of the RL Agent. The action space of the RL Agent includes designing test cases, creating simulation objects, etc. Then, the grammatical structure information is extracted as the initial state of the RL Agent. According to the initial state, the RL Agent performs the actions of generating test cases and filling test data to generate the target test code.

[0057] Step S206: Integrate the target test code into the main branch corresponding to the target source code.

[0058] The main branch in the above steps is the main development line of the software project corresponding to the target source code. Integrating the target test code into the main branch means matching the generated target test code with the target source code and saving them to ensure that the target test code becomes part of the software project.

[0059] In an optional embodiment, the target test code can be added to the unit test directory to maintain a logical association with the target source code. The target test code and the target source code changes are then packaged together through the CI system and merged into the main branch corresponding to the target source code.

[0060] In another optional embodiment, the target test code can be integrated using the automated commit and pull request process of Git (a distributed version control system). Specifically, the target test code is added to the local test directory and committed to a temporary branch with a commit message describing the changes. Git then creates a pull request to the main branch, and based on the pull request, the target test code is integrated into the main branch corresponding to the target source code.

[0061] In an embodiment of the present application, in response to monitoring changes in the target source code in the code base, the target source code is parsed to obtain the grammatical structure information of the target source code; based on the target source code and the grammatical structure information, the target test code corresponding to the target source code is generated; and the test code is integrated into the main branch corresponding to the target source code. It is easy to notice that after monitoring the changes in the code base, the source code parsing and test code generation process is started, and the specific logic of the target source code and the interaction method with the external environment can be obtained by parsing the target source code and obtaining the grammatical structure information of the target source code. The target test code is generated based on the target source code and the grammatical structure information, ensuring that the target test code can more accurately target each part of the target source code, covering more boundary conditions and exception handling, while replacing the process of developers manually writing test code, improving the generation efficiency of the target test code, thereby achieving the technical effect of improving the efficiency of source code testing, and then solving the technical problem of low source code testing efficiency.

[0062] In one embodiment of the present application, a target test code corresponding to a target source code is generated based on a target source code and grammatical structure information, including: determining an evaluation index of the target source code; when it is determined based on the evaluation index that the target source code is not a complex code, generating a target test code based on the target source code and the grammatical structure information, wherein the complexity of the execution logic of the complex code is greater than or equal to a preset degree.

[0063] The evaluation indicators in the above steps are quantitative standards for measuring the complexity of the target source code, which may include but are not limited to the number of lines of code, the number of function branches, the depth and breadth of function calls, the complexity of the data structures involved, etc.

[0064] The execution logic in the above steps refers to the process and decision path when the target source code is executed, which describes how the target source code generates output and behavior based on input and internal state.

[0065] The complexity of the execution logic in the above steps is a quantitative indicator used to measure the complexity of the execution logic.

[0066] The preset degree in the above steps is a preset threshold value used to determine whether the target source code is complex code, which can be set according to the actual application situation, and the specific content of the preset degree is not limited here. For example, the preset degree can be 50 lines of code or 10 branches of a function, but is not limited thereto.

[0067] In an optional embodiment, the evaluation index of the target source code can be determined. Exemplarily, the evaluation index of the target source code can be the number of lines of code and the number of branches of a function. Then, a code analysis tool or a customized code parser can be used to perform static code analysis on the target source code, and specific data corresponding to the evaluation index, that is, the number of lines of code and the number of branches of a function, is extracted from the target source code to obtain the complexity of the execution logic of the target source code. Specifically, the code analysis tool can be SonarQube or ESLint, but is not limited thereto.

[0068] Then, the complexity of the execution logic of the target source code is compared with the preset degree. If the complexity of the execution logic of the target source code is greater than the preset degree, it is determined whether the target source code is a complex code. Exemplarily, the preset degree may be 50 lines of code and 10 branches of a function. If the number of lines of code of the target source code exceeds 50, it is determined that the target source code is a complex code; if the number of branches of the function of the target source code is greater than 10, it is determined that the target source code is a complex code; if the number of lines of code of the target source code exceeds 50 and the number of branches of the function of the target source code is greater than 10, it is determined that the target source code is a complex code.

[0069] In the case where the target source code is not a complex code, the target test code is generated based on the target source code and the grammatical structure information. Specifically, test cases including normal input, boundary value input, and abnormal input can be designed according to the target source code and the grammatical structure information, external dependencies such as databases and network requests can be simulated, and a check function can be generated to determine whether the output of the target source code meets expectations. In addition, the test cases, simulated external dependencies, check functions, etc. can be organized into unit test files in accordance with the specifications of the test framework to obtain the target test code.

[0070] In one embodiment of the present application, target test code is generated based on target source code and grammatical structure information, including: generating prompt text based on the target source code and grammatical structure information; inputting the prompt text into a code generation model, and generating target test code using the code generation model.

[0071] The prompt text in the above steps contains task requirements and context information. Specifically, the prompt text may include the parameter type and name of the function in the target source code, the return type of the function, other function or class information called by the function, and the guiding ideology for designing test cases. Exemplarily, the prompt text may indicate the boundary conditions that need to be tested, the exception handling path, and the expected output under different inputs. The prompt text can be represented in the form of prompt assembly. When using the LLM model to generate the target test code, the prompt assembly is to combine the grammatical structure information, function call relationship, input and output parameter information, and other possible context information of the target source code into a prompt text in a certain format.

[0072] The code generation model in the above steps is used to generate target test code according to the provided prompt text. The code generation model can infer how to generate corresponding target test code for the target source code by learning the generation patterns of historical source code and historical test code, including designing test cases, handling function calls and boundary conditions, etc. The code generation model can be a model based on the LLM architecture, but is not limited thereto.

[0073] In an optional embodiment, the target source code and grammatical structure information can be parsed to extract the code structure in the function body, other functions or class information called, and the data structure involved. Then the extracted information is converted into a natural language expression, and combined with the basic principles of unit test design, a prompt text that can guide the code generation model to generate the target test code is generated. Among them, the basic principles of unit test design can include but are not limited to 100% code coverage, test independence and repeatability, etc.

[0074] When the prompt text is input into the code generation model and the target test code is generated by the code generation model, the prompt text can be preprocessed by word segmentation, word vectorization, etc. to convert the prompt text into an input format that the code generation model can understand. Then the preprocessed prompt text is input into the code generation model, and the code generation model generates the target test code according to the input prompt text.

[0075] In one embodiment of the present application, the method also includes: when the target source code is a complex code, cutting the target source code into blocks to obtain multiple code blocks, wherein the complexity of the execution logic of the code blocks is less than a preset degree; based on any code block and grammatical structure information, generating a sub-test code corresponding to any code block; based on the sub-test codes corresponding to multiple code blocks, generating a target test code.

[0076] The code block in the above steps refers to a smaller part of code separated from the complex target source code and with relatively simple execution logic. The code block contains one or several relatively independent functions or logic paths, and retains the context dependency.

[0077] The sub-test code in the above steps is the test code generated based on the code block.

[0078] In an optional embodiment, the complex target source code can be divided into relatively simple code blocks according to the function logic to ensure that the complexity of each code block is lower than a preset threshold. Then, a prompt text is constructed for each code block based on the grammatical structure information, and the prompt text contains code details and test requirements. The prompt text is input into the code generation model to generate the corresponding sub-test code. Afterwards, the sub-test codes are integrated to obtain the target test code.

[0079] The purpose of segmenting the target source code is to reduce the code complexity so that the code generation model can effectively process each part, thereby generating more accurate target test code.

[0080] In one embodiment of the present application, a target source code is segmented to obtain a plurality of code blocks, including: using a code segmentation model to identify the execution logic of the target source code to obtain an identification result; inputting the identification result into the code segmentation model, and using the code segmentation model to segment the target source code to obtain a plurality of code blocks.

[0081] The code segmentation model in the above steps can identify the execution logic of the target source code and segment the target source code into smaller and easier-to-process code blocks.

[0082] The recognition results in the above steps are output information about the code structure and logic block division, which can be used to determine which parts of the target source code should be divided into independent code blocks. The recognition results may include but are not limited to the location, range and relationship of each logic block in the target source code.

[0083] In an optional embodiment, the execution logic of the complex target source code is analyzed using a code segmentation model to identify different logic blocks, determine the location, range and mutual relationship of each logic block, and obtain a recognition result. Then, the recognition result is input into the code segmentation model, and the code segmentation model determines the boundary of the code block according to the recognition result to ensure that the complexity of the execution logic of the code block is less than a preset degree. Subsequently, the code segmentation model divides the target source code into multiple code blocks according to the boundaries of the code blocks.

[0084] In one embodiment of the present application, a sub-test code corresponding to any code block is generated based on any code block and grammatical structure information, including: determining the execution order of multiple code blocks; generating a sub-test code corresponding to the first code block based on the first code block and the grammatical structure information according to the execution order; generating a sub-test code corresponding to the current code block based on the current code block, the grammatical structure information and the sub-test code corresponding to the previous code block, wherein the current code block is any code block among the multiple code blocks except the first code block, and the previous code block is a code block executed before the current code block.

[0085] The execution order in the above steps is the execution order between multiple code blocks, which is used to ensure that the generated target test code can be executed in a correct logical order.

[0086] In an optional embodiment, the execution order of multiple code blocks can be determined based on the dependency relationship between the code blocks and the execution flow of the target source code. The first code block is determined based on the execution order, and the sub-test code corresponding to the first code block is generated using the code generation model according to the first code block and the grammatical structure information. Then the second code block is determined based on the execution order, and the sub-test code corresponding to the second code block is generated according to the second code block, the grammatical structure information and the sub-test code corresponding to the first code block, and so on, until the sub-test code corresponding to the last code block is generated.

[0087] Since there are dependencies between code blocks, the correct execution of the current code block may depend on the state of the previous code block. Therefore, the sub-test code of the current code block is generated based on the current code block, the syntax structure information, and the sub-test code corresponding to the previous code block, which can ensure the consistency and integrity of the target test code. At the same time, referring to the sub-test code of the previous code block when generating the sub-test code of the current code block also helps maintain the consistency of the target test code and the target source code environment, ensuring that the generated target test code can run normally with the previously generated test code without introducing any conflicts or incompatibility issues.

[0088] A preferred embodiment is described below as an example. Figure 3 is a schematic diagram of an optional generation of sub-test codes according to an embodiment of the present application, such as Figure 3 As shown, when generating the sub-test code corresponding to the code block, the first code block is input into the large language model, that is, the code generation model. The large language model generates the sub-test code corresponding to the first code block based on the grammatical structure information of the first code block and the first code block, that is, the test code of the first code block. Then the sub-test code corresponding to the first code block and the second code block are input into the large language model. The large language model generates the sub-test code corresponding to the second code block based on the sub-test code corresponding to the first code block, the second code block, and the grammatical structure information of the second code block, wherein the sub-test code corresponding to the second code block includes the test code of the first code block and the test code of the second code block. Afterwards, the sub-test code corresponding to the second code block and the third code block are input into the large language model. The large language model generates the sub-test code corresponding to the third code block based on the sub-test code corresponding to the second code block, the third code block, and the grammatical structure information of the third code block, wherein the sub-test code corresponding to the third code block includes the test code of the first code block, the test code of the second code block, and the test code of the third code block. Then, by analogy, the sub-test codes corresponding to other code blocks are continuously generated.

[0089] In one embodiment of the present application, a target test code is generated based on sub-test codes corresponding to multiple code blocks, including: determining the execution order of multiple code blocks; integrating the sub-test code corresponding to the current code block with the integration result corresponding to the previous code block in the execution order to obtain the integration result corresponding to the current code block, wherein the integration result corresponding to the first code block is the sub-test code corresponding to the first code block, the current code block is any one of the multiple code blocks except the first code block, and the previous code block is the code block executed before the current code block; and determining the integration result corresponding to the last code block as the target test code.

[0090] In an optional embodiment, the execution order of multiple code blocks can be determined according to the function calls and dependencies in the target source code. Then, according to the execution order of the multiple code blocks, the first code block is determined, and the sub-test code corresponding to the first code block is determined as the integration result corresponding to the first code block; according to the execution order of the multiple code blocks, the second code block is determined, and the sub-test code corresponding to the second code block is integrated with the integration result corresponding to the first code block to obtain the integration result corresponding to the second code block; according to the execution order of the multiple code blocks, the third code block is determined, and the sub-test code corresponding to the third code block is integrated with the integration result corresponding to the second code block to obtain the integration result corresponding to the third code block; and so on, until the integration result corresponding to the last code block is obtained, and the integration result corresponding to the last code block is determined as the target test code.

[0091] In one embodiment of the present application, the target test code is integrated into the main branch corresponding to the target source code, including: reviewing the target test code to obtain a review result, wherein the review result is used to characterize whether the target test code is allowed to be integrated into the main branch; when the review result characterizes that the target test code is allowed to be integrated into the main branch, the target test code is integrated into the main branch; when the review result characterizes that the target test code is not allowed to be integrated into the main branch, the target test code is modified to obtain the modified code, and the modified code is integrated into the main branch.

[0092] In an optional embodiment, when reviewing the target test code, the review may be conducted manually, or an automatic review tool may be used to review the target test code.

[0093] The following uses the automatic review tool to review the target test code. The automatic review tool may include but is not limited to static code analysis tools, unit test coverage analysis tools, etc. The automatic review tool can review whether there are syntax errors, compilation errors, or uncovered code paths in the target test code, and obtain the review results. The review results can be a mark indicating whether the review has passed or failed, and detailed modification suggestions can also be provided when the review fails.

[0094] If the review result indicates that the target test code is allowed to be integrated into the main branch, it means that the quality of the target test code meets the requirements and the target test code can be integrated into the main branch to become part of the software project. If the review result indicates that the target test code is not allowed to be integrated into the main branch, it means that the target test code has defects, such as logical errors, incomplete coverage, or violation of coding standards. The target test code can be modified according to the modification suggestions provided in the review result, and the modified code can be reviewed again. After the review is passed, the modified code can be integrated into the main branch.

[0095] In one embodiment of the present application, the method also includes: in response to monitoring a code change instruction, determining the submitted code corresponding to the code change instruction; comparing the submitted code with the source code on the main branch to obtain a code comparison result; based on the code comparison result, determining the target source code that has changed in the code library.

[0096] The code change instructions in the above steps are operation instructions for modifying source code in the code base, and can be submitted by developers.

[0097] The submitted code in the above steps is the code change content uploaded to the code library corresponding to the change instruction.

[0098] In an optional embodiment, a continuous integration system can be used to monitor whether there are new submissions in the code base. Once a new submission is detected, it is considered that a code change instruction has been received. By extracting the latest submission record, the submitted code corresponding to the code change instruction can be determined, that is, the code change content submitted by the developer. The submitted code is compared with the source code on the main branch to obtain a code comparison result, wherein the code comparison result can be presented in the form of a code diff. By analyzing the code diff, the source code that has changed in the code base can be located, and the source code that has changed in the code base can be determined as the target source code that needs to be regenerated or updated for the test code.

[0099] The following is a description of a preferred embodiment. Figure 4 is an optional flow chart of generating unit test code according to an embodiment of the present application, such as Figure 4 As shown, an optional process for generating unit test code is as follows:

[0100] Step S402, submit the branch.

[0101] After receiving the requirements, developers can create a new branch in the version control system, develop the requirements, and write code. After writing is completed, the branch where the code is located is submitted to the code library to generate code change instructions.

[0102] Step S404, triggering the continuous integration pipeline.

[0103] The code change instruction triggers the continuous integration pipeline, also known as the CI pipeline, which determines the submitted code corresponding to the code change instruction.

[0104] Step S406: code difference check.

[0105] The CI pipeline then compares the submitted code corresponding to the code change instruction with the existing source code on the main branch, that is, performs a code (diff) difference check to determine the target source code for generating test code.

[0106] Step S408: write unit test code for the large language model.

[0107] Then the target source code is processed by the large language model, that is, the code generation model, to generate the unit test code corresponding to the target source code, that is, the target test code mentioned above.

[0108] Step S410, reviewing the unit test code.

[0109] The unit test code is then reviewed manually or with an automated review tool to determine whether there are syntax errors, compilation errors, or uncovered code paths in the target test code.

[0110] Step S412: branch merging.

[0111] When the unit test code quality meets the requirements and passes the review, the unit test code will be integrated into the main branch where the target source code is located.

[0112] Step S414, executing unit testing.

[0113] Execute the unit test code to perform unit testing.

[0114] Figure 5 is a schematic diagram of an optional generation of unit test code according to an embodiment of the present application, such as Figure 5 As shown, the CI pipeline compares the submitted code with the existing source code on the main branch to obtain the code difference, that is, the code diff. Then, the tested function that needs to generate the test code is located according to the code diff. The tested function is also the target source code mentioned above. At the same time, the code AST is parsed for the tested function stored in the code repository to obtain the structured code information, that is, the grammatical structure information mentioned above. Then, based on the tested function and the structured code information, prompt words (prompt) are assembled, that is, prompt text. The prompt text is then input into the large language model, and the large language model generates unit test code based on the prompt text to obtain the unit test code file, that is, the target test code mentioned above.

[0115] According to an embodiment of the present application, another code generation method is also provided. Figure 6 is a flowchart of another code generation method according to an embodiment of the present application, such as Figure 6 As shown, the method includes:

[0116] Step S602, acquiring source code in a code library by calling a first interface, wherein the first interface includes a first parameter, and a parameter value of the first parameter includes the source code;

[0117] Step S604, in response to a change in the target source code in the source code, parsing the target source code to obtain grammatical structure information of the target source code, wherein the grammatical structure information includes an internal structure and an external interface of the target source code;

[0118] Step S606, generating a target test code corresponding to the target source code based on the target source code and the grammatical structure information;

[0119] Step S608, integrating the target test code into the main branch corresponding to the target source code;

[0120] Step S610: outputting the main branch by calling the second interface, wherein the second interface includes a second parameter, and a parameter value of the second parameter includes the main branch.

[0121] According to an embodiment of the present application, another code generation method is also provided. Figure 7 is a flowchart of another code generation method according to an embodiment of the present application, such as Figure 7 As shown, the method includes:

[0122] Step S702, responding to the code submission instruction on the operation interface, monitoring whether the source code in the code library is changed;

[0123] Step S704, in response to detecting that a target source code has been changed, parsing the target source code to obtain grammatical structure information of the target source code, wherein the grammatical structure information includes an internal structure and an external interface of the target source code;

[0124] Step S706, generating a target test code corresponding to the target source code based on the target source code and the grammatical structure information, and displaying the target test code on the operation interface;

[0125] Step S708 , responding to the code confirmation instruction on the operation interface, integrating the target test code into the main branch corresponding to the target source code.

[0126] It should be noted that the preferred implementation scheme involved in the above embodiments of the present application is the same as the scheme provided in the above embodiments, as well as the application scenario and implementation process, but is not limited to the scheme provided in the above embodiments.

[0127] 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 this application 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 relevant countries and regions, and provide corresponding operation entrances for users to choose to authorize or refuse.

[0128] It should be noted that, for the aforementioned method embodiments, for the sake of simplicity, they are all expressed as a series of action combinations, but those skilled in the art should be aware that the present application is not limited by the described order of actions, because according to the present application, 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 present application.

[0129] Through the description of the above implementation methods, those skilled in the art can clearly understand that the method according to the above embodiment can be implemented by means of software plus a necessary general hardware platform, and of course, it can also be implemented by hardware. Based on this understanding, the technical solution of the present application, or the part that contributes to the prior art, can be embodied in the form of a software product, which is stored in a storage medium (such as ROM / RAM, disk, CD), and includes a number of instructions for a terminal device (which can be a mobile phone, computer, server, or network device, etc.) to execute the methods described in each embodiment of the present application.

[0130] An embodiment of the present application may provide a computing device. Figure 8 is a structural block diagram of a computing device according to an embodiment of the present application. Figure 8 As shown, the computing device 800 may include: one or more (only one is shown in the figure) processors 802, a memory 804, a storage controller, and a peripheral interface.

[0131] The above-mentioned computing device can be understood as an integrated intelligent terminal, including but not limited to a server, a desktop computer, a PC (Personal Computer), a model all-in-one machine, etc., and the computing device can be pre-installed with the model described in the above-mentioned embodiments of the present application.

[0132] Specifically, the computing device can pre-set multiple types of models, including but not limited to models in the fields of natural language processing, visual processing, speech processing, code processing, multimodal task processing, etc., so as to provide a variety of model choices. In different product forms, the computing device can support one or more model usage methods, including but not limited to model training, model calling, model fine-tuning, model deployment, model reasoning and application, etc. In some product forms, the computing device also supports model management, including but not limited to multi-type model management (supporting the management of multiple types of models such as discriminants and generative models), model version control (supporting the control of different model versions), model evaluation (based on model evaluation tools, evaluating the performance and effect of the model), etc. In other product forms, the computing device can also create applications based on models, provide API calling capabilities, and can call models to the created applications through API interfaces, while providing application management tools to achieve application management and control.

[0133] Furthermore, the computing device can also include data management (supporting the creation and management of model tuning data sets), training center (providing rich training resources to help users learn and master AI technology), and basic management and control capabilities (providing enterprise-level basic management and control capabilities to ensure the security and efficient operation of the system). Through the above functions, a comprehensive, integrated AI development, training, deployment and application device is provided.

[0134] Among them, the memory can be used to store software programs and modules, such as program instructions / modules corresponding to the methods and devices in the embodiments of the present application. The processor executes various functional applications and data processing by running the software programs and modules stored in the memory, that is, implementing the methods in the above embodiments. The memory may include a high-speed random access memory, and may also include a non-volatile memory, such as one or more magnetic storage devices, flash memory, or other non-volatile solid-state memory. In some instances, the memory may further include a memory remotely arranged relative to the processor, and these remote memories may be connected to the terminal A via a network. Examples of the above-mentioned network include, but are not limited to, the Internet, an intranet, a local area network, a mobile communication network, and combinations thereof.

[0135] The processor may call the executable program stored in the memory through the transmission device to execute the method described in any one of the above embodiments.

[0136] An embodiment of the present application may provide an electronic device. Fig. 9 is a structural block diagram of an electronic device according to an embodiment of the present application. Fig. 9 As shown, the electronic device may include: an input-output device 92 ; a memory 94 and a processor 96 , wherein the processor 96 is connected to the input-output device 92 and the memory 94 via a bus 98 .

[0137] Among them, the memory can be used to store software programs and modules, such as program instructions / modules corresponding to the methods and devices in the embodiments of the present application. The processor executes various functional applications and data processing by running the software programs and modules stored in the memory, that is, implementing the methods in the above embodiments. The memory may include a high-speed random access memory, and may also include a non-volatile memory, such as one or more magnetic storage devices, flash memory, or other non-volatile solid-state memory. In some instances, the memory may further include a memory remotely arranged relative to the processor, and these remote memories may be connected to the terminal A via a network. Examples of the above-mentioned network include, but are not limited to, the Internet, an intranet, a local area network, a mobile communication network, and combinations thereof.

[0138] The processor may call the executable program stored in the memory through the transmission device to execute the method described in any one of the above embodiments.

[0139] It will be understood by those skilled in the art that the structure shown in the figure is for illustration only, and the computing device may also be a smart phone (such as an Android phone, an iOS phone, etc.), a tablet computer, a PDA, a mobile Internet device (Mobile Internet Devices, MID), a PAD, and other terminal devices. The figure does not limit the structure of the above computing device. For example, the computing device 800 may also include more or fewer components (such as a network interface, a display device, etc.) than those shown in the figure, or have a configuration different from that shown in the figure.

[0140] A person of ordinary skill in the art can understand that all or part of the steps in the various methods of the above embodiments can be completed by instructing the hardware related to the terminal device through a program, and the program can be stored in a computer-readable storage medium, and the storage medium may include: a flash drive, a read-only memory (ROM), a random access memory (RAM), a magnetic disk or an optical disk, etc.

[0141] The embodiment of the present application further provides a computer-readable storage medium. Optionally, in this embodiment, the computer-readable storage medium can be used to store the program code executed by the method provided in the above embodiment.

[0142] Optionally, in this embodiment, the above storage medium may be located in a computing device.

[0143] Optionally, in this embodiment, the computer-readable storage medium is configured to store an executable program, and when the executable program is running, the device where the computer-readable storage medium is located is controlled to execute the method described in any one of the above embodiments.

[0144] The embodiment of the present application further provides a computer program product. Optionally, in this embodiment, the computer program product may include a computer program, and the computer program implements the method provided in the embodiment when executed by a processor.

[0145] The embodiments of the present application also provide a computer program product. Optionally, the computer program product may include a non-volatile computer-readable storage medium, which may be used to store a computer program, and when the computer program is executed by a processor, the method provided in the embodiments is implemented.

[0146] The embodiment of the present application further provides a computer program. Optionally, in this embodiment, when the computer program is executed by a processor, the method provided in the above embodiment is implemented.

[0147] In the above embodiments of the present application, 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.

[0148] In the several embodiments provided in this application, it should be understood that the disclosed technical content can be implemented in other ways. Among them, the device embodiments described above are only schematic. For example, the division of the units is only a logical function division. There may be other division methods in actual implementation. For example, multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the mutual coupling or direct coupling or communication connection shown or discussed can be through some interfaces, indirect coupling or communication connection of units or modules, which can be electrical or other forms.

[0149] The units described as separate components may or may not be physically separated, and the components shown as units may or may not be physical units, that is, they may be located in one place or distributed on multiple network units. Some or all of the units may be selected according to actual needs to achieve the purpose of the solution of this embodiment.

[0150] In addition, each functional unit in each embodiment of the present application may be integrated into one processing unit, or each unit may exist physically separately, or two or more units may be integrated into one unit. The above-mentioned integrated unit may be implemented in the form of hardware or in the form of software functional units.

[0151] If the integrated unit is implemented in the form of a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the present application, or the part that contributes to the prior art, or all or part of the technical solution can be embodied in the form of a software product. The computer software product is stored in a storage medium, including several instructions for a computer device (which can be a personal computer, server or network device, etc.) to execute all or part of the steps of the method described in each embodiment of the present application. The aforementioned storage medium includes: U disk, read-only memory (ROM, Read-Only Memory), random access memory (RAM, Random Access Memory), mobile hard disk, disk or optical disk, etc., which can store program code.

[0152] The above is only a preferred implementation of the present application. It should be pointed out that for ordinary technicians in this technical field, several improvements and modifications can be made without departing from the principles of the present application. These improvements and modifications should also be regarded as the scope of protection of the present application.

Claims

1. A code generation method, characterized in that: include: In response to monitoring a change in a target source code in a code base, parsing the target source code to obtain grammatical structure information of the target source code, wherein the grammatical structure information includes an internal structure and an external interface of the target source code; Based on the target source code and the grammatical structure information, generating a target test code corresponding to the target source code; Integrate the target test code into the main branch corresponding to the target source code.

2. The method according to claim 1, characterized in that The generating a target test code corresponding to the target source code based on the target source code and the grammatical structure information includes: Determining an evaluation index of the target source code; When it is determined based on the evaluation index that the target source code is not the complex code, the target test code is generated based on the target source code and the grammatical structure information, wherein the complexity of the execution logic of the complex code is greater than or equal to a preset degree.

3. The method according to claim 2, characterized in that The generating the target test code based on the target source code and the grammatical structure information comprises: Generate a prompt text based on the target source code and the grammatical structure information; The prompt text is input into a code generation model, and the target test code is generated using the code generation model.

4. The method according to claim 2, characterized in that: The method further comprises: In the case where the target source code is the complex code, the target source code is cut into blocks to obtain a plurality of code blocks, wherein the complexity of the execution logic of the code blocks is less than the preset degree; Based on any code block and the grammatical structure information, generate a sub-test code corresponding to any code block; The target test code is generated based on the sub-test codes corresponding to the multiple code blocks.

5. The method according to claim 4, characterized in that The target source code is cut into blocks to obtain multiple code blocks, including: Using the code segmentation model to identify the execution logic of the target source code, and obtain an identification result; The recognition result is input into the code segmentation model, and the target source code is segmented using the code segmentation model to obtain the multiple code blocks.

6. The method according to claim 4, characterized in that The generating of a sub-test code corresponding to any code block based on any code block and the grammatical structure information includes: Determining an execution order of the plurality of code blocks; Generate a sub-test code corresponding to the first code block based on the first code block and the grammatical structure information according to the execution order; Based on the current code block, the grammatical structure information and the sub-test code corresponding to the previous code block, the sub-test code corresponding to the current code block is generated, wherein the current code block is any one of the multiple code blocks except the first code block, and the previous code block is a code block executed before the current code block.

7. The method according to claim 4, characterized in that The generating the target test code based on the sub-test codes corresponding to the multiple code blocks includes: Determining an execution order of the plurality of code blocks; Integrate the sub-test code corresponding to the current code block with the integration result corresponding to the previous code block according to the execution order to obtain the integration result corresponding to the current code block, wherein the integration result corresponding to the first code block is the sub-test code corresponding to the first code block, the current code block is any one of the multiple code blocks except the first code block, and the previous code block is a code block executed before the current code block; Determine that the integration result corresponding to the last code block is the target test code.

8. The method according to any one of claims 1 to 7, characterized in that Integrating the target test code into the main branch corresponding to the target source code includes: Reviewing the target test code to obtain a review result, wherein the review result is used to indicate whether the target test code is allowed to be integrated into the main branch; In the case where the review result indicates that the target test code is allowed to be integrated into the main branch, integrating the target test code into the main branch; When the review result indicates that the target test code is not allowed to be integrated into the main branch, the target test code is modified to obtain a modified code, and the modified code is integrated into the main branch.

9. The method according to any one of claims 1 to 3, characterized in that: The method further comprises: In response to monitoring a code change instruction, determining a submitted code corresponding to the code change instruction; Compare the submitted code with the source code on the main branch to obtain a code comparison result; Based on the code comparison result, the target source code that has been changed in the code library is determined.

10. A code generation method, characterized in that: include: Acquire source code in a code library by calling a first interface, wherein the first interface includes a first parameter, and a parameter value of the first parameter includes the source code; In response to a change in a target source code in the source code, the target source code is parsed to obtain grammatical structure information of the target source code, wherein the grammatical structure information includes an internal structure and an external interface of the target source code; Based on the target source code and the grammatical structure information, generating a target test code corresponding to the target source code; Integrate the target test code into the main branch corresponding to the target source code; The main branch is output by calling a second interface, wherein the second interface includes a second parameter, and a parameter value of the second parameter includes the main branch.

11. A code generation method, characterized in that: include: Respond to the code submission instruction on the operation interface and monitor whether the source code in the code library has been changed; In response to detecting a change in the target source code, parsing the target source code to obtain grammatical structure information of the target source code, wherein the grammatical structure information includes an internal structure and an external interface of the target source code; Based on the target source code and the grammatical structure information, generating a target test code corresponding to the target source code, and displaying the target test code on the operation interface; In response to a code confirmation instruction on the operation interface, the target test code is integrated into a main branch corresponding to the target source code.

12. A computing device, characterized in that: include: A memory storing an executable program; A processor, configured to run the program, wherein the program executes the method according to any one of claims 1 to 11 when running.

13. An electronic device, characterized in that: include: A memory storing an executable program; A processor, connected to the memory via a bus, and configured to run the program, wherein the program executes the method described in any one of claims 1 to 11 when running.

14. A computer-readable storage medium, characterized in that: The computer-readable storage medium includes a stored executable program, wherein when the executable program is executed, the device where the computer-readable storage medium is located is controlled to execute the method according to any one of claims 1 to 11.

15. A computer program product, characterized in that The invention comprises a computer program which, when executed by a processor, implements the method according to any one of claims 1 to 11.

Citation Information

Patent Citations

  • Unit test generation method and device

    CN117909239A

  • Code completion method and device, electronic equipment and medium

    CN118227106A

  • Data testing method and device, storage medium and computer program product

    CN118939559A

  • Code map construction method and device, electronic equipment and computer storage medium

    CN119149757A

  • Code translation method and system, electronic equipment and computer readable storage medium

    CN119576333A