Test case generation method and device and medium
By extracting the characteristics of source code and test cases and using generative models to generate target test cases, the problem of heavy writing of test cases and incomplete scenario coverage in the prior art is solved, and more efficient and quality software testing is achieved.
Patent Information
- Application Number
- CN202411758453.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-12-03
- Publication Date
- 2025-03-14
AI Technical Summary
The prior art has heavy work and incomplete scenario coverage in the writing and management of test cases, resulting in inefficient testing and insufficient software reliability.
Generate target test cases by extracting the features of existing source code and test cases and inputting them into a pre-trained generative model. The method includes static code analysis, natural language processing, and deep learning techniques.
It reduces the heavy work in writing test cases, improves the scenario coverage of test cases, and thus improves the efficiency and quality of software testing.
Smart Images

Figure CN119621575A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of machine learning, and specifically to a test case generation method, device and medium. Background Art
[0002] In the field of software development, testing is a key link to ensure product quality and stability. However, the prior art has the following two problems in the writing and management of test cases: On the one hand, the writing of test cases is cumbersome and redundant, which brings great burden to testers. With the continuous increase and complexity of software functions, the number of test cases that need to be written has increased sharply. Testers often need to invest a lot of time and energy to manually write and maintain these cases, which is not only inefficient but also easy to introduce human errors. On the other hand, the test cases in the prior art are often difficult to fully cover all possible test scenarios, resulting in unforeseen errors and vulnerabilities in the actual operation of the software. This problem of incomplete scenario coverage seriously affects the reliability of the software and user experience. Therefore, there is an urgent need for an innovative solution that can not only reduce the heavy work of test case writing, but also improve the scenario coverage of test cases, thereby comprehensively improving the efficiency and quality of software testing. Summary of the invention
[0003] In order to solve the above problems, the present application proposes a test case generation method, device and medium, wherein the method includes:
[0004] According to the test case generation request sent by the user, the existing source code and the existing test cases of the corresponding project are obtained; the features of the existing source code and the existing test cases are extracted to obtain code features and test case features; the code features and the test case features are input into the pre-trained generation model to obtain the target test case.
[0005] In one example, the method of obtaining existing source code and existing test cases of a corresponding project based on a test case generation request sent by a user specifically includes: determining the corresponding project, the target module of the target test case, and the target function of the target test case based on the test case generation request; determining source code related to the corresponding project, the target module of the target test case, and the target function of the target test case in an existing code library, the source code including at least one of function code, class code, and module code; determining test case information related to the corresponding project, the target module of the target test case, and the target function of the target test case in an existing test case library, the test case information including input parameters, expected output, and actual output.
[0006] In one example, feature extraction is performed on the existing source code, specifically including: using a static code analysis tool to parse the code structure of the existing source code to extract relevant features; the relevant features include: lexical features, grammatical features, semantic features, metric features, and behavioral features; the lexical features are information directly extracted from the source code text, including at least one of keywords, identifiers, numbers, and string literals; the grammatical features are the grammatical structure of the code, including at least one of an abstract syntax tree and a control flow graph; the semantic features are features related to the specific meaning of the code, including at least one of the scope of variables, type information, and function call relationships; the metric features are used to reflect the characteristics of the code by calculating various metric values, including at least one of the number of lines of code, cyclomatic complexity, and class inheritance hierarchy; the behavioral features are used to focus on the runtime behavior of the code, including at least one of memory usage, I / O operations, and exception handling; based on the lexical features, grammatical features, semantic features, metric features, and behavioral features, source code models corresponding to different functional interfaces are constructed, and the code features are standardized.
[0007] In one example, feature extraction is performed on the existing test case, specifically including: extracting semantic features of the test case through natural language processing technology and code analysis tools, the semantic features are used to reflect the function and purpose of the test case, including: test module, expected output, boundary conditions, at least one; extracting structural features of the test case through a test execution tool, the structural features are used to reflect the organization of the test case, including the number of test steps, the complexity of test data, at least one; extracting coverage features of the test case through a code coverage tool; the coverage features are used to reflect the coverage degree of the test case on the code, including code line coverage, branch coverage, path coverage at least one of; the test management system records the historical execution results of the test case and extracts the historical features of the test case; the historical features are used to reflect the performance of the test case in different versions, including at least one of the pass rate, the number of failures, and the repair time; by parsing the description and configuration file of the test case, the dependency features of the test case are extracted; the dependency features are used to reflect the dependency relationship between the test case and other test cases or system components, including at least one of the preconditions and postconditions; based on the test case features, a test case model is generated, and the test case model is associated with the source code model, so that each functional interface corresponds to all test cases related to the functional interface.
[0008] In one example, before inputting the code features and the test case features into a pre-trained generative model to obtain a target test case, the method further includes: determining an initial generative model, the initial generative model comprising an input layer, a hidden layer, and an output layer; the input layer is used to receive preprocessed source code feature data and corresponding test case feature data; the hidden layer is used to capture features of sequence data, and comprises at least one neural network structure layer, the neural network structure layer comprises at least one of a recurrent neural network, a long short-term memory network, and a convolutional gated recurrent unit; the output layer is used to generate a probability distribution of the next character or word; the loss function of the initial generative model is a cross entropy loss function; the initial generative model is trained by the gradient and learning rate of the loss function of the initial generative model relative to the model parameters until the loss function converges or reaches a predetermined number of iterations.
[0009] In one example, the code features and the test case features are input into a pre-trained generation model to obtain a target test case, specifically including: determining the input format of the generation model based on a starting sequence selected by a user as an input prefix, and determining input data of the corresponding input format; predicting the probability distribution of the next character or word through the generation model, and sampling the next character according to the probability distribution; adding the generated character to the current sequence as input for the next prediction until a complete test case is generated.
[0010] In one example, after obtaining the target test case, the method also includes: performing a syntax check on the target test case using a syntax checking tool; after the syntax check passes, applying the target test case to the target software system and performing unit testing to check whether it can run correctly and return expected results; using a code coverage tool to analyze the code coverage of the target test case; deleting duplicate test cases and merging similar test cases to reduce the number of test cases.
[0011] In one example, after obtaining the target test case, the method further includes: obtaining an evaluation result of the target test case; if the quality and coverage of the target test case are lower than a preset threshold, adjusting the model parameters of the generated model and increasing training data.
[0012] The present application also provides a test case generation device, comprising: at least one processor; and a memory communicatively connected to the at least one processor; wherein the memory stores instructions executable by the at least one processor, and the instructions are executed by the at least one processor so that the at least one processor can execute: according to a test case generation request sent by a user, obtain existing source code and existing test cases of the corresponding project; perform feature extraction on the existing source code and the existing test cases to obtain code features and test case features; and input the code features and the test case features into a pre-trained generation model to obtain a target test case.
[0013] The present application also provides a non-volatile computer storage medium storing computer executable instructions, wherein the computer executable instructions are configured to: obtain existing source code and existing test cases of a corresponding project according to a test case generation request sent by a user; perform feature extraction on the existing source code and the existing test cases to obtain code features and test case features; and input the code features and the test case features into a pre-trained generation model to obtain a target test case.
[0014] The method proposed in this application can bring the following beneficial effects: automatic generation of test cases through deep learning can reduce the tedious workload of testers in writing test cases during software development and improve work efficiency. At the same time, it also reduces the problem of incomplete test case coverage caused by human factors, resulting in low test accuracy. BRIEF DESCRIPTION OF THE DRAWINGS
[0015] The drawings described herein are used to provide a further understanding of the present application and constitute a part of the present application. The illustrative embodiments of the present application and their descriptions are used to explain the present application and do not constitute an improper limitation on the present application. In the drawings:
[0016] Figure 1 A flow chart of a test case generation method in an embodiment of the present application;
[0017] Figure 2 A schematic diagram of a stream-based code feature extraction method in an embodiment of the present application;
[0018] Figure 3 A schematic diagram of model training for generating a model in an embodiment of the present application;
[0019] Figure 4 A schematic diagram of the structure of a test case generating device in an embodiment of the present application. DETAILED DESCRIPTION
[0020] In order to make the purpose, technical solution and advantages of the present application clearer, the technical solution of the present application will be clearly and completely described below in combination with the specific embodiments of the present application and the corresponding drawings. Obviously, the described embodiments are only part of the embodiments of the present application, not all of the embodiments. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in this field without making creative work are within the scope of protection of the present application.
[0021] The technical solutions provided by various embodiments of the present application are described in detail below in conjunction with the accompanying drawings.
[0022] Figure 1 A schematic flow chart of a test case generation method provided for one or more embodiments of this specification. The test case generated by this method can be applied to different business fields, such as Internet financial business field, e-commerce business field, instant messaging business field, game business field, official business field, etc. The process can be executed by a computing device in the corresponding field, and some input parameters or intermediate results in the process allow manual intervention and adjustment to help improve accuracy. The implementation of the analysis method involved in the embodiment of the present application can be a terminal device or a server, and the present application does not make any special restrictions on this. For the convenience of understanding and description, the following embodiments are described in detail using the server as an example.
[0023] It should be noted that the server can be a single device or a system composed of multiple devices, that is, a distributed server, and this application does not make any specific limitations on this.
[0024] like Figure 1 As shown, the embodiment of the present application provides a test case generation method, comprising:
[0025] S101: Generate a request for a test case sent by a user and obtain existing source code and existing test cases of a corresponding project.
[0026] First, the server receives a test case generation request sent by a user to determine a project corresponding to the test case to be generated, thereby obtaining existing source code and existing test cases related to the project.
[0027] In one embodiment, when obtaining existing source code and test cases, it is necessary to generate a request according to the test case to determine the target module of the corresponding project, the target test case, and the target function of the target test case. Then, in the existing code base, determine the source code related to the target module of the corresponding project, the target test case, and the target function of the target test case, where the source code includes at least one of function code, class code, and module code. And traverse the source code of the project to find all the APIs provided by the external library, framework, or system called. Record the full name of these APIs, the name of the library or framework, and the version number (if applicable). For each API found, collect its detailed document information, including function description, parameter list, return value type, exception handling, etc. If the API has different overloaded versions or variants, it should also be collected. When sorting, it can be classified according to the function or field of the API, such as database operation API, network communication API, file processing API, etc. It can also be classified according to the access level of the API (such as public API, private API) or stability (such as stable API, experimental API). By organizing all the involved APIs and categorizing them separately, team members can better understand the dependencies of the project and ensure that the generated test cases can cover all important API calls, thereby improving the comprehensiveness and accuracy of the test. At the same time, this also helps with subsequent maintenance and upgrades, because when the API changes, the relevant test cases can be quickly located and updated.
[0028] When obtaining existing test cases, it is necessary to determine test case information related to the corresponding project, the target module of the target test case, and the target function of the target test case in the existing test case library. The test case information here includes: input parameters, expected output, and actual output.
[0029] S102: Extract features from the existing source code and the existing test case to obtain code features and test case features.
[0030] After obtaining the existing source code and the existing test cases, it is necessary to perform feature extraction on the source code and the test cases, so as to obtain features corresponding to the source code and the test cases respectively.
[0031] like Figure 2As shown in the figure, when extracting code features, static code analysis tools (such as SonarQube, ESLint, PyLint, etc.) can be used to parse the code structure. Extract relevant features. The relevant features here include: lexical features, grammatical features, semantic features, metric features, and behavioral features. Among them, lexical features are information directly extracted from the source code text, such as keywords, identifiers, numbers, string literals, etc., collecting API request paths, method method parameters, input parameter objects, and return value objects. Syntactic features are based on the grammatical structure of the code, such as abstract syntax trees (ASTs), control flow graphs (CFGs), etc., collecting the logical branch information of the code. Semantic features involve the specific meaning of the code, including the scope of variables, type information, function call relationships, etc. Metric features reflect the characteristics of the code by calculating various metrics, such as the number of lines of code (LOC), cyclomatic complexity, class inheritance hierarchy, etc. Behavioral features focus on the runtime behavior of the code, such as memory usage, I / O operations, exception handling, etc. The extracted code features are standardized into a unified format and corresponding to the corresponding API. A source code model is constructed based on the API and the API source code features parsed above. The input parameter set of the model is request path, request method, parameter object, return object, logical branch, exception handling, etc., which is convenient for model learning and processing.
[0032] When extracting test case features, test case features include semantic features, structural features, coverage features, historical features, and dependency features. Among them, the semantic features of test cases reflect their functions and purposes, such as the target module of the test, expected output, boundary conditions, etc. Keywords and phrases can be extracted from the description of the test cases through natural language processing technology (NLP), and information such as function calls and variable usage in the test cases can also be extracted through code analysis tools. The structural features of test cases reflect their organizational form, such as the number of test steps and the complexity of test data. The execution log of the test cases can be recorded through the test execution tool to extract information such as execution time, memory usage, and CPU occupancy. The coverage features of test cases reflect the degree of code coverage, such as code line coverage, branch coverage, path coverage, etc., and coverage reports can be generated through code coverage tools (such as JaCoCo, gcov) to extract specific coverage indicators. The historical features of test cases reflect their performance in different versions, such as pass rate, number of failures, and repair time. The historical execution results of test cases can be recorded through the test management system to extract information such as pass rate and number of failures. The dependency characteristics of a test case reflect its dependency relationship with other test cases or system components, such as preconditions, postconditions, etc. Preconditions, postconditions and other information can be extracted by parsing the description and configuration files of the test case. A test case model is constructed with the extracted test case characteristics. The input of the model is the parameter key-value pairs of the request header, the boundary value set of the test case input parameters, the return status code of the test case, the number of records and the return value string, etc., and it is associated with the source code model to ensure that each function accurately corresponds to all its test cases, and establish a corresponding relationship of "function-function feature value set-test case feature value set-test case set".
[0033] S103: Input the code features and the test case features into a pre-trained generation model to obtain a target test case.
[0034] The extracted feature data is input into the trained generation model to generate the corresponding target test cases.
[0035] like Figure 3As shown, in one embodiment, before using the generative model, the generative model needs to be trained first. Specifically, when selecting a model, commonly used generative models include recurrent neural networks, long short-term memory networks, variational autoencoders, and generative adversarial networks, etc.; sequence-to-sequence model: Since test cases can usually be regarded as a kind of sequence data, a sequence-to-sequence learning model can be used to generate test cases. After determining the initial generative model, it is necessary to construct the initial generative model. Here, the initial generative model includes an input layer, a hidden layer, and an output layer; the input layer is used to receive the preprocessed source code feature data and the corresponding test case feature data; the hidden layer is used to capture the features of the sequence data, including one or more RNN / LSTM / GRU layers; the output layer is used to generate the probability distribution of the next character or word; the loss function of the initial generative model is a cross entropy loss function. When training, the data set needs to be divided into a training set, a validation set, and a test set; then the mini-batch gradient descent method is used for training to improve training efficiency. First, randomly initialize the weights and biases of the model, then calculate the predicted value of the model, then use the loss function (such as mean square error, cross entropy, etc.) to calculate the difference between the current predicted value and the true value, then calculate the gradient of the loss function to the model parameters, and then update the model parameters according to the gradient and learning rate, and finally repeat the above steps until the loss function converges or reaches the predetermined number of iterations. At the same time, hyperparameters such as learning rate, hidden layer size, batch size, etc. can also be adjusted to optimize model performance. At the same time, it is necessary to monitor the loss value and accuracy during training to prevent overfitting.
[0036] After the model is trained, when generating test cases, the user needs to select a starting sequence (such as a known test case prefix) as the input prefix to determine the input format of the model. This may include code snippets, function signatures, API documentation, or requirement descriptions. According to the input format, prepare the corresponding data to trigger the model to generate test cases, and then use the trained model to predict the probability distribution of the next character or word, sample the next character or word according to the probability distribution, add the generated character or word to the current sequence as the input for the next prediction, and repeat the above steps until a complete test case is generated.
[0037] In one embodiment, after obtaining the target test case, a compiler or syntax checking tool can be used for verification to ensure that the logic of the test case is correct and can cover the expected test scenario. Static analysis tools or manual review can be used. At the same time, the generated test case should be applied to the target software system to perform unit testing to check whether it can run correctly and return the expected results. The execution results of each test case are recorded, including pass, fail, exception, etc. When performing coverage analysis, code coverage tools (such as JaCoCo, gcov, etc.) are used to analyze the coverage of the code by the generated test cases to ensure that the critical paths and boundary conditions are fully tested, that each branch is covered by the test case, and that each possible path is covered by the test case. In order to improve the test efficiency, duplicate test cases can be deleted to avoid redundancy, similar test cases can be merged, and the number of test cases can be reduced.
[0038] In one embodiment, feedback information can also be collected to optimize the model based on the evaluation results. If the quality of the generated test cases is not high or the coverage is insufficient, consider adjusting the model parameters (refining the characteristic value parameters of the source code model, increasing the characteristic dimensions of the API, such as the boundary values of the input objects, the basic types of the parameters, the number of permutations and combinations of the parameters, etc.) and increasing the training data.
[0039] like Figure 4 As shown, the embodiment of the present application also provides a test case generation device, including:
[0040] at least one processor; and a memory in communication with the at least one processor; wherein the memory stores instructions executable by the at least one processor, the instructions being executed by the at least one processor to enable the at least one processor to:
[0041] According to the test case generation request sent by the user, the existing source code and the existing test cases of the corresponding project are obtained; the features of the existing source code and the existing test cases are extracted to obtain code features and test case features; the code features and the test case features are input into the pre-trained generation model to obtain the target test case.
[0042] The embodiment of the present application further provides a non-volatile computer storage medium storing computer executable instructions, wherein the computer executable instructions are configured as follows:
[0043] According to the test case generation request sent by the user, the existing source code and the existing test cases of the corresponding project are obtained; the features of the existing source code and the existing test cases are extracted to obtain code features and test case features; the code features and the test case features are input into the pre-trained generation model to obtain the target test case.
[0044] Each embodiment in this application is described in a progressive manner, and the same or similar parts between the embodiments can be referred to each other, and each embodiment focuses on the differences from other embodiments. In particular, for the device and medium embodiments, since they are basically similar to the method embodiments, the description is relatively simple, and the relevant parts can be referred to the partial description of the method embodiments.
[0045] The devices and media provided in the embodiments of the present application correspond one-to-one to the methods. Therefore, the devices and media also have similar beneficial technical effects as the corresponding methods. Since the beneficial technical effects of the methods have been described in detail above, the beneficial technical effects of the devices and media will not be repeated here.
[0046] Those skilled in the art will appreciate that the embodiments of the present application may be provided as methods, systems, or computer program products. Therefore, the present application may adopt the form of a complete hardware embodiment, a complete software embodiment, or an embodiment in combination with software and hardware. Moreover, the present application may adopt the form of a computer program product implemented in one or more computer-usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) that include computer-usable program code.
[0047] The present application is described with reference to the flowcharts and / or block diagrams of the methods, devices (systems), and computer program products according to the embodiments of the present application. It should be understood that each process and / or box in the flowchart and / or block diagram, as well as the combination of the processes and / or boxes in the flowchart and / or block diagram, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, a special-purpose computer, an embedded processor, or other programmable data processing device to generate a machine, so that the instructions executed by the processor of the computer or other programmable data processing device generate instructions for implementing the processes in the flowchart and / or block diagram. Figure 1 A process or multiple processes and / or boxes Figure 1 A device that provides the functions specified in a block or multiple blocks.
[0048] These computer program instructions may also be stored in a computer-readable memory capable of directing a computer or other programmable data processing device to operate in a specific manner, so that the instructions stored in the computer-readable memory produce an article of manufacture comprising an instruction device, which implements the process Figure 1 A process or multiple processes and / or boxes Figure 1 A function specified in one or more boxes.
[0049] These computer program instructions can also be loaded onto a computer or other programmable data processing device so that a series of operating steps are executed on the computer or other programmable device to produce a computer-implemented process, thereby providing instructions for implementing the process. Figure 1 A process or multiple processes and / or boxes Figure 1 A step that specifies a function in one or more boxes.
[0050] In a typical configuration, a computing device includes one or more processors (CPU), input / output interfaces, network interfaces, and memory.
[0051] The memory may include non-permanent storage in a computer-readable medium, random access memory (RAM) and / or non-volatile memory in the form of read-only memory (ROM) or flash RAM. The memory is an example of a computer-readable medium.
[0052] Computer readable media include permanent and non-permanent, removable and non-removable media that can be implemented by any method or technology to store information. Information can be computer readable instructions, data structures, program modules or other data. Examples of computer storage media include, but are not limited to, phase change memory (PRAM), static random access memory (SRAM), dynamic random access memory (DRAM), other types of random access memory (RAM), read-only memory (ROM), electrically erasable programmable read-only memory (EEPROM), flash memory or other memory technology, compact disk read-only memory (CD-ROM), digital versatile disk (DVD) or other optical storage, magnetic cassettes, magnetic tape magnetic disk storage or other magnetic storage devices or any other non-transmission media that can be used to store information that can be accessed by a computing device. As defined herein, computer readable media does not include transitory media such as modulated data signals and carrier waves.
[0053] It should also be noted that the terms "include", "comprises" or any other variations thereof are intended to cover non-exclusive inclusion, so that a process, method, commodity or device including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, commodity or device. In the absence of more restrictions, the elements defined by the sentence "comprises a ..." do not exclude the existence of other identical elements in the process, method, commodity or device including the elements.
[0054] The above is only an embodiment of the present application and is not intended to limit the present application. For those skilled in the art, the present application may have various changes and variations. Any modification, equivalent replacement, improvement, etc. made within the spirit and principle of the present application should be included in the scope of the claims of the present application.
Claims
1. A test case generation method, characterized in that: include: Generate a request based on the test case sent by the user and obtain the existing source code and test cases of the corresponding project; Performing feature extraction on the existing source code and the existing test case to obtain code features and test case features; The code features and the test case features are input into a pre-trained generation model to obtain a target test case.
2. The method according to claim 1, characterized in that The generating request according to the test case sent by the user and obtaining the existing source code and the existing test case of the corresponding project specifically includes: Determine, according to the test case generation request, a corresponding project, a target module of the target test case, and a target function of the target test case; In an existing code base, determining source code related to the corresponding project, the target module of the target test case, and the target function of the target test case, wherein the source code includes at least one of function code, class code, and module code; In the existing test case library, test case information related to the corresponding project, the target module of the target test case and the target function of the target test case is determined, and the test case information includes: input parameters, expected output and actual output.
3. The method according to claim 1, characterized in that Performing feature extraction on the existing source code specifically includes: Using a static code analysis tool to analyze the code structure of the existing source code to extract relevant features; The relevant features include: lexical features, grammatical features, semantic features, metric features, and behavioral features; The lexical features are information directly extracted from the source code text, including at least one of keywords, identifiers, numbers, and string literals; The grammatical feature is the grammatical structure of the code, including at least one of an abstract syntax tree and a control flow graph; Semantic features are features related to the specific meaning of the code, including at least one of the scope of a variable, type information, and function call relationship; The metric feature is used to reflect the characteristics of the code by calculating various metric values, including at least one of the number of lines of code, cyclomatic complexity, and class inheritance hierarchy; Behavioral features are used to focus on the runtime behavior of the code, including at least one of memory usage, I / O operations, and exception handling; Based on the lexical features, grammatical features, semantic features, metric features, and behavioral features, source code models corresponding to different functional interfaces are constructed, and the code features are standardized.
4. The method according to claim 3, characterized in that Performing feature extraction on the existing test case specifically includes: Extracting semantic features of the test case through natural language processing technology and code analysis tools, wherein the semantic features are used to reflect the function and purpose of the test case, including at least one of a test module, an expected output, and a boundary condition; Extracting structural features of the test case through a test execution tool, wherein the structural features are used to reflect the organization form of the test case, including at least one of the number of test steps and the complexity of test data; Extracting coverage features of the test case through a code coverage tool; the coverage features are used to reflect the coverage degree of the test case on the code, including at least one of code line coverage, branch coverage, and path coverage; The test management system records the historical execution results of the test cases and extracts the historical features of the test cases; the historical features are used to reflect the performance of the test cases in different versions, including at least one of the pass rate, the number of failures, and the repair time; Extracting dependency features of the test case by parsing the description and configuration file of the test case; the dependency features are used to reflect the dependency relationship between the test case and other test cases or system components, including at least one of a precondition and a postcondition; Based on the test case features, a test case model is generated, and the test case model is associated with the source code model so that each functional interface corresponds to all test cases related to the functional interface.
5. The method according to claim 1, characterized in that: Before inputting the code features and the test case features into the pre-trained generation model to obtain the target test case, the method further includes: Determine an initial generation model, wherein the initial generation model includes an input layer, a hidden layer, and an output layer; The input layer is used to receive the preprocessed source code feature data and the corresponding test case feature data; the hidden layer is used to capture the features of the sequence data, and includes at least one neural network structure layer, and the neural network structure layer includes at least one of a recurrent neural network, a long short-term memory network, and a convolutional gated recurrent unit; the output layer is used to generate the probability distribution of the next character or word; the loss function of the initial generation model is a cross entropy loss function; The initial generation model is trained by the gradient and learning rate of the loss function of the initial generation model relative to the model parameters until the loss function converges or reaches a predetermined number of iterations.
6. The method according to claim 1, characterized in that The step of inputting the code features and the test case features into a pre-trained generation model to obtain a target test case specifically includes: Determining an input format of the generative model based on a starting sequence selected by a user as an input prefix, and determining input data corresponding to the input format; Predicting the probability distribution of the next character or word through the generation model, and sampling the next character according to the probability distribution; The generated characters are added to the current sequence as input for the next prediction until a complete test case is generated.
7. The method according to claim 1, characterized in that After obtaining the target test case, the method further includes: Performing a syntax check on the target test case using a syntax check tool; After the syntax check passes, the target test case is applied to the target software system and unit testing is performed to check whether it can run correctly and return expected results; Use a code coverage tool to analyze the code coverage of the target test case; Delete duplicate test cases and merge similar test cases to reduce the number of test cases.
8. The method according to claim 1, characterized in that After obtaining the target test case, the method further includes: Obtaining an evaluation result of the target test case; If the quality and coverage of the target test case are lower than a preset threshold, the model parameters of the generated model are adjusted and training data is increased.
9. A test case generating device, characterized in that: include: at least one processor; and, a memory communicatively connected to the at least one processor; wherein, The memory stores instructions executable by the at least one processor, the instructions being executed by the at least one processor to enable the at least one processor to execute: Generate a request based on the test case sent by the user and obtain the existing source code and test cases of the corresponding project; Performing feature extraction on the existing source code and the existing test case to obtain code features and test case features; The code features and the test case features are input into a pre-trained generation model to obtain a target test case.
10. A non-volatile computer storage medium storing computer executable instructions, characterized in that: The computer executable instructions are configured to: Generate a request based on the test case sent by the user and obtain the existing source code and test cases of the corresponding project; Performing feature extraction on the existing source code and the existing test case to obtain code features and test case features; The code features and the test case features are input into a pre-trained generation model to obtain a target test case.
Citation Information
Cited By
Server test scheme generation method and device, electronic equipment and storage medium
CN120596389A
Server test scheme generation method and device, electronic equipment and storage medium
CN120596389B
Method and system for generating software test case based on adversarial network
CN120763071A
A Method and System for Generating Software Test Cases Based on Adversarial Networks
CN120763071B