Software test code generation method and device, related equipment and computer program product
By analyzing the structure information of the code under test and combining it with the test cases to input the large model to generate the test code, it solves the "illusion" problem when the large model generates code and the problem of insufficient coverage of the template generation tool, and improves the quality of the test code and the success of compilation. Rate.
Patent Information
- Application Number
- CN202410974255.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-07-19
- Publication Date
- 2024-10-01
AI Technical Summary
In existing test code generation solutions, large models are prone to "illusion" during code generation, resulting in more grammatical errors in the generated test code, lower compilation success rate, and template-based generation tools have low test scenario coverage.
By parsing the structural information of the code under test, determining the framework information required for the test code to be generated, and combining it with the generated test cases, inputting the large model to generate test code, simulating human processing logic, improving code quality and compilation success Rate.
It improves the accuracy and compilation success rate of test code, enhances test scenario coverage, and reduces developers’ secondary modification work.
Smart Images

Figure CN118733472A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of software testing technology, and more specifically, to a software testing code generation method, apparatus, related equipment and computer program product. Background Art
[0002] Code testing refers to the process of testing the developed code, which is an important technology in software engineering. Code testing can ensure software quality and improve software reliability. The code testing process can be divided into unit testing, system testing, etc. according to the scale of the tested code. Taking unit testing as an example, its main purpose is to verify whether the smallest testable unit (usually a function or method) in software development is executed according to the design and produces the expected effect.
[0003] The code testing process requires the generation of test code for the code under test (also known as test sample code). By executing the test code, the test process of the code under test can be completed. Current test code generation solutions generally use template-based generation tools. This method uses predefined templates and combines the actual code structure and requirements to generate test code that meets specific specifications and formats. Although this method can improve development efficiency and reduce repetitive work, the fixed structure of the template leads to limitations in covering test scenarios.
[0004] With the continuous development of artificial intelligence technology, using large models to generate test code has gradually become an efficient and intelligent choice. Large models have strong reasoning and analysis capabilities, and can generate test code for the code under test in different scenarios, covering a wide range of test scenarios. The current solution is generally to feed the code under test into the large model and let the large model generate the test code for the code under test end to end. However, due to the certain "illusions" of the large model in code generation, the generated test code has many syntax errors and a low compilation success rate. In actual applications, developers still need to make secondary modifications. Summary of the invention
[0005] In view of the above problems, this application is proposed to provide a software test code generation method, apparatus, related equipment and computer program product to improve the effect of the existing test code generation scheme. The specific scheme is as follows:
[0006] In a first aspect, a method for generating software test code is provided, comprising:
[0007] Parsing structural information associated with the code under test, and determining framework information required for the test code to be generated from the structural information;
[0008] Sending the code under test to the configured big model to instruct the big model to generate test cases corresponding to the code under test;
[0009] The combination of the framework information and the test case is sent to the large model to instruct the large model to supplement and generate test code based on the combination of the framework information and the test case.
[0010] In a possible design, in another implementation of the first aspect of the embodiment of the present application, the process of sending the code under test to a configured big model to instruct the big model to generate a test case corresponding to the code under test includes:
[0011] The code under test and the structural information associated with the code under test are sent to the configured large model together to instruct the large model to generate a test case corresponding to the code under test based on the structural information associated with the code under test.
[0012] In a possible design, in another implementation of the first aspect of the embodiment of the present application, the process of sending the code under test and the structural information associated with the code under test to a configured large model to instruct the large model to generate a test case corresponding to the code under test based on the structural information associated with the code under test includes:
[0013] Obtain a configured first prompt instruction prompt format template, the template comprising a tested code slot, a structure information slot and a task instruction, the task instruction is used to instruct the large model to generate a test case based on the tested code in the tested code slot and the structure information in the structure information slot;
[0014] Fill the tested code into the tested code slot, and fill the structure information associated with the tested code into the structure information slot, to obtain a first prompt instruction prompt;
[0015] Input the first prompt instruction prompt into the large model to obtain the test case corresponding to the tested code output by the large model.
[0016] In a possible design, in another implementation of the first aspect of the embodiment of the present application, the combination of the framework information and the test case is sent to the large model to instruct the large model to supplement the process of generating the test code based on the combination of the framework information and the test case, including:
[0017] Obtain a configured second prompt instruction prompt format template, the template including a framework slot, a test case slot and a task instruction, the task instruction is used to instruct the large model to supplement and generate test code according to the framework information in the framework slot and the test case in the test case slot;
[0018] Fill the framework information into the framework slot, fill the test case corresponding to the tested code into the test case slot, and obtain a second prompt instruction prompt;
[0019] The second prompt instruction prompt is input into the large model to obtain the test code output by the large model.
[0020] In a possible design, in another implementation of the first aspect of the embodiment of the present application, the combination of the framework information and the test case is sent to the large model to instruct the large model to supplement the process of generating the test code based on the combination of the framework information and the test case, including:
[0021] The framework information, the test cases, the structural information and the code under test are sent to the large model together to instruct the large model to generate additional test code for the code under test based on the framework information, the test cases and the structural information.
[0022] In a possible design, in another implementation of the first aspect of the embodiment of the present application, the framework information, the test case, the structure information, and the tested code are sent to the large model together to instruct the large model to supplement the process of generating the test code, including:
[0023] Obtain a configured third prompt instruction prompt format template, the template including a framework slot, a test case slot, a structure information slot, a tested code slot and a task instruction, the task instruction is used to instruct the large model to supplement and generate test code for the tested code in the tested code slot according to the framework information in the framework slot, the test case in the test case slot and the structure information in the structure information slot;
[0024] Fill the framework information into the framework slot, fill the test case corresponding to the tested code into the test case slot, fill the structure information associated with the tested code into the structure information slot, fill the tested code into the tested code slot, and obtain a third prompt instruction prompt;
[0025] The third prompt instruction prompt is input into the large model to obtain the test code output by the large model.
[0026] In a possible design, in another implementation of the first aspect of the embodiment of the present application, the process of parsing the structural information associated with the tested code includes:
[0027] For the code under test, a code parser is used to parse the structure of the class where the code under test is located and the file structure on which the code under test depends to obtain structural information.
[0028] In a possible design, in another implementation of the first aspect of the embodiments of the present application, the framework information includes at least one of the following:
[0029] The test code needs to import dependency packages, simulate the Mock environment information, and inject attribute information.
[0030] In one possible design, in another implementation of the first aspect of the embodiment of the present application, the test case includes: a use case scenario description, the input of the use case and the method that needs to be simulated Mock, and the output of the use case.
[0031] In a second aspect, a software test code generation device is provided, comprising:
[0032] A framework acquisition unit, used to parse the structural information associated with the code under test, and determine the framework information required for the test code to be generated from the structural information;
[0033] A test case generation unit, used to send the code under test to a configured large model to instruct the large model to generate a test case corresponding to the code under test;
[0034] The test code generation unit is used to send the combination of the framework information and the test case into the large model to instruct the large model to supplement the generation of test code based on the combination of the framework information and the test case.
[0035] In a third aspect, an electronic device is provided, comprising: a memory and a processor;
[0036] The memory is used to store programs;
[0037] The processor is used to execute the program to implement the software test code generation method described in any one of the first aspects of the present application.
[0038] In a fourth aspect, a readable storage medium is provided, on which a computer program is stored. When the computer program is executed by a processor, the software test code generation method described in any one of the first aspects of the present application is implemented.
[0039] In a fifth aspect, a computer program product is provided, including a computer program, which, when executed by a processor, implements the software test code generation method described in any one of the first aspects of the present application.
[0040] With the help of the above technical scheme, in order to improve the problem that when traditional schemes use large models to generate test codes end-to-end, the generated test codes are easily inaccurate due to the "big model illusion" and the compilation success rate is low, this application adopts a thinking chain generation method in the generation strategy, that is, the task of generating test code for the code under test is split into first generating test cases for the code under test according to the processing logic, and further generating test code based on the test cases to simulate human processing logic, thereby improving the accuracy and compilation success rate of the test code finally generated.
[0041] The specific processing process is to parse the structural information associated with the code under test to improve the quality of the test code generated by the large model, and determine the framework information required for the test code to be generated based on the parsed structural information. The large model is called to generate test cases for the code under test, and then the framework information and test cases can be combined and sent to the large model, so that the large model can refer to the framework information and test cases to generate test code. Compared with the traditional solution of directly generating test code based on the code under test, the large model has richer reference information, thereby improving the quality of the generated test code.
[0042] In addition, with the help of the strong reasoning and analysis capabilities of the large model, the test code generated for the code under test can cover more complete test scenarios, improving the problem of low test scenario coverage of test code generated by traditional template-based generation tools. BRIEF DESCRIPTION OF THE DRAWINGS
[0043] Various other advantages and benefits will become apparent to those of ordinary skill in the art by reading the detailed description of the preferred embodiments below. The accompanying drawings are only for the purpose of illustrating the preferred embodiments and are not to be considered as limiting the present application. Also, the same reference symbols are used throughout the accompanying drawings to represent the same components. In the accompanying drawings:
[0044] Figure 1 A schematic diagram of an implementation system architecture of a software test code generation method provided in an embodiment of the present application;
[0045] Figure 2 A schematic diagram of a terminal structure provided in an embodiment of the present application;
[0046] Figure 3 A schematic diagram of a server structure provided in an embodiment of the present application;
[0047] Figure 4 A schematic diagram of a process flow of a method for generating software test code provided in an embodiment of the present application;
[0048] Figure 5 A schematic diagram of the composition of a framework information provided in an embodiment of the present application;
[0049] Figure 6 A schematic diagram of the composition of a test case provided in an embodiment of the present application;
[0050] Figure 7 A schematic diagram of a combination code of framework information and test cases and a position of test code to be filled in provided in an embodiment of the present application;
[0051] Figure 8 A schematic diagram of the structure of a software test code generation device provided in an embodiment of the present application;
[0052] Fig. 9 A schematic diagram of the structure of an electronic device provided in an embodiment of the present application. DETAILED DESCRIPTION
[0053] Before introducing this application solution, the relevant concepts involved in this article are first explained:
[0054] prompt: Instructions. When interacting with AI (such as an artificial intelligence model), you need to send instructions to AI. It can be a text description, such as "Please recommend me a pop song" when you interact with AI, or it can be a parameter description in a certain format. For example, if you want AI to draw in a certain format, you need to describe the relevant drawing parameters.
[0055] Large models: In the field of artificial intelligence, large models usually refer to large-scale pre-trained models. Such models are called "large" because they can be pre-trained on a large amount of data and can be transferred to a variety of downstream tasks. Their full English name is Large Pre-Trained Models or Large-Scale Pre-Training Models. Large models are characterized by their large scale and contain billions or even more parameters, which help them learn complex patterns in the data. The emerging capabilities of large models include but are not limited to: contextual learning, instruction following, code generation, step-by-step reasoning capabilities, etc. Large models can include large language models (LLMs) and multimodal large models. Large language models are mainly used to process text modal data. Multimodal large models further integrate multimodal capabilities on the basis of large language models and can process information in multiple modalities, such as images, text, audio, etc.
[0056] The following will be combined with the drawings in the embodiments of the present application to clearly and completely describe the technical solutions 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 are within the scope of protection of this application.
[0057] This application provides a software test code generation method, which can be applied to Figure 1 The system architecture shown in FIG. 1 may include a terminal 100 and a server 200. The server 200 may include one or more servers ( Figure 1 A server is included as an example for explanation).
[0058] The terminal 100 or the server 200 can be used alone to execute the software test code generation method provided in the embodiment of the present application. In addition, the terminal 100 and the server 200 can also be used in collaboration to execute the software test code generation method provided in the embodiment of the present application.
[0059] Next describe Figure 1 The product form of the mid-terminal 100;
[0060] The terminal 100 in the embodiment of the present application can be a mobile phone, a tablet computer, a wearable device, an augmented reality (AR) / virtual reality (VR) device, a laptop computer, an ultra-mobile personal computer (UMPC), a netbook, a personal digital assistant (PDA), etc., and the embodiment of the present application does not impose any limitation on this.
[0061] Figure 2 An optional hardware structure diagram of the terminal 100 is shown.
[0062] refer to Figure 2 As shown, the terminal 100 may include a radio frequency unit 110, a memory 120, an input unit 130, a display unit 140, a camera 150 (optional), an audio circuit 160, a speaker 161, a microphone 162, an earphone jack 163 (optional), a processor 170, an external interface 180, a power supply 190 and other components. Those skilled in the art will appreciate that Figure 2 These are merely examples of terminals or multi-function devices and do not constitute limitations on the terminals or multi-function devices, which may include more or fewer components than those shown in the figures, or combinations of certain components, or different components.
[0063] The input unit 130 can be used to receive input digital or character information, and generate key signal input related to the user settings and function control of the portable multifunctional device. Specifically, the input unit 130 may include a touch screen 131 and / or other input devices 132. The touch screen 131 can collect the user's touch operations on or near it (such as the user's operation on or near the touch screen using any suitable object such as fingers, joints, stylus, etc.), and drive the corresponding connection device according to a pre-set program. The touch screen can detect the user's touch action on the touch screen, convert the touch action into a touch signal and send it to the processor 170, and can receive and execute the command sent by the processor 170; the touch signal at least includes the touch point coordinate information. The touch screen 131 can provide an input interface and an output interface between the terminal 100 and the user. In addition, the touch screen can be implemented using multiple types such as resistive, capacitive, infrared and surface acoustic wave. In addition to the touch screen 131, the input unit 130 can also include other input devices. Specifically, other input devices 132 may include, but are not limited to, one or more of a physical keyboard, function keys (such as a volume control key, a switch key, etc.), a trackball, a mouse, a joystick, and the like.
[0064] Among them, the input device 132 can receive input data and the like.
[0065] The display unit 140 can be used to display information input by the user or information provided to the user, various menus of the terminal 100, interactive interfaces, file display and / or playback of any multimedia file. In the embodiment of the present application, the display unit 140 can be used to display various interactive interfaces, processing results, etc. in the software test code generation method.
[0066] The memory 120 can be used to store instructions and data. The memory 120 can mainly include an instruction storage area and a data storage area. The data storage area can store various data, such as multimedia files, texts, etc.; the instruction storage area can store software units such as operating systems, applications, instructions required for at least one function, or their subsets and extensions. It can also include a non-volatile random access memory; provide the processor 170 with hardware, software and data resources including management of computing and processing equipment, and support control software and applications. It is also used for the storage of multimedia files, and the storage of running programs and applications.
[0067] The processor 170 is the control center of the terminal 100. It uses various interfaces and lines to connect various parts of the entire terminal 100. By running or executing instructions stored in the memory 120 and calling data stored in the memory 120, it executes various functions of the terminal 100 and processes data, thereby controlling the terminal device as a whole. Optionally, the processor 170 may include one or more processing units; preferably, the processor 170 may integrate an application processor and a modem processor, wherein the application processor mainly processes the operating system, user interface and application program, and the modem processor mainly processes wireless communication. It is understandable that the above-mentioned modem processor may not be integrated into the processor 170. In some embodiments, the processor and the memory may be implemented on a single chip, and in some embodiments, they may also be implemented separately on separate chips. The processor 170 may also be used to generate corresponding operation control signals, send them to corresponding components of the computing and processing device, read and process data in the software, especially read and process data and programs in the memory 120, so that each functional module therein performs corresponding functions, thereby controlling the corresponding components to act according to the requirements of the instructions.
[0068] Among them, the memory 120 can be used to store software codes related to the software test code generation method, the processor 170 can execute the steps of the software test code generation method, and can also schedule other units (such as the above-mentioned input unit 130 and display unit 140) to implement corresponding functions.
[0069] The radio frequency unit 110 (optional) can be used for receiving and sending information or receiving and sending signals during a call, for example, after receiving the downlink information of the base station, it is sent to the processor 170 for processing; in addition, the designed uplink data is sent to the base station. Generally, the RF circuit includes but is not limited to an antenna, at least one amplifier, a transceiver, a coupler, a low noise amplifier (Low Noise Amplifier, LNA), a duplexer, etc. In addition, the radio frequency unit 110 can also communicate with network devices and other devices through wireless communication. The wireless communication can use any communication standard or protocol, including but not limited to Global System of Mobile communication (Global System of Mobile communication, GSM), General Packet Radio Service (General Packet Radio Service, GPRS), Code Division Multiple Access (Code Division Multiple Access, CDMA), Wideband Code Division Multiple Access (Wideband Code Division Multiple Access, WCDMA), Long Term Evolution (Long Term Evolution, LTE), email, Short Messaging Service (SMS), etc.
[0070] In the embodiment of the present application, the RF unit 110 can send data to the server 200 and receive the processing result sent by the server 200. Exemplarily, the RF unit 110 sends the tested code to the server 200, the server 200 generates a test code corresponding to the tested code, and returns the test code to the terminal 100 for subsequent code testing.
[0071] It should be understood that the radio frequency unit 110 is optional and can be replaced by other communication interfaces, such as a network port.
[0072] The terminal 100 also includes a power supply 190 (such as a battery) for supplying power to various components. Preferably, the power supply can be logically connected to the processor 170 through a power management system, so that the power management system can manage functions such as charging, discharging, and power consumption.
[0073] The terminal 100 further includes an external interface 180 , which may be a standard Micro USB interface or a multi-pin connector, and may be used to connect the terminal 100 to communicate with other devices, or to connect a charger to charge the terminal 100 .
[0074] Although not shown, the terminal 100 may also include a flashlight, a wireless fidelity (WiFi) module, a Bluetooth module, sensors with different functions, etc., which are not described in detail here. Some or all of the methods described below may be applied in the following embodiments. Figure 2 In the terminal 100 shown.
[0075] Next describe Figure 1 The product form of the server 200;
[0076] Figure 3 A schematic diagram of the structure of a server 200 is provided, such as Figure 3 As shown, the server 200 includes a bus 201, a processor 202, a communication interface 203, and a memory 204. The processor 202, the memory 204, and the communication interface 203 communicate with each other via the bus 201.
[0077] The bus 201 may be a peripheral component interconnect (PCI) bus or an extended industry standard architecture (EISA) bus. The bus may be divided into an address bus, a data bus, a control bus, etc. For ease of representation, Figure 3 Only one thick line is used in the diagram, but this does not mean that there is only one bus or only one type of bus.
[0078] The processor 202 may be any one or more of a central processing unit (CPU), a graphics processing unit (GPU), a microprocessor (MP), or a digital signal processor (DSP).
[0079] The memory 204 may include a volatile memory, such as a random access memory (RAM). The memory 204 may also include a non-volatile memory, such as a read-only memory (ROM), a flash memory, a hard drive (HDD), or a solid state drive (SSD).
[0080] The memory 204 may be used to store software codes related to the software test code generation method, and the processor 202 may execute the steps of the software test code generation method, and may also schedule other units to implement corresponding functions.
[0081] It should be understood that the above-mentioned terminal 100 and server 200 can be centralized or distributed devices, and the processors in the above-mentioned terminal 100 and server 200 (such as processor 170 and processor 202) can be hardware circuits (such as application specific integrated circuit (ASIC), field-programmable gate array (FPGA), general-purpose processor, digital signal processor (DSP), microprocessor or microcontroller, etc.), or a combination of these hardware circuits. For example, the processor can be a hardware system with an instruction execution function, such as a CPU, DSP, etc., or a hardware system without an instruction execution function, such as an ASIC, FPGA, etc., or a combination of the above-mentioned hardware systems without an instruction execution function and hardware systems with an instruction execution function.
[0082] There are generally two current test code generation schemes:
[0083] One is to use template-based generation tools. The advantages of this method are: (1) Standardized test code can be quickly generated through templates, significantly improving development efficiency; (2) The generated code format is unified, which is convenient for team collaboration and code review; (3) The generated code has a high compilation success rate and rarely has syntax errors. However, it also has some disadvantages, such as:
[0084] (1) Lack of flexibility: The fixed structure of the template may not be able to handle all test scenarios and requirements.
[0085] The reasons include but are not limited to the following:
[0086] First, the versatility of the template:
[0087] Template-based code generation tools usually use predefined templates to generate test code. In order to maintain generality, these templates can only cover a part of common test scenarios. When faced with specific and complex test requirements, general templates may not be able to fully meet them, resulting in the generated test code not being able to cover all test scenarios.
[0088] Second, the limitations of custom rules:
[0089] Template-based code generation tools process the code under test according to the rules defined in the template. However, the code under test can be written in a variety of ways, and a single rule cannot cover the diverse code writing methods, resulting in the final generated test code being unable to cover all test scenarios.
[0090] (2) Maintenance cost: Updating and maintaining templates requires extra time and effort, especially in projects where requirements change frequently.
[0091] (3) Dependence on template quality: The quality of the generated test code is highly dependent on the design and writing quality of the template.
[0092] The other approach is to use large model technology to generate test code end-to-end. The advantages of this approach are: (1) high efficiency in code generation; (2) large models have strong reasoning and analysis capabilities, and can generate test code for different scenarios for the code under test, covering a wide range of test scenarios.
[0093] The main process of using big model technology to generate test code is generally to send the code under test to the big model through prompt instructions, instructing the big model to generate test sample code for testing the code under test. However, due to the existence of various factors such as training data bias, model complexity and overfitting, the current big models are prone to "hallucinations", that is, unrealistic or unreasonable content that appears in the prediction or generation tasks of the big models. The big model also has certain "hallucinations" in code generation, resulting in more syntax errors in the generated test code and a low compilation success rate. In actual applications, developers still need to make secondary modifications.
[0094] In view of the problems existing in the current test code generation scheme, the present application provides a test code generation scheme to solve or improve at least part of the problems existing in the traditional scheme to a certain extent.
[0095] The present invention provides a method for generating software test code. Figure 1 Taking the computer device in the example as an example, the computer device can be Figure 1 The terminal 100 or the system consisting of the terminal 100 and the server 200. Figure 4 , the software test code generation method specifically includes the following steps:
[0096] Step S100: parsing the structural information associated with the code under test, and determining the framework information required for the test code to be generated from the structural information.
[0097] The code under test refers to the code that needs to be tested. It can be the smallest testable unit in software development (usually a function or method), or a section of code that needs to be tested or a complete system code.
[0098] The test code to be generated is the test sample code used when performing software testing on the code under test.
[0099] In this step, the process of parsing the structural information associated with the tested code can use a code parser (common code parsers such as tree-sitter, javalang, etc.) to parse the structure of the class where the tested code is located and the file structure on which the tested code depends to obtain the parsed structural information.
[0100] Furthermore, in order to provide more reference information to the large model when the large model is called to generate test code later, the framework information required for the test code to be generated is determined from the structural information in this step.
[0101] Exemplarily, the framework information includes, but is not limited to: dependency packages that the test code needs to import (also referred to as Import information), environment information of the simulated Mock, and injected attribute information.
[0102] Combination Figure 5 , which provides a specific example of framework information, including Import information, Mock environment information, and injected attribute information.
[0103] Among them, Import information is used to introduce or include definitions of functions, classes, variables, etc. in other files (usually libraries, modules, or frameworks) so that they can be used in the current file. This means that these external resources can be used in the current program without rewriting existing functions.
[0104] Mock environment information refers to a virtual or simulated environment created to simulate certain parts of the real environment (especially external dependencies, such as API interfaces, databases, or other services).
[0105] Injected property information usually refers to the process of automatically inserting external dependencies or configuration information into a class or object in the Dependency Injection (DI) mechanism. This mechanism is widely used in various programming frameworks and libraries, especially those languages and frameworks that support object-oriented programming, such as Spring Framework (Java), ASP.NET Core (C#), Guice (Java), etc.
[0106] Step S110: Send the code under test to the configured big model to instruct the big model to generate test cases corresponding to the code under test.
[0107] In this embodiment, the capability of the large model can be called to generate test cases corresponding to the code under test.
[0108] A test case is a set of detailed test steps designed to verify the correctness of a specific function, performance or other characteristics. Test cases can be used to conduct tests in a planned and organized manner to ensure that each function of the software product works correctly according to the established requirements, while improving test efficiency, reducing omissions, and facilitating the evaluation of test results and tracking of software defects.
[0109] Combination Figure 6 As shown, the test case generated in this embodiment may include the following key components:
[0110] The use case scenario description, the input of the use case and the methods that need to be simulated, and the output of the use case.
[0111] Step S120: Send the combination of framework information and test cases to the big model to instruct the big model to generate additional test code based on the combination of framework information and test cases.
[0112] In this step, the framework information and test cases obtained in the previous two steps are combined, such as filling the test cases in the framework information. The combined code is sent to the big model, and the code generation capability of the big model is used to complete the corresponding test code.
[0113] Combination Figure 7 , which illustrates a method of Figure 5 The frame information shown and Figure 6 The code after the test cases are combined is shown, and the locations where the large model is needed for code completion. It should be noted that Figure 6 The test cases in the example are the test cases output by the large model according to the set format. In order to ensure the grammatical correctness when combining it with the framework information, Figure 6 The test cases shown are expressed in the form of comments, that is, Figure 7 Through " / "and" / " marks the test case comments.
[0114] The software test code generation method provided in this embodiment parses the structural information associated with the code under test, extracts the framework information required for the test code to be generated, and further allows the big model to generate test code under this framework, thereby reducing the "big model illusion", improving the quality of the generated test code, and increasing the compilation success rate of the test code.
[0115] Furthermore, in this embodiment, the big model is first called to generate test cases corresponding to the code under test, and then the test cases are combined through the framework information. The big model is called based on the combination of framework information and test cases to generate test code. The thinking chain generation method is adopted, that is, the task of generating test code for the code under test is split into first generating test cases for the code under test according to the processing logic, and further generating test code based on the test cases, simulating human processing logic, thereby improving the quality of the test code finally generated.
[0116] In addition, with the help of the strong reasoning and analysis capabilities of the large model, the test code generated for the code under test can cover more complete test scenarios, improving the problem of low test scenario coverage of test code generated by traditional template-based generation tools.
[0117] In some embodiments of the present application, the process of calling the large model to generate test cases in the aforementioned step S110 is described.
[0118] In some possible implementations, only the code under test may be fed into the big model to instruct the big model to generate test cases corresponding to the code under test.
[0119] In some other possible implementations, the code under test and the structural information associated with the code under test may be sent to the large model together to instruct the large model to generate test cases corresponding to the code under test based on the structural information associated with the code under test.
[0120] By feeding the structural information associated with the tested code into the big model, more abundant reference information can be provided to the big model, thereby improving the quality of the generated test cases.
[0121] In order to better call the big model, an embodiment of the present application designs a prompt format template, which is defined as a first prompt instruction prompt format template. The template includes a tested code slot, a structure information slot and a task instruction. The task instruction is used to instruct the big model to generate a test case based on the tested code in the tested code slot and the structure information in the structure information slot.
[0122] On this basis, the tested code can be filled into the tested code slot, and the structural information associated with the tested code can be filled into the structural information slot to obtain the first prompt instruction prompt. The first prompt instruction prompt is input into the large model to obtain the test case corresponding to the tested code output by the large model.
[0123] By adopting the method of this embodiment, by defining the above-mentioned first prompt format template, the prompt format specification when calling the large model to generate test cases is standardized, which can better stimulate the capabilities of the large model and generate high-quality test cases.
[0124] In some embodiments of the present application, the process of calling the large model to generate test code in the aforementioned step S120 is described.
[0125] In some possible implementations, only the combination of framework information and test cases may be fed into the big model to instruct the big model to supplementally generate test codes based on the combined code.
[0126] The embodiment of the present application designs a prompt format template, which is defined as a second prompt instruction prompt format template. The template includes a framework slot, a test case slot and a task instruction. The task instruction is used to instruct the large model to supplement the generation of test code based on the framework information in the framework slot and the test case in the test case slot.
[0127] On this basis, the framework information can be filled into the framework slot, and the test case corresponding to the tested code can be filled into the test case slot to obtain the second prompt instruction prompt. The second prompt instruction prompt is input into the large model to obtain the test code output by the large model.
[0128] By defining the above second prompt format template, the prompt format specification when calling the large model to generate test code is standardized, which can better stimulate the capabilities of the large model and generate high-quality test code.
[0129] In other possible implementations, in order to provide a larger model with richer reference information, framework information, test cases, structural information and the code under test can also be sent to the large model together to instruct the large model to generate additional test code for the code under test based on the framework information, test cases and structural information.
[0130] An embodiment of the present application designs a prompt format template, which is defined as a third prompt instruction prompt format template. The template includes a framework slot, a test case slot, a structure information slot, a tested code slot and a task instruction. The task instruction is used to instruct the large model to supplement and generate test code for the tested code in the tested code slot according to the framework information in the framework slot, the test case in the test case slot and the structure information in the structure information slot.
[0131] On this basis, the framework information can be filled into the framework slot, the test case corresponding to the tested code can be filled into the test case slot, the structure information associated with the tested code can be filled into the structure information slot, and the tested code can be filled into the tested code slot to obtain the third prompt instruction prompt. The third prompt instruction prompt is input into the large model to obtain the test code output by the large model.
[0132] The method provided in the above embodiment further sends the structural information associated with the tested code and the tested code itself into the large model, providing additional reference information for the large model to generate test code, which can assist the large model in improving the quality of the generated test code.
[0133] In addition, by defining the third prompt format template mentioned above, the prompt format specification when calling the large model to generate test code is standardized, which can better stimulate the capabilities of the large model and generate high-quality test code.
[0134] The software test code generation device provided in an embodiment of the present application is described below. The software test code generation device described below and the software test code generation method described above can be referenced to each other.
[0135] See also Figure 8 , Figure 8 A schematic diagram of the structure of a software test code generation device disclosed in an embodiment of the present application.
[0136] like Figure 8 As shown, the device may include:
[0137] The framework acquisition unit 11 is used to parse the structural information associated with the code under test, and determine the framework information required for the test code to be generated from the structural information;
[0138] A test case generating unit 12, used for sending the code under test into a configured large model to instruct the large model to generate a test case corresponding to the code under test;
[0139] The test code generation unit 13 is used to send the combination of the framework information and the test case to the large model to instruct the large model to supplement the generation of test code based on the combination of the framework information and the test case.
[0140] In some possible implementations, the process in which the test case generation unit sends the code under test to the configured big model to instruct the big model to generate a test case corresponding to the code under test includes:
[0141] The code under test and the structural information associated with the code under test are sent to the configured large model together to instruct the large model to generate a test case corresponding to the code under test based on the structural information associated with the code under test.
[0142] In some possible implementations, the test case generation unit sends the code under test and the structural information associated with the code under test to the configured big model to instruct the big model to generate a test case corresponding to the code under test based on the structural information associated with the code under test, including:
[0143] Obtain a configured first prompt instruction prompt format template, the template comprising a tested code slot, a structure information slot and a task instruction, the task instruction is used to instruct the large model to generate a test case based on the tested code in the tested code slot and the structure information in the structure information slot;
[0144] Fill the tested code into the tested code slot, and fill the structure information associated with the tested code into the structure information slot, to obtain a first prompt instruction prompt;
[0145] Input the first prompt instruction prompt into the large model to obtain the test case corresponding to the tested code output by the large model.
[0146] In some possible implementations, the test code generation unit sends the combination of the framework information and the test case to the large model to instruct the large model to supplement the process of generating the test code based on the combination of the framework information and the test case, including:
[0147] Obtain a configured second prompt instruction prompt format template, the template including a framework slot, a test case slot and a task instruction, the task instruction is used to instruct the large model to supplement and generate test code according to the framework information in the framework slot and the test case in the test case slot;
[0148] Fill the framework information into the framework slot, fill the test case corresponding to the tested code into the test case slot, and obtain a second prompt instruction prompt;
[0149] The second prompt instruction prompt is input into the large model to obtain the test code output by the large model.
[0150] In some possible implementations, the test code generation unit sends the combination of the framework information and the test case to the large model to instruct the large model to supplement the process of generating the test code based on the combination of the framework information and the test case, including:
[0151] The framework information, the test cases, the structural information and the code under test are sent to the large model together to instruct the large model to generate additional test code for the code under test based on the framework information, the test cases and the structural information.
[0152] In some possible implementations, the test code generation unit sends the framework information, the test case, the structure information, and the code under test to the big model to instruct the big model to supplement the process of generating the test code for the code under test based on the framework information, the test case, and the structure information, including:
[0153] Obtain a configured third prompt instruction prompt format template, the template including a framework slot, a test case slot, a structure information slot, a tested code slot and a task instruction, the task instruction is used to instruct the large model to supplement and generate test code for the tested code in the tested code slot according to the framework information in the framework slot, the test case in the test case slot and the structure information in the structure information slot;
[0154] Fill the framework information into the framework slot, fill the test case corresponding to the tested code into the test case slot, fill the structure information associated with the tested code into the structure information slot, fill the tested code into the tested code slot, and obtain a third prompt instruction prompt;
[0155] The third prompt instruction prompt is input into the large model to obtain the test code output by the large model.
[0156] In some possible implementations, the process of the framework obtaining the structural information associated with the unit parsing the tested code includes:
[0157] For the code under test, a code parser is used to parse the structure of the class where the code under test is located and the file structure on which the code under test depends to obtain structural information.
[0158] The present application also provides an electronic device in an embodiment. Fig. 9 , which shows a schematic diagram of the structure of an electronic device suitable for implementing the embodiments of the present application. The electronic devices in the embodiments of the present application may include, but are not limited to, mobile phones, tablet computers, wearable devices, augmented reality (AR) / virtual reality (VR) devices, laptop computers, ultra-mobile personal computers (UMPC), netbooks, personal digital assistants (PDA), etc. Fig. 9 The electronic device shown is merely an example and should not bring any limitation to the functions and scope of use of the embodiments of the present application.
[0159] like Fig. 9As shown, the electronic device may include a processing device (such as a central processing unit, a graphics processing unit, etc.) 601, which can perform various appropriate actions and processes according to a program stored in a read-only memory (ROM) 602 or a program loaded from a storage device 608 to a random access memory (RAM) 603, so as to implement the software test code generation method of the aforementioned embodiment of the present application. When the electronic device is powered on, various programs and data required for the operation of the electronic device are also stored in the RAM 603. The processing device 601, the ROM 602, and the RAM 603 are connected to each other via a bus 604. An input / output (I / O) interface 605 is also connected to the bus 604.
[0160] Typically, the following devices may be connected to the I / O interface 605: an input device 606 including, for example, a touch screen, a touch pad, a keyboard, a mouse, a camera, a microphone, an accelerometer, a gyroscope, etc.; an output device 607 including, for example, a liquid crystal display (LCD), a speaker, a vibrator, etc.; a storage device 608 including, for example, a memory card, a hard disk, etc.; and a communication device 609. The communication device 609 may allow the electronic device to communicate with other devices wirelessly or by wire to exchange data. Although Fig. 9 An electronic device having various devices is shown, but it should be understood that it is not required to implement or possess all the devices shown. More or fewer devices may be implemented or possessed instead.
[0161] An embodiment of the present application also provides a computer program product including computer-readable instructions. When the computer-readable instructions are executed on an electronic device, the electronic device implements any one of the software test code generation methods provided in the embodiments of the present application.
[0162] A computer-readable storage medium is also provided in an embodiment of the present application. The storage medium carries one or more computer programs. When the one or more computer programs are executed by an electronic device, the electronic device can implement any software test code generation method provided in the embodiment of the present application.
[0163] It should also be noted that the device embodiments described above are merely schematic, wherein the units described as separate components may or may not be physically separated, and the components displayed as units may or may not be physical units, that is, they may be located in one place, or they may be distributed over multiple network units. Some or all of the modules may be selected according to actual needs to achieve the purpose of the scheme of this embodiment. In addition, in the drawings of the device embodiments provided by the present application, the connection relationship between the modules indicates that there is a communication connection between them, which may be specifically implemented as one or more communication buses or signal lines.
[0164] Through the description of the above implementation mode, the technicians in the field can clearly understand that the present application can be implemented by means of software plus necessary general hardware, and of course, it can also be implemented by special hardware including special integrated circuits, special CPUs, special memories, special components, etc. In general, all functions completed by computer programs can be easily implemented by corresponding hardware, and the specific hardware structure used to implement the same function can also be various, such as analog circuits, digital circuits or special circuits. However, for the present application, software program implementation is a better implementation mode in more cases. Based on such an understanding, the technical solution of the present application is essentially or the part that contributes to the prior art can be embodied in the form of a software product, which is stored in a readable storage medium, such as a computer floppy disk, a U disk, a mobile hard disk, a ROM, a RAM, a disk or an optical disk, etc., including a number of instructions to enable a computer device (which can be a personal computer, a training device, or a network device, etc.) to execute the methods described in each embodiment of the present application.
[0165] In the above embodiments, all or part of the embodiments may be implemented by software, hardware, firmware or any combination thereof. When implemented by software, all or part of the embodiments may be implemented in the form of a computer program product.
[0166] The computer program product includes one or more computer instructions. When the computer program instructions are loaded and executed on a computer, the process or function described in the embodiment of the present application is generated in whole or in part. The computer may be a general-purpose computer, a special-purpose computer, a computer network, or other programmable devices. The computer instructions may be stored in a computer-readable storage medium, or transmitted from one computer-readable storage medium to another computer-readable storage medium. For example, the computer instructions may be transmitted from a website site, a computer, a training device, or a data center by wired (e.g., coaxial cable, optical fiber, digital subscriber line (DSL)) or wireless (e.g., infrared, wireless, microwave, etc.) mode to another website site, computer, training device, or data center. The computer-readable storage medium may be any available medium that a computer can store or a data storage device such as a training device, a data center, etc. that includes one or more available media integrations. The available medium may be a magnetic medium, (e.g., a floppy disk, a hard disk, a tape), an optical medium (e.g., a DVD), or a semiconductor medium (e.g., a solid-state drive (SSD)), etc.
[0167] The various embodiments in this specification are described in a progressive manner, and each embodiment focuses on the differences from other embodiments. The various embodiments can be combined as needed, and the same or similar parts can refer to each other.
Claims
1. A method for generating software test code, It is characterized in that include: Parsing structural information associated with the code under test, and determining framework information required for the test code to be generated from the structural information; Sending the code under test to the configured big model to instruct the big model to generate test cases corresponding to the code under test; The combination of the framework information and the test case is sent to the large model to instruct the large model to supplement and generate test code based on the combination of the framework information and the test case.
2. The method according to claim 1, It is characterized in that The process of sending the code under test to a configured large model to instruct the large model to generate a test case corresponding to the code under test includes: The code under test and the structural information associated with the code under test are sent to the configured large model together to instruct the large model to generate a test case corresponding to the code under test based on the structural information associated with the code under test.
3. The method according to claim 2, It is characterized in that The process of sending the code under test and the structural information associated with the code under test to the configured big model to instruct the big model to generate a test case corresponding to the code under test based on the structural information associated with the code under test includes: Obtain a configured first prompt instruction prompt format template, the template comprising a tested code slot, a structure information slot and a task instruction, the task instruction is used to instruct the large model to generate a test case based on the tested code in the tested code slot and the structure information in the structure information slot; Fill the tested code into the tested code slot, and fill the structure information associated with the tested code into the structure information slot, to obtain a first prompt instruction prompt; Input the first prompt instruction prompt into the large model to obtain the test case corresponding to the tested code output by the large model.
4. The method according to claim 1, It is characterized in that The process of sending the combination of the framework information and the test case into the large model to instruct the large model to supplement the generation of test code based on the combination of the framework information and the test case includes: Obtain a configured second prompt instruction prompt format template, the template including a framework slot, a test case slot and a task instruction, the task instruction is used to instruct the large model to supplement and generate test code according to the framework information in the framework slot and the test case in the test case slot; Fill the framework information into the framework slot, fill the test case corresponding to the tested code into the test case slot, and obtain a second prompt instruction prompt; The second prompt instruction prompt is input into the large model to obtain the test code output by the large model.
5. The method according to claim 1, It is characterized in that The process of sending the combination of the framework information and the test case into the large model to instruct the large model to supplement the generation of test code based on the combination of the framework information and the test case includes: The framework information, the test cases, the structural information and the code under test are sent to the large model together to instruct the large model to generate additional test code for the code under test based on the framework information, the test cases and the structural information.
6. The method according to claim 5, It is characterized in that The framework information, the test case, the structure information and the tested code are sent to the large model to instruct the large model to supplement the generation of the test code, including: Obtain a configured third prompt instruction prompt format template, the template including a framework slot, a test case slot, a structure information slot, a tested code slot and a task instruction, the task instruction is used to instruct the large model to supplement and generate test code for the tested code in the tested code slot according to the framework information in the framework slot, the test case in the test case slot and the structure information in the structure information slot; Fill the framework information into the framework slot, fill the test case corresponding to the tested code into the test case slot, fill the structure information associated with the tested code into the structure information slot, fill the tested code into the tested code slot, and obtain a third prompt instruction prompt; The third prompt instruction prompt is input into the large model to obtain the test code output by the large model.
7. The method according to any one of claims 1 to 6, It is characterized in that The process of parsing the structural information associated with the code under test includes: For the code under test, a code parser is used to parse the structure of the class where the code under test is located and the file structure on which the code under test depends to obtain structural information.
8. The method according to any one of claims 1 to 6, It is characterized in that The framework information includes at least one of the following: The test code needs to import dependency packages, simulate the Mock environment information, and inject attribute information.
9. The method according to any one of claims 1 to 6, It is characterized in that The test case includes: a use case scenario description, the input of the use case and the method that needs to be simulated Mock, and the output of the use case.
10. A software test code generation device, It is characterized in that include: A framework acquisition unit, used to parse the structural information associated with the code under test, and determine the framework information required for the test code to be generated from the structural information; A test case generation unit, used to send the code under test to a configured large model to instruct the large model to generate a test case corresponding to the code under test; The test code generation unit is used to send the combination of the framework information and the test case into the large model to instruct the large model to supplement the generation of test code based on the combination of the framework information and the test case.
11. An electronic device, It is characterized in that include: Memory and processor; The memory is used to store programs; The processor is used to execute the program to implement each step of the software test code generation method according to any one of claims 1 to 9.
12. A readable storage medium having a computer program stored thereon, It is characterized in that When the computer program is executed by a processor, each step of the software test code generation method according to any one of claims 1 to 9 is implemented.
13. A computer program product comprising a computer program, It is characterized in that When the computer program is executed by a processor, each step of the software test code generation method as described in any one of claims 1 to 9 is implemented.
Citation Information
Cited By
Method for generating Prompt based on large language model low-code programming
CN119847497A
Method for generating prompt based on large language model low code programming
CN119847497B
Detection tool generation method and device, medium and program product
CN120724434A
A method, apparatus, medium, and program product for generating a detection tool
CN120724434B
Test case generation method and related device
WO2026086131A1
