Vehicle unit test code generation method and device, equipment, medium and product
By introducing a test intent generation engine and a large language model, and combining call chain analysis and abstract syntax tree to generate control flow graphs, the problem of low accuracy in automatically generated test code is solved, achieving efficient and accurate generation of vehicle unit test code, reducing manual revision costs and driving safety risks.
Patent Information
- Application Number
- CN202511025134.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-07-24
- Publication Date
- 2025-11-07
AI Technical Summary
The automatically generated test code in the existing technology has low accuracy and high error rate, and requires a lot of manual revision, making it difficult to meet the high quality and safety requirements of vehicle software.
By introducing a test intent generation engine, combined with a large language model and template library, structured test intents are generated to accurately match vehicle testing requirements. The control flow graph is generated using call chain analysis and abstract syntax tree, and precise test code is generated by combining depth-first search to traverse conditional paths.
It significantly improves the accuracy and usability of test code, reduces the cost of manual revisions, enhances branch coverage and driving safety of unit tests, and reduces code generation time and maintenance costs.
Smart Images

Figure CN120909933A_ABST
Abstract
Description
TECHNICAL FIELD
[0001] The present application relates to the technical field of vehicle software testing, and in particular to a vehicle unit test code generation method, device, equipment, medium and product. BACKGROUND
[0002] With the continuous progress of science and technology and the rapid development of the automobile industry, intelligent vehicles have become one of the main development directions of the automobile industry, and the functional complexity of vehicle software has also increased greatly. The quality and safety of vehicle software have become the focus of attention of automobile manufacturers and consumers, and building reliable and high-quality vehicle software faces more and more challenges. Unit testing is a process of isolating and verifying the smallest testable unit in software engineering, aiming to check the logical correctness through automated assertions, and ensuring that code changes do not break the basic functions. Unit testing plays a crucial role in ensuring software quality, and unit testing often accounts for about 30% of the time of developers. Traditional manual writing of unit test code faces three major pain points of low efficiency, insufficient coverage, and high maintenance cost. Improving the efficiency of developing unit tests and improving the accuracy and adoption rate of automatically generated unit test code are complex problems that need to be solved. Currently, some technologies introduce large language models into the test code writing scenario. By submitting the target code to be tested to the large language model and inputting the test requirements from the user, the large language model automatically generates a test code for testing the accuracy of the target code after analysis. However, the current scheme still has a serious problem of AI illusion, and the error rate of the generated test code is high, and the usability is still low. It is necessary to improve the scheme to ensure the accuracy of the automatically generated test code. SUMMARY
[0003] Therefore, the present application provides a vehicle unit test code generation method, device, equipment, medium and product to solve the problem of improving the accuracy of automatically generated test code.
[0004] In a first aspect, the present application provides a vehicle unit test code generation method, which comprises: obtaining a code to be tested; inputting the code to be tested and an intent prompt word into a large language model for analysis, and generating a test intent through the large language model, the test intent being used to indicate the process required for testing the code to be tested; inputting the test intent, the code to be tested and a template prompt word into the large language model for analysis, to generate a test strategy through the large language model, and recommend a code template and parameter values required for testing from a template library; integrating the recommended code template according to the test strategy, and filling the parameter values into the code template to obtain a test code.
[0005] According to the above technical means, by introducing the test intention generation engine, the problem of high error rate of large language model generation caused by user input demand ambiguity is effectively solved, and the accuracy and usability of the test code are significantly improved. By associating the original test code with the structured test intention generated by the large language model, the ambiguous test requirements are converted into standardized instructions that can be accurately executed by machines, avoiding the AI illusion problem caused by unclear requirements description in traditional solutions; based on the test intention driven template intelligent assembly mechanism, the real vehicle scene is accurately matched, the compilation pass rate of the generated code is improved, and the pain point of "generated test code needs a lot of manual revision" is solved; the test intention layer can clearly decompose the signal simulation timing and assertion logic, and then generate vehicle-level test code through a special template library, the branch coverage of unit testing is improved, and the driving safety risk is significantly reduced.
[0006] In some optional embodiments, the obtaining the test code comprises: reading a project source code file to be tested, and determining a test function; removing classes, attributes and methods irrelevant to the test function from the project source code file through link analysis, to obtain a simplified file; obtaining external functions called by the test function through link analysis, and adding the external functions to the simplified file; performing abstract syntax tree analysis on the simplified file to obtain a control flow graph of the code in the simplified file; and extracting each conditional path in the control flow graph.
[0007] According to the above technical means, by performing deep simplification and structured analysis on the test code, the efficiency and coverage of test generation are significantly improved. First, irrelevant code is removed through link analysis to focus on the core logic of the test function and reduce invalid data processing burden; second, a control flow graph is generated based on abstract syntax tree, and all conditional paths are exhaustively enumerated through depth-first search to ensure boundary condition coverage; finally, the simplified code and complete path information are input into the large language model to provide accurate context for test intention generation, avoiding AI illusion interference from the source. Not only does it reduce the time-consuming of generating unit test code for vehicle software, but also improves the branch coverage of complex functions, and simultaneously reduces the maintenance cost of traditional methods when the code is changed.
[0008] In some optional embodiments, the inputting the test intent, the test code and the template prompt word into the large language model for analysis to generate a test strategy by the large language model and recommending a code template and a parameter value required for testing from a template library comprises: inputting the test intent corresponding to the current conditional path, the test code and the template prompt word into the large language model, wherein the template prompt word comprises a template framework determined according to a code feature corresponding to the current conditional path; and receiving the test strategy, the code template and the parameter value recommended by the large language model from the template library, wherein the template library manages code templates in a hierarchical classification manner, class names, method names, parameter types and dependency items in the code template are configured by the parameter value, and the template library is configured with code templates having the same function for different template frameworks.
[0009] According to the above technical means, on the one hand, a test intent is generated for each conditional path, and on the other hand, a fuzzy vehicle test requirement is converted into a standardized instruction that can be accurately executed by a machine through a structured four-step prompt word framework (test input / pounding behavior / test call / result verification), thereby significantly eliminating the AI illusion problem caused by requirement ambiguity in a traditional scheme. Specifically, the test input prompt word explicitly defines the parameter boundary, thereby avoiding the model from fabricating illegal values; the pounding behavior prompt word forcibly specifies the vehicle signal simulation logic, thereby ensuring that the dependency item behavior conforms to the real vehicle environment; the test call prompt word locks the core verification function, thereby preventing the model from generating irrelevant calls; and the result verification prompt word restricts the assertion type, thereby enabling the output verification to cover the safety scenario. The technical means significantly improves the accuracy of vehicle unit test code generation, and the code executable rate of complex functions is higher, thereby simultaneously reducing a large amount of manual correction cost.
[0010] In some optional embodiments, the inputting the test intent, the test code and the template prompt word into the large language model for analysis to generate a test strategy by the large language model and recommending a test strategy, a code template and a parameter value required for testing from a template library comprises: inputting the test intent corresponding to the current conditional path, the test code and the template prompt word into the large language model, wherein the template prompt word comprises a template framework determined according to a code feature corresponding to the current conditional path; and receiving the test strategy, the code template and the parameter value recommended by the large language model from the template library, wherein the template library manages code templates in a hierarchical classification manner, class names, method names, parameter types and dependency items in the code template are configured by the parameter value, and the template library is configured with code templates having the same function for different template frameworks.
[0011] According to the above technical means, through the dynamic cooperation mechanism of test intention and code characteristics, precise matching and efficient generation of vehicle unit test templates are realized. First, based on the code characteristics of the current conditional path, the optimal template framework is automatically selected, which improves the template recommendation accuracy. Second, the recommended templates can be dynamically filled through the parameterization configuration layer, solving the template compatibility problem caused by vehicle software version iteration in traditional solutions, and making the subsequent test code generation based on the template more flexible. It is particularly noted that, combined with the intelligent management ability of the hierarchical template library, millisecond-level retrieval and reuse of special templates are realized. The template library is configured with code templates with the same function for different template frameworks, that is, a template of a function can be compatible with different software programming frameworks, and the compatibility of the recommended template for test code generation is also significantly improved.
[0012] In some optional embodiments, the integrating the recommended code templates according to the test strategy and filling the parameter values into the code templates to obtain test code comprises: obtaining the recommended code templates and filling the parameter values into the code templates; combining each code template according to the test strategy, and processing the dependencies and conflicts between templates to obtain a comprehensive template; repairing the template content and template order of the comprehensive template; formatting the repaired comprehensive template to generate test code.
[0013] According to the above technical means, through parameterized filling, the template is accurately adapted to the code characteristics of the current vehicle model, solving the compatibility breakage problem caused by vehicle software version iteration in traditional solutions. Second, based on the test strategy, the templates are intelligently combined and the dependencies and conflicts are automatically resolved, ensuring the success rate of multi-template cooperative test code generation. Finally, through the pre-check rule library, the template content and order are repaired, 90% of the potential defects before compilation are eliminated, and the vehicle-level test code can be compiled and run for the first time, reducing the manual correction cost to near zero.
[0014] In some optional embodiments, before the step of integrating the recommended code template according to the test strategy and filling the parameter values into the code template to obtain test code, the method further comprises: verifying whether the code template selection is appropriate; when the template selection verification passes, verifying whether the parameter value recommendation is correct; when the parameter value correctness verification passes, verifying the test coverage for a special scenario according to the test strategy, the code template and the parameter value; when the test coverage reaches a preset condition, verifying the syntax and reference integrity of the test strategy; when the syntax and reference integrity verification passes, jumping to the step of integrating the recommended code template according to the test strategy and filling the parameter values into the code template to obtain test code; when any one of the above verification steps fails, summarizing the corresponding verification problem; creating a modification prompt word according to the verification problem, and taking the modification prompt word as input of the large language model, returning to the step of inputting the code to be tested and the intention prompt word into the large language model for analysis, and generating test intention through the large language model.
[0015] According to the above technical means, after the large language model provides the corresponding test strategy, code template and parameters, the test code is not directly generated using the code template and parameters, but the reliability of the test strategy, code template and parameters given by the large language model is verified first, and the code generation is performed only after the verification passes. By constructing a progressive four-order verification mechanism (template applicability→parameter accuracy→scenario coverage→syntax integrity), the iterative correction and one-time pass rate problem in vehicle test code generation is solved. First, the adaptability of the template is verified at the template selection layer to avoid compilation failure caused by template mismatch from the source; second, the parameter values are subjected to static type and vehicle regulation semantic verification to eliminate 90% of the vehicle environment simulation distortion risk; third, the control flow backtracking is used to ensure that all conditional paths are covered to ensure the branch test integrity rate of complex vehicle functions; finally, the syntax and reference layer pre-inspects the compatibility of dependent items to block runtime crashes caused by environment configuration errors. The scheme combines a closed-loop feedback mechanism, generates a structured problem description automatically when the verification fails, drives the large language model to accurately correct the test intention, test strategy, recommended test template, etc., so that the first generation pass rate of vehicle unit test code is greatly improved and the correction iteration times are reduced.
[0016] In some optional embodiments, the method further comprises: checking the syntax correctness, type compatibility and code dependency of the test code; compiling the test code when the checking passes; performing a test through the test code when the compilation is successful; obtaining error information when the checking of the test code fails or the compilation is unsuccessful; inputting the test code and the error information into the large language model for analysis to obtain a repair suggestion output by the large language model; repairing the corresponding code template and parameter value according to the repair suggestion, and returning the test code obtained by integrating the recommended code template according to the test strategy and filling the parameter value into the code template.
[0017] According to the above technical means, through the triple closed-loop mechanism of compilation verification, error diagnosis and dynamic repair, the landing execution difficulty of vehicle test code is completely solved. First, before compilation, vehicle-level deep verification is performed, syntax checking intercepts basic errors, type compatibility verification vehicle signal mapping relationship, and dependency analysis ensures the completeness of the vehicle environment, which eliminates the risk of runtime crash from the source. Second, when the verification fails, the intelligent repair engine of the large language model accurately locates the vehicle scene error, and generates a repair scheme based on the vehicle knowledge base to solve the vehicle environment compatibility problem. Finally, through the closed-loop backflow mechanism, the template integration layer guarantees the integrity of the test strategy after repair, which significantly increases the compilation pass rate of the vehicle test code, and further improves the accuracy and reliability of the test code generation.
[0018] In a second aspect, the present application provides a vehicle unit test code generation device, the device comprising: an input module for obtaining a test code; a large model first analysis module for inputting the test code and an intention prompt word into a large language model for analysis, and generating a test intention through the large language model, the test intention being used to indicate a process required for testing the test code; a large model second analysis module for inputting the test intention, the test code and a template prompt word into a large language model for analysis, to generate a test strategy through the large language model, and recommend a code template and parameter value required for testing from a template library; and a test code generation module for integrating the recommended code template according to the test strategy, and filling the parameter value into the code template to obtain a test code.
[0019] In a third aspect, the present application provides a computer device, comprising: a memory and a processor, which are communicatively connected with each other, and the memory stores computer instructions; the processor executes the computer instructions to perform the method of the first aspect or any of the corresponding embodiments thereof.
[0020] In a fourth aspect, the present application provides a computer readable storage medium having stored thereon computer instructions for causing a computer to execute the method of the first aspect or any of the corresponding embodiments thereof.
[0021] In a fifth aspect, the present application provides a computer program product comprising computer instructions for causing a computer to execute the method of the first aspect or any of the corresponding embodiments thereof.
[0022] The technical solution provided by the present application has the following advantages:
[0023] (1) According to the above technical means, by introducing the test intention generation engine, the problem of high error rate of large language model generation caused by user input demand ambiguity is effectively solved, and the accuracy and usability of the test code are significantly improved. By associating the original test code with the structured test intention generated by the large language model, the ambiguous test requirements are converted into machine-executable standardized instructions, avoiding the AI illusion problem caused by unclear requirements description in traditional solutions; based on the template intelligent assembly mechanism driven by the test intention, the real vehicle scene is accurately matched, the compilation pass rate of the generated code is improved, and the pain point of "generated test code needs a lot of manual revision" is solved; the test intention layer can clearly disassemble the signal simulation timing and assertion logic, and then generate vehicle-level test code through a special template library, the branch coverage of unit testing is improved, and the driving safety risk is significantly reduced.
[0024] (2) According to the above technical means, by performing deep simplification and structured analysis on the test code, the efficiency and coverage of test generation are significantly improved. First, irrelevant code is removed by calling link analysis, focusing on the core logic of the test function, and reducing the burden of invalid data processing; second, based on the abstract syntax tree, a control flow graph is generated, and all conditional paths are exhaustively enumerated by combining depth-first search, ensuring boundary condition coverage; finally, the simplified code and complete path information are input into the large language model to provide accurate context for test intention generation, avoiding AI illusion interference from the source. Not only does it reduce the time-consuming of generating unit test code for vehicle software, but it also improves the branch coverage of complex functions and reduces the maintenance cost of traditional methods when the code changes.
[0025] (3) According to the above technical means, on the one hand, a test intention is generated for each condition path, and on the other hand, a structured four-step prompt word framework (test input / piling behavior / measured call / result verification) is used to convert vague vehicle test requirements into standardized instructions that can be accurately executed by machines, thereby significantly eliminating the AI illusion problem caused by requirement ambiguity in traditional solutions. Specifically, the test input prompt word explicitly specifies the parameter boundary, avoiding the model from fabricating illegal values; the piling behavior prompt word specifies the vehicle signal simulation logic, ensuring that the dependent item behavior conforms to the real vehicle environment; the measured call prompt word locks the core verification function, preventing the model from generating irrelevant calls; and the result verification prompt word constrains the assertion type, making the output verification cover safe scenarios. This technical means significantly improves the accuracy of vehicle unit test code generation, increases the executable rate of complex function code, and reduces a large amount of manual correction costs.
[0026] (4) According to the above technical means, through the dynamic coordination mechanism of test intention and code characteristics, precise matching and efficient generation of vehicle unit test templates are achieved. First, the optimal template framework is automatically selected based on the code characteristics of the current condition path, improving the accuracy of template recommendation; second, the recommended templates can be dynamically filled through the parameterization configuration layer, solving the template compatibility problem caused by vehicle software version iteration in traditional solutions, and making the subsequent test code generation based on the template more flexible. It is particularly noted that, combined with the intelligent management capability of the hierarchical template library, millisecond-level retrieval and reuse of special templates are achieved. The template library also configures code templates with the same function for different template frameworks, i.e., a template for a function can be compatible with different software programming frameworks, which significantly improves the compatibility of recommended templates for test code generation.
[0027] (5) According to the above technical means, parameterized filling ensures that the template accurately adapts to the code characteristics of the current vehicle model, solving the compatibility breakage problem caused by vehicle software version iteration in traditional solutions; second, based on the intelligent combination of test strategies, the template is automatically dissolved to ensure the success rate of multi-template collaborative test code generation; finally, through the pre-check rule library, the template content and order are repaired, eliminating 90% of potential defects before compilation, making the vehicle-level test code first-generation compilable and executable, and reducing the manual correction cost to near zero.
[0028] (6) According to the above technical means, after the large language model provides the corresponding test strategy, code template and parameters, the test code is not directly generated by using the code template and parameters, but the reliability of the test strategy, code template and parameters given by the large language model is verified first, and the code is generated only after the verification is passed. By constructing a progressive four-order verification mechanism (template applicability-> parameter accuracy-> scene coverage-> syntax integrity), the iterative correction and one-time pass rate problem in vehicle test code generation is solved. First, the adaptability of the template is verified at the template selection layer to avoid compilation failure caused by template mismatch from the source; secondly, the static type and vehicle regulation semantic verification is performed on the parameter value to eliminate 90% of the vehicle environment simulation distortion risk; thirdly, the control flow backtracking is used to ensure that all conditional paths are covered to ensure the branch test integrity rate of complex vehicle functions; finally, the syntax and reference layer is pre-checked for compatibility to block runtime crashes caused by environment configuration errors. The scheme combines a closed-loop feedback mechanism, and when the verification fails, a structured problem description is automatically generated to drive the large language model to accurately correct the test intent, test strategy, recommended test template, etc., so that the first generation pass rate of vehicle unit test code is greatly improved, and the correction iteration times are reduced.
[0029] (7) According to the above technical means, through the triple closed-loop mechanism of compilation verification, error diagnosis and dynamic repair, the landing execution problem of vehicle test code is completely solved. First, the vehicle regulation level deep verification is performed before the test code is compiled, the basic error is intercepted by syntax checking, the vehicle signal mapping relationship is verified by type compatibility verification, and the vehicle environment is ensured to be complete by dependency analysis, so as to eliminate the runtime crash risk from the source; secondly, when the verification fails, the large language model intelligent repair engine is used to accurately locate the vehicle regulation scene error, and a repair scheme is generated based on the vehicle knowledge base to solve the vehicle environment compatibility problem; finally, through the closed-loop backflow mechanism, the test strategy integrity is ensured after the template integration layer is returned after repair, which makes the compilation pass rate of the vehicle test code significantly increase, and further improves the accuracy and reliability of the test code generation. BRIEF DESCRIPTION OF DRAWINGS
[0030] In order to more clearly illustrate the specific embodiments of the present application or the technical solutions in the prior art, the drawings needed in the following specific embodiments or prior art description will be briefly introduced. Obviously, the drawings in the following description are some embodiments of the present application, and those skilled in the art can also obtain other drawings according to these drawings without creative labor.
[0031] Figure 1 is a flowchart of a vehicle unit test code generation method according to an embodiment of the present application;
[0032] Figure 2is an effect schematic diagram of control flow graph and conditional path extraction according to an embodiment of the application;
[0033] Figure 3 is an effect schematic diagram of inputting large language model intention prompt words according to an embodiment of the application;
[0034] Figure 4 is an effect schematic diagram of template hierarchical classification storage according to an embodiment of the application;
[0035] Figure 5 is an effect schematic diagram of Mockito framework template instantiation according to an embodiment of the application;
[0036] Figure 6 is an effect schematic diagram of inputting large language model template prompt words and outputting recommended templates according to an embodiment of the application;
[0037] Figure 7 is a flow schematic diagram of rule check verification of a test generation layer according to an embodiment of the application;
[0038] Figure 8 is another flow schematic diagram of a vehicle unit test code generation method according to an embodiment of the application;
[0039] Figure 9 is a flow schematic diagram of an initialization stage of a vehicle unit test code generation method according to an embodiment of the application;
[0040] Figure 10 is a flow schematic diagram of a code analysis stage of a vehicle unit test code generation method according to an embodiment of the application;
[0041] Figure 11 is a flow schematic diagram of a verification and analysis stage of a vehicle unit test code generation method according to an embodiment of the application;
[0042] Figure 12 is a flow schematic diagram of a test generation stage of a vehicle unit test code generation method according to an embodiment of the application;
[0043] Figure 13 is an effect schematic diagram of template parameter filling according to an embodiment of the application;
[0044] Figure 14 is a flow schematic diagram of a verification and feedback stage of a vehicle unit test code generation method according to an embodiment of the application;
[0045] Figure 15 is a structural schematic diagram of a vehicle unit test code generation device according to an embodiment of the application;
[0046] Figure 16 Fig. 1 is a schematic diagram of a hardware structure of a computer device according to an embodiment of the present application. DETAILED DESCRIPTION
[0047] In order to make the objects, technical solutions and advantages of the embodiments of the present application clearer, the technical solutions in the embodiments of the present application will be described clearly and completely below with reference to the drawings in the embodiments of the present application. Obviously, the described embodiments are some but not all of the embodiments of the present application. Based on the embodiments in the present application, all other embodiments obtained by those skilled in the art without creative work fall within the scope of protection of the present application.
[0048] According to the embodiments of the present application, a vehicle unit test code generation method is provided. It should be noted that the steps shown in the flowchart of the drawings can be executed in a computer system such as a set of computer executable instructions, and although the logical order is shown in the flowchart, in some cases, the steps shown or described can be executed in an order different from that here.
[0049] In the present embodiment, a vehicle unit test code generation method is provided, Figure 1 Fig. 1 is a flowchart of a vehicle unit test code generation method according to an embodiment of the present application, which includes the following steps:
[0050] Step S101, obtaining the code to be tested.
[0051] Specifically, the present embodiment provides a vehicle unit test code generation method, mainly including software processing components such as input layer, large model layer, test generation layer, so as to automatically generate test code according to user demand through different processing of different levels. In the present embodiment, the first step of test code generation is to obtain the code to be tested through the input layer. The function of the input layer is to process user input, read the code to be tested and extract test target information, and integrate input information. The code to be tested refers to a code unit in the vehicle software that needs to be tested.
[0052] Step S102, inputting the code to be tested and the intention prompt word into the large language model for analysis, and generating a test intention through the large language model, the test intention being used to indicate the process required for testing the code to be tested.
[0053] Specifically, the embodiment of the present application determines two main factors causing the AI hallucination problem through analysis. One is that the user's test requirement description is not accurate, that is, the user needs to test the code in what way, and it is difficult for users with weak expression ability to clearly describe it in words. The second is that the large language model often does not know what the test framework, available test functions or complex test logic of the user under a certain programming language are, that is, even if the large language model is explicitly informed of the user's test requirements, the large language model may still make up "blindly", resulting in that the test code output by the large language model cannot match the real requirements of the vehicle software (such as syntax requirements, type requirements), so as to be difficult to ensure the accuracy of the test code.
[0054] The embodiment of the present application overcomes the problem of inaccurate test code caused by AI hallucination through two large model engines of test intention generation and test template configuration. After obtaining the code to be tested, the embodiment of the present application edits intention prompt words, and inputs the code to be tested and the intention prompt words into the large language model for analysis. The intention prompt words are used to inform the large language model to analyze the code to be tested as a whole, and the user wants to test the reliability of the code to be tested, which aspects of the code to be tested should be tested, or how to test, and what are the steps of the test? The intention prompt words serve as questions, and the large language model helps the user to answer how to test the code to be tested, so as to finally generate a test intention through the large language model. The test intention as an answer is the process that the large language model thinks is needed when testing the code to be tested. The "test intention" serves as the "test requirement", and based on the rich analysis ability and the standard expression ability of the large language model, the problem of unclear input test requirement of the user is overcome.
[0055] In step S103, the test intention, the code to be tested and the template prompt words are input into the large language model for analysis, so as to generate a test strategy through the large language model, and recommend the code template and parameter value required for the test from the template library.
[0056] Specifically, after obtaining the test intention, the test template configuration engine of the large language model is continued to be told by using the test intention, the large language model is caused to analyze the test intention, then it is caused to think what test templates are needed to complete the test, and then suitable code templates are screened out from a large number of code templates, and suitable test parameter values are recommended. In the embodiment, the code template refers to a semi-finished template for writing test code, and the code template often has a code function for implementing a specific function and is written according to some specific syntax, especially for very complex test logic, and the user can reuse it infinitely once. The parameter value refers to an adaptive configuration parameter value that needs to be filled into the code template according to different test requirements and different test scenarios of the user, such as a certain signal value, a simulation value of a certain sensor, and the like. Therefore, the template prompt word also needs to be input to the large language model in this step, aiming to inform the large language model of the specific location of the template library, which templates in the template library can be selected, and which templates cannot be selected, so as to further improve the accuracy of the large language model in recommending templates and parameter values. Finally, the result output by the large language model includes a test strategy, a recommended code template required for testing, and a parameter value, wherein the test strategy refers to informing the test generation layer how to sort the recommended templates, how to combine the templates, and how to fill the parameter value into the corresponding template (for example, how to fill and where to fill).
[0057] In step S104, the recommended code templates are integrated according to the test strategy, and the parameter values are filled into the code templates to obtain test code.
[0058] Specifically, through the processing of the above two processes, the test generation layer of the embodiment integrates the recommended code templates according to the predetermined test strategy, fills the parameter values into the code templates, and finally automatically generates test code through formatting. The reliability of the tested code can be detected by executing the test code.
[0059] According to the above technical means, by introducing the test intention generation engine, the problem of high error rate of large language model generation caused by user input ambiguity is effectively solved, and the accuracy and usability of the test code are significantly improved. By associating the original test code with the structured test intention generated by the large language model, the ambiguous test requirements are converted into standardized instructions that can be accurately executed by machines, thereby avoiding the AI hallucination problem caused by unclear requirements in traditional solutions. In addition, based on the test intention driven template intelligent assembly mechanism, the real vehicle scene is accurately matched, the compilation pass rate of the generated code is improved, and the pain point of "generated test code needs a lot of manual revision" is solved; The test intention layer can clearly decompose the signal simulation timing and assertion logic, and then generate vehicle-level test code through a special template library. The complex logic about testing in the template can be written once and reused infinitely, the branch coverage rate of unit testing is improved, and the driving safety risk is significantly reduced. Because the test code is automatically generated based on the template written by the user in advance, it can ensure that the automatically generated test code strictly conforms to the programming standards of vehicle software in framework, type and syntax, avoiding the problems of private methods and private variables (such as methods and variables that cannot be called externally inside some functions, test code can only call the public code interface provided uniformly, and cannot use these private methods and variables) and "blind composition", and improving the reliability of the test code.
[0060] In some optional embodiments, the step S101 comprises:
[0061] Step a1, reading the project source code file to be tested, and determining the function to be tested;
[0062] Step a2, deleting classes, attributes and methods irrelevant to the function to be tested from the project source code file by calling link analysis, to obtain a simplified file;
[0063] Step a3, obtaining external functions called by the function to be tested by calling link analysis, and adding the external functions to the simplified file;
[0064] Step a4, performing abstract syntax tree analysis on the simplified file to obtain a control flow graph of the code in the simplified file;
[0065] Step a5, extracting each conditional path in the control flow graph.
[0066] Specifically, the vehicle software has more than one billion lines of code, and manual selection of test targets is like "finding a needle in a haystack". In traditional solutions, redundant code (such as debugging logs and unused variables) will interfere with the analysis of the large language model, resulting in "AI hallucination" (such as fabricating non-existent signal inputs) in the generated test intention.
[0067] The input layer of the embodiment of the present application first reads the project source code file provided by the user through a source code reading module, receives the dependent object of the current project and the measured function to be tested. Through a test target extraction module, call link analysis is performed to remove classes, attributes and methods in the current source code file that are not related to the current measured function, to obtain a simplified test file, so as to locate the measured unit (i.e. the smallest testable code block) in the vehicle software. Among them, the call link analysis refers to constructing a function dependency graph through static code analysis technology, and identifying classes / attributes / methods (such as log tool classes and non-critical UI components) that are not directly associated with the measured function.
[0068] Since the function implementation of the measured function often needs to call some external functions, the external functions are interfaces (such as hardware drivers and communication protocols) that are dependent on the measured function but not implemented in the file. In order to ensure that the test environment can completely simulate the vehicle running scene, it is necessary to obtain the external functions called by the measured function through link analysis, and add the external functions to the simplified file.
[0069] Then, the embodiment of the present application requests the rule engine layer (a software component for rule verification deployed in the embodiment) to generate an abstract syntax tree, parses the code into a tree structure (dissolves the code into functions, loops, variables, etc. and displays the hierarchical structure) through the abstract syntax tree (AST, Abstract Syntax Tree), then performs DFS (Depth-First Search, Depth-First Search) analysis through the abstract syntax tree to generate a control flow graph (CFG, Control Flow Graph), each node in the control flow graph is a branch condition, such as a Switch statement, an IF statement, an Exception statement, etc. Each condition path of the measured function is extracted by traversing each condition, and each condition path is a conditional branch to be tested. For example Figure 2 As shown, the source code is disassembled into a control flow graph including three condition paths, and the middle condition path in the control flow graph is exemplified as the lowermost condition path in Figure 2 Finally, all the collected information is transmitted to the large model layer for intent analysis and template analysis.
[0070] According to the technical scheme provided by the embodiment of the application, irrelevant code is removed through link analysis, the measured function is focused on the core logic, and the invalid data processing burden is reduced. Secondly, a control flow graph is generated based on an abstract syntax tree, and all conditional paths are enumerated through deep-first search traversal, so that boundary condition coverage is ensured, auxiliary functions are provided for test intent understanding of the large language model, the context understanding of the large language model is enhanced, the working scene and working process of the large language model are fully allowed, the understanding difficulty of the large language model for the measured code is reduced, and the accuracy of subsequent test intent generation is improved. Finally, the simplified code and complete path information are input into the large language model to provide accurate context for test intent generation, and the interference of AI illusion is avoided from the source. Not only the code generation time of the unit test of the vehicle software is reduced, but also the branch coverage of complex functions is improved, and the maintenance cost of the traditional method in code change is reduced.
[0071] In some optional embodiments, the step S102 includes:
[0072] In step b1, the measured code, the context code of the measured code, the dependent function of the measured code, the current conditional path and the intent prompt word are input into the large language model, wherein the intent prompt word includes a test input prompt word, a stubbing behavior prompt word, a measured call prompt word and a result verification prompt word. The test input prompt word is used to inform the large language model of the parameters required for the test code input, the stubbing behavior prompt word is used to inform the large language model of the real vehicle signal that needs to be simulated feedback, the measured call prompt word is used to inform the large language model of the dependent function that needs to be called, and the result verification prompt word is used to inform the large language model of the output result that needs to be asserted.
[0073] In step b2, the test intent output by the large language model for the current conditional path is received.
[0074] Specifically, vehicle software test engineers often fall into such a dilemma when trying to generate test code. Even if the complete measured code and test requirements are submitted to the large language model, the test code obtained still deviates from the actual requirements. For example, in one generation, the model fabricated a "tire pressure monitoring interface call" that did not exist at all, and in another generation, it confused the cause-and-effect relationship between the steering wheel angle signal and the brake pressure, and designed a dangerous use case. This kind of demand understanding misalignment is like driving in fog, and the root cause lies in two blind spots: first, engineers have difficulty in accurately describing the complex coupling relationship between vehicle signals (such as the dynamic mapping of yaw angular velocity and road adhesion coefficient); second, the model lacks knowledge of the vehicle regulation test framework, and often misuses private methods or ignores safety assertion standards. Therefore, the test intent that can represent the test requirements is crucial, and how to make the large language model generate very accurate test intent for the user is more important.
[0075] To pierce this layer of demand fog, embodiments of the invention construct a four-step navigation-style prompting system, constructing structured prompting words. Call large language model base services, according to the analysis results of the conditional path, formulate test intent for each conditional path. For example Figure 3 As shown, the content of the large language model is input in the test intent generation stage. The test input prompt word, the pile behavior prompt word (Mock process), the call prompt word and the result verification prompt word are constructed to generate the test intent prompt word. Each conditional path corresponds to different writing schemes and generation logic. The test intent is the textual description of each step, and the operation required in each step. By abstracting the test intent, the model can adapt to different test scenarios and test frameworks in the generation process.
[0076] For example: Suppose in an actual test scenario, the parameter boundary is delimited by the test input prompt word: inject yaw rate value range 0~50 degrees / second, road adhesion coefficient 0.1~0.9; the signal logic is anchored by the pile behavior prompt word: when the adhesion coefficient is <0.3, the simulated torque sensor returns resistance torque >=150Nm; the core function interface is locked by the call prompt word: the StabilizationControl() method of the control module must be called; the test result verification method is specified by the result verification prompt word: the brake pressure must rise to >=12MPa within 100ms and the yaw angle change rate <=5 degrees / second to pass the verification. This set of instructions is like turning the vague "go north" into the precise "arrive at the destination along the G65 highway through the 34°N, 108°E coordinate point". The test intent output by the model naturally eliminates fictional dependencies and strictly follows the vehicle control logic.
[0077] According to the above technical means, on the one hand, a test intention is generated for each condition path, and on the other hand, a structured four-step prompt word framework (test input / piling behavior / invoked call / result verification) is used to convert the ambiguous vehicle test requirements into standardized instructions that can be accurately executed by machines, thereby significantly eliminating the AI hallucination problem caused by requirement ambiguity in traditional solutions. Specifically, the test input prompt word explicitly specifies the parameter boundary, avoiding the model from fabricating illegal values; the piling behavior prompt word specifies the vehicle signal simulation logic, ensuring that the dependent item behavior conforms to the real vehicle environment; the invoked call prompt word locks the core verification function, preventing the model from generating irrelevant calls; and the result verification prompt word restricts the assertion type, making the output verification cover safe scenarios. This technical means significantly improves the accuracy of vehicle unit test code generation, increases the executable rate of complex function code, and reduces the cost of manual correction. This mechanism eliminates the problem of inaccurate test requirements caused by ambiguous requirements in unit testing, significantly reducing the time required for manual correction of test cases. By using four-step prompt words to translate vehicle engineering experience into machine language executable by AI, the large language model can truly understand the user's test requirements. By accurately mapping each condition path, vehicle software testing has officially left the "blind man touching an elephant" era.
[0078] In some optional embodiments, the step S103 comprises:
[0079] Step c1, inputting the test intention corresponding to the current condition path, the code to be tested, and the template prompt word into the large language model, wherein the template prompt word comprises a template framework determined according to the code characteristics corresponding to the current condition path;
[0080] Step c2, receiving the test strategy, code template, and parameter value recommended by the large language model from the template library, wherein the template library manages the code templates in a hierarchical manner, the class name, method name, parameter type, and dependent item in the code template are configured by the parameter value, and the template library is configured with code templates having the same function for different template frameworks.
[0081] Specifically, in the field of vehicle software testing, the test code generation method based on large language model often simply relies on the experience of large language model, resulting in frequent "blind compilation" and other AI illusion problems of test code. In the scenario of obtaining test code by configuring test templates through rules, the traditional template matching scheme often falls into the framework lock-in dilemma. For example, when an engineer generates test code for a vehicle air conditioning control function, if the JUnit framework template is initially selected, the subsequent switch to pytest requires complete rewriting of the test cases, resulting in a cross-platform reuse rate of less than 30%. More seriously, vehicle software version iteration (such as upgrading vehicle Android from version 11 to version 12) often causes template compatibility to break down. The root cause lies in the fact that the template selection and parameter filling systems operate in isolation, the template library passively responds to manual instructions, and cannot perceive the dynamic changes in code characteristics; parameter configuration is fixed in a specific framework, and cross-language migration ability is lost.
[0082] The embodiment of the application breaks this deadlock through a test intent and code feature dual driving mechanism. First, the template library is configured with code templates with the same function for different template frameworks by inputting template prompt words to determine the template framework according to the code characteristics of the current condition path. Taking the "high temperature protection mode" condition path (forced start of refrigeration when the temperature inside the vehicle is greater than 40°C) in the air conditioning control function as an example: the system first analyzes the code characteristics to identify the optimal template framework (for example, select the template in the JAVA environment, rather than the C++, Python, etc. software framework). When deployed on an Android vehicle platform, the Robolectric component test template is automatically matched; if running on a Linux chassis control system, the hardware simulation template of the Gotest framework is switched to, which can accurately solve the compatibility problem of test code in different test environments.
[0083] Second, the class name, method name, parameter type, and dependencies in the code template are configured through parameter values, and the key breakthrough lies in the bridging role of the parameterized configuration layer. The class name AirConditioning, the method name setTemperature, the temperature threshold parameter highTempThreshold = 40, and other elements are all abstracted as independent variables, which are injected into the large language model through template prompt words. The model accordingly calls functionally equivalent templates from the hierarchically managed template library - for example, both Android and Linux versions contain "high temperature protection verification" logic, but they are encapsulated as different implementations of RobolectricTestRunner and GTestEnvironment, respectively.
[0084] Third, the template library manages code templates in a hierarchical and classified manner, with multi-level template combination, supporting multi-level combination of basic templates, scenario templates, and special processing templates, thereby improving the flexibility of test code generation. For example Figure 4As shown, the code templates are classified in multiple dimensions according to test type (unit test, integration test), test framework, and application scenario (regular method, asynchronous operation, UI component, etc.), test step (initialize template, Mock template, Verify template), test level (function template, class template, variable template), etc. For example, the template types mainly include the following 5 types:
[0085] ① Foundation test templates provide general test framework initialization support, including: test class / file structure templates (such as JUnit, pytest, Gotest, etc.), basic structure of test methods (assertions, Setup / Teardown, etc.), and test framework adaptation for different languages (such as JUnit5, pytest, Go's testing package, etc.). ② Mock templates support Mock tools in multiple languages, providing simulation implementations of dependencies, including: Java: Mockito, PowerMock, EasyMock, Robolectric, Go: TestifyMock, GoMock, MonkeyPatching (such as bou.ke / monkey), Python: unittest.mock, pytest-mock, Kotlin: MockK, etc. Cross-language support for interface simulation, dependency injection isolation, and other general techniques. ③ Component / framework test templates are designed for different language ecosystems, providing test templates for core components, including: Android: Activity, Fragment, Service, and other component lifecycle and interaction tests, Jetpack component (such as ViewModel, Room) test adaptation, Java: Spring (Controller, Service, Repository), MyBatis, Hibernate ORM operations, Go: HTTP service (Gin, Echo), database (GORM, SQLx), microservices (gRPC, gRPC-Gateway), Python: Flask, FastAPI web service, SQLAlchemy ORM test, asynchronous framework (FastAPI, Quart) and others. ④ Asynchronous operation test templates are used to cover asynchronous programming test scenarios in multiple languages, including: coroutines / goroutines: Go language's sync.WaitGroup, context.Context, Reactive programming: RxJava (Java / Kotlin), RxPy (Python), thread / asynchronous task Thread, Executor (Java), asyncio (Python) templates, and test tool support Go: testing.T.Parallel(), Java: Awaitility, Python: pytest-asyncio.(5) Network request test templates are used to provide test solutions for different language network libraries, including: Java / Kotlin: Retrofit, OkHttp (MockWebServer), Go: HTTP client (net / http), gRPCMock, Python: Requests, aiohttp (asynchronous HTTP), and other templates.
[0086] Taking the Mockito test framework in Java code as an example, the user selects the current Mock framework as Mockito, and the assertion framework uses Junit. Based on the current test intent, the corresponding template is selected from the module library corresponding to Mockito and Junit, and the specific parameter value is filled in. For example Figure 5 is an example of a template included in the Mockito framework, including instance method spy templates, instance method stubbing templates, basic verification templates, and reverse injection templates. For example Figure 6 is the user inputting the test intent, the code to be tested, and the template prompt word into the large language model in the left area. The right area is the code template recommended by the large language model after analysis, which has been filled with parameter values.
[0087] According to the above technical means, through the dynamic cooperation mechanism of test intent and code characteristics, precise matching and efficient generation of vehicle unit test templates are realized. First, the optimal template framework is automatically selected based on the code characteristics of the current condition path, which improves the accuracy of template recommendation. Second, the recommended templates can be dynamically filled with templates through the parameterization configuration layer, solving the template compatibility problem caused by vehicle software version iteration in traditional solutions, and making the subsequent test code generation based on templates more flexible. It is particularly noted that, in combination with the intelligent management capability of the hierarchical template library, millisecond-level retrieval and reuse of special templates are realized. The template library is configured with code templates with the same function for different template frameworks, that is, a template of a function can be compatible with different software programming frameworks, which significantly improves the compatibility of the recommended templates for test code generation.
[0088] In some optional embodiments, the above step S104 comprises:
[0089] Step d1, obtaining the recommended code template and filling the parameter value into the code template;
[0090] Step d2, combining each code template according to the test strategy and processing the dependencies and conflicts between templates to obtain a comprehensive template;
[0091] Step d3, repairing the template content and template order of the comprehensive template;
[0092] Step d4, formatting the repaired comprehensive template to generate test code.
[0093] Specifically, the test generation layer receives the code templates, parameter values and test strategies recommended by the large language model through testing. The test generation layer acquires the selected test template code from the template library according to the recommendation, fills in the template parameters, including class name, method name, test data, and the like. Then, according to the guidance of the test strategy, multiple template fragments are combined in order, the dependency relationship between them is processed, and possible conflicts between templates are solved, such as repeated import, naming conflict and the like. Finally, based on common errors of unit testing, the template content and the template order are repaired, the generated test code is formatted, and the code style is ensured to be consistent.
[0094] For example, in the traditional scheme, implicit conflicts between templates occur frequently. The signal simulation template may return a floating-point temperature value of 25.5℃, while the temperature control algorithm template expects an integer input of 26℃. The type mismatch causes the compilation to fail. In the Android car test scene, when the UI thread test template coexists with the CAN bus communication template, a deadlock is caused due to the conflict of thread scheduling order. Some templates may incorrectly call private methods (such as directly accessing privateCalibrate() instead of the public interface), which violates the vehicle software safety architecture. The above problems are like the misalignment of the gears of a precision instrument, which seriously hinders the usability of the test code.
[0095] The embodiments of the present application realize the lossless assembly of templates. For example, parameterized accurate filling, automatic identification of type constraints of target templates, and elimination of type mismatch risk by rounding off parameters; for example, replacing the private method call privateCalibrate() with the public interface publicCalibrate(). The key technical breakthrough lies in the intervention of the pre-repair engine, which detects vehicle-grade errors through static scanning and automatically corrects the templates.
[0096] According to the above technical means, parameterized filling is used to ensure that the templates accurately adapt to the code characteristics of the current vehicle model, solving the compatibility breakage problem caused by vehicle software version iteration in the traditional scheme. Secondly, based on the test strategy, the templates are intelligently combined and the dependency conflicts are automatically eliminated, ensuring the success rate of multi-template collaborative test code generation.
[0097] In addition, in the embodiments of the present application, the test generation layer stores an experience-based error handling mechanism, for example Figure 7As shown, by collecting some common errors in actual application development, a rule library for error identification and error repair is built, and the output template is verified and repaired to a certain extent through predefined rules. For example, some private methods and private variable calls that the large language model may have (such as a fictional Set method that does not actually exist), incorrect template selection (such as a Mock template for a static function for a normal function, an Android component selects an incorrect Mock template, and Mock refers to a stubbing behavior), and incorrect class references. Through the pre-check rule library, the template content and order are repaired, 90% of the potential defects before compilation are eliminated, the vehicle-level test code can be compiled and run for the first time, the manual correction cost tends to zero, and the reliability of the generated test code is further improved.
[0098] In some optional embodiments, before step S104, the method for generating vehicle unit test code provided by the embodiment of the application further includes:
[0099] Step e1, verifying whether the code template selection is appropriate;
[0100] Step e2, when the template selection verification is passed, verifying whether the parameter value recommendation is correct;
[0101] Step e3, when the parameter value correctness verification is passed, verifying the test coverage for special scenarios according to the test strategy, the code template, and the parameter value;
[0102] Step e4, when the test coverage reaches the preset condition, verifying the syntax and reference integrity of the test strategy;
[0103] Step e5, when the syntax and reference integrity verification is passed, jumping to the step of integrating the recommended code template according to the test strategy and filling the parameter value into the code template to obtain the test code;
[0104] Step e6, when any one of the above verification steps fails, summarizing the corresponding verification problem;
[0105] Step e7, creating a modification prompt word according to the verification problem, and taking the modification prompt word as input of the large language model, returning to the step of inputting the code to be tested and the intention prompt word into the large language model for analysis, and generating the test intention through the large language model.
[0106] Specifically, there is a significant risk of error accumulation in the vehicle software test code generation process. For example, when a large language model generates test code for an electronic parking system, multiple hidden dangers may arise due to the lack of systematic checking: for example, selecting a normal I / O template instead of a waterproof vehicle scale template for a slope sensor test, resulting in low-temperature test failure; or recommending an 80MPa brake pressure value (20MPa above the safety threshold), causing a hardware protective shutdown; more likely to miss the "ice and snow road + steep slope start-stop" key scene. These problems are like a domino effect, where early minor errors will trigger a chain of failures later on. If there is no checking, the test code generated directly according to the output of the large language model may have low reliability.
[0107] The present scheme builds an error-proof system through a four-stage progressive verification driven by a rule engine, and performs safety checking before generating test code. Taking the EPB system test as an example, template adaptability verification is performed: the rule library scans the template attributes, intercepts the normal I / O template and forcibly replaces it with the waterproof vehicle scale template; parameter value range strong checking is performed: based on vehicle signal rules (such as brake pressure value range 0-20MPa), 80MPa out-of-range values are detected, triggering an alarm; scene coverage audit is performed: whether the "ice + 30% slope" working condition is covered is checked through path backtracking, and is automatically supplemented when missing; syntax dependency penetration testing is performed: verify that the key dependencies such as CAN bus library are completely mounted. When any of the above verification links fails, the corresponding verification problem is summarized, and a modification prompt word is created according to the verification problem, and the modification prompt word is also input to the large language model as an input, and returns to step S102, and the large language model outputs the test intent again through closed-loop feedback driving.
[0108] In addition, it should be noted that the embodiment of the present application adopts an incremental repair strategy, and the repair process only modifies the problem part instead of re-generating, further accelerating the iteration efficiency of the test code.
[0109] The embodiment of the present application brings three technical values: 1. Safety barrier: vehicle regulation rule library intercepts out-of-range parameters and error templates, eliminating the risk of executor overload; 2. Coverage guarantee: path backtracking ensures 100% coverage of extreme working conditions; 3. Efficiency leap: closed-loop correction compresses 80% of manual debugging iterations. Just like the online quality inspection system in precision manufacturing, the four verification gates build a full-process quality defense line for vehicle software testing, further improving the reliability of the test code.
[0110] According to the above technical means, after the large language model provides the corresponding test strategy, code template and parameters, the test code is not directly generated by using the code template and parameters, but the reliability of the test strategy, code template and parameters given by the large language model is verified first, and the code is generated only after the verification is passed. By constructing a progressive four-order verification mechanism (template applicability→parameter accuracy→scene coverage→syntax integrity), the iterative correction and one-time pass rate problem in vehicle test code generation is solved. The scheme combines a closed-loop feedback mechanism, automatically generates a structured problem description when the verification fails, and drives the large language model to accurately correct the test intent, test strategy, recommended test template, etc., so that the first generation pass rate of the vehicle unit test code is greatly improved, and the number of correction iterations is reduced.
[0111] In some optional embodiments, the method further comprises:
[0112] Step f1, checking the syntax correctness, type compatibility and code dependency of the test code;
[0113] Step f2, when the checking passes, compiling the test code;
[0114] Step f3, when the compilation is successful, executing the test through the test code;
[0115] Step f4, when the test code fails to pass the check or the compilation is unsuccessful, obtaining error information;
[0116] Step f5, inputting the test code and the error information into the large language model for analysis to obtain repair suggestions output by the large language model;
[0117] Step f6, repairing the corresponding code template and parameter value according to the repair suggestions, and returning the step of integrating the recommended code template according to the test strategy and filling the parameter value into the code template to obtain the test code.
[0118] Specifically, in the final landing link of the vehicle software test code, the traditional scheme often causes systematic failure due to insufficient cross-platform compatibility. Taking the test code generation of the electronic parking control module as an example, when the automatically generated test code is transplanted from the development environment to the target vehicle platform, type conversion exceptions or dependency losses frequently occur, resulting in nearly half of the test cases being unable to execute. This fault in dynamic environment adaptation makes the test strategy designed in the early stage fail in the final compilation stage, so it is necessary to perform relevant formal checking after code generation.
[0119] To solve this problem, the present application constructs an intelligent self-healing type of compilation verification closed loop. The core of the present application is to seamlessly embed a five-step verification process into the code generation pipeline. First, a static scanning engine is used to filter the test code at the vehicle regulation level. Syntax level checks are used to check for basic errors such as missing semicolons, type system verification is used to verify compatibility, such as whether the brake pressure value is within a safe range, and dependency chain analysis is used to ensure that key components such as CAN bus drivers are properly mounted. If the compilation fails, the compilation verifier returns the compilation error information, the test generation layer requests error analysis from the rule engine, and the rule engine requests repair suggestions from the large language model. The large language model returns the repair suggestions, which are passed to the rule engine, which then passes the corrected templates or parameters to the test generation layer, which applies the repairs and regenerates the code, and the test generation layer re-submits the code to the compilation verifier.
[0120] For example, when a type compatibility error is detected for the target platform (e.g., the floating-point pressure value 25.5 is not converted to an integer), the rule engine generates a machine-readable diagnostic report. This report is input into the large language model along with the original code, and the model outputs precise repair suggestions based on a vehicle engineering knowledge base. Finally, the test generation layer performs incremental template updates, modifying only the local code of the pressure handling module, ensuring that the verified functionality is not disturbed. This dynamic feedback mechanism enables the vehicle test code to continue running even when it encounters environmental mutations, as if it were equipped with an adaptive shock absorber.
[0121] According to the above technical means, through the three closed loop mechanisms of compilation verification, error diagnosis and dynamic repair, the problem of landing execution of vehicle test code is completely solved. Before compilation, vehicle regulation level deep verification is performed, syntax checking is used to intercept basic errors, type compatibility verification is used to verify vehicle signal mapping relationship, and dependency analysis is used to ensure that the vehicle environment is complete, thereby eliminating the risk of runtime crashes. When the verification fails, the large language model intelligent repair engine accurately locates the vehicle regulation scene error, and generates a repair scheme based on the vehicle knowledge base to solve the vehicle environment compatibility problem; finally, through the closed loop feedback mechanism, the repaired template integration layer ensures the integrity of the test strategy, which significantly improves the compilation pass rate of the vehicle test code, and further improves the accuracy and reliability of the test code generation.
[0122] In a complete embodiment, the present application provides a method for generating vehicle unit test code, which relies on the following seven core components:
[0123] The input layer is used to process user input, read the test code and extract test target information, and integrate the input information.
[0124] The large model layer is used to analyze the code using a large language model, generate test intent information, develop corresponding test strategies for different unit test branches, and match test templates based on test intent.
[0125] The large language model base service is a bottom service with code understanding and analysis capability of the large language model.
[0126] The rule engine layer is configured with a large number of rules in advance, and the configured rules are used for responsible code structure analysis and verification, construction of a control flow graph, and extraction of a conditional path.
[0127] The template library is used for storing various preset test templates.
[0128] The test generation layer is used for generating and assembling final test code.
[0129] The compilation verifier is used for verifying the correctness of the generated code and providing feedback.
[0130] Based on the above seven components, as shown in the figure, the complete process of the vehicle unit test code generation method provided by the embodiment of the application is as follows: Figure 8
[0131] I. The initialization stage as shown in the figure: Figure 9
[0132] 1) The user provides source code to be tested and test targets.
[0133] 2) The input layer reads the source code, extracts test target information, and extracts dependency relationships.
[0134] 3) The input layer requests the rule engine layer to generate an AST.
[0135] 4) The rule engine layer generates an AST of the source code, analyzes code features, and generates a conditional path.
[0136] 5) The rule engine layer identifies test difficulties and special cases.
[0137] 6) The rule engine layer queries test templates matched with the current software framework, obtains a set of applicable templates, and is used for subsequent template prompt word limitation of the template in a suitable template framework, to narrow the search range of the large language model.
[0138] 7) The rule engine layer returns the analysis result to the input layer.
[0139] 8) The input layer integrates information and transmits it to the large model layer (LLMLayer). Figure 9 II. The code analysis stage as shown in the figure:
[0140] Figure 10 1) The large model layer prepares an intention prompt word Prompt containing the context of the code to be tested.
[0141] 2) The large model layer generates a test code template based on the intention prompt word.
[0142] 2) Large model layer calls large language model base service (LLM model base service) for code analysis, and generates different test intentions by parsing conditional paths. Figure 10
[0143] 3) Large language model base service returns analysis results to large model layer.
[0144] 4) Large model layer plans test strategies according to different test intentions.
[0145] 5) Large model layer selects appropriate test templates.
[0146] 6) Large model layer determines template parameters and filling values.
[0147] 7) Large model layer generates configuration files for testing.
[0148] 8) Large model layer passes test strategies, recommended templates and parameters to rule engine layer in the form of configuration files.
[0149] Three, as shown in the verification and analysis phase: Figure 11
[0150] 1) Rule engine layer parses configuration files provided by large model layer.
[0151] 2) Rule engine layer verifies the appropriateness of template selection.
[0152] 3) Rule engine layer verifies the correctness of parameter filling.
[0153] 4) Rule engine layer verifies the coverage of test strategies for special cases according to the difficulties and special cases of the code under test.
[0154] 5) Rule engine layer performs static analysis and checks to verify the syntax and reference integrity of templates and test strategies.
[0155] 6) If problems are found, rule engine layer requests large model layer for secondary analysis.
[0156] 7) Rule engine layer provides specific problem description to large model layer.
[0157] 8) Large model layer provides more detailed context and problem description of the code under test to large language model base service.
[0158] 9) Large language model base service returns correction results.
[0159] 10) Large model layer passes the corrected configuration to rule engine layer, and rule engine layer modifies the configuration file output by large model layer through incremental correction, and then re-verifies.
[0160] 11) The rule engine passes the validated configuration to the test generation layer.
[0161] Four, as Figure 12 shown in the test generation phase:
[0162] 1) The test generation layer obtains the recommended code template from the template library.
[0163] 2) The test template library finds the corresponding code template, associates the template metadata, loads the dependencies, and then returns the code template.
[0164] 3) As Figure 13 shown, the test generation layer fills in the template according to the template parameters.
[0165] 4) The test generation layer combines multiple template fragments.
[0166] 5) The test generation layer handles dependencies and conflicts between templates, and fixes existing problems in the template through a pre-configured rule library for error identification and error repair.
[0167] 6) The test generation layer formats the generated test code.
[0168] Five, as Figure 14 shown in the verification and feedback phase:
[0169] 1) The test generation layer submits the generated test code to the compilation verifier.
[0170] 2) The compilation verifier performs syntax checking, type checking, and dependency resolution on the test code.
[0171] 3) The compilation verifier compiles the test code.
[0172] 4) If the compilation fails, the compilation verifier returns the compilation error information. The test generation layer requests error analysis from the rule engine layer through the compilation error information, and the rule engine layer requests repair suggestions from the large model layer. The large model layer provides error information and the context of the test code to the large language model base service, and the large language model base service returns repair suggestions. The large model layer passes the repair suggestions to the rule engine layer, and the rule engine layer passes the corrected template or parameters to the test generation layer. The test generation layer applies the repair and regenerates the test code, and the test generation layer resubmits the test code to the compilation verifier.
[0173] 5) If the compilation is successful, the compilation verifier can execute the test case. The compilation verifier can collect test coverage data, and the compilation verifier returns the success status and related indicators.
[0174] The test generation layer outputs the final test code to the test directory. The system provides the test results and reports to the user.
[0175] Through the technical scheme provided by the embodiment of the application, the large language model and the rule engine layer work cooperatively, combining the intelligent understanding ability of the large language model and the accurate analysis ability of the rule engine. The large language model is responsible for code understanding and strategy planning, and the rule engine layer is responsible for structured analysis and verification. The large language model provides creative test strategies, and the rule engine ensures the accuracy of the generated code, producing a complementary enhanced technical effect. Through the double verification process, the rule engine verifies the output of the large language model, reducing the "illusion" problem of the large language model.
[0176] The rule engine extracts code features and AST structures, analyzes the condition path through CFG control flow, enhances the understanding context of the large language model, and provides auxiliary functions for the test intention of the large language model.
[0177] The embodiment of the application generates test code by using a template-driven method instead of directly generating. The template of the parameterized template system is not a static code, but a dynamic template containing multiple parameterized points, and the configuration is more flexible. Multi-level template combination supports the multi-level combination of basic templates, scene templates and special processing templates, further improving the test coverage. The large language model intelligently selects the most suitable template combination according to the code features, improving the accuracy of template selection. The template library maintains the dependency relationship and compatibility between templates to ensure the consistency of the combined code. Template-based test point repair can intervene in advance at the template parsing layer to identify some error points in the model output, eliminate the illusion problem caused by the model, and improve the stability of the final generation.
[0178] At the template type level, the embodiment of the application designs special test templates for Android-specific components and scenes, providing more special test functions. For example, templates are provided for component lifecycle testing (Activity, Fragment, etc.); support for interactive testing of View, Adapter, and other UI components; support for Handler, AsyncTask, coroutine, and other asynchronous operation testing; support for SharedPreferences, Room, and other data persistence testing; support for system permission and system service testing simulation; and support for compatibility testing of different Android versions.
[0179] The embodiment of the application designs a complete multi-level verification and automatic repair mechanism. The rule engine performs static analysis and verification before code generation. The compiler verifier performs dynamic compilation verification on the generated code. The compilation errors are classified and located to specific templates and parameters, and the verification results are fed back to the upstream large language model in a structured manner. The large model intelligently analyzes the errors and provides a repair scheme. The repair process uses an incremental method to modify only the problem part instead of regenerating.
[0180] A vehicle unit test code generation apparatus is also provided in the embodiment, which is configured to implement the above-described embodiments and preferred embodiments, and details of which have been described above. As used below, the term "module" can be a combination of software and / or hardware that implements a predetermined function. Although the apparatus described in the following embodiments is preferably implemented in software, implementation in hardware, or a combination of software and hardware, is also possible and contemplated.
[0181] The embodiment provides a vehicle unit test code generation apparatus, as shown in Figure 15 The apparatus comprises:
[0182] An input module 1501 configured to obtain a code under test;
[0183] A large model first analysis module 1502 configured to input the code under test and an intent prompt word into a large language model for analysis, and generate a test intent through the large language model, the test intent being used to indicate a process required for testing the code under test;
[0184] A large model second analysis module 1503 configured to input the test intent, the code under test, and a template prompt word into the large language model for analysis, to generate a test strategy through the large language model, and recommend a code template and parameter values required for testing from a template library;
[0185] A test code generation module 1504 configured to integrate the recommended code template according to the test strategy, and fill the parameter values into the code template to obtain a test code.
[0186] In some optional embodiments, before the test code generation module 1504 described above, the apparatus further comprises:
[0187] A template verification module configured to verify whether the selected code template is appropriate;
[0188] A parameter verification module configured to, when the template selection verification passes, verify whether the recommended parameter values are correct;
[0189] A coverage verification module configured to, when the parameter value correctness verification passes, verify a test coverage for a special scenario according to the test strategy, the code template, and the parameter values;
[0190] A syntax verification module configured to, when the test coverage reaches a preset condition, verify a syntax and reference integrity of the test strategy;
[0191] A return module configured to, when the syntax and reference integrity verification passes, jump to the step of integrating the recommended code template according to the test strategy, and filling the parameter values into the code template to obtain a test code;
[0192] a problem summary module configured to summarize a corresponding verification problem when any one of the verification steps fails;
[0193] a correction intention module configured to create a modification prompt word according to the verification problem, input the modification prompt word into the large language model as an input, return to the step of inputting the code to be tested and the intention prompt word into the large language model for analysis, and generate a test intention through the large language model.
[0194] In some optional embodiments, the device further comprises:
[0195] a code checking module configured to check the syntax correctness, type compatibility and code dependency of the test code;
[0196] a compiling module configured to compile the test code when the checking passes;
[0197] a testing module configured to execute a test through the test code when the compiling succeeds;
[0198] a compiling error acquisition module configured to acquire error information when the checking of the test code fails or the compiling fails;
[0199] a repair suggestion module configured to input the test code and the error information into the large language model for analysis to obtain a repair suggestion output by the large language model;
[0200] a code repair module configured to repair a corresponding code template and a parameter value according to the repair suggestion, and return to the step of integrating the recommended code template according to the test strategy and filling the parameter value into the code template to obtain the test code.
[0201] Further function descriptions of the above-mentioned modules and units are the same as those of the corresponding embodiments, and will not be repeated here.
[0202] The embodiment of the present application also provides a computer device, please refer to Figure 16 , Figure 16 is a structural schematic diagram of a computer device provided by an optional embodiment of the present application, as Figure 16As shown, the computer device includes one or more processors 10, memory 20, and interfaces 30 for external devices such as a keyboard and a mouse and peripheral devices such as disk devices or other storage devices. One or more busses 10 can be used to implement the interface between the various circuits and components of the computer device. It will be appreciated that the bus 10 can be implemented using any one or more of a variety of bus structures, such as a Peripheral Component Interconnect (PCI) bus, a Bluetooth bus, an Industry Standard Architecture (ISA) bus, an Enhanced ISA bus, an Accelerated Graphics Port (AGP) bus, a Video Electronics Standards Association (VESA) local bus, a Micro Channel Architecture (MCA) bus, a Universal Serial Bus (USB), and the like. Figure 16 The processor 10 is used in the embodiments as an example.
[0203] The processor 10 can be a central processing unit, a network processor, or a combination thereof. The processor 10 can further include a hardware chip. The hardware chip can be an application specific integrated circuit, a programmable logic device, or a combination thereof. The programmable logic device can be a complex programmable logic device, a field programmable logic device, a general array logic, or any combination thereof.
[0204] The memory 20 stores instructions that can be executed by the at least one processor 10, so that the at least one processor 10 can perform the method shown in the above embodiments.
[0205] The memory 20 can include a program region and a data region. The program region can store an operating system and application programs required by at least one function. The data region can store data created by the use of the computer device, and the like. In addition, the memory 20 can include a high-speed random access memory, and can further include a non-transitory memory, such as at least one magnetic disk storage device, a flash memory device, or other non-transitory solid-state memory device. In some alternative embodiments, the memory 20 can optionally include a memory that is remotely arranged with respect to the processor 10, and these remote memories can be connected to the computer device through a network. Examples of the network include, but are not limited to, the Internet, an intranet, a local area network, a mobile communication network, and a combination thereof.
[0206] The memory 20 can include a volatile memory, such as a random access memory, and can also include a non-volatile memory, such as a flash memory, a hard disk, or a solid state disk. The memory 20 can further include a combination of the above-mentioned memories.
[0207] The computer device further includes a communication interface 30 for communication with other devices or communication networks.
[0208] The embodiments of the present application further provide a computer readable storage medium, and the method according to the embodiments of the present application can be implemented in hardware, firmware, or recorded in a storage medium, or stored in a remote storage medium or a non-transitory machine readable storage medium and downloaded to a local storage medium through network, so that the method described herein can be processed by such software on a storage medium using a general purpose computer, a special purpose processor, or programmable or special hardware. The storage medium can be a magnetic disk, an optical disk, a read-only memory, a random access memory, a flash memory, a hard disk, or a solid state disk, etc. Further, the storage medium can also include a combination of the above-mentioned memories. It can be understood that the computer, the processor, the microprocessor controller, or the programmable hardware includes a storage component that can store or receive software or computer code, when the software or computer code is accessed and executed by the computer, the processor, or the hardware, the method shown in the above embodiments is implemented.
[0209] Part of the present application can be applied as a computer program product, for example, computer program instructions, when executed by a computer, through the operation of the computer, the method and / or technical solutions according to the present application can be called or provided. Those skilled in the art should understand that the form of computer program instructions in a computer readable medium includes but is not limited to source files, executable files, installation package files, etc. Correspondingly, the way of executing computer program instructions by computer includes but is not limited to: the computer directly executes the instructions, or the computer compiles the instructions and then executes the corresponding compiled program, or the computer reads and executes the instructions, or the computer reads and installs the instructions and then executes the corresponding installed program. Here, the computer readable medium can be any available computer readable storage medium or communication medium accessible to the computer.
[0210] Although the embodiments of the present application are described in conjunction with the accompanying drawings, various modifications and changes can be made by those skilled in the art without departing from the spirit and scope of the present application, and such modifications and changes fall within the scope defined by the appended claims.
Claims
1. A method of generating test code for a vehicle unit, characterized by, The method comprises: acquiring a code under test; inputting the code under test and an intent prompt word into a large language model for analysis, and generating a test intent through the large language model, the test intent being used to indicate a process required for testing the code under test; inputting the test intent, the code under test and a template prompt word into the large language model for analysis, to generate a test strategy through the large language model, and to recommend a code template and a parameter value required for testing from a template library; integrating the recommended code template according to the test strategy, and filling the parameter value into the code template to obtain test code.
2. The method of claim 1, wherein, The acquiring of the code under test comprises: reading a project source code file under test, and determining a function under test; obtaining a simplified file by removing classes, attributes and methods irrelevant to the function under test from the project source code file through call link analysis; obtaining external functions called by the function under test, and adding the external functions to the simplified file through call link analysis; obtaining a control flow graph of code in the simplified file through abstract syntax tree analysis of the simplified file; extracting each conditional path in the control flow graph.
3. The method of claim 2, wherein, The inputting of the code under test and the intent prompt word into the large language model for analysis, and the generation of the test intent through the large language model, comprises: inputting the code under test, context code of the code under test, dependent functions of the code under test, a current conditional path and an intent prompt word into the large language model, wherein the intent prompt word comprises a test input prompt word, a stubbing behavior prompt word, a function under test call prompt word and a result verification prompt word, the test input prompt word is used to inform the large language model of parameters required for test code input, the stubbing behavior prompt word is used to inform the large language model of real vehicle signals required for simulation feedback, the function under test call prompt word is used to inform the large language model of dependent functions required for calling, and the result verification prompt word is used to inform the large language model of output results required for assertion; receiving a test intent output by the large language model for the current conditional path.
4. The method of claim 3, wherein, The inputting of the test intent, the code under test and the template prompt word into the large language model for analysis, to generate a test strategy through the large language model, and to recommend a code template and a parameter value required for testing from a template library, comprises: inputting a test intent corresponding to the current conditional path, the code under test and the template prompt word into the large language model, the template prompt word comprising a template framework determined according to a code feature corresponding to the current conditional path; receiving the test strategy, the code template and the parameter value recommended by the large language model from the template library, the template library performing hierarchical classification management on code templates, class names, method names, parameter types and dependent items in the code templates being configured through the parameter value, and the template library configuring code templates with the same use function for different template frameworks.
5. The method of claim 4, wherein, The integration of the recommended code template according to the test strategy, and the filling of the parameter value into the code template to obtain test code, comprises: obtaining the recommended code template and filling the parameter value into the code template; combining each code template according to the test strategy, processing the dependencies and conflicts between templates, and obtaining a comprehensive template; repairing the template content and template sequence of the comprehensive template; formatting the repaired comprehensive template to generate test code.
6. The method of claim 4, wherein, Before the step of integrating the recommended code template according to the test strategy and filling the parameter value into the code template to obtain test code, the method further includes: verifying whether the code template selection is appropriate; when the template selection verification passes, verifying whether the parameter value recommendation is correct; when the parameter value correctness verification passes, verifying the test coverage for special scenarios according to the test strategy, the code template, and the parameter value; when the test coverage reaches the preset condition, verifying the syntax and reference integrity of the test strategy; when the syntax and reference integrity verification passes, jumping to the step of integrating the recommended code template according to the test strategy and filling the parameter value into the code template to obtain test code; when any one of the verification steps fails, summarizing the corresponding verification problem; creating a modification prompt word according to the verification problem, and inputting the modification prompt word into the large language model as an input, and returning to the step of inputting the code under test and intention prompt word into the large language model for analysis, and generating test intention through the large language model.
7. The method of claim 1, wherein, The method further includes: checking the syntax correctness, type compatibility, and code dependency of the test code; when the checking passes, compiling the test code; when the compilation is successful, performing testing through the test code; when the test code checking fails or the compilation is unsuccessful, obtaining error information; inputting the test code and the error information into the large language model for analysis to obtain repair suggestions output by the large language model; repairing the corresponding code template and parameter value according to the repair suggestions, and returning to the step of integrating the recommended code template according to the test strategy and filling the parameter value into the code template to obtain test code.
8. An apparatus for generating test code for a vehicle unit, characterized by The device includes: an input module for obtaining code under test; a first analysis module of a large model for inputting the code under test and intention prompt word into a large language model for analysis, and generating test intention through the large language model, the test intention being used to indicate the process required for testing the code under test; a second analysis module of a large model for inputting the test intention, the code under test, and template prompt word into a large language model for analysis, generating a test strategy through the large language model, and recommending code template and parameter value required for testing from a template library; a test code generation module for integrating the recommended code template according to the test strategy, and filling the parameter value into the code template to obtain test code.
9. A computer device, comprising: includes: A memory and a processor, which are connected in communication with each other, the memory storing computer instructions, and the processor executing the computer instructions to perform the method of any one of claims 1 to 7.
10. A computer-readable storage medium, characterized in that, The computer readable storage medium stores computer instructions for causing a computer to perform the method of any one of claims 1 to 7.
11. A computer program product, characterised in that, The computer readable storage medium stores computer instructions for causing a computer to perform the method of any one of claims 1 to 7. The computer readable storage medium stores computer instructions for causing a computer to perform the method of any one of claims 1 to 7.
Citation Information
Cited By
Heating ventilation and air conditioning system control code automatic generation method based on large language model
CN121277068A
Robot skill primitive library automatic generation method and system based on large language model
CN122311404A