Method and system for generating test code of unit to be tested and storage medium

By obtaining source code project files and abstract syntax trees, generating and evaluating test code, the problem of low efficiency in testing code generation in the existing technology is solved, automatic detection and correction are realized, and generation efficiency is improved.

CN119537245APending Publication Date: 2025-02-28AGRICULTURAL BANK OF CHINA
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202411726798.8
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-11-28
Publication Date
2025-02-28

AI Technical Summary

Technical Problem

The prior art is difficult to efficiently generate test codes that meet unit testing needs, resulting in users requiring secondary modifications, which reduces the generation efficiency.

Method used

Generate test code and evaluate it by obtaining the source code project file of the unit to be tested and its abstract syntax tree. If the evaluation results do not meet the standards, correct them until they meet the standards.

Benefits of technology

Automatic detection and correction of unmet codes is realized, and the test code that meets the standard is automatically generated without manual participation, improving the efficiency of the test code generation.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119537245A_ABST
    Figure CN119537245A_ABST
Patent Text Reader

Abstract

The invention provides a method and system for generating a test code of a to-be-tested unit and a storage medium, and is used for automatically generating the test code and automatically correcting the substandard test code so as to improve the generation effect of the test code. The method comprises the steps of obtaining a source code project file of the to-be-tested unit, and an abstract syntax tree of the source code project file; generating a test code based on the source code project file and the abstract syntax tree of the source code project file; evaluating the test code to obtain a code evaluation result; and if the code evaluation result is that the test code does not reach the standard, correcting the test code until the code evaluation result is that the test code reaches the standard.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the field of data processing technology, and in particular to a method, system and storage medium for generating test codes for a unit to be tested. Background Art

[0002] As the scale and complexity of software gradually expand, code quality and security issues become increasingly prominent, and unit testing has received more and more attention. In software development, unit testing is at the bottom of the test chain and is also the most important layer. Unit testing has the smallest test granularity, but it helps the most in discovering problems. Intercepting code defects in unit testing can effectively prevent the spread of defects and improve system security. Good unit testing in software can improve code quality, promote code optimization, improve R&D efficiency, and increase confidence in refactoring.

[0003] One possible way to implement unit testing is to automatically generate test code for unit testing. However, the quality of automatically generated test code is poor and it is difficult to meet the needs of unit testing. Users are required to modify the test code themselves, which leads to low efficiency in test code generation. Therefore, how to improve the efficiency of test code generation has become a technical problem that needs to be solved. Summary of the invention

[0004] In view of the above problems, the object of the present invention is to provide a method, system and storage medium for generating test code for a unit to be tested, so as to improve the generation efficiency of unit test code. The specific scheme is as follows:

[0005] In a first aspect, an embodiment of the present application provides a method for generating test code for a unit to be tested, the method comprising:

[0006] Obtaining a source code project file of a unit to be tested and an abstract syntax tree of the source code project file;

[0007] Generate test code based on the source code project file and the abstract syntax tree of the source code project file;

[0008] Evaluate the test code to obtain a code evaluation result;

[0009] If the code evaluation result is not up to standard, the test code is corrected until the code evaluation result is up to standard.

[0010] Optionally, the evaluating the test code to obtain a code evaluation result includes:

[0011] The test code is evaluated to obtain a code test report; the code test report includes the code evaluation result.

[0012] Optionally, the modifying the test code until the code evaluation result is up to standard includes:

[0013] Inputting the code test report and the source code project file into a suggestion model to generate code modification suggestion information;

[0014] Based on the modification suggestion information and the code test report, the test code is corrected to obtain a corrected test code;

[0015] Evaluating the modified test code to obtain a code evaluation result of the modified test code;

[0016] If the code evaluation result is not up to standard, the correction test code is corrected until the code evaluation result is up to standard.

[0017] Optionally, the modifying the test code based on the modification suggestion information and the code test report to obtain the modified test code includes:

[0018] Determine the revised content and the content to be replaced based on the modification suggestion information and the code test report;

[0019] The content to be replaced in the test code is replaced with the revised content to obtain the revised test code.

[0020] Optionally, the evaluating the modified test code to obtain a code evaluation result of the modified test code includes:

[0021] The revised content is evaluated to obtain a code evaluation result of the revised test code.

[0022] Optionally, the step of obtaining a source code project file of the unit to be tested and an abstract syntax tree of the source code project file includes:

[0023] Obtain the source code project file;

[0024] Based on the source code project file, determining the type of the source code project file;

[0025] Based on the type of the source code project file, the source code project file is analyzed to obtain an abstract syntax tree of the source code project file.

[0026] Optionally, the generating test code based on the source code project file and the abstract syntax tree of the source code project file includes:

[0027] Extracting source code information from the source code project file;

[0028] Test code is generated based on the source code information and the abstract syntax tree of the source code project file.

[0029] Optionally, the method further includes:

[0030] Obtain code modification suggestion information satisfaction;

[0031] When the satisfaction level of the code modification suggestion information is lower than a preset value, obtaining feedback data;

[0032] The suggestion model is optimized based on the feedback data.

[0033] In a second aspect, an embodiment of the present application provides a system for generating test code for a unit to be tested, the system comprising:

[0034] An acquisition module, used to acquire a source code project file of a unit to be tested and an abstract syntax tree of the source code project file;

[0035] A generating module, configured to generate a test code based on the source code project file and an abstract syntax tree of the source code project file;

[0036] An evaluation module, used to evaluate the test code and obtain a code evaluation result;

[0037] The correction module is used to correct the test code if the code evaluation result is not up to standard, until the code evaluation result is up to standard.

[0038] In a third aspect, an embodiment of the present application provides a computer-readable storage medium having a computer program stored thereon, wherein the computer program, when executed by a processor, implements any of the above-mentioned methods for generating a test code for a unit to be tested.

[0039] Compared with the prior art, this application has the following beneficial effects:

[0040] By obtaining the source code project file of the unit to be tested and the abstract syntax tree of the source code project file. Based on the source code project file and the abstract syntax tree of the source code project file, generate test code. Evaluate the test code to obtain the code evaluation result. If the code evaluation result is not up to standard, correct the test code until the code evaluation result is up to standard, realize automatic detection of non-standard code, and automatically correct the non-standard code when it is detected, so as to automatically generate qualified test code. In this way, qualified test code can be automatically generated without human intervention, thereby improving the efficiency of test code generation. BRIEF DESCRIPTION OF THE DRAWINGS

[0041] In order to more clearly illustrate the embodiments of the present application or the technical solutions in the prior art, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are only some embodiments recorded in the present application. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.

[0042] Figure 1 A flowchart of a method for generating test code for a unit to be tested provided in an embodiment of the present application;

[0043] Figure 2 A schematic diagram of the structure of a device for generating test code for a unit to be tested provided in an embodiment of the present application;

[0044] Figure 3 A flowchart of another method for generating test code for a unit to be tested provided in an embodiment of the present application;

[0045] Figure 4 A schematic diagram of a process for optimizing a suggestion model provided in an embodiment of the present application;

[0046] Figure 5 A schematic diagram of the structure of a system for generating test code for a unit to be tested provided in an embodiment of the present application. DETAILED DESCRIPTION

[0047] The following will be combined with the drawings in the embodiments of the present application to clearly and completely describe the technical solutions in the embodiments of the present application. Obviously, the described embodiments are only part of the embodiments of the present application, not all of the embodiments. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of this application.

[0048] The terms "including" and "having" and any variations thereof in the specification and claims of this application and the above-mentioned drawings are intended to cover non-exclusive inclusions. For example, a process, method or apparatus comprising a series of steps or units is not necessarily limited to those steps or units explicitly listed, but may include other steps or units not explicitly listed or inherent to these processes, methods, products or apparatuses.

[0049] Since the unit to be tested may have multiple test scenarios and the required test scenarios are relatively complex, the currently automatically generated test code is difficult to meet the various test scenarios required by the unit to be tested, resulting in poor quality of the test code and failure to meet the test requirements of the unit to be tested. Therefore, users often need to make secondary modifications to the test code that does not meet the requirements to improve the quality of the test code, resulting in low efficiency in generating test code that meets the requirements. To solve this technical problem, the technical solution adopted by this application is as follows.

[0050] like Figure 1 As shown, an embodiment of the present application provides a method for generating test code for a unit to be tested, the method comprising:

[0051] S101: Obtain a source code project file of a unit to be tested and an abstract syntax tree of the source code project file.

[0052] The above method is specifically applied to a device for generating test code for a unit to be tested, such as Figure 2 The device for generating the test code of the unit to be tested shown includes a unit test control unit, a unit test design unit, a unit test evaluation unit and an intelligent code prompt unit. The user arranges the unit test code generation process through the unit test control unit and designs the Robotic Process Automation (RPA) process so that the unit test control unit coordinates the unit test design unit, the unit test evaluation unit and the intelligent code prompt unit. The RAP process can also adjust and expand the devices in the unit based on actual needs. For example, for units to be tested in different projects, the design of the modified device can be modified or the device can be replaced.

[0053] Specifically, the source code project file of the unit to be tested is input into the unit test design unit, so that the unit test design unit analyzes the source code project file to obtain an abstract syntax tree of the source code project file. The unit test design unit includes a source code scanner, a dependency analyzer, a code generator and a code corrector.

[0054] It should be noted that a source code project file may include multiple functions or methods.

[0055] Furthermore, the source code project file of the unit to be tested and the abstract syntax tree of the source code project file are obtained, including:

[0056] Get the source code project file;

[0057] Based on the source code project file, determining the type of the source code project file;

[0058] Based on the type of the source code project file, the source code project file is analyzed to obtain an abstract syntax tree of the source code project file.

[0059] like Figure 3 As shown, a source code scanner is used to scan all source code files in the source code project file input by the user to obtain the type of the source code project file and the project architecture. The type of the source code project file is the type of the programming language of the source code file, such as C language, python, etc. A parser for analyzing the type of the source code project file is selected, the source code project file is analyzed, and an abstract syntax tree (AST) of the source code project file is constructed. The abstract syntax tree includes the semantic information of all function methods in the source code file, that is, the meta information of the code, so that the abstract syntax tree can reflect the behavior and intention of the code.

[0060] For example, the unit test control unit calls a parser for parsing source code project files of the C language type through the RPA controller to parse source code project files a, b, and c. The programming languages ​​parsed by source code project files a, b, and c are all C language.

[0061] S102: Generate test code based on the source code project file and the abstract syntax tree of the source code project file.

[0062] The dependency analyzer determines the public dependency objects or function methods in the source code project file located in other code files based on the abstract syntax tree. A baffle is set for the public dependency objects or function methods located in other code files to separate the dependency relationship between the code in the source code project file and other code files, decoupling the dependency, and providing a test environment for the source code project file.

[0063] Specifically, based on the source code project file and the abstract syntax tree of the source code project file, the test code is generated, including:

[0064] Extract source code information from source code project files;

[0065] Generate test code based on source code information and an abstract syntax tree of the source code project file.

[0066] After dependency decoupling, the dependency analyzer analyzes the specific behavior of functions or methods in the source code project file, strips off the behavior involving external dependencies, and obtains source code information to prevent the generated test code from being affected by external dependencies.

[0067] The code generator inputs the source code information and the information in the abstract syntax tree into the code generation template based on the type of the source code project file and the project architecture to obtain the test code. For example, the RPA process is orchestrated so that RPA executes the specified code generation program according to the preset conditions in combination with the specific code context, so as to fill the source code information into the code generation template according to the preset conditions, thereby generating the test code combined with the code context. The test code includes the unit test environment setting code, the target method calling code, the test data code and the execution result verification code.

[0068] It should be noted that the test code may include multiple test cases, and any one of the multiple test cases is used to test a function or method in the source code project file.

[0069] S103: Evaluate the test code to obtain a code evaluation result.

[0070] The unit test evaluation unit monitors whether the test code has changed, and when the test code has changed, the test code is evaluated to obtain the code evaluation result. For example, the test code is evaluated when the unit test code is first generated, or when the test code is modified.

[0071] In an optional embodiment, the test code is evaluated to obtain a code evaluation result, including:

[0072] The test code is evaluated to obtain a code test report; the code test report includes the code evaluation result.

[0073] The unit test evaluation unit includes a unit test result scanner and a unit test report generator. The unit test result scanner monitors whether the test code has changed. When the test code has changed, the changed code in the test code is passed to the unit test report generator. Since the unmodified code part will not affect the unit test indicators, the unit test result scanner only needs to pass the changed code in the test code to the unit test report generator to avoid repeated evaluation of the same code and speed up the generation efficiency of the code test report.

[0074] For example, when the unit test result scanner generates the test code of the unit to be tested for the first time, it passes the generated test code to the unit test report generator. When the test code of the same unit to be tested is modified, the code that changes in the test code is the code that is modified in the test code, and the unit test result scanner passes the modified code in the test code to the unit test report generator, thereby improving the efficiency of generating code test reports and saving power consumption of code evaluation.

[0075] The evaluation indicators of the test code are obtained. The unit test report generator evaluates the test code based on the evaluation indicators to obtain a code test report containing the code evaluation results. The evaluation indicators include at least one of the following: coverage indicators, error detection indicators, test efficiency indicators, test maintenance indicators, and other indicators used to measure code quality. Coverage indicators are used to measure the coverage of the target code by unit tests, including statement coverage, branch coverage, condition coverage, and path coverage. Error detection indicators are used to determine whether the unit test code can detect errors in the code, including error rate, error density, and error discovery rate. Test efficiency indicators are used to measure the efficiency and resource consumption during the execution of test cases, including test case execution time, memory occupancy, and CPU utilization. Test maintenance indicators are used to measure the maintainability and reusability of test cases, including complexity, readability, and maintainability.

[0076] For example, when the evaluation index includes coverage index, and the coverage index includes statement coverage, if the statement coverage of the test code is greater than or equal to the preset statement coverage value set by the user, the code evaluation result in the code test report is up to standard. If the statement coverage of the test code is less than the preset statement coverage value set by the user, the code evaluation result in the code test report is not up to standard.

[0077] It should be noted that the devices in each unit in this application can be integrated with the existing software system to adapt to different unit testing scenarios. For example, when the current unit testing scenario has high requirements on the accuracy of the code test report, when the single test report generator evaluates the test code based on the evaluation index, it can call multiple different single test software to evaluate the test code at the same time, and integrate the evaluation results of each single test software to obtain a more accurate code test report. For example, the unit test control unit uses single test software A, single test software B and single test software C to evaluate the test code through the RPA controller call and the single test report generator, and integrates the evaluation results of single test software A, single test software B and single test software C to obtain a code test report.

[0078] Furthermore, the code test report also includes the code in the test code that does not meet the indicators, thereby refining the evaluation indicators to the code level, so that users can determine the code that needs to be optimized, providing a basis and optimization direction for the modification of the test code.

[0079] S104: If the code evaluation result is not up to standard, the test code is corrected until the code evaluation result is up to standard.

[0080] When the code evaluation result is not up to standard, the intelligent code prompting unit corrects the test code until the code evaluation result is up to standard.

[0081] Specifically, the test code is modified until the code evaluation result meets the standard, including:

[0082] Input the code test report and source code project file into the suggestion model to generate code modification suggestion information;

[0083] Based on the modification suggestion information and the code test report, the test code is modified to obtain the modified test code;

[0084] Evaluate the modified test code to obtain a code evaluation result of the modified test code;

[0085] If the code evaluation result is not up to standard, the test code will be corrected until the code evaluation result is up to standard.

[0086] The intelligent code prompt unit includes an artificial intelligence (AI) code generator, which integrates a code generation AI model, that is, a suggestion model. The suggestion model is used to generate code modification suggestion information based on the input code test report and source code information in the source code project file. After the test code is corrected, the corrected test code is evaluated to obtain a code evaluation result. If the code evaluation result is still not up to standard, the corrected test code continues to be corrected until the code evaluation result for the corrected test code is up to standard.

[0087] Further, based on the modification suggestion information and the code test report, the test code is corrected to obtain the corrected test code, including:

[0088] Determine the contents to be corrected and replaced based on the modification suggestion information and code test report;

[0089] The content to be replaced in the test code is replaced with the revised content to obtain the revised test code.

[0090] When correcting the test code, the code generator generates the correction content based on the evaluation indicators and modification suggestion information fed back in the code test report and the code generation template. The code corrector determines the content to be replaced from the code test report, and overwrites the content to be replaced with the correction content to obtain the corrected test code.

[0091] Furthermore, before the code corrector overwrites the content to be replaced with the corrected content, the present application may also provide feedback of the corrected content to the user. If the user accepts the corrected content and does not modify the corrected content, the code corrector overwrites the content to be replaced with the corrected content. If the user does not accept the corrected content, the code corrector receives the latest corrected content provided by the user and overwrites the content to be replaced with the latest corrected content provided by the user. The latest corrected content provided by the user is the corrected content manually modified by the user, or a code file that conforms to the user's correction opinion.

[0092] It should be noted that the present application may determine whether the user accepts the revised content based on the user's rating of the revised content, or the user's satisfaction with the revised content, etc. For example, when the satisfaction with the code modification suggestion information is lower than a preset value, it is determined that the user does not accept the revised content, and when the satisfaction with the code modification suggestion information is higher than or equal to a preset value, it is determined that the user accepts the revised content.

[0093] Furthermore, since different projects and different users have different specific requirements for unit testing, the actual environment may be more complex, and the suggested model cannot adapt to so many scenarios. In order to improve the correction ability of the suggested model and adapt to the user's usage scenarios, the above method also includes:

[0094] Obtain code modification suggestion information satisfaction;

[0095] When the code modification suggestion information satisfaction is lower than the preset value, feedback data is obtained;

[0096] The proposed model is optimized based on the feedback data.

[0097] The intelligent code prompt unit also includes a model optimizer, which is used to optimize the suggestion model. When the satisfaction level of the code modification suggestion information is greater than or equal to the preset value, it means that the correction content generated by the suggestion model meets the user's requirements and no further optimization is required. When the satisfaction level of the code modification suggestion information is lower than the preset value, it means that the correction content generated by the suggestion model does not meet the user's requirements. At this time, the model optimizer uses the latest correction content provided by the user as feedback data, and uses the feedback data as the correct data under the current source code project file and code test report to train and optimize the suggestion model, so that the correction code output by the suggestion model is closer to the latest correction content provided by the user. In this way, by enabling the suggestion model to continuously learn the correction content provided by the user, the optimization iteration of the suggestion model is achieved, the quality of the correction content is improved, and the correction content generated by the suggestion model is more in line with the needs of the user, gradually reducing the content and number of modifications required by the user, and improving the user experience.

[0098] It should be noted that, since the unit test result scanner only passes the changed code in the test code to the unit test report generator when the test code is changed, when the test code is modified, the unit test report generator only evaluates the modified content and obtains the code evaluation result of the modified test code. Therefore, there is no need to repeatedly evaluate the code in the test code that has met the evaluation indicators, which improves the evaluation efficiency.

[0099] For easier understanding, the following example is given:

[0100] like Figure 4 As shown, after the test code is generated, the test code is evaluated. If the code evaluation result is not up to standard, modification suggestion information is generated. It is determined whether there is the latest correction content. If there is the latest correction content, the suggestion model is iteratively optimized based on the latest correction content, and the test code is corrected based on the latest correction content. When the corrected test code meets the standard, the corrected test code is output. If there is no latest correction content, the test code is corrected based on the modification suggestion information. When the corrected test code meets the standard, the corrected test code is output. In this way, continuous iterative optimization of the suggestion model is achieved, making the correction content generated by the suggestion model more accurate.

[0101] By obtaining the source code project file of the unit to be tested and the abstract syntax tree of the source code project file. Based on the source code project file and the abstract syntax tree of the source code project file, generate test code. Evaluate the test code to obtain the code evaluation result. If the code evaluation result is not up to standard, correct the test code until the code evaluation result is up to standard, realize automatic detection of non-standard code, and automatically correct the non-standard code when it is detected, so as to automatically generate qualified test code. In this way, qualified test code can be automatically generated without human intervention, thereby improving the efficiency of test code generation.

[0102] like Figure 5 As shown, the embodiment of the present application also provides a system for generating test code for a unit to be tested, the system comprising:

[0103] An acquisition module 501 is used to acquire a source code project file of a unit to be tested and an abstract syntax tree of the source code project file;

[0104] A generating module 502, configured to generate test code based on the source code project file and the abstract syntax tree of the source code project file;

[0105] Evaluation module 503, used to evaluate the test code and obtain a code evaluation result;

[0106] The correction module 504 is used to correct the test code if the code evaluation result is not up to standard, until the code evaluation result is up to standard.

[0107] In an optional embodiment, the evaluation module 503 is specifically used for:

[0108] The test code is evaluated to obtain a code test report; the code test report includes the code evaluation result.

[0109] In an optional embodiment, the correction module 504 is specifically used for:

[0110] Input the code test report and source code project file into the suggestion model to generate code modification suggestion information;

[0111] Based on the modification suggestion information and the code test report, the test code is modified to obtain the modified test code;

[0112] The evaluation module 503 is also used to evaluate the modified test code to obtain a code evaluation result of the modified test code;

[0113] If the code evaluation result is not up to standard, the test code will be corrected until the code evaluation result is up to standard.

[0114] In an optional embodiment, the correction module 504 is specifically used for:

[0115] Determine the contents to be corrected and replaced based on the modification suggestion information and code test report;

[0116] The content to be replaced in the test code is replaced with the revised content to obtain the revised test code.

[0117] In an optional embodiment, the evaluation module 503 is specifically used for:

[0118] The revised content is evaluated to obtain a code evaluation result of the revised test code.

[0119] In an optional embodiment, the acquisition module 501 is specifically used for:

[0120] Get the source code project file;

[0121] Based on the source code project file, determining the type of the source code project file;

[0122] Based on the type of the source code project file, the source code project file is analyzed to obtain an abstract syntax tree of the source code project file.

[0123] In an optional embodiment, the generating module 502 is specifically used for:

[0124] Extract source code information from source code project files;

[0125] Generate test code based on source code information and an abstract syntax tree of the source code project file.

[0126] In an optional embodiment, the system further includes an optimization module, and the optimization module is used to:

[0127] Obtain code modification suggestion information satisfaction;

[0128] When the code modification suggestion information satisfaction is lower than the preset value, feedback data is obtained;

[0129] The proposed model is optimized based on the feedback data.

[0130] By obtaining the source code project file of the unit to be tested and the abstract syntax tree of the source code project file. Based on the source code project file and the abstract syntax tree of the source code project file, generate test code. Evaluate the test code to obtain the code evaluation result. If the code evaluation result is not up to standard, correct the test code until the code evaluation result is up to standard, realize automatic detection of non-standard code, and automatically correct the non-standard code when it is detected, so as to automatically generate qualified test code. In this way, qualified test code can be automatically generated without human intervention, thereby improving the efficiency of test code generation.

[0131] An embodiment of the present application provides a computer storage medium for storing a computer program. When the computer program is executed, it is used to implement any of the above methods for generating a test code for a unit to be tested.

