Test case generation method and device and program product
By using generative deep learning models trained across multiple business domains and reinforcement learning strategies, the test case generation process is optimized, solving the problem of low coverage caused by a single model and achieving more efficient test case generation and software quality assurance.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-12-12
- Publication Date
- 2026-03-31
AI Technical Summary
Existing AI-based test case generation technologies mostly employ a single generation model and lack effective optimization methods, resulting in low test case coverage.
Generative deep learning models are trained using historical test cases from multiple business domains. The initial test case set is evaluated using coverage metrics, and the target test case set is generated through iterative optimization using reinforcement learning strategies and multi-objective optimization algorithms, including techniques such as constructing abstract syntax trees, calculating cosine similarity, and configuring reward functions.
It significantly improves the comprehensiveness and quality of test cases, reduces manual intervention, shortens the software testing cycle, and improves overall R&D efficiency.
Smart Images

Figure CN121764797A_ABST
Abstract
Description
Technical Field
[0001] This application applies to the fields of artificial intelligence and fintech, and specifically relates to a method, apparatus, and program product for generating test cases. Background Technology
[0002] Software testing is a crucial step in ensuring software quality and stability. Traditionally, test case generation relies on manual writing or automated tools based on fixed rules. However, with the increasing complexity of software, the limitations of this generation method have become increasingly apparent: on the one hand, manually designing test cases is time-consuming and prone to overlooking boundary scenarios and anomalies; on the other hand, while rule-based automated tools can improve efficiency, they lack flexibility and struggle to adapt to changing software requirements and complex system architectures.
[0003] In recent years, artificial intelligence technology has been increasingly applied in the field of software testing, especially in the automatic generation of test cases, demonstrating enormous potential. Machine learning, particularly deep learning models, has become a key technology for automated test case generation due to its powerful data processing and pattern recognition capabilities. For example, generative deep learning models can automatically generate test cases that match the functions of the software under test by learning from a large number of historical test cases.
[0004] Nevertheless, existing AI-based test case generation technologies still have significant shortcomings. Most systems employ a single generation model, lacking effective feedback and self-optimization mechanisms, resulting in a deficiency in the diversity and comprehensiveness of the generated test cases.
[0005] Currently, there is no effective solution to the problem that the generation of test cases in related technologies often uses a single generation model and lacks effective optimization methods, resulting in low test case coverage. Summary of the Invention
[0006] The main objective of this application is to provide a method, apparatus, and program product for generating test cases, in order to solve the problem that in related technologies, the generation of test cases often uses a single generation model and lacks effective optimization methods, resulting in low coverage of test cases.
[0007] To achieve the above objectives, according to one aspect of this application, a method for generating test cases is provided. The method includes: receiving source code data to be tested, wherein the source code data is code running in the system to be tested; processing the source code data using a target generation model to generate an initial test case set, wherein the target generation model is obtained by training a generative deep learning model using historical test cases from multiple business domains; evaluating the initial test case set based on a coverage metric to obtain an evaluation result; and iteratively optimizing the initial test case set based on the evaluation result to obtain a target test case set for the system to be tested.
[0008] Further, the coverage metric includes at least a first metric, a second metric, and a third metric. For each initial test case in the initial test case set, the initial test case set is evaluated based on the coverage metric to obtain an evaluation result, including: constructing an abstract syntax tree corresponding to the source code to be tested, and determining the executed code and the executed code path by executing the initial test cases in a preset environment; calculating the first metric based on the abstract syntax tree and the executed code, and calculating the second metric based on the abstract syntax tree and the executed code path, wherein the first metric represents the code coverage ratio of the test case, and the second metric represents the code branch coverage ratio of the test case; calculating the cosine similarity between each initial test case based on the cosine similarity calculation method, and calculating the third metric based on the cosine similarity, wherein the third metric represents the degree of difference between the test cases; and determining the evaluation result based on the first metric, the second metric, and the third metric.
[0009] Further, after processing the source code data using a target generation model to generate an initial test case set, the process includes: determining the state space of the reinforcement learning strategy based on the coverage metric; determining the action space of the reinforcement learning strategy based on target parameters, wherein the target parameters include: a temperature parameter, a beam search width, and a sampling strategy, the temperature parameter being a parameter used by the target generation model to control the randomness and sampling diversity of test cases when generating test cases; determining the reward function of the reinforcement learning strategy based on the growth rate of the coverage metric; iteratively optimizing the target parameters based on the state space, the action space, and the reward function until the coverage metric reaches a preset threshold, or the number of iterations reaches a preset number, and updating the target parameters based on the optimized target parameters.
[0010] Further, the initial test case set is iteratively optimized based on the evaluation results to obtain a target test case set for the system under test. This includes: determining a target optimization function based on multiple optimization objectives, wherein the multiple optimization objectives include at least: the coverage index, test case execution time, and resource consumption information; using fast non-dominated sorting to iteratively optimize a preset population based on the target optimization function to obtain multiple levels, wherein the preset population contains multiple individuals, each individual representing parameter information for generating test cases, and multiple individuals in the (N+1)th level are superior to multiple individuals in the Nth level in at least one optimization objective, wherein the parameter information includes at least: population size, crossover probability, and mutation probability; and filtering the initial test case set based on the crowding of the multiple levels to obtain the target test case set.
[0011] Furthermore, the generative deep learning model includes at least a generator and a discriminator. Before processing the source code data using the target generative model to generate the initial test case set, the method further includes: collecting real test cases from multiple business domains and historical test code corresponding to the real test cases from preset channels, wherein the real test cases include test cases of different complexity; inputting the sample feature vectors corresponding to the historical test code into the generator, generating historical test cases through the generator's self-attention mechanism and autoregressive method; inputting the historical test cases into the discriminator, extracting test case features through the convolutional structure in the discriminator, and calculating the similarity between the historical test cases and the real test cases based on the test case features, wherein the test case features include at least local features and global features; calculating the loss function value based on the similarity, and training the generative deep learning model based on the batch gradient descent algorithm until the model converges to obtain the target generative model.
[0012] Further, the sample feature vector corresponding to the historical test code is input into the generator, and historical test cases are generated through the generator's self-attention mechanism and autoregressive method. This includes: inputting the sample feature vector into the generator, wherein the generator includes multiple converter modules, each converter module adopts a variational autoencoder architecture, and each converter module includes an encoder and a decoder. The encoder calculates the association weights of each element in the input sample feature vector based on the self-attention mechanism, and fuses the outputs of the multiple converter modules to obtain a fused feature vector. The decoder generates a candidate test case sequence for each label in the fused feature vector based on the autoregressive method, and uses a beam search algorithm to filter the candidate test case sequence to obtain the historical test cases.
[0013] Furthermore, before inputting the sample feature vector corresponding to the historical test code into the generator, the method further includes: performing a preprocessing operation on the historical test code to obtain processed code, wherein the preprocessing operation includes at least: deleting redundant information, standardizing naming, and standardizing data format; performing lexical analysis on the processed code using a syntax parsing model to obtain multiple tags, and performing syntax analysis on the multiple tags to construct an abstract syntax tree; extracting code semantic information from the abstract syntax tree using a semantic analysis model, wherein the code semantic information includes at least: code function information, function call relationships, data dependency information, and code control paths; and converting the code semantic information into the sample feature vector using a word embedding algorithm, wherein the sample feature vector is stored in tensor format.
[0014] Further, after obtaining the target test case set for the system under test, the method further includes: converting the target test case set into a structured standard data format to obtain a test file, wherein the test file includes at least one of the following: a first type of file, a second type of file, and a third type of file, wherein the first type of file includes at least test class information, test method information, and assertion statement information, the second type of file includes at least test function information and test scenario information, and the third type of file includes at least test case metadata information, input information, and expected output information; performing syntax verification on the test file and performing feasibility verification on the test file using a symbolic execution algorithm to obtain a verification result; and importing the test file into the system under test if the verification result indicates that the verification is successful.
[0015] To achieve the above objectives, according to another aspect of this application, a test case generation apparatus is provided. The apparatus includes: a receiving unit for receiving source code data to be tested, wherein the source code data is code running on the system under test; a first generation unit for processing the source code data using a target generation model to generate an initial test case set, wherein the target generation model is obtained by training a generative deep learning model using historical test cases from multiple business domains; an evaluation unit for evaluating the initial test case set based on a coverage metric to obtain an evaluation result; and a first optimization unit for iteratively optimizing the initial test case set based on the evaluation result to obtain a target test case set for the system under test.
[0016] Further, the coverage metric includes at least a first metric, a second metric, and a third metric. For each initial test case in the initial test case set, the evaluation unit includes: a construction subunit, used to construct an abstract syntax tree corresponding to the source code to be tested, and to determine the executed code and the executed code path by executing the initial test case in a preset environment; a first calculation subunit, used to calculate the first metric based on the abstract syntax tree and the executed code, and to calculate the second metric based on the abstract syntax tree and the executed code path, wherein the first metric represents the code coverage ratio of the test case, and the second metric represents the code branch coverage ratio of the test case; a second calculation subunit, used to calculate the cosine similarity between each initial test case based on the cosine similarity calculation method, and to calculate the third metric based on the cosine similarity, wherein the third metric represents the degree of difference between the test cases; and a first determination subunit, used to determine the evaluation result based on the first metric, the second metric, and the third metric.
[0017] Further, the apparatus includes: a first determining unit, configured to determine the state space of a reinforcement learning strategy based on the coverage metric after processing the source code data using a target generation model to generate an initial test case set; a second determining unit, configured to determine the action space of the reinforcement learning strategy based on target parameters, wherein the target parameters include: a temperature parameter, a beam search width, and a sampling strategy, wherein the temperature parameter is a parameter used by the target generation model to control the randomness and sampling diversity of test cases when generating test cases; a third determining unit, configured to determine the reward function of the reinforcement learning strategy based on the growth rate of the coverage metric; and a second optimizing unit, configured to iteratively optimize the target parameters based on the state space, the action space, and the reward function until the coverage metric reaches a preset threshold or the number of iterations reaches a preset number, and update the target parameters based on the optimized target parameters.
[0018] Further, the first optimization unit includes: a second determining subunit, used to determine a target optimization function based on multiple optimization objectives, wherein the multiple optimization objectives include at least: the coverage index, test case execution time and resource consumption information; an optimization subunit, used to iteratively optimize a preset population based on the target optimization function using fast non-dominated sorting to obtain multiple levels, wherein the preset population contains multiple individuals, each individual representing parameter information for generating test cases, and multiple individuals in the (N+1)th level are superior to multiple individuals in the Nth level in at least one optimization objective, wherein the parameter information includes at least: population size, crossover probability, and mutation probability; and a filtering subunit, used to filter the initial test case set based on the crowding of the multiple levels to obtain the target test case set.
[0019] Furthermore, the generative deep learning model includes at least a generator and a discriminator, and the apparatus further includes: a collection unit, used to collect real test cases and historical test code corresponding to the real test cases from preset channels before processing the source code data using the target generative model to generate an initial test case set, wherein the real test cases include test cases of different complexity; a second generation unit, used to input the sample feature vector corresponding to the historical test code into the generator, and generate historical test cases through the generator's self-attention mechanism and autoregressive method; a calculation unit, used to input the historical test cases into the discriminator, extract test case features through the convolutional structure in the discriminator, and calculate the similarity between the historical test cases and the real test cases based on the test case features, wherein the test case features include at least local features and global features; and a training unit, used to calculate the loss function value based on the similarity, and train the generative deep learning model based on the batch gradient descent algorithm until the model converges to obtain the target generative model.
[0020] Further, the second generation unit includes a processing subunit for inputting the sample feature vector into the generator, wherein the generator includes multiple converter modules, each converter module adopts a variational autoencoder architecture, and each converter module includes an encoder and a decoder. The encoder calculates the correlation weights of each element in the sample feature vector input based on a self-attention mechanism, and fuses the outputs of the multiple converter modules to obtain a fused feature vector. The decoder generates a sequence of candidate test cases for each label in the fused feature vector based on an autoregressive approach, and uses a beam search algorithm to filter the sequence of candidate test cases to obtain the historical test cases.
[0021] Furthermore, the device further includes: a first processing unit, configured to preprocess the historical test code before inputting the sample feature vector corresponding to the historical test code into the generator to obtain processed code, wherein the preprocessing operation includes at least: deleting redundant information, standardizing naming, and standardizing data format; a second processing unit, configured to perform lexical analysis on the processed code using a syntax parsing model to obtain multiple tags, and perform syntax analysis on the multiple tags to construct an abstract syntax tree; an extraction unit, configured to extract code semantic information from the abstract syntax tree using a semantic analysis model, wherein the code semantic information includes at least: code function information, function call relationships, data dependency information, and code control paths; and a first conversion unit, configured to convert the code semantic information into the sample feature vector using a word embedding algorithm, wherein the sample feature vector is stored in tensor format.
[0022] Furthermore, the apparatus further includes: a second conversion unit, configured to convert the target test case set for the system under test into a structured standard data format to obtain a test file after obtaining the target test case set for the system under test, wherein the test file includes at least one of the following: a first type of file, a second type of file, and a third type of file, wherein the first type of file includes at least test class information, test method information, and assertion statement information, the second type of file includes at least test function information and test scenario information, and the third type of file includes at least test case metadata information, input information, and expected output information; a verification unit, configured to perform syntax verification on the test file and perform feasibility verification on the test file using a symbolic execution algorithm to obtain a verification result; and a third processing unit, configured to import the test file into the system under test if the verification result indicates that the verification is successful.
[0023] To achieve the above objectives, according to one aspect of this application, a computer program product is provided, comprising a computer program that, when executed by a processor, implements the test case generation method described in any of the above claims, and the computer program, when executed by a processor, implements the steps of the test case generation method described in various embodiments of this application.
[0024] To achieve the above objectives, according to one aspect of this application, a computer-readable storage medium is provided, the computer-readable storage medium including stored computer instructions, wherein, when the computer instructions are executed by a processor, the method for generating test cases described in any one of the above claims is implemented.
[0025] To achieve the above objectives, according to one aspect of this application, an electronic device is provided, including one or more processors and a memory, the memory being used to store one or more programs, wherein when the one or more programs are executed by the one or more processors, the one or more processors cause the one or more processors to implement the test case generation method described in any one of the above claims.
[0026] In this embodiment, source code data to be tested is received, wherein the source code data is code running in the system to be tested; the source code data is processed using a target generation model to generate an initial test case set, wherein the target generation model is obtained by training a generative deep learning model with historical test cases from multiple business domains; the initial test case set is evaluated based on a coverage metric to obtain an evaluation result; and the initial test case set is iteratively optimized based on the evaluation result to obtain a target test case set for the system to be tested. This solves the technical problem that the generation of test cases often uses a single generation model and lacks effective optimization methods, resulting in low test case coverage.
[0027] By employing a cross-domain trained target generation model to process the source code data to be tested, an initial test case set is generated. This initial test case set is then iteratively optimized based on coverage metrics. This dynamic adjustment of the generation strategy ensures that test cases comprehensively cover code, paths, and requirements, significantly improving test case quality. The optimized target test case set more accurately locates and verifies potential defects in the software, further enhancing the comprehensiveness and effectiveness of software testing. Simultaneously, it reduces manual intervention, accelerates the software testing cycle, and improves overall development efficiency. Attached Figure Description
[0028] The accompanying drawings, which form part of this application, are used to provide a further understanding of this application. The illustrative embodiments and descriptions of this application are used to explain this application and do not constitute an undue limitation of this application. In the drawings:
[0029] Figure 1 This is a hardware structure block diagram of a computer terminal (or mobile device) for implementing a test case generation method according to Embodiment 1 of this application;
[0030] Figure 2 This is a flowchart of an optional test case generation method provided according to Embodiment 1 of this application;
[0031] Figure 3 This is a schematic diagram of the structure of the optional test case generation system provided in Embodiment 1 of this application;
[0032] Figure 4This is a flowchart illustrating the optional test case generation and optimization process provided in Embodiment 1 of this application;
[0033] Figure 5 This is a flowchart illustrating the optional test case loop optimization provided in Embodiment 1 of this application;
[0034] Figure 6 This is a flowchart illustrating the multi-objective optimization and output of optional test cases according to Embodiment 1 of this application;
[0035] Figure 7 This is a schematic diagram of a test case generation device according to Embodiment 2 of this application;
[0036] Figure 8 This is a schematic diagram of an electronic device generated according to the test cases provided in Embodiment 3 of this application. Detailed Implementation
[0037] It should be noted that, unless otherwise specified, the embodiments and features described in this application can be combined with each other. This application will now be described in detail with reference to the accompanying drawings and embodiments.
[0038] It should be noted that the processing method, apparatus, storage medium, and electronic device specified in this application can be used in the process of generating test cases in the financial technology field, thereby improving the coverage of test cases. They can also be used in any field other than the financial technology field. The application fields of the processing method, apparatus, storage medium, and electronic device specified in this application are not limited.
[0039] It should be noted that the information collected in this application (including but not limited to user device information, user personal information, collected data, used data, generated data, processed data, etc.) and the data (including but not limited to data used for analysis, stored data, displayed data, collected information, used information, generated information, processed information, etc.) are all information and data authorized by the user or fully authorized by all parties. Furthermore, the collection, storage, use, processing, transmission, provision, disclosure, and application of the relevant data all comply with the relevant laws, regulations, and standards of the relevant countries and regions, have taken necessary confidentiality measures, do not violate public order and good morals, and provide corresponding operation entry points for users to choose to authorize or refuse. For example, this system has interfaces with relevant users or organizations, providing users with corresponding operation entry points for users to choose to agree to or refuse automated decision results; if the user chooses to refuse, the process proceeds to the expert decision-making stage.
[0040] Example 1
[0041] According to an embodiment of this application, a method embodiment for generating test cases is also provided. It should be noted that the steps shown in the flowchart in the accompanying drawings can be executed in a computer system such as a set of computer-executable instructions. Furthermore, although a logical order is shown in the flowchart, in some cases, the steps shown or described may be executed in a different order than that shown here.
[0042] The method embodiment provided in Embodiment 1 of this application can be executed in a mobile terminal, computer terminal or similar computing device. Figure 1 This is a hardware structure block diagram of a computer terminal (or mobile device) for implementing a test case generation method, according to Embodiment 1 of this application. Figure 1 As shown, the computer terminal 10 (or mobile device) may include one or more processors 102 (shown as 102a, 102b, ..., 102n in the figure) 102 (processor 102 may include, but is not limited to, a microprocessor MCU or a programmable logic device FPGA, etc.), a memory 104 for storing data, and a transmission device 106 for communication functions. In addition, it may also include: a display, an input / output interface (I / O interface), a universal serial bus (USB) port (which may be included as one of the ports of a BUS bus), a network interface, a power supply, and / or a camera. Those skilled in the art will understand that... Figure 1 The structure shown is for illustrative purposes only and does not limit the structure of the aforementioned electronic device. For example, computer terminal 10 may also include... Figure 1 The more or fewer components shown, or having the same Figure 1 The different configurations shown.
[0043] It should be noted that the aforementioned one or more processors 102 and / or other data processing circuits are generally referred to herein as "data processing circuits". These data processing circuits may be embodied, in whole or in part, in software, hardware, firmware, or any other combination thereof. Furthermore, the data processing circuits may be a single, independent processing module, or may be integrated, in whole or in part, into any other element within the computer terminal 10 (or mobile device). As involved in the embodiments of this application, the data processing circuits serve as a processor control mechanism (e.g., selection of a variable resistor termination path connected to an interface).
[0044] The memory 104 can be used to store software programs and modules of application software, such as the program instructions / data storage device corresponding to the test case generation method in this embodiment. The processor 102 executes various functional applications and data processing by running the software programs and modules stored in the memory 104, thereby implementing the aforementioned test case generation method. The memory 104 may include high-speed random access memory, and may also include non-volatile memory, such as one or more magnetic storage devices, flash memory, or other non-volatile solid-state memory. In some instances, the memory 104 may further include memory remotely located relative to the processor 102, and these remote memories can be connected to the computer terminal 10 via a network. Examples of such networks include, but are not limited to, the Internet, corporate intranets, local area networks, mobile communication networks, and combinations thereof.
[0045] The transmission device 106 is used to receive or send data via a network. Specific examples of the network described above may include a wireless network provided by the communication provider of the computer terminal 10. In one example, the transmission device 106 includes a Network Interface Controller (NIC), which can connect to other network devices via a base station to communicate with the Internet. In another example, the transmission device 106 may be a Radio Frequency (RF) module, used for wireless communication with the Internet.
[0046] The display may be, for example, a touchscreen liquid crystal display (LCD) that allows the user to interact with the user interface of the computer terminal 10 (or mobile device).
[0047] Under the aforementioned operating environment, this application provides the following: Figure 2 The method for generating test cases is shown. Figure 2 This is a flowchart of an optional test case generation method provided according to Embodiment 1 of this application.
[0048] Step S201: Receive the source code data to be tested, wherein the source code data is the code running on the system to be tested.
[0049] The executing entity of Embodiment 1 of this application can be a test case generation system, which aims to automatically generate and optimize software test cases. This system receives the requirements specification description or source code of the software under test as input, and generates test cases that match the functions of the software under test by using generative deep learning models and reinforcement learning algorithms. Through the above-mentioned functions, the test case generation system strives to improve the automation level of software testing, reduce manual intervention, and improve the comprehensiveness and effectiveness of test cases, while balancing testing efficiency and resource consumption while ensuring test quality.
[0050] In this embodiment 1, to generate test cases, it is necessary to receive and process source code data from the system under test. Source code data specifically refers to the set of programming instructions that constitute the software entity under test; these instructions define the software's behavior and functions. The purpose of receiving this data is to enable the generation system to understand and analyze the software's functions and execution logic.
[0051] For example, source code data enters the system via an interface or file upload. It is then cleaned, normalized, and its features extracted, transforming it into a format suitable for generative deep learning models. This process ensures the accuracy and applicability of the input data, laying the foundation for subsequent test case generation.
[0052] Step S202: The source code data is processed using a target generation model to generate an initial test case set. The target generation model is obtained by training a generative deep learning model with historical test cases from multiple business domains.
[0053] In this embodiment 1, when processing source code data, the model first parses and extracts key features from the source code, transforming it into a format that can be processed by machine learning algorithms. Then, the model utilizes its learning experience from historical test cases to generate test cases related to the functions of the system under test. The generation process follows statistical regularities and patterns from historical test cases to cover various execution paths and boundary conditions of the software.
[0054] For example, during the training of a generative deep learning model to obtain the target generative model, historical test cases from different business domains can be used for training. This enables the model to understand the construction principles of test cases and the key points of test coverage, thus allowing it to generate effective test cases that are both syntactically correct and closely reflect the characteristics of the system software under test when faced with new source code. These initial test case sets form the basis for subsequent optimization and improvement, ensuring the comprehensiveness and automation level of testing.
[0055] Step S203: Evaluate the initial test case set based on the coverage metric to obtain the evaluation results.
[0056] In this embodiment 1, the quality of the generated initial test cases is evaluated based on coverage metrics. Coverage metrics include, but are not limited to, code coverage, path coverage, and requirement coverage, which reflect the proportion of software code lines, execution paths, and functional requirements covered by the test cases, respectively. The system calculates the coverage values of the initial test cases, which serve as the basis for evaluation. The evaluation results not only include quantitative data on coverage but may also involve correlation analysis between coverage and test case diversity, as well as evaluations of test case execution efficiency and resource consumption. The evaluation results are used to guide the subsequent test case optimization process.
[0057] Step S204: Based on the evaluation results, iteratively optimize the initial test case set to obtain the target test case set for the system to be tested.
[0058] In this embodiment 1, based on the coverage metric evaluation results calculated in the previous stage, deficiencies in test cases regarding software requirements, execution paths, and code coverage are identified. The evaluation results serve as feedback input, guiding the reinforcement learning algorithm to adjust its generation strategy to enhance the diversity and effectiveness of test cases. In iterative optimization, the system dynamically adjusts generation parameters, such as temperature parameters, beam search width, and sampling strategies, to generate new test case variants to explore uncovered software areas. Through multiple iterations, the system continuously evaluates and improves the test case set, resulting in the target test case set. Through this iterative optimization process, the system can learn and adjust itself, gradually improving the quality of the test case set to meet software testing requirements.
[0059] Optionally, in the test case generation method provided in Embodiment 1 of this application, the coverage metric includes at least a first metric, a second metric, and a third metric. For each initial test case in the initial test case set, the initial test case set is evaluated based on the coverage metric to obtain an evaluation result, including: constructing an abstract syntax tree corresponding to the source code to be tested, and determining the executed code and the executed code path by executing the initial test cases in a preset environment; calculating the first metric based on the abstract syntax tree and the executed code, and calculating the second metric based on the abstract syntax tree and the executed code path, wherein the first metric represents the code coverage ratio of the test case, and the second metric represents the code branch coverage ratio of the test case; calculating the cosine similarity between each initial test case based on the cosine similarity calculation method, and calculating the third metric based on the cosine similarity, wherein the third metric represents the degree of difference between the test cases; and determining the evaluation result based on the first metric, the second metric, and the third metric.
[0060] In this embodiment 1, to quantify the coverage quality and diversity of the test case set for software testing, the system first receives the source code data to be tested and constructs a corresponding Abstract Syntax Tree (AST). This process involves parsing the syntactic structure of the source code to form a tree-like representation that can be used to calculate code coverage. Then, the initial test case set is executed in a preset test environment, and the code segments and paths touched by each test case are monitored and recorded, thereby determining the specific scope of the executed code and the detailed trajectory of the executed code paths.
[0061] Secondly, based on the constructed abstract syntax tree and the executed code recorded during execution, the system calculates code coverage, the first metric mentioned above. This metric visually displays the coverage density of the source code by the test cases, reflecting the completeness of the test case set at the code level. Next, based on the same obtained abstract syntax tree and executed code paths, the system calculates path coverage, the second metric mentioned above. This metric focuses on the test cases' ability to reach branches of the software logic, ensuring comprehensive testing and leaving no possible execution paths unchecked.
[0062] For example, code coverage can be calculated as follows: Code coverage = (Number of executed lines of code / Total number of lines of code) × 100%. Path coverage can be calculated as follows: Path coverage = (Number of covered paths / Total number of paths) × 100%.
[0063] Finally, in order to evaluate the degree of difference between test cases, that is, the diversity of the test case set, the system uses the cosine similarity calculation method to calculate the cosine similarity between the feature vectors of different test cases. Based on the calculation results, a diversity index is determined, namely the third index mentioned above. This index quantifies the degree of dispersion of test cases in the input space, which helps to identify duplicate or highly similar test cases, avoid overlapping coverage, and enhance the effectiveness of testing.
[0064] For example, diversity metrics can be analyzed using a cosine similarity matrix to examine the differences between test cases. Elements in the cosine similarity matrix S... ,in, and Let i and j represent the feature vectors of the i-th and j-th test cases, respectively. When generating the feature vectors of test cases, existing technologies can be used to complete this preprocessing step. These technologies include, but are not limited to, Natural Language Processing (NLP), code analysis, symbolic execution, static analysis, and dynamic analysis, etc., and are not specifically limited in this embodiment 1.
[0065] Through the above steps, the system integrates three key indicators: code coverage, path coverage, and diversity metrics, forming a comprehensive evaluation result. This guides subsequent test case optimization work, ensuring that the test case set covers software requirements and logical branches while maintaining sufficient diversity. This improves the efficiency and quality of software testing, achieving the technical effect of optimizing the overall performance of test cases while ensuring test depth.
[0066] Optionally, in the test case generation method provided in Embodiment 1 of this application, after processing the source code data using a target generation model to generate an initial test case set, the method includes: determining the state space of the reinforcement learning strategy based on the coverage metric; determining the action space of the reinforcement learning strategy based on the target parameters, wherein the target parameters include: temperature parameters, beam search width, and sampling strategy, and the temperature parameter is a parameter used by the target generation model to control the randomness and sampling diversity of test cases when generating test cases; determining the reward function of the reinforcement learning strategy based on the growth rate of the coverage metric; iteratively optimizing the target parameters based on the state space, action space, and reward function until the coverage metric reaches a preset threshold or the number of iterations reaches a preset number, and updating the target parameters based on the optimized target parameters.
[0067] In this embodiment 1, to dynamically improve the coverage and quality of test cases, a state representation system based on coverage metrics is first constructed. This system uses the code coverage, path coverage, and requirement coverage reflected after the execution of test cases as components of the state space. The purpose of designing the state space is to provide an intuitive and quantitative view of the current test coverage status, facilitating the reinforcement learning algorithm's understanding of test progress.
[0068] Then, the test case generation system defines an action space, which covers the adjustment of target parameters, including the temperature parameter, beam search width, and sampling strategy. The temperature parameter is mainly used to control the randomness and diversity of the model when generating test cases, the beam search width affects the extent to which multiple possible solutions are explored during the generation process, and the sampling strategy determines how test cases are selected from the candidate set. By configuring the action space, specific directions for parameter adjustment can be provided for reinforcement learning, ensuring the controllability and targeting of the optimization process.
[0069] Secondly, a reward function is configured. This function takes the rate of improvement in coverage as its core input and quantifies the increase in coverage as a reward value, thus incentivizing the algorithm to make decisions in the direction of improving test coverage. The design of the reward function allows reinforcement learning to provide positive feedback as the gap between actual test results and the target narrows, promoting the algorithm's self-correction and optimization capabilities.
[0070] Finally, the system leverages the state space, action space, and reward function to perform an iterative optimization process for the target parameters. This process continues until the monitored coverage metric reaches a predetermined threshold or the number of optimization iterations reaches a preset upper limit. This ensures that the optimization process does not run indefinitely and avoids resource waste due to over-optimization. The optimization results directly update the target parameters, enabling dynamic adjustment of the testing strategy and enhancing the performance and adaptability of the test case set in subsequent tests.
[0071] In an optional embodiment, the Proximal Policy Optimization (PPO) algorithm can be used to dynamically adjust the test case generation strategy. The state space s is defined as s=[C, P, D], including code coverage C, path coverage P, and diversity metric D, where each dimension ranges from [0, 100]. The action space a is defined as the adjustment operations for test case generation parameters, including the temperature parameter τ (range 0.1 to 1.0), the bundle search width b (range 1 to 10), and the sampling strategy p (deterministic sampling or random sampling). The reward function comprehensively considers coverage and diversity improvements, and can be expressed as: R=0.1×(ΔC / 1)+0.05×(ΔP / 1)+0.01×(ΔD / 1), where R represents the reward value, ΔC represents the improvement in code coverage (in %), ΔP represents the improvement in path coverage (in %), and ΔD represents the improvement in test case diversity metric (in %). The policy network employs a two-layer fully connected neural network with 256 hidden layers and uses the ReLU activation function. The value function network has a similar structure and is used to evaluate state values.
[0072] The optimization process uses the Adam optimizer with a learning rate set between 0.0001 and 0.0003. Each iteration processes 1000 to 5000 test case samples, and the generator parameters are updated via policy gradient. ,in, Indicates time steps The policy parameter vector at time. These parameters control the policy. Behavior, strategy This determines the probability distribution of the action 'a' taken by the target generation model in each state 's', where α represents the learning rate. This represents the performance objective function, also known as the strategy. Given the expected reward across all possible states and action sequences, the goal of reinforcement learning is to find a set of parameters θ such that... Maximization, that is, the strategy adopted by the goal generation model to obtain the highest cumulative reward. The performance objective function J represents the parameter J. The gradient is calculated. After each optimization iteration, the system re-evaluates the quality of test cases and continues to optimize until the preset coverage threshold or the maximum number of iterations is reached.
[0073] Through the above steps, the test case generation strategy is automatically and continuously optimized using reinforcement learning algorithms. This significantly improves the breadth of test case coverage of software functions and the ability to capture abnormal situations, while maintaining the efficiency of test execution and the rationality of resource utilization. This effectively promotes the improvement of software testing automation, reduces testing costs, and shortens the testing cycle.
[0074] Optionally, in the test case generation method provided in Embodiment 1 of this application, the initial test case set is iteratively optimized based on the evaluation results to obtain a target test case set for the system to be tested. This includes: determining a target optimization function based on multiple optimization objectives, wherein the multiple optimization objectives include at least: coverage metrics, test case execution time and resource consumption information; using fast non-dominated sorting to iteratively optimize a preset population based on the target optimization function to obtain multiple levels, wherein the preset population contains multiple individuals, each individual representing parameter information for generating test cases, and multiple individuals in the N+1th level are superior to multiple individuals in the Nth level in at least one optimization objective, and the parameter information includes at least: population size, crossover probability, and mutation probability; and filtering the initial test case set based on the crowding of the multiple levels to obtain the target test case set.
[0075] In this embodiment 1, to optimize the test case set to meet multi-dimensional performance requirements, a target optimization function is first constructed based on various software testing evaluation criteria, such as coverage metrics, test case execution time, and resource consumption. This function aims to comprehensively evaluate the comprehensiveness, efficiency, and cost-effectiveness of the test case set, ensuring a balance between test depth and resource management during the testing process.
[0076] For example, the objective function can be expressed as follows: f(x) = [ (x), (x), [(x)] = [1 / C(x), T(x), M(x)], where C(x) represents the coverage of test case x (in %), T(x) represents the execution time of test case x (in ms), and M(x) represents the memory usage of test case x (in MB). Furthermore, constraints can be configured for the objective function variables, for example, constraints... It can be represented as: (x)=T(x)≤100ms, constraint condition It can be represented as: (x)=M(x)≤50MB.
[0077] Then, the system initializes a preset population, consisting of multiple individuals. Each individual represents a set of parameters for generating test cases, including but not limited to population size, crossover probability, and mutation probability. Next, a fast non-dominated sorting algorithm is applied to the preset population to identify individuals that perform better on at least one optimization objective, forming multiple hierarchical levels from low to high. Higher hierarchical levels indicate better performance on the optimization objective. This process, through genetic algorithm operations such as selection, crossover, and mutation, promotes the evolution of parameter information among individuals, aiming to improve the overall performance of the test case set in subsequent iterations. The termination condition for iterative optimization is set when the coverage metric reaches a preset threshold or the number of iterations reaches a predetermined value, ensuring the achievement of the predetermined optimization goals.
[0078] Finally, the initial test case set is filtered using the individual crowding density of each level across multiple layers. Crowding density evaluates the distribution density of a set of individuals in the optimization target space. By filtering out individuals with high crowding density (low density), i.e., test cases, the system can effectively remove redundant or highly similar test samples, ultimately forming the target test case set. This set maintains high coverage while having low resource consumption and execution time, achieving both test case simplification and efficient execution.
[0079] In one optional embodiment, the population size is set to 100 to 200 individuals, with each individual representing a test case configuration. A binary tournament selection operation is used, with a crossover probability of 0.8 to 0.9 and a mutation probability of 0.01 to 0.1. The optimization process iterates for 100 to 500 generations, ultimately selecting the optimal solution set from the Pareto front, providing multiple alternatives for different testing scenarios. For scenarios with high real-time requirements, solutions with shorter execution times are prioritized; for scenarios with high security requirements, solutions with higher coverage are prioritized.
[0080] Through the above steps, the performance of the test case set is comprehensively improved, including coverage, execution efficiency, and economy, while eliminating repetitive tests and achieving the technical effect of rational resource allocation and utilization. This multi-objective optimization strategy ensures that the predetermined testing results can be achieved in the process of software testing automation, while controlling costs and time overhead. It is suitable for testing scenarios of large-scale software systems and improves the intelligence level and overall efficiency of the testing process.
[0081] Optionally, in the test case generation method provided in Embodiment 1 of this application, the generative deep learning model includes at least a generator and a discriminator. Before processing the source code data using the target generation model to generate the initial test case set, the method further includes: collecting real test cases and corresponding historical test code from multiple business domains through preset channels, wherein the real test cases include test cases of different complexity; inputting the sample feature vectors corresponding to the historical test code into the generator, and generating historical test cases through the generator's self-attention mechanism and autoregression method; inputting the historical test cases into the discriminator, extracting test case features through the convolutional structure in the discriminator, and calculating the similarity between historical test cases and real test cases based on the test case features, wherein the test case features include at least local features and global features; calculating the loss function value based on the similarity, and training the generative deep learning model based on the batch gradient descent algorithm until the model converges to obtain the target generation model.
[0082] In this embodiment 1, in order to construct a target generation model capable of automatically generating effective test cases, the system first collects real test cases and their corresponding historical test code from preset channels across multiple business domains to ensure the diversity and representativeness of the training dataset. Then, the system transforms the historical test code into sample feature vectors, which become the basis for the generator's input. The generator uses a self-attention mechanism and an autoregressive approach to generate simulated historical test cases based on the input feature vectors. This process aims to allow the model to learn and imitate the construction patterns of real test cases.
[0083] Secondly, the generated historical test cases are input into the discriminator. The discriminator's convolutional structure extracts features from the test cases, including identifying local and global features. Based on this, the similarity between the generated test cases and the real test cases is calculated. Quantifying the similarity helps evaluate the quality of the model's generation. Finally, the system defines a loss function based on the calculated similarity and continuously updates the parameters of the generative deep learning model using the batch gradient descent algorithm until the model converges, i.e., reaches the preset performance metric, thus obtaining the target generative model.
[0084] Through the above steps, the goal of learning test case generation patterns from historical test cases in different business domains was achieved. The resulting target generation model can automatically generate a set of test cases that conform to syntax specifications and effectively cover software functions when faced with new source code. This process not only reduces reliance on manual writing and improves the efficiency of test case generation, but also enhances the comprehensiveness and effectiveness of test cases for software testing through model training and optimization. This significantly improves the automation level and testing quality of the overall workflow during the software development and testing phase.
[0085] Optionally, in the test case generation method provided in Embodiment 1 of this application, the sample feature vector corresponding to the historical test code is input into the generator, and historical test cases are generated through the generator's self-attention mechanism and autoregressive method. This includes: inputting the sample feature vector into the generator, wherein the generator includes multiple converter modules, each converter module adopts a variational autoencoder architecture, and each converter module includes an encoder and a decoder. The encoder calculates the correlation weight of each element in the sample feature vector input based on the self-attention mechanism, and fuses the outputs of multiple converter modules to obtain a fused feature vector. The decoder generates a candidate test case sequence for each label in the fused feature vector based on the autoregressive method, and uses a beam search algorithm to filter the candidate test case sequence to obtain historical test cases.
[0086] In this embodiment 1, to train a generative deep learning model to automatically generate high-quality test cases, the sample feature vectors converted from historical test code are first input into the generator. This step aims to enable the model to learn and understand the generation patterns of test cases. Then, the sample feature vectors are processed by multiple transformer modules within the generator. Each transformer module is based on a variational autoencoder (VAE) architecture and undertakes encoding and decoding tasks. The encoder uses a self-attention mechanism to accurately calculate the association weights between elements in the input vector. The self-attention mechanism quantifies the relationship strength between vector elements by calculating the weighted sum between the query matrix (Q), key matrix (K), and value matrix (V), providing richer contextual information for the subsequent decoding process.
[0087] Next, the outputs of multiple converter modules are fused to form a fused feature vector that integrates information from all converter modules. This step ensures that the model can grasp the generation logic of test cases from multiple perspectives. Finally, the decoder generates candidate test case sequences for each token in the fused feature vector using an autoregressive approach. Then, a beam search algorithm is used to filter the generated candidate sequences, selecting those sequences most likely to become valid test cases. This yields historical test cases, which serve as feedback data for model training.
[0088] In one optional embodiment, the generative deep learning model employs a Generative Adversarial Network (GAN) architecture. The generator, based on a 12-layer Transformer structure, includes 768 hidden layers and 12 attention heads, generating candidate test cases based on the input data. After receiving the input vector, the generator calculates the association weights of each element in the input sequence using a self-attention mechanism. The calculation formula can be expressed as: Where Q represents the query matrix, K represents the key matrix, and V represents the value matrix. The dimension of the key vector is represented (set to 64). The generator progressively constructs an abstract representation of the test cases through multiple Transformer blocks, and finally outputs the initial text representation of the test cases through a fully connected layer. The discriminator uses an 8-layer one-dimensional convolutional neural network with kernel sizes of 3, 5, and 7, and channel numbers of 128, 256, and 512, respectively. It extracts local and global features of the test cases through convolution operations and evaluates the similarity between the generated test cases and the distribution of real test cases.
[0089] The latent space dimension of the generative deep learning model is set between 128 and 512. Diverse test case variants are generated through random sampling. The generation process adopts an autoregressive approach, generating test case content token by token. A beam search algorithm is used to maintain generation quality, and the beam width is set to 4 to 8. The generated initial test case set contains 1,000 to 5,000 test cases, covering the basic functional scenarios, boundary conditions, and exception handling paths of the system under test.
[0090] Through the above steps, an efficient conversion from sample feature vectors to historical test cases is achieved. The self-attention mechanism and bundle search algorithm used ensure that the generated test cases both follow software logic and possess a certain degree of diversity, thereby enhancing the model's learning ability and the accuracy of generated test cases. This process, through repeated training, enables the model to gradually master the ability to generate effective test cases, laying a solid foundation for subsequent practical applications.
[0091] Optionally, in the test case generation method provided in Embodiment 1 of this application, before inputting the sample feature vector corresponding to the historical test code into the generator, the method further includes: performing a preprocessing operation on the historical test code to obtain processed code, wherein the preprocessing operation includes at least: deleting redundant information, standardizing naming, and standardizing data format; performing lexical analysis on the processed code through a syntax parsing model to obtain multiple tags, and performing syntax analysis on the multiple tags to construct an abstract syntax tree; extracting code semantic information from the abstract syntax tree through a semantic analysis model, wherein the code semantic information includes at least: code function information, function call relationships, data dependency information, and code control paths; and converting the code semantic information into sample feature vectors using a word embedding algorithm, wherein the sample feature vectors are stored in tensor format.
[0092] In this embodiment 1, in order to effectively train the generative deep learning model and ensure that it can accurately generate test cases from the source code, the collected historical test code is first preprocessed to eliminate redundant information, unify naming conventions, and standardize data formats. The purpose is to remove messy data that may interfere with model learning and provide a clear and consistent input format.
[0093] Then, a professional syntax analysis model is used to perform detailed lexical and syntactic analysis on the preprocessed operation code, decomposing the code into a series of tags. Subsequently, an Abstract Syntax Tree (AST) reflecting the code structure is constructed so that the model can more intuitively understand and simulate the code logic. Next, a semantic analysis model is used to deeply analyze the AST, extracting key code semantic information, including but not limited to the code's functional description, function call relationships, data dependency chains, and code control flow. This information is crucial for generating test cases for specific functions.
[0094] Finally, the system uses a word embedding algorithm to transform the extracted semantic information of the code into numerical sample feature vectors. These vectors are stored in tensor form, which facilitates processing and learning by deep learning models. Moreover, each sample feature vector can accurately reflect the connotation and structural characteristics of the source code.
[0095] In one optional embodiment, the feature extraction process converts the code semantic information into a numerical vector representation. Word embedding technology is used to map each token to a 128-dimensional or 256-dimensional vector, with the sequence length set to 500 to 1000 tokens depending on system complexity. Data normalization employs a min-max normalization method, scaling feature values to the [0, 1] interval. Preprocessed data is stored in tensor format, supporting batch processing. Each batch contains 32 to 64 samples, ensuring the integrity and consistency of the input data and providing a high-quality input foundation for subsequent test case generation. The entire preprocessing process is executed in a distributed computing environment, supporting parallel processing of multiple data streams, with a throughput of 1000 to 5000 function points per second.
[0096] Through the above steps, complex source code is effectively converted into sample feature vectors that the model can understand and manipulate, greatly improving the efficiency and quality of model training. This transformation from raw data to feature vectors not only filters out irrelevant information but also extracts the core semantic elements of the code, enabling the model to focus more on key logic and functions during the learning process. Ultimately, the resulting feature vectors, used as training data, can drive generative deep learning models to more accurately generate test cases that conform to software logic and requirements, thereby achieving the technical effect of improving the level of software testing automation and the quality of test cases.
[0097] Optionally, in the test case generation method provided in Embodiment 1 of this application, after obtaining the target test case set for the system to be tested, the method further includes: converting the target test case set into a structured standard data format to obtain a test file, wherein the test file includes at least one of the following: a first type of file, a second type of file, and a third type of file. The first type of file includes at least test class information, test method information, and assertion statement information; the second type of file includes at least test function information and test scenario information; and the third type of file includes at least test case metadata information, input information, and expected output information. The test file is then subjected to syntax verification, and the test file is subjected to feasibility verification using a symbolic execution algorithm to obtain a verification result. If the verification result indicates that the verification is successful, the test file is imported into the system to be tested.
[0098] In this embodiment 1, in order to ensure that the generated test cases are compatible with different testing platforms and have the effectiveness of execution, the optimized target test case set is first converted into a structured standard data format to generate test files.
[0099] For example, a format converter transforms the optimized target test case set into a structured standard data format. The format converter supports the generation of JUnit scripts (test case code written using the Java unit testing framework JUnit), i.e., the first type of file mentioned above, including test class definitions, test method annotations, and assertion statements; the generation of Cucumber scenario descriptions (a text format used in Behavior-Driven Development (BDD) frameworks to describe the behavior and functionality of software applications), i.e., the second type of file mentioned above, including feature files and scenario step definitions to ensure the comprehensiveness and relevance of testing; and the generation of custom JSON structures, i.e., the third type of file mentioned above, containing test case metadata, input parameters, and expected outputs to meet the needs of automated testing and data analysis.
[0100] The system then performs comprehensive syntax validation on the generated test files, including Java syntax validation, Gherkin syntax validation, and JSON format validation. By comparing the files against predefined syntax standards, the system confirms the completeness of the content, the compliance of the format, and the accuracy of the expression, thus preventing test failures due to syntax errors.
[0101] Secondly, a symbolic execution algorithm is used to verify the feasibility of the test files. By symbolically processing the input parameters of the test cases and executing the test logic, the system can detect whether the test cases cover the software's execution path, whether there are unreachable code segments, or whether there are illogical conditional judgments. This verification process ensures the rationality and executability of the test cases. Finally, when the test files are syntactically and feasibility-verified, the system imports them into the software environment under test, ready to execute the test process.
[0102] Through the above steps, it is ensured that the automatically generated test cases are not only correctly formatted but also logically sound and comprehensively comprehensive, enabling seamless integration into the system under test and significantly improving the level of test automation. Furthermore, this series of verification measures effectively prevents software testing oversights caused by improper test case design, enhancing the practical application value of the test cases. This, in turn, reduces debugging time and improves testing efficiency and software quality during the software testing phase.
[0103] Optionally, in this embodiment 1, Figure 3 This is a schematic diagram of the system structure generated based on the optional test cases provided in Embodiment 1 of this application. For example... Figure 3 As shown, firstly, the input module includes a data receiving interface and a data preprocessing unit. It receives and processes software source code or requirement specifications, transforming the raw unstructured data into model-readable numerical vectors or sequences through cleaning, normalization, and feature extraction, preparing it for subsequent model processing. Secondly, the AI processing module receives the preprocessed data, with its core component being a generative deep learning model. This model automatically generates an initial set of test cases related to the input software. Thirdly, the optimization module includes a coverage optimization unit and a reinforcement learning algorithm. The former calculates the test case coverage metric, while the latter dynamically adjusts the generation strategy based on this metric. Through a feedback loop, the algorithm continuously optimizes the test case set to improve the comprehensiveness and efficiency of testing. Finally, the output module converts the target test case set into a structured standard data format and ensures its syntactic and logical correctness through a data validation component, ultimately outputting structured test cases for software testing execution and management.
[0104] Optionally, in this embodiment 1, Figure 4 This is a flowchart illustrating the optional test case generation and optimization process provided in Embodiment 1 of this application. Figure 4As shown, the system first receives the requirements specification or source code as input. Then, through a data preprocessing stage, the input data is cleaned and standardized, undergoing necessary cleaning and feature extraction to ensure data quality and suitability for subsequent generative deep learning model processing. Next, the generative deep learning model parses the preprocessed data and uses deep learning techniques to generate a preliminary set of test cases. Then, the generated initial test cases are executed, and the system collects and analyzes coverage metrics in real time during the testing process to determine whether these metrics have met the predetermined coverage target. Finally, if the coverage target is met, the system converts the initial test cases into a standard test format for easy execution on the testing platform; conversely, if the coverage target is not met, the system uses a reinforcement learning algorithm to calculate rewards, adjusts the test case generation strategy accordingly, and iteratively generates new test cases to improve coverage in the next round of testing until the preset coverage standard is met.
[0105] This flowchart visually illustrates the working principle of the automatic test case generation system, emphasizing the dynamic optimization mechanism based on the generative deep learning model, as well as the closed-loop process of test case generation, execution, and feedback, ensuring continuous improvement in the quality and coverage of the generated test cases.
[0106] Optionally, in this embodiment 1, Figure 5 This is a flowchart illustrating the optional test case loop optimization provided in Embodiment 1 of this application. For example... Figure 5 As shown, firstly, the system configures the test case generation strategy based on preset parameters or the results of the previous iteration, including parameter settings and the selection of the generation algorithm. Then, according to the current strategy, the generative deep learning model generates initial test cases. Secondly, the system evaluates the code coverage and diversity of each initial test case, collecting detailed data on test performance. Finally, based on the results of execution and coverage analysis, the system calculates reward signals reflecting the degree of improvement in coverage and diversity. These signals are input into a reinforcement learning algorithm to adjust and optimize the test case generation strategy, ensuring that the test cases generated in the next round better cover software requirements and improve testing efficiency.
[0107] Optionally, in this embodiment 1, Figure 6 This is a flowchart illustrating the multi-objective optimization and output of optional test cases provided in Embodiment 1 of this application. Figure 6As shown, firstly, the test case set generated by the generative deep learning model and coverage analysis is input into the multi-objective optimization module. The objectives here are not limited to improving test coverage but also involve balancing execution time and resource consumption, ensuring that the test cases achieve optimal performance across different dimensions. Then, using advanced optimization algorithms, the system calculates and generates a Pareto optimal solution set—the set of solutions where no objective can be further improved without sacrificing other objectives. This allows the system to provide the most suitable test case set for different testing scenarios and requirements. Secondly, the optimized target test case set (i.e., the target test case set mentioned above) is sent to the output format conversion stage. Here, the test cases are converted into various standard test formats according to the requirements of the testing platform, including JUnit scripts, Cucumber scenario descriptions, or custom JSON structures, supporting compatibility with different testing frameworks and environments. Finally, the converted test cases are output to the test management platform in a structured data format. This process includes data validation to ensure the syntactic correctness and logical integrity of the test cases, allowing them to be directly used in the software testing cycle.
[0108] Through the above process, intelligent and efficient automatic generation of test cases is achieved. The optimized test cases can better meet the needs of software testing, while the flexibility and standardization of the output also greatly facilitate the management and execution of test cases.
[0109] It should be noted that the steps shown in the flowchart in the accompanying drawings can be executed in a computer system such as a set of computer-executable instructions, and although a logical order is shown in the flowchart, in some cases the steps shown or described may be executed in a different order than that shown here.
[0110] In summary, the test case generation method provided in this application receives source code data to be tested, which is code running in the system under test; processes the source code data using a target generation model to generate an initial test case set, wherein the target generation model is obtained by training a generative deep learning model with historical test cases from multiple business domains; evaluates the initial test case set based on a coverage metric to obtain an evaluation result; and iteratively optimizes the initial test case set based on the evaluation result to obtain a target test case set for the system under test. This method solves the problem in related technologies where a single generation model is often used when generating test cases, lacking effective optimization methods, resulting in low test case coverage.
[0111] By employing a cross-domain trained target generation model to process the source code data to be tested, an initial test case set is generated. This initial test case set is then iteratively optimized based on coverage metrics. This dynamic adjustment of the generation strategy ensures that test cases comprehensively cover code, paths, and requirements, significantly improving test case quality. The optimized target test case set more accurately locates and verifies potential defects in the software, further enhancing the comprehensiveness and effectiveness of software testing. Simultaneously, it reduces manual intervention, accelerates the software testing cycle, and improves overall development efficiency.
[0112] Example 2
[0113] This application also provides a test case generation apparatus. It should be noted that the test case generation apparatus of this application can be used to execute the test case generation method provided in this application. The test case generation apparatus provided in this application will be described below.
[0114] According to an embodiment of this application, an apparatus for implementing the above-described test case generation method is also provided. Figure 7 This is a schematic diagram of a test case generation device according to Embodiment 2 of this application. Figure 7 As shown, the device includes: a receiving unit 701, a first generating unit 702, an evaluation unit 703, and a first optimization unit 704.
[0115] Specifically, receiving unit 701 is used to receive source code data to be tested, wherein the source code data is code running on the system to be tested.
[0116] The first generation unit 702 is used to process the source code data using the target generation model to generate an initial test case set. The target generation model is obtained by training a generative deep learning model with historical test cases from multiple business domains.
[0117] Evaluation unit 703 is used to evaluate the initial test case set based on the coverage metric and obtain the evaluation results.
[0118] The first optimization unit 704 is used to iteratively optimize the initial test case set based on the evaluation results to obtain the target test case set for the system to be tested.
[0119] The test case generation apparatus provided in this application embodiment receives source code data to be tested through a receiving unit 701, wherein the source code data is code running in the system to be tested; a first generation unit 702 processes the source code data using a target generation model to generate an initial test case set, wherein the target generation model is obtained by training a generative deep learning model using historical test cases from multiple business domains; an evaluation unit 703 evaluates the initial test case set based on a coverage metric to obtain an evaluation result; and a first optimization unit 704 iteratively optimizes the initial test case set based on the evaluation result to obtain a target test case set for the system to be tested. This solves the problem in related technologies where a single generation model is often used when generating test cases, lacking an effective optimization method, resulting in low test case coverage.
[0120] By employing a cross-domain trained target generation model to process the source code data to be tested, an initial test case set is generated. This initial test case set is then iteratively optimized based on coverage metrics. This dynamic adjustment of the generation strategy ensures that test cases comprehensively cover code, paths, and requirements, significantly improving test case quality. The optimized target test case set more accurately locates and verifies potential defects in the software, further enhancing the comprehensiveness and effectiveness of software testing. Simultaneously, it reduces manual intervention, accelerates the software testing cycle, and improves overall development efficiency.
[0121] Optionally, in the test case generation apparatus provided in Embodiment 2 of this application, the aforementioned coverage metrics include at least a first metric, a second metric, and a third metric. For each initial test case in the initial test case set, the evaluation unit 703 includes: a construction subunit, used to construct an abstract syntax tree corresponding to the source code to be tested, and to determine the executed code and the executed code path by executing the initial test cases in a preset environment; a first calculation subunit, used to calculate the first metric based on the abstract syntax tree and the executed code, and to calculate the second metric based on the abstract syntax tree and the executed code path, wherein the first metric represents the code coverage ratio of the test case, and the second metric represents the code branch coverage ratio of the test case; a second calculation subunit, used to calculate the cosine similarity between each initial test case based on the cosine similarity calculation method, and to calculate the third metric based on the cosine similarity, wherein the third metric represents the degree of difference between the test cases; and a first determination subunit, used to determine the evaluation result based on the first metric, the second metric, and the third metric.
[0122] Optionally, in the test case generation apparatus provided in Embodiment 2 of this application, the apparatus includes: a first determining unit, configured to determine the state space of the reinforcement learning strategy based on the coverage index after processing the source code data using a target generation model to generate an initial test case set; a second determining unit, configured to determine the action space of the reinforcement learning strategy based on target parameters, wherein the target parameters include: a temperature parameter, a beam search width, and a sampling strategy, and the temperature parameter is a parameter used by the target generation model to control the randomness and sampling diversity of test cases when generating test cases; a third determining unit, configured to determine the reward function of the reinforcement learning strategy based on the growth rate of the coverage index; and a second optimization unit, configured to iteratively optimize the target parameters based on the state space, action space, and reward function until the coverage index reaches a preset index threshold or the number of iterations reaches a preset number, and update the target parameters based on the optimized target parameters.
[0123] Optionally, in the test case generation apparatus provided in Embodiment 2 of this application, the first optimization unit 704 includes: a second determining subunit, used to determine a target optimization function based on multiple optimization objectives, wherein the multiple optimization objectives include at least: coverage index, test case execution time and resource consumption information; an optimization subunit, used to iteratively optimize a preset population based on the target optimization function using fast non-dominated sorting to obtain multiple levels, wherein the preset population contains multiple individuals, each individual represents parameter information for generating test cases, and multiple individuals in the N+1th level are superior to multiple individuals in the Nth level in at least one optimization objective, and the parameter information includes at least: population size, crossover probability, and mutation probability; and a filtering subunit, used to filter the initial test case set based on the crowding of multiple levels to obtain a target test case set.
[0124] Optionally, in the test case generation apparatus provided in Embodiment 2 of this application, the aforementioned generative deep learning model includes at least a generator and a discriminator. The apparatus further includes: a collection unit, used to collect real test cases and historical test codes corresponding to real test cases from multiple business domains from preset channels before processing source code data using the target generation model to generate an initial test case set, wherein the real test cases include test cases of different complexity; a second generation unit, used to input the sample feature vectors corresponding to the historical test codes into the generator, and generate historical test cases through the generator's self-attention mechanism and autoregressive method; a calculation unit, used to input the historical test cases into the discriminator, extract test case features through the convolutional structure in the discriminator, and calculate the similarity between historical test cases and real test cases based on the test case features, wherein the test case features include at least local features and global features; and a training unit, used to calculate the loss function value based on the similarity, and train the generative deep learning model based on the batch gradient descent algorithm until the model converges to obtain the target generation model.
[0125] Optionally, in the test case generation apparatus provided in Embodiment 2 of this application, the second generation unit mentioned above includes: a processing subunit, used to input the sample feature vector into the generator, wherein the generator includes multiple converter modules, each converter module adopts a variational autoencoder architecture, and each converter module includes an encoder and a decoder. The encoder calculates the correlation weight of each element in the sample feature vector input based on a self-attention mechanism, and fuses the outputs of multiple converter modules to obtain a fused feature vector. The decoder generates a candidate test case sequence for each label in the fused feature vector based on an autoregressive method, and uses a beam search algorithm to filter the candidate test case sequence to obtain historical test cases.
[0126] Optionally, in the test case generation apparatus provided in Embodiment 2 of this application, the apparatus further includes: a first processing unit, configured to preprocess the historical test code before inputting the sample feature vector corresponding to the historical test code into the generator to obtain processed code, wherein the preprocessing operation includes at least: deleting redundant information, standardizing naming, and standardizing data format; a second processing unit, configured to perform lexical analysis on the processed code through a syntax parsing model to obtain multiple tags, and perform syntax analysis on the multiple tags to construct an abstract syntax tree; an extraction unit, configured to extract code semantic information from the abstract syntax tree through a semantic analysis model, wherein the code semantic information includes at least: code function information, function call relationship, data dependency information, and code control path; and a first conversion unit, configured to convert the code semantic information into sample feature vectors using a word embedding algorithm, wherein the sample feature vectors are stored in tensor format.
[0127] Optionally, in the test case generation apparatus provided in Embodiment 2 of this application, the apparatus further includes: a second conversion unit, configured to convert the target test case set for the system under test into a structured standard data format after obtaining the target test case set for the system under test, to obtain a test file, wherein the test file includes at least one of the following: a first type of file, a second type of file, and a third type of file, wherein the first type of file includes at least test class information, test method information, and assertion statement information, the second type of file includes at least test function information and test scenario information, and the third type of file includes at least test case metadata information, input information, and expected output information; a verification unit, configured to perform syntax verification on the test file and perform feasibility verification on the test file through a symbolic execution algorithm to obtain a verification result; and a third processing unit, configured to import the test file into the system under test if the verification result indicates that the verification is successful.
[0128] It should be noted that the receiving unit 701, the first generating unit 702, the evaluation unit 703, and the first optimization unit 704 mentioned above correspond to steps S201 to S204 in Embodiment 1. The two modules and the corresponding steps implement the same instances and application scenarios, but are not limited to the content disclosed in Embodiment 1. It should be noted that the above modules or units can be hardware or software components stored in memory (e.g., memory 104) and processed by one or more processors (e.g., processors 102a, 102b, ..., 102n). The above modules can also be part of the device and run in the computer terminal 10 provided in Embodiment 1.
[0129] Example 3
[0130] Embodiments of this application may provide an electronic device. Figure 8 This is a schematic diagram of an electronic device generated according to the test cases provided in Embodiment 3 of this application. For example... Figure 8 As shown, the electronic device may include: one or more ( Figure 8 (Only one is shown) processor 802, memory 804, memory controller, and peripheral interface, wherein the peripheral interface is connected to the radio frequency module, audio module and display.
[0131] The memory can be used to store software programs and modules, such as the program instructions / modules corresponding to the methods and apparatus in the embodiments of this application. The processor executes various functional applications and data processing by running the software programs and modules stored in the memory, thereby implementing the above-described methods. The memory may include high-speed random access memory, and may also include non-volatile memory, such as one or more magnetic storage devices, flash memory, or other non-volatile solid-state memory. In some instances, the memory may further include memory remotely located relative to the processor, and these remote memories can be connected to the terminal via a network. Examples of such networks include, but are not limited to, the Internet, corporate intranets, local area networks, mobile communication networks, and combinations thereof.
[0132] The processor can access information and applications stored in memory via a transmission device to execute the following steps: receiving source code data to be tested; processing the source code data using a target generation model to generate an initial test case set; evaluating the initial test case set based on coverage metrics to obtain evaluation results; and iteratively optimizing the initial test case set based on the evaluation results to obtain a target test case set for the system under test. This solves the technical problem of low test case coverage caused by the frequent use of a single generation model and the lack of effective optimization methods when generating test cases. By using a cross-domain trained target generation model to process the source code data to be tested, generating an initial test case set, and iteratively optimizing the initial test case set based on coverage metrics, the generation strategy can be dynamically adjusted to ensure that test cases comprehensively cover code, paths, and requirements, significantly improving the quality of test cases. The optimized target test case set more accurately locates and verifies potential defects in the software, further improving the comprehensiveness and effectiveness of software testing, while reducing manual intervention, accelerating the software testing cycle, and improving overall R&D efficiency.
[0133] Those skilled in the art will understand that Figure 8 The structure shown is for illustrative purposes only. Electronic devices can also be smartphones, tablets, handheld computers, mobile internet devices (MIDs), PADs, and other terminal devices. Figure 8 This does not limit the structure of the aforementioned electronic device. For example, electronic devices may also include components that are more... Figure 8 The more or fewer components shown (such as network interfaces, display devices, etc.), or having the same Figure 8 The different configurations shown.
[0134] Those skilled in the art will understand that all or part of the steps in the various methods of the above embodiments can be implemented by a program instructing the hardware related to the terminal device. The program can be stored in a computer-readable storage medium, which may include: flash drive, read-only memory (ROM), random access memory (RAM), disk or optical disk, etc.
[0135] Example 4
[0136] Embodiments of this application also provide a storage medium. Optionally, in this embodiment, the storage medium can be used to store the program code executed by the test case generation method provided in Embodiment 1. Optionally, in this embodiment, the storage medium can be located in any computer terminal in a group of computer terminals in a computer network, or in any mobile terminal in a group of mobile terminals. This application also provides a computer program product, which, when executed on a data processing device, is suitable for executing the steps of the test case generation method.
[0137] The sequence numbers of the embodiments in this application are merely for description and do not represent the superiority or inferiority of the embodiments. In the above embodiments of this application, the descriptions of each embodiment have their own emphasis; parts not described in detail in a certain embodiment can be referred to in the relevant descriptions of other embodiments. It should be understood that the disclosed technical content in the several embodiments provided in this application can be implemented in other ways. The device embodiments described above are merely illustrative; for example, the division of units is only a logical functional division, and in actual implementation, there may be other division methods. For example, multiple units or components may be combined or integrated into another system, or some features may be ignored or not executed. Furthermore, the displayed or discussed mutual coupling or direct coupling or communication connection may be through some interfaces; the indirect coupling or communication connection of units or modules may be electrical or other forms.
[0138] The units described as separate components may or may not be physically separate. The components shown as units may or may not be physical units; that is, they may be located in one place or distributed across multiple network units. Some or all of the units can be selected to achieve the purpose of this embodiment according to actual needs. Furthermore, the functional units in the various embodiments of this application may be integrated into one processing unit, or each unit may exist physically separately, or two or more units may be integrated into one unit. The integrated units described above can be implemented in hardware or as software functional units.
[0139] If the integrated unit is implemented as a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of this application, in essence, or the part that contributes to the prior art, or all or part of the technical solution, can be embodied in the form of a software product. This computer software product is stored in a storage medium and includes several instructions to cause a computer device (which may be a personal computer, server, or network device, etc.) to execute all or part of the steps of the methods described in the various embodiments of this application. The aforementioned storage medium includes various media capable of storing program code, such as a USB flash drive, read-only memory (ROM), random access memory (RAM), portable hard drive, magnetic disk, or optical disk.
[0140] The above description is only a preferred embodiment of this application. It should be noted that for those skilled in the art, several improvements and modifications can be made without departing from the principle of this application, and these improvements and modifications should also be considered within the scope of protection of this application.
Claims
1. A method for generating test cases, characterized by, The method comprises: receiving source code data to be tested, wherein the source code data is code running in a system to be tested; processing the source code data using a target generation model to generate an initial test case set, wherein the target generation model is obtained by training a generative deep learning model using historical test cases of multiple business domains; evaluating the initial test case set based on a coverage index to obtain an evaluation result; iteratively optimizing the initial test case set according to the evaluation result to obtain a target test case set for the system to be tested.
2. The method of claim 1, wherein, The coverage index at least includes a first index, a second index and a third index. For each initial test case in the initial test case set, the initial test case set is evaluated based on the coverage index to obtain an evaluation result, which includes: constructing an abstract syntax tree corresponding to the source code to be tested, and determining executed code and executed code paths by executing the initial test case in a preset environment; calculating the first index according to the abstract syntax tree and the executed code, and calculating the second index according to the abstract syntax tree and the executed code paths, wherein the first index represents the code coverage ratio of the test case, and the second index represents the code branch coverage ratio of the test case; calculating the cosine similarity between each initial test case based on a cosine similarity calculation method, and calculating the third index according to the cosine similarity, wherein the third index represents the difference between test cases; determining the evaluation result according to the first index, the second index and the third index.
3. The method of claim 1, wherein, After processing the source code data using the target generation model to generate an initial test case set, the method comprises: determining a state space of a reinforcement learning strategy based on the coverage index; determining an action space of the reinforcement learning strategy according to a target parameter, wherein the target parameter includes a temperature parameter, a beam search width and a sampling strategy, and the temperature parameter is a parameter for controlling the randomness and sampling diversity of test cases when the target generation model generates test cases; determining a reward function of the reinforcement learning strategy according to the growth rate of the coverage index; iteratively optimizing the target parameter according to the state space, the action space and the reward function until the coverage index reaches a preset index threshold or the number of iterations reaches a preset number, and updating the target parameter according to the optimized target parameter.
4. The method of claim 1, wherein, According to the evaluation result, the initial test case set is iteratively optimized to obtain a target test case set for the system to be tested, which comprises: determining a target optimization function according to multiple optimization objectives, wherein the multiple optimization objectives at least include the coverage index, test case execution time and resource consumption information. The preset population is iteratively optimized based on the target optimization function by using fast non-dominated sorting, to obtain multiple levels, wherein the preset population includes multiple individuals, each individual representing parameter information of a generated test case, and the individuals in the N+1th level of the multiple levels are superior to the individuals in the Nth level in at least one optimization target, and the parameter information at least includes population size, crossover probability, and mutation probability; The initial test case set is filtered based on the congestion degree of the multiple levels to obtain the target test case set.
5. The method of claim 1, wherein, The method further comprises, before the source code data is processed by using the target generative model to generate the initial test case set: Real test cases in the multiple business fields and historical test codes corresponding to the real test cases are collected from a preset channel, wherein the real test cases include test cases with different complexity levels; A sample feature vector corresponding to the historical test code is input into the generator, and a historical test case is generated by using a self-attention mechanism and a self-regression manner of the generator; The historical test case is input into the discriminator, test case features are extracted by using a convolution structure in the discriminator, and a similarity between the historical test case and the real test case is calculated based on the test case features, wherein the test case features at least include local features and global features; A loss function value is calculated according to the similarity, and the generative deep learning model is trained based on a batch gradient descent algorithm until the model converges, to obtain the target generative model.
6. The method of claim 5, wherein, The historical test code corresponding to the sample feature vector is input into the generator, and the historical test case is generated by using a self-attention mechanism and a self-regression manner of the generator, including: The sample feature vector is input into the generator, wherein the generator includes multiple transformer modules, each transformer module adopts a variational autoencoder architecture, and each transformer module includes an encoder and a decoder, the encoder calculates correlation weights of elements in the sample feature vector based on a self-attention mechanism, and fuses outputs of the multiple transformer modules to obtain a fused feature vector, and the decoder generates a candidate test case sequence for each token in the fused feature vector based on a self-regression manner, and selects the candidate test case sequence by using a beam search algorithm to obtain the historical test case.
7. The method of claim 5, wherein, Before the sample feature vector corresponding to the historical test code is input into the generator, the method further comprises: A preprocessing operation is performed on the historical test code to obtain a processed code, wherein the preprocessing operation at least includes deleting redundant information, standardizing naming, and standardizing data format; A syntax analysis model is used to perform lexical analysis on the processed code to obtain multiple tokens, and syntax analysis is performed on the multiple tokens to construct an abstract syntax tree; extracting code semantic information from the abstract syntax tree through a semantic analysis model, wherein the code semantic information at least includes code function information, function call relationship, data dependency information and code control path; adopting a word embedding algorithm to convert the code semantic information into the sample feature vector, wherein the sample feature vector is stored in a tensor format.
8. The method of claim 1, wherein, After obtaining the target test case set for the to-be-tested system, the method further includes: converting the target test case set into a structured standard data format to obtain a test file, wherein the test file at least includes one of the following: a first type of file, a second type of file and a third type of file, the first type of file at least includes test class information, test method information and assertion statement information, the second type of file at least includes test function information and test scenario information, and the third type of file at least includes test case metadata information, input information and expected output information; performing syntax verification on the test file and performing feasibility verification on the test file through a symbolic execution algorithm to obtain a verification result; in a case where the verification result indicates that the verification is passed, importing the test file into the to-be-tested system.
9. An apparatus for generating test cases, characterized by: comprise: a receiving unit configured to receive source code data to be tested, wherein the source code data is code running in a to-be-tested system; a first generating unit configured to generate an initial test case set by processing the source code data using a target generating model, wherein the target generating model is obtained by training a generative deep learning model using historical test cases of multiple business domains; an evaluation unit configured to evaluate the initial test case set based on a coverage rate index to obtain an evaluation result; a first optimization unit configured to iteratively optimize the initial test case set according to the evaluation result to obtain a target test case set for the to-be-tested system.
10. A computer program product comprising computer instructions, characterized in that, The computer instructions, when executed by a processor, implement the steps of the test case generation method of any one of claims 1 to 8.