Software test case automatic generation method and system
By fine-tuning and optimizing the pre-trained model, combined with software source code analysis, test cases are generated and optimized, the problem of insufficient reliability and adaptability in complex software testing is solved, and more efficient test coverage and repair suggestions are achieved.
Patent Information
- Application Number
- CN202410968488.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-07-18
- Publication Date
- 2024-10-29
AI Technical Summary
Existing software test cases generated based on large models have poor reliability and adaptability in complex software testing, and are difficult to fully cover test scenarios.
By collecting and labeling the code base, test case sets and error logs to form a data set, fine-tuning and optimizing the pre-training model based on the labeled data set, parsing the software source code to identify function points, generating test scenarios and use cases, and analyzing the tests in real time Logic to identify potential issues and generate fix recommendations.
It improves the accuracy, reliability and adaptability of test cases, ensures comprehensive coverage and facilitates manual repair, and improves testing efficiency and quality.
Smart Images

Figure CN118860883A_ABST
Abstract
Description
Technical Field
[0001] The invention belongs to the field of software engineering, and in particular relates to a method and system for automatically generating software test cases. Background Art
[0002] The writing of traditional software test cases mostly relies on manual experience, and professional testers are required to design test cases based on software requirement documents. This process is not only time-consuming and labor-intensive, but also difficult to ensure that all test scenarios are covered. In recent years, with the development of artificial intelligence, the automatic writing of test cases can be achieved through technologies such as deep learning models and natural speech processing. However, this can only be used for simple single test scenarios. For complex software testing, the reliability and adaptability of the test cases generated are not high. Summary of the invention
[0003] In view of this, an embodiment of the present invention provides a method and system for automatically generating software test cases, which are used to solve the problems of poor reliability and adaptability of test cases currently generated based on large models.
[0004] In a first aspect of an embodiment of the present invention, a method for automatically generating software test cases is provided, comprising:
[0005] Collect and annotate the code base, test case set, and error logs related to the pre-test system to form a data set, fine-tune the pre-trained model based on the labeled data set, and evaluate and optimize the fine-tuned pre-trained model;
[0006] Parse the source code of the software to be tested, identify the functional points to be tested, automatically generate test scenarios and test cases through the fine-tuned pre-trained model, optimize and screen the test cases, and then output the test cases;
[0007] Analyze test case logic in real time, identify potential logical errors, test redundancy or insufficient coverage of test cases by comparing software specifications and code logic, provide feedback on potential problems, and generate suggestions for repairing the test cases.
[0008] In a second aspect of an embodiment of the present invention, a system for automatically generating software test cases is provided, comprising:
[0009] The data collection module is used to collect and annotate the code base, test case set and error log related to the pre-test system to form a data set;
[0010] The use case generation module is used to fine-tune the pre-trained model based on the labeled data set, evaluate and optimize the fine-tuned pre-trained model, parse the source code of the software to be tested, identify the functional points to be tested, automatically generate test scenarios and test cases through the fine-tuned pre-trained model, and output the test cases after optimizing and screening the test cases;
[0011] The use case optimization module is used to analyze the test case logic in real time. By comparing the software specification and code logic, it identifies potential logical errors, test redundancy or insufficient coverage of the test case, and provides feedback on potential problems and generates use case repair suggestions.
[0012] In a third aspect of an embodiment of the present invention, an electronic device is provided, comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor implements the steps of the method described in the first aspect of the embodiment of the present invention when executing the computer program.
[0013] In a fourth aspect of an embodiment of the present invention, a computer-readable storage medium is provided, wherein the computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the steps of the method provided in the first aspect of the embodiment of the present invention are implemented.
[0014] In an embodiment of the present invention, test cases are generated by a fine-tuned pre-trained model, and the generated test cases are compared with the software specification and code logic to identify potential problems therein. This not only enables automatic generation of test cases, but also enables identification of problems with the test cases, thereby facilitating manual repair of the test cases and improving the accuracy, reliability and adaptability of the test cases. BRIEF DESCRIPTION OF THE DRAWINGS
[0015] In order to more clearly illustrate the technical solutions in the embodiments of the present invention, the following briefly introduces the drawings required for use in the embodiments or the description of the prior art. Obviously, the drawings described below are only some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without creative work.
[0016] Figure 1 A flowchart of a method for automatically generating software test cases provided by one embodiment of the present invention;
[0017] Figure 2 A schematic diagram of the structure of a software test case automatic generation system provided by one embodiment of the present invention;
[0018] Figure 3 A schematic structural diagram of an electronic device provided by an embodiment of the present invention. DETAILED DESCRIPTION
[0019] In order to make the purpose, features and advantages of the present invention more obvious and easy to understand, the technical solutions in the embodiments of the present invention will be clearly and completely described below in conjunction with the drawings in the embodiments of the present invention. Obviously, the embodiments described below are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.
[0020] It should be understood that the term "including" and other similar expressions in the specification or claims of the present invention and the above-mentioned drawings are intended to cover non-exclusive inclusions, such as a process, method, system, or device including a series of steps or units is not limited to the listed steps or units. In addition, "first" and "second" are used to distinguish different objects, not to describe a specific order.
[0021] See also Figure 1 , a flowchart of a method for automatically generating software test cases provided by an embodiment of the present invention includes:
[0022] S101. Collect and annotate the code base, test case set and error log related to the pre-test system to form a data set, fine-tune the pre-trained model based on the labeled data set, and evaluate and optimize the fine-tuned pre-trained model;
[0023] The pre-test system refers to a software system that needs to be tested. The functional points that need to be tested and the error modes that need to be covered in the pre-test system are obtained, and the corresponding codes, test cases and error logs are collected. For example, a test case is generated for an online shopping system. The system has functions such as product browsing, shopping cart management, order submission and payment. Its test functional points may include login, search, order placement, payment, etc. The error modes that need to be covered may be null pointer exceptions, insufficient permissions, etc., thereby corresponding training data can be collected from open source projects, internal code libraries or related forums.
[0024] Among them, the collected pre-test system related data is cleaned and pre-processed, such as removing duplicates, formatting codes, etc., and the collected data is annotated, the code snippets are marked with functional tags, and the error logs are marked with error type tags.
[0025] The pre-trained model may be a model based on the Transformer architecture, such as BERT, GPT, etc. In this embodiment, by evaluating the performance and applicability of different pre-trained models, and taking into account the characteristics of the code language (such as Java, Python, etc.) and the complexity of the task, it is necessary to select the pre-trained model that best suits the software testing task.
[0026] Among them, it is necessary to set a pre-training task for the pre-training model to train the pre-training model.
[0027] Fine-tuning means further training part or all of the parameters of the model based on the pre-trained model for specific downstream tasks (such as text classification, code recognition, etc.) using a relatively small but labeled target dataset.
[0028] Specifically, set fine-tuning tasks, train pre-trained models with labeled data sets, optimize model parameters, monitor the training process, and adjust hyperparameters such as learning rate and batch size to obtain the best performance. Fine-tuning tasks refer to making minor adjustments to specific tasks (such as code classification, error detection, etc.) based on pre-trained models to make the model better adaptable to new tasks or data in software testing.
[0029] The evaluation and optimization process of the pre-trained model includes: designing evaluation indicators to quantify the performance of the model on software testing tasks, using the validation set to evaluate the fine-tuned pre-trained model, such as evaluating the model accuracy, recall rate, F1 score, etc., checking whether the model has overfitting or underfitting problems, and optimizing the model according to the evaluation results, such as adding data enhancement, trying different model architectures, etc.
[0030] In some embodiments, the performance of the pre-trained model in actual applications is monitored, feedback data such as false positives and false negatives are collected, the model is iteratively trained using the feedback data to improve the accuracy and coverage of the model, and new model architectures and algorithms are continuously tried to meet new software testing requirements.
[0031] S102, parsing the source code of the software to be tested, identifying the functional points to be tested, automatically generating test scenarios and test cases through the fine-tuned pre-trained model, and outputting the test cases after optimizing and screening the test cases;
[0032] Parse the source code of the software to be tested, and identify the functions, classes, variables, control flow structures (such as conditional statements, loops), etc. in the code. For example, generate a test case for a simple online calculator application, parse its source code, and identify the implementation of addition, subtraction, multiplication, division and other calculation functions; after parsing the source code, identify the key functional points in the software, which usually correspond to user-interactive features or core functions of the software. For example, for a calculator application, the key functional points include addition, subtraction, multiplication and division.
[0033] Among them, based on the function points to be tested, corresponding test scenarios are generated, and the test scenarios include how to test each function point under different conditions. For example, for an online calculator application, the following test scenarios are generated for addition operations: normal case, adding two positive numbers; boundary value case, at least one of the two numbers is zero or close to the maximum / minimum value of the integer type; exception handling case, input non-numeric characters or special characters.
[0034] The number of generated test cases may be huge, and they need to be optimized and screened to ensure the effectiveness and efficiency of the test. Optimization can be done by removing redundancies in test cases and increasing scenario coverage.
[0035] Optionally, the test case optimization screening includes: removing redundant test cases, merging similar test cases, and increasing the coverage of key scenarios. For example, it may be found that some test cases are redundant (such as 2+3 and 3+2 are basically the same in the addition scenario), and they can be merged into one test case. It may also be found that some key scenarios (such as the case where the divisor is not zero in the division scenario), and relevant test cases can be added to ensure full coverage.
[0036] The generated test cases can be exported to executable formats such as XML, CSV, etc., and can be integrated into existing test frameworks or automated testing tools for execution. For example, the generated test cases can be exported to XML files in JUnit format and integrated into continuous integration tools such as Jenkins for automated execution and execution result monitoring.
[0037] S103. Analyze the test case logic in real time, identify potential logical errors, test redundancy or insufficient coverage of the test case by comparing the software specification and code logic, and provide feedback on potential problems and generate suggestions for repairing the test case.
[0038] Specifically, parse the test case, extract the test steps, input data and expected output in the test case, perform logical analysis on the test steps, and understand the test intent and expected behavior behind them; compare the test steps and expected output in the test case with the software specification to verify whether the test case covers all functional points, business rules and abnormal situations in the software specification; simulate the code logic of the software according to the test steps and input data in the test case, compare the results of the simulated execution with the expected output in the test case, and verify whether there are any differences; determine the types of potential problems in the test case based on the comparison results between the test case and the software specification and code logic, and the potential problems include logical errors, test redundancy or insufficient coverage.
[0039] Logical errors refer to logical inconsistencies or incorrect assumptions that may exist in test cases; test redundancy refers to the identification of duplicate or invalid test cases; insufficient coverage refers to key functional points or abnormal situations that are not covered by test cases. For these potential problems, they are presented to users through a graphical user interface, highlighting potential problems and generating repair suggestions.
[0040] Preferably, the pre-trained model is iteratively updated according to the potential problem.
[0041] In this embodiment, by comparing the model-generated test cases with the software specifications and code logic, potential problems in the test cases are identified, which facilitates the testers to repair the corresponding test cases and ensures the accuracy, reliability and adaptability of the test cases. At the same time, it is convenient for the iterative update of the pre-trained model.
[0042] In one embodiment, historical test cases, software specifications, and software code logic are collected as training data to train the AI optimization model; the test cases generated by the pre-trained model are input into the trained AI optimization model, and the AI optimization model is used to evaluate the effectiveness of the current test cases, identify potential optimization points, and generate optimization suggestions.
[0043] Analyze the effectiveness of test cases based on AI optimization models, identify potential optimization points, and provide optimization suggestions. Potential optimization points refer to functional points that potentially need to be optimized. Optimization suggestions include streamlining, merging, supplementing or restructuring test cases to improve the overall quality of the test case set.
[0044] Collect relevant data such as test case sets, software specifications, code logic, etc., and pre-process the collected data, including removing duplicate data, converting data formats, etc., so that the AI model can understand and process it; use historical test case data, software specifications, and code logic as training data to train the AI optimization model, which can learn the effectiveness evaluation criteria of test cases and understand the relationship between test cases and software functions; input the test case set to be optimized into the trained AI optimization model, which will analyze each test case, evaluate its effectiveness, and identify potential points to be optimized; based on the analysis results of the AI optimization model, generate intelligent optimization suggestions for test cases, which may include test case simplification (deleting redundant test cases), merging (merging test cases that test the same functional points), supplementing (adding test cases that cover key functional points) or structural adjustment (adjusting the execution order or dependencies of test cases), etc.
[0045] In this embodiment, the quality of the test cases can be further improved by optimizing the automatically generated test cases through training the AI optimization model.
[0046] The optimization suggestions generated by the AI optimization model are presented to testers in a visual way, and tools or interfaces for applying the suggestions are provided. Testers can optimize the test case set according to the suggestions and verify whether the optimized test set improves the test effect.
[0047] In one embodiment, the test cases generated by the pre-trained model are edited based on a graphical user interface to obtain the final version of the test cases. It is understood that the graphical user interface can also receive repair suggestions and optimization suggestions generated by the AI optimization model, and the user edits the generated test cases according to different suggestions.
[0048] It should be understood that the serial numbers of the steps in the above embodiments do not imply a sequence of execution, and the execution sequence of each process should be determined by its function and internal logic, and should not constitute any limitation on the implementation process of the embodiments of the present invention.
[0049] Figure 2 A schematic diagram of the structure of a software test case automatic generation system provided by an embodiment of the present invention, the system comprising:
[0050] The data collection module 210 is used to collect and annotate the code base, test case set and error log related to the pre-test system to form a data set;
[0051] The use case generation module 220 is used to fine-tune the pre-trained model based on the labeled data set, evaluate and optimize the fine-tuned pre-trained model, parse the source code of the software to be tested, identify the function points to be tested, automatically generate test scenarios and test cases through the fine-tuned pre-trained model, optimize and screen the test cases, and then output the test cases;
[0052] The optimization and screening of test cases includes:
[0053] Remove redundant use cases, merge similar use cases, and increase coverage of key scenarios.
[0054] The use case optimization module 230 is used to analyze the test case logic in real time, identify potential logical errors, test redundancy or insufficient coverage of the test case by comparing the software specification and the code logic, and feedback potential problems and generate use case repair suggestions.
[0055] Specifically, the use case optimization module 220 includes:
[0056] A parsing unit, used to parse the test case and extract the test steps, input data and expected output in the test case;
[0057] The first verification unit is used to compare the test steps and expected outputs in the test case with the software specification to verify whether the test case covers all functional points, business rules and abnormal situations in the software specification;
[0058] The second inspection unit is used to simulate the code logic of the execution software according to the test steps and input data in the test case, and compare the result of the simulation execution with the expected output in the test case to check whether there is any difference;
[0059] A judgment unit is used to judge the type of potential problems in the test case based on the comparison results between the test case and the software specification and code logic, wherein the potential problems include logical errors, test redundancy or insufficient coverage. Preferably, the use case optimization module 230 also includes:
[0060] The AI optimization module 240 is used to collect historical test cases, software specifications and software code logic as training data, train the AI optimization model, input the test cases generated by the pre-trained model into the trained AI optimization model, and use the AI optimization model to evaluate the effectiveness of the current test cases, identify potential optimization points and generate optimization suggestions.
[0061] Optionally, the use case optimization module 230 further includes:
[0062] The user interaction module is used to edit the test cases generated by the pre-trained model based on a graphical user interface to obtain the final version of the test cases.
[0063] Those skilled in the art can clearly understand that, for the convenience and brevity of description, the specific working processes of the above-described systems and modules can refer to the corresponding processes in the aforementioned method embodiments, and will not be repeated here.
[0064] Figure 3 1 is a schematic diagram of the structure of an electronic device provided by an embodiment of the present invention. The electronic device is used for automatic generation of test cases. Figure 3 As shown, the electronic device 3 of this embodiment includes: a memory 310, a processor 320 and a system bus 330, wherein the memory 310 includes an executable program 3101 stored thereon, and those skilled in the art can understand that Figure 3 The electronic device structure shown in the figure does not constitute a limitation of the electronic device, and may include more or less components than shown in the figure, or combine certain components, or arrange the components differently.
[0065] Combine the following Figure 3 A detailed introduction to the various components of electronic equipment:
[0066] The memory 310 can be used to store software programs and modules, and the processor 320 executes various functional applications and data processing of the electronic device by running the software programs and modules stored in the memory 310. The memory 310 may mainly include a program storage area and a data storage area, wherein the program storage area may store an operating system, an application required for at least one function (such as a sound playback function, an image playback function, etc.), etc.; the data storage area may store data created according to the use of the electronic device (such as cache data), etc. In addition, the memory 310 may include a high-speed random access memory, and may also include a non-volatile memory, such as at least one disk storage device, a flash memory device, or other volatile solid-state storage devices.
[0067] The memory 310 includes an executable program 3101 of a network request method, and the executable program 3101 can be divided into one or more modules / units, which are stored in the memory 310 and executed by the processor 320 to realize the generation and optimization of test cases, etc. The one or more modules / units can be a series of computer program instruction segments that can complete specific functions, and the instruction segments are used to describe the execution process of the computer program 3101 in the electronic device 3. For example, the computer program 3101 can be divided into functional modules such as a data collection module, a use case generation module, and a use case optimization module.
[0068] The processor 320 is the control center of the electronic device. It uses various interfaces and lines to connect various parts of the entire electronic device. By running or executing software programs and / or modules stored in the memory 310, and calling data stored in the memory 310, it performs various functions of the electronic device and processes data, thereby monitoring the overall status of the electronic device. Optionally, the processor 320 may include one or more processing units; preferably, the processor 320 may integrate an application processor and a modem processor, wherein the application processor mainly processes the operating system, application programs, etc., and the modem processor mainly processes wireless communications. It is understandable that the above-mentioned modem processor may not be integrated into the processor 320.
[0069] The system bus 330 is used to connect the various functional components inside the computer, and can transmit data information, address information, and control information. Its types can be, for example, PCI bus, ISA bus, CAN bus, etc. The instructions of the processor 320 are transmitted to the memory 310 through the bus, and the memory 310 feeds back data to the processor 320. The system bus 330 is responsible for the data and instruction exchange between the processor 320 and the memory 310. Of course, the system bus 330 can also be connected to other devices, such as network interfaces, display devices, etc.
[0070] In the embodiment of the present invention, the executable program executed by the processor 320 included in the electronic device includes:
[0071] Collect and annotate the code base, test case set, and error logs related to the pre-test system to form a data set, fine-tune the pre-trained model based on the labeled data set, and evaluate and optimize the fine-tuned pre-trained model;
[0072] Parse the source code of the software to be tested, identify the functional points to be tested, automatically generate test scenarios and test cases through the fine-tuned pre-trained model, optimize and screen the test cases, and then output the test cases;
[0073] Analyze test case logic in real time, identify potential logical errors, test redundancy or insufficient coverage of test cases by comparing software specifications and code logic, provide feedback on potential problems, and generate suggestions for repairing the test cases.
[0074] Those skilled in the art can clearly understand that, for the convenience and brevity of description, the specific working processes of the systems, devices and modules described above can refer to the corresponding processes in the aforementioned method embodiments and will not be repeated here.
[0075] In the above embodiments, the description of each embodiment has its own emphasis. For parts that are not described or recorded in detail in a certain embodiment, reference can be made to the relevant descriptions of other embodiments.
[0076] As described above, the above embodiments are only used to illustrate the technical solutions of the present invention, rather than to limit the same. Although the present invention has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that the technical solutions described in the aforementioned embodiments may still be modified, or some of the technical features thereof may be replaced by equivalents. However, these modifications or replacements do not deviate the essence of the corresponding technical solutions from the spirit and scope of the technical solutions of the embodiments of the present invention.
Claims
1. A method for automatically generating software test cases. It is characterized in that include: Collect and annotate the code base, test case set, and error logs related to the pre-test system to form a data set, fine-tune the pre-trained model based on the labeled data set, and evaluate and optimize the fine-tuned pre-trained model; Parse the source code of the software to be tested, identify the functional points to be tested, automatically generate test scenarios and test cases through the fine-tuned pre-trained model, optimize and screen the test cases, and then output the test cases; Analyze test case logic in real time, identify potential logical errors, test redundancy or insufficient coverage of test cases by comparing software specifications and code logic, provide feedback on potential problems, and generate suggestions for repairing the test cases.
2. The method according to claim 1, It is characterized in that The test case optimization screening includes: Remove redundant use cases, merge similar use cases, and increase coverage of key scenarios.
3. The method according to claim 1, It is characterized in that The real-time analysis of test case logic, by comparing the software specification and code logic, identifies potential logic errors, test redundancy or insufficient coverage of test cases, including: Parse test cases and extract test steps, input data and expected outputs from them; Compare the test steps and expected outputs in the test case with the software specification to verify whether the test case covers all functional points, business rules and exceptions in the software specification; According to the test steps and input data in the test case, simulate the code logic of the software and compare the simulation execution results with the expected output in the test case to check whether there are any differences; Based on the comparison results of the test case with the software specification and code logic, the types of potential problems in the test case are determined, and the potential problems include logical errors, test redundancy or insufficient coverage.
4. The method according to claim 1, It is characterized in that The above-mentioned identification of potential logical errors, test redundancy or insufficient coverage of test cases by comparing the software specification and code logic, and feedback of potential problems and generation of repair suggestions for test cases also include: Collect historical test cases, software specifications, and software code logic as training data to train the AI optimization model; The test cases generated by the pre-trained model are input into the trained AI optimization model, and the AI optimization model is used to evaluate the effectiveness of the current test cases, identify potential optimization points, and generate optimization suggestions.
5. The method according to claim 1, It is characterized in that The above-mentioned identification of potential logical errors, test redundancy or insufficient coverage of test cases by comparing the software specification and code logic, and feedback of potential problems and generation of repair suggestions for test cases also include: Edit the test cases generated by the pre-trained model based on the graphical user interface to obtain the final version of the test cases.
6. A software test case automatic generation system, It is characterized in that include: The data collection module is used to collect and annotate the code base, test case set and error log related to the pre-test system to form a data set; The use case generation module is used to fine-tune the pre-trained model based on the labeled data set, evaluate and optimize the fine-tuned pre-trained model, parse the source code of the software to be tested, identify the functional points to be tested, automatically generate test scenarios and test cases through the fine-tuned pre-trained model, and output the test cases after optimizing and screening the test cases; The use case optimization module is used to analyze the test case logic in real time. By comparing the software specification and code logic, it identifies potential logical errors, test redundancy or insufficient coverage of the test case, and provides feedback on potential problems and generates use case repair suggestions.
7. The system according to claim 6, It is characterized in that The use case optimization module also includes: The AI optimization module is used to collect historical test cases, software specifications and software code logic as training data, train the AI optimization model, input the test cases generated by the pre-trained model into the trained AI optimization model, and use the AI optimization model to evaluate the effectiveness of the current test cases, identify potential optimization points and generate optimization suggestions.
8. The system according to claim 6, It is characterized in that The use case optimization module also includes: The user interaction module is used to edit the test cases generated by the pre-trained model based on a graphical user interface to obtain the final version of the test cases.
9. An electronic device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, It is characterized in that When the processor executes the computer program, the steps of the method for automatically generating software test cases as described in any one of claims 1 to 5 are implemented.
10. A computer-readable storage medium storing a computer program. It is characterized in that When the computer program is executed, the steps of a method for automatically generating software test cases as described in any one of claims 1 to 5 are implemented.
Citation Information
Cited By
Software testing automation platform and intelligent optimization method thereof
CN119415393A
Mobile equipment automatic testing method based on variable optimization
CN119829426A
Software automatic testing method and device
CN119883907A
Software automated testing method and device
CN119883907B
Chip test method and system of general test machine based on artificial intelligence model
CN120891359A