[0132] 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 temporary computer readable media (transitory media), such as modulated data signals and carrier waves.

[0133] Professionals may further appreciate that the units and algorithm steps of each example described in conjunction with the embodiments disclosed herein can be implemented in electronic hardware, computer software, or a combination of the two. In order to clearly illustrate the interchangeability of hardware and software, the composition and steps of each example have been generally described in the above description according to function. Whether these functions are performed in hardware or software depends on the specific application and design constraints of the technical solution. Professionals and technicians may use different methods to implement the described functions for each specific application, but such implementation should not be considered to be beyond the scope of the present invention.

[0134] The technical content provided by the present invention is introduced in detail above. Specific examples are used in this article to illustrate the principles and implementation methods of the present invention. The description of the above embodiments is only used to help understand the method of the present invention and its core idea. At the same time, for general technical personnel in the field, according to the idea of ​​the present invention, there will be changes in the specific implementation methods and application scope. In summary, the content of this specification should not be understood as a limitation on the present invention.

Claims

1. A method for generating test code for a unit to be tested, characterized in that: The method comprises: Obtaining a source code project file of a unit to be tested and an abstract syntax tree of the source code project file; Generate test code based on the source code project file and the abstract syntax tree of the source code project file; Evaluate the test code to obtain a code evaluation result; If the code evaluation result is not up to standard, the test code is corrected until the code evaluation result is up to standard.

2. The method according to claim 1, characterized in that The step of evaluating the test code to obtain a code evaluation result includes: The test code is evaluated to obtain a code test report; the code test report includes the code evaluation result.

3. The method according to claim 2, characterized in that The test code is modified until the code evaluation result is up to standard, including: Inputting the code test report and the source code project file into a suggestion model to generate code modification suggestion information; Based on the modification suggestion information and the code test report, the test code is corrected to obtain a corrected test code; Evaluating the modified test code to obtain a code evaluation result of the modified test code; If the code evaluation result is not up to standard, the correction test code is corrected until the code evaluation result is up to standard.

4. The method according to claim 3, characterized in that The step of correcting the test code based on the modification suggestion information and the code test report to obtain a corrected test code includes: Determine the revised content and the content to be replaced based on the modification suggestion information and the code test report; The content to be replaced in the test code is replaced with the revised content to obtain the revised test code.

5. The method according to claim 4, characterized in that The step of evaluating the modified test code to obtain a code evaluation result of the modified test code includes: The revised content is evaluated to obtain a code evaluation result of the revised test code.

6. The method according to claim 1, characterized in that The step of obtaining a source code project file of a unit to be tested and an abstract syntax tree of the source code project file includes: Obtain the source code project file; Based on the source code project file, determining the type of the source code project file; Based on the type of the source code project file, the source code project file is analyzed to obtain an abstract syntax tree of the source code project file.

7. The method according to claim 6, characterized in that The generating test code based on the source code project file and the abstract syntax tree of the source code project file comprises: Extracting source code information from the source code project file; Test code is generated based on the source code information and the abstract syntax tree of the source code project file.

8. The method according to claim 3, characterized in that The method further comprises: Obtain code modification suggestion information satisfaction; When the satisfaction level of the code modification suggestion information is lower than a preset value, obtaining feedback data; The suggestion model is optimized based on the feedback data.

9. A system for generating test code for a unit to be tested, characterized in that: The system comprises: An acquisition module, used to acquire a source code project file of a unit to be tested and an abstract syntax tree of the source code project file; A generating module, configured to generate a test code based on the source code project file and an abstract syntax tree of the source code project file; An evaluation module, used to evaluate the test code and obtain a code evaluation result; The correction module is used to correct the test code if the code evaluation result is not up to standard, until the code evaluation result is up to standard.

10. A computer-readable storage medium, characterized in that: A computer program is stored thereon, wherein when the computer program is executed by a processor, the method for generating a test code for a unit to be tested as claimed in any one of claims 1 to 8 is implemented.