Test case generation method and device
By generating test cases using a neural network model, the problems of time-consuming and insufficient coverage of traditional manual generation are solved, achieving efficient and accurate test case generation that adapts to rapid software iteration.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2026-03-02
- Publication Date
- 2026-04-14
AI Technical Summary
Traditional test case generation relies on manual operation, which is time-consuming, inefficient, and lacks coverage. It cannot meet the needs of rapid software iteration and increases the risk of software release.
A pre-trained neural network model is used to extract and fuse target code features. Test cases are generated using feature encoding and decoding networks. The results are verified using test case management tools, and model parameters are optimized to improve accuracy.
It enables the rapid generation of test cases that meet actual needs, improves coverage and accuracy, reduces labor costs, and adapts to the needs of rapid software iteration.
Smart Images

Figure CN121858461A_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of software testing technology, and specifically to a method and apparatus for generating test cases. Background Technology
[0002] In the software development and testing process, the generation of test cases is a key step in ensuring software quality.
[0003] In related technologies, the design and writing of most test cases rely on the experience of test engineers and their understanding of requirements documents. This approach, which involves manually writing test cases, requires multiple steps, including manually analyzing requirements documents, understanding business logic, and designing test procedures. The entire process is time-consuming and prone to errors, making it unsuitable for the rapid iteration demands of current software development models. Furthermore, because manually designing test cases is often limited by personal experience, it cannot comprehensively cover all possible test scenarios, resulting in some software functions not being fully tested and increasing the risk of problems arising after software release. Summary of the Invention
[0004] This invention provides a method and apparatus for generating test cases, which solves the problems of traditional test case generation, which generally relies on manual operation, resulting in time-consuming, inefficient, and insufficient test coverage.
[0005] According to the first aspect, this embodiment provides a method for generating test cases, the method comprising:
[0006] Obtain the target test code; Extract the target code features corresponding to the target test code. The target code features include: target code attribute features and target code execution features. The target code attribute features and target code execution features are fused to obtain the target fusion features; The target fusion features are input into the test case generation model to generate the target test cases.
[0007] In some optional implementations, the target code attribute features include: title attribute features, comment attribute features, and function attribute features; the target code execution features include: task calculation features, task verification features, task transformation features, and task sending features; and the target test case attribute features and target test case execution features corresponding to the target test cases are extracted, including: The feature extraction network is used to extract title attribute features, comment attribute features, function attribute features, task calculation features, task verification features, task transformation features, and task sending features corresponding to the target test code.
[0008] In some optional implementations, the test case generation model is a pre-trained neural network model, and the training process of the test case generation model includes: Retrieve historical test code; Extract historical code features corresponding to historical test code. Historical code features include: historical code attribute features and historical code execution features. By fusing historical code attribute features and historical code execution features, a historical fusion feature is obtained; The historical fusion features are input into the test case generation model for training, which yields the historical test cases corresponding to the target test code, as well as the loss value between the historical test cases and the historical test code. The loss value is used to update the test case generation model.
[0009] In some optional implementations, the target fusion features are input into a test case generation model to generate target test cases, including: The target fusion feature is encoded using the feature encoding network in the test case generation model to obtain the feature vector of the target fusion feature; The target test case is obtained by decoding the encoded target fusion feature using the feature decoding network in the test case generation model.
[0010] In some optional implementations, during the training of the test case generation model, when the loss value is greater than or equal to a preset threshold, the weight parameters of the test case generation model are repeatedly adjusted until the loss value is less than the preset threshold.
[0011] In some optional implementations, the test case generation method in this embodiment further includes: Verify whether the target test case has an error using the test case management tool. If an error occurs in the target test case, return to the step of obtaining the test code until the target test case is verified to be correct using the test case management tool.
[0012] According to the second aspect, this embodiment also provides a test case generation apparatus, the apparatus comprising: The code acquisition module is used to acquire the target test code. The feature extraction module is used to extract target code features corresponding to the target test code. Target code features include: code attribute features and code execution features. The feature fusion module is used to fuse code attribute features and code execution features to obtain the target fused features; The test case generation module is used to input the target fusion features into the test case generation model to generate target test cases.
[0013] Thirdly, this embodiment also provides an electronic device, including: The memory and processor are interconnected and communicate with each other. The memory stores computer instructions, and the processor executes the computer instructions to perform the test case generation method in the first aspect or any embodiment of the first aspect.
[0014] Fourthly, this embodiment also provides a computer-readable storage medium storing computer instructions, which are used to cause a computer to execute the test case generation method in the first aspect or any embodiment of the first aspect.
[0015] Fifthly, this embodiment also provides a computer program product, including computer instructions, which are used to cause a computer to execute the test case generation method in the first aspect or any embodiment of the first aspect.
[0016] The technical solution of this invention has the following advantages: This invention discloses a method and apparatus for generating test cases. The method includes: obtaining target test code; extracting target code features corresponding to the target test code, including target code attribute features and target code execution features; fusing the target code attribute features and target code execution features to obtain target fused features; and inputting the target fused features into a test case generation model to generate target test cases. This invention utilizes a pre-trained neural network model to quickly generate target test cases, thereby improving test case coverage and accuracy. By introducing a test case generation model, this invention can better understand natural language descriptions, generating test cases that better meet actual needs and reducing the risks associated with subsequent software development. Furthermore, this invention improves the efficiency of test case generation and saves labor costs. Attached Figure Description
[0017] To more clearly illustrate the specific embodiments of the present invention or the technical solutions in the prior art, the drawings used in the description of the specific embodiments or the prior art will be briefly introduced below. Obviously, the drawings described below are some embodiments of the present invention. For those skilled in the art, other drawings can be obtained from these drawings without creative effort.
[0018] Figure 1 This is a first flowchart illustrating a method for generating test cases according to an embodiment of the present invention; Figure 2 This is a schematic diagram of a second method for generating test cases according to an embodiment of the present invention; Figure 3This is a structural block diagram of a test case generation device according to an embodiment of the present invention; Figure 4 This is a schematic diagram of the hardware structure of an electronic device according to an embodiment of the present invention. Detailed Implementation
[0019] To make the objectives, technical solutions, and advantages of the embodiments of the present invention clearer, the technical solutions of the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of the present invention, not all embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of the present invention.
[0020] It is understood that before using the technical solutions disclosed in the various embodiments of the present invention, users should be informed of the types, scope of use, and usage scenarios of the personal information involved in the present invention and their authorization should be obtained in accordance with relevant laws and regulations through appropriate means.
[0021] The terms "first" and "second" are used for descriptive purposes only and should not be construed as indicating or implying relative importance or implicitly specifying the number of technical features indicated. Thus, a feature defined as "first" or "second" may explicitly or implicitly include one or more of that feature. In the description of this invention, "a plurality of" means two or more, unless otherwise explicitly specified.
[0022] This embodiment provides a method for generating test cases, which can be used on computer devices or mobile terminals, such as mobile phones, tablets, desktop computers, laptops, servers, etc. Figure 1 This is a flowchart of a test case generation method according to an embodiment of the present invention, such as... Figure 1 As shown, the process includes the following steps: Step S101: Obtain the target test code.
[0023] Specifically, the target test code can be the source code of the software testing project.
[0024] Step S102: Extract the target code features corresponding to the target test code. The target code features include: target code attribute features and target code execution features.
[0025] In some examples, target code attribute features include: title attribute features, comment attribute features, and function attribute features; target code execution features include: task calculation features, task verification features, task transformation features, and task sending features. The target test case attribute features and target test case execution features corresponding to the target test cases are extracted, including: Specifically, extract attributes such as titles, comments, and functions associated with the target test code, as well as code calculations, verifications, transformations, and transmissions associated with executing the code, from the target test code.
[0026] In some specific implementations, the target test case attribute features and target test case execution features corresponding to the target test case are extracted, including: The feature extraction network is used to extract title attribute features, comment attribute features, function attribute features, task calculation features, task verification features, task transformation features, and task sending features corresponding to the target test code.
[0027] Feature extraction networks can include, but are not limited to, convolutional neural networks, recurrent neural networks, and self-attention mechanism networks.
[0028] Step S103: The target code attribute features and target code execution features are fused to obtain the target fused features.
[0029] Step S104: Input the target fusion features into the test case generation model to generate the target test cases.
[0030] In a specific example, the test case generation model in this embodiment is a pre-trained neural network model.
[0031] Specifically, the test case generation model can be a test case generation model, namely the Prompt prompt model. Through this test case generation model, the target test cases can be generated relatively accurately.
[0032] In traditional test case generation, the design and writing of most test cases rely heavily on the experience of test engineers and their understanding of the requirements document. This approach, involving manual test case writing, requires multiple steps, including manually analyzing the requirements document, understanding the business logic, and designing test procedures. The entire process is time-consuming and prone to errors, making it unsuitable for the rapid iteration demands of current software development models. Furthermore, because manually designed test cases are often limited by individual experience, they cannot comprehensively cover all possible test scenarios, resulting in some software functions not being fully tested and increasing the risk of problems arising after software release.
[0033] In some optional implementations, step S104 involves inputting the target fusion features into a test case generation model to generate target test cases, including: Step a1: Use the feature encoding network in the test case generation model to encode the target fusion features to obtain the feature vector of the target fusion features.
[0034] The encoding network includes a first multi-head attention module, which is generated based on the target fusion features. This includes: forming a dot-product attention module using a dot-product attention mechanism; setting a linear transformation module on the dot-product attention module to map the fusion features input to the dot-product attention module into multiple sets of vectors of predetermined dimensions through linear transformation; setting a connection module on the dot-product attention module to connect the vectors obtained after processing by the dot-product attention module; and forming the first multi-head attention module through the dot-product attention module, the linear transformation model, and the connection module. Step a2: Use the feature decoding network in the test case generation model to decode the encoded target fusion features to obtain the target test cases corresponding to the target fusion features.
[0035] The decoding network includes a second multi-head attention module and a prediction module. The prediction module includes a first attention module that uses dot product attention to calculate the currently predicted test cases and the already generated test cases, and a second attention module that uses dot product attention to calculate the currently predicted test cases and the future predicted test cases. Specifically, the first attention module and the second attention module are merged to form a fused attention module; the fused attention module is used to form a dot product attention module; a linear transformation module is set on the dot product attention module to map the input of the dot product attention module into multiple sets of vectors of predetermined dimensions; a connection module is set on the dot product attention module to connect the vectors obtained after processing by the dot product attention module; and a prediction module is formed by the dot product attention module, the linear transformation module, and the connection module.
[0036] Therefore, this embodiment utilizes a pre-trained neural network model to quickly generate target test cases, thereby improving test case coverage and accuracy. By introducing a test case generation model, it can better understand natural language descriptions and generate test cases that better meet actual needs. Furthermore, this embodiment improves the efficiency of test case generation and saves labor costs.
[0037] In some optional implementations, the training process for the test case generation model includes: Step b1: Obtain historical test code.
[0038] Specifically, historical test code is used as training sample code.
[0039] Step b2: Obtain the historical code features corresponding to the historical test code. The historical code features include: historical code attribute features and historical code execution features.
[0040] Step b3: Fuse the historical code attribute features and historical code execution features to obtain the historical fused features.
[0041] Step b4: Input the historical fusion features into the test case generation model for training to obtain the historical test cases corresponding to the target test code, and the loss value between the historical test cases and the historical test code. The loss value is used to update the test case generation model.
[0042] In some specific implementations, historical fusion features are input into a test case generation model to generate target test cases, including: Step c1: Use the feature encoding network in the test case generation model to encode the historical fusion features to obtain the feature vector of the historical fusion features.
[0043] Step c2: Use the feature decoding network in the test case generation model to decode the encoded historical fusion features to obtain the target test cases corresponding to the historical fusion features.
[0044] The specific structures of the feature encoding network and the feature decoding network are described in the above embodiments and will not be repeated here.
[0045] In some specific examples, during the training of the test case generation model, when the loss value is greater than or equal to a preset threshold, the weight parameters of the test case generation model are repeatedly adjusted until the loss value is less than the preset threshold.
[0046] The preset threshold can be a value between 0 and 1, and can be flexibly set according to the actual application scenario. Since the test case generation model often has inaccuracies during the training process, its parameters can be continuously optimized based on the output results to improve the accuracy of the test case generation model.
[0047] This embodiment provides a method for generating test cases, which can be used on computer devices or mobile terminals, such as mobile phones, tablets, desktop computers, laptops, servers, etc. Figure 2 This is a flowchart of a test case generation method according to an embodiment of the present invention, such as... Figure 2 As shown, the process includes the following steps: Step S101: Obtain the target test code.
[0048] Step S102: Extract the target code features corresponding to the target test code. The target code features include: target code attribute features and target code execution features.
[0049] Step S103: The target code attribute features and target code execution features are fused to obtain the target fused features.
[0050] Step S104: Input the target fusion features into the test case generation model to generate the target test cases.
[0051] Steps S101-S104 have been described in the above embodiments and will not be repeated here.
[0052] Step S105: Verify whether the target test case has an error using the test case management tool. If the target test case has an error, return to the step of obtaining the test code until the target test case is verified to be correct using the test case management tool.
[0053] In this embodiment, the test case management tool can be TestRail, Jira, or similar tools.
[0054] This embodiment verifies the target test cases using the aforementioned test case management tool, in order to further enhance the accuracy of the target test cases generated by the test case generation model.
[0055] This embodiment also provides a test case generation apparatus for implementing the above embodiments and preferred embodiments; details already described will not be repeated. As used below, the term "module" can be a combination of software and / or hardware that implements a predetermined function. Although the apparatus described in the following embodiments is preferably implemented in software, hardware implementation, or a combination of software and hardware, is also possible and contemplated.
[0056] This embodiment provides a test case generation device, such as... Figure 3 As shown, the device includes: Code acquisition module 301 is used to acquire the target test code; The feature extraction module 302 is used to extract target code features corresponding to the target test code. The target code features include: code attribute features and code execution features. The feature fusion module 303 is used to fuse code attribute features and code execution features to obtain target fused features; The test case generation module 304 is used to input the target fusion features into the test case generation model to generate target test cases.
[0057] In some optional implementations, the target code attribute features include: title attribute features, comment attribute features, and function attribute features; the target code execution features include: task calculation features, task verification features, task transformation features, and task sending features; and the feature extraction module 302 includes: The feature extraction submodule is used to extract title attribute features, comment attribute features, function attribute features, task calculation features, task verification features, task transformation features, and task sending features corresponding to the target test code using a feature extraction network.
[0058] In some optional implementations, the test case generation model is a pre-trained neural network model, and the training process of the test case generation model in the test case generation module 304 includes: The code retrieval submodule is used to retrieve historical test code; The feature extraction submodule is used to extract historical code features corresponding to historical test code. Historical code features include: historical code attribute features and historical code execution features. The feature fusion submodule is used to fuse historical code attribute features and historical code execution features to obtain historical fused features; The feature training submodule is used to input historical fused features into the test case generation model for training, to obtain the historical test cases corresponding to the target test code, and the loss value between the historical test cases and the historical test code. The loss value is used to update the test case generation model.
[0059] In some optional implementations, the target fusion features are input into a test case generation model to generate target test cases, including: The feature encoding network in the test case generation model is used to encode the target fusion features to obtain the feature vector of the target fusion features; The feature decoding network in the test case generation model is used to decode the encoded target fusion features to obtain the target test cases corresponding to the target fusion features.
[0060] In some optional implementations, during the training of the test case generation model, when the loss value is greater than or equal to a preset threshold, the weight parameters of the test case generation model are repeatedly adjusted until the loss value is less than the preset threshold.
[0061] In some optional implementations, the test case generation method in this embodiment further includes: The test case verification module is used to verify whether the target test case has an error through the test case management tool. If the target test case has an error, it returns to the step of obtaining the test code until the target test case is verified to be correct through the test case management tool.
[0062] The test case generation apparatus provided in this embodiment of the invention can execute the test case generation method provided in any embodiment of the invention, and has the corresponding functional modules and beneficial effects of the execution method. Further functional descriptions of the various modules and units described above are the same as in the corresponding embodiments described above, and will not be repeated here.
[0063] Figure 4 A schematic diagram of the structure of an electronic device provided in an embodiment of the present invention.
[0064] The following is a detailed reference. Figure 4 , Figure 4 This is a schematic diagram of the structure of an electronic device provided in an embodiment of the present invention.
[0065] The following is a detailed reference. Figure 4 This diagram illustrates a suitable structural schematic for implementing an electronic device according to embodiments of the present invention. The electronic device may include a processor (e.g., a central processing unit, graphics processor, etc.) 401, which can perform various appropriate actions and processes based on a program stored in read-only memory (ROM) 402 or a program loaded from memory 408 into random access random access memory (RAM) 403. The RAM 403 also stores various programs and data required for the operation of the electronic device. The processor 401, ROM 402, and RAM 403 are interconnected via a bus 404. An input / output (I / O) interface 405 is also connected to the bus 404.
[0066] Typically, the following devices can be connected to I / O interface 405: input devices 406 including, for example, touchscreens, touchpads, keyboards, mice, cameras, microphones, accelerometers, gyroscopes, etc.; output devices 407 including, for example, liquid crystal displays (LCDs), speakers, vibrators, etc.; memory devices 408 including, for example, magnetic tapes, hard disks, etc.; and communication devices 409. Communication device 409 allows electronic devices to communicate wirelessly or wiredly with other devices to exchange data. Although Figure 4 Electronic devices with various devices are shown, but it should be understood that it is not required to implement or have all of the devices shown, and more or fewer devices may be implemented or have instead.
[0067] In particular, according to embodiments of the present invention, the processes described above with reference to the flowcharts can be implemented as computer software programs. For example, embodiments of the present invention include a computer program product comprising a computer program carried on a non-transitory computer-readable medium, the computer program containing program code for performing the methods shown in the flowcharts. In such embodiments, the computer program can be downloaded and installed from a network via a communication device 409, or installed from a memory 408, or installed from a ROM 402. When the computer program is executed by the processor 401, it performs the functions defined in the test case generation method of the embodiments of the present invention.
[0068] Figure 4 The electronic device shown is merely an example and should not be construed as limiting the functionality and scope of use of the embodiments of the present invention.
[0069] This invention also provides a computer-readable storage medium. The methods described above according to embodiments of the invention can be implemented in hardware or firmware, or implemented as recordable on a storage medium, or implemented as computer code originally stored on a remote storage medium or a non-transitory machine-readable storage medium and subsequently stored on a local storage medium after being downloaded via a network. Thus, the methods described herein can be processed by software stored on a storage medium using a general-purpose computer, a dedicated processor, or programmable or dedicated hardware. The storage medium can be a magnetic disk, optical disk, read-only memory, random access memory, flash memory, hard disk, or solid-state drive, etc.; further, the storage medium may also include combinations of the above types of memory. It is understood that computers, processors, microprocessor controllers, or programmable hardware include storage components capable of storing or receiving software or computer code. When the software or computer code is accessed and executed by the computer, processor, or hardware, the test case generation method shown in the above embodiments is implemented.
[0070] A portion of this invention can be applied as a computer program product, such as computer program instructions, which, when executed by a computer, can invoke or provide the methods and / or technical solutions according to the invention through the operation of the computer. Those skilled in the art will understand that the forms in which computer program instructions exist in a computer-readable medium include, but are not limited to, source files, executable files, installation package files, etc. Correspondingly, the ways in which computer program instructions are executed by a computer include, but are not limited to: the computer directly executing the instructions, or the computer compiling the instructions and then executing the corresponding compiled program, or the computer reading and executing the instructions, or the computer reading and installing the instructions and then executing the corresponding installed program. Here, the computer-readable medium can be any available computer-readable storage medium or communication medium accessible to a computer.
[0071] Although embodiments of the invention have been described in conjunction with the accompanying drawings, those skilled in the art can make various modifications and variations without departing from the spirit and scope of the invention, and such modifications and variations all fall within the scope defined by the appended claims.
Claims
1. A method for generating test cases, characterized in that, The method includes: Obtain the target test code; Extract the target code features corresponding to the target test code, the target code features including: target code attribute features and target code execution features; The target code attribute features and the target code execution features are fused to obtain the target fusion features; The target fusion features are input into the test case generation model to generate target test cases.
2. The method according to claim 1, characterized in that, The target code attribute features include: title attribute features, comment attribute features, and function attribute features. The target code execution features include: task calculation features, task verification features, task transformation features, and task sending features. The target test case attribute features and target test case execution features corresponding to the target test case are extracted, including: The feature extraction network is used to extract the title attribute features, comment attribute features, function attribute features, task calculation features, task verification features, task transformation features, and task sending features corresponding to the target test code.
3. The method according to claim 1, characterized in that, The test case generation model is a pre-trained neural network model, and the training process of the test case generation model includes: Retrieve historical test code; Extract the historical code features corresponding to the historical test code, the historical code features including: historical code attribute features and historical code execution features; The historical code attribute features and the historical code execution features are fused together to obtain historical fusion features; The historical fusion features are input into the test case generation model for training to obtain the historical test cases corresponding to the target test code, and the loss value between the historical test cases and the historical test code. The loss value is used to update the test case generation model.
4. The method according to claim 1, characterized in that, The target fusion features are input into the test case generation model to generate target test cases, including: The target fusion feature is encoded using the feature encoding network in the test case generation model to obtain the feature vector of the target fusion feature; The target test case is obtained by decoding the encoded target fusion feature using the feature decoding network in the test case generation model.
5. The method according to claim 3, characterized in that, During the training of the test case generation model, when the loss value is greater than or equal to a preset threshold, the weight parameters of the test case generation model are repeatedly adjusted until the loss value is less than the preset threshold.
6. The method according to claim 1, characterized in that, Also includes: The test case management tool is used to verify whether the target test case has an error. If the target test case has an error, the process returns to the step of obtaining the test code until the target test case is verified to be correct by the test case management tool.
7. A test case generation device, characterized in that, The device includes: The code acquisition module is used to acquire the target test code. The feature extraction module is used to extract target code features corresponding to the target test code, wherein the target code features include: code attribute features and code execution features; The feature fusion module is used to fuse the code attribute features and the code execution features to obtain the target fused features; The test case generation module is used to input the target fusion features into the test case generation model to generate target test cases.
8. An electronic device, characterized in that, include: A memory and a processor are communicatively connected, the memory storing computer instructions, and the processor executing the computer instructions to perform the test case generation method of any one of claims 1 to 6.
9. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores computer instructions for causing a computer to execute the test case generation method according to any one of claims 1 to 6.
10. A computer program product, characterized in that, It includes computer instructions for causing a computer to execute the method for generating test cases according to any one of claims 1 to 6.