Automobile software unit test code generation method and system
By converting automotive software functional specifications into Mermaid flowcharts and combining them with manual review, and using AI to generate GTest test cases, the consistency and coverage issues in existing technologies are resolved, achieving efficient and accurate unit test code generation.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- ATECH AUTOMOTIVE WUHU
- Filing Date
- 2026-01-17
- Publication Date
- 2026-05-08
AI Technical Summary
Existing automotive software testing suffers from problems such as difficulty in ensuring consistency, incomplete branch coverage, low efficiency, and maintenance difficulties. In particular, when manually writing test code, errors are prone to occur, and it is difficult to quickly verify the accuracy of the code logic.
By converting functional specifications into Mermaid flowcharts, introducing human review, and then using AI to generate GTest test cases, logical correctness and high branch coverage are ensured, and unit test code is generated through human-machine collaboration.
It achieves strict consistency between test code and specifications, improves branch coverage, simplifies code maintenance, and enhances testing efficiency and accuracy.
Smart Images

Figure CN121996550A_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of software automated testing, and more particularly to the field of automotive software testing technology. Background Technology
[0002] In the software development of automotive electronic control units (such as body domain controllers), the writing of test code relies heavily on manual work by test engineers. Engineers need to read and understand natural language functional specifications in PDF or other formats (such as "seat heating function specifications"), manually design test cases, and write unit test code using frameworks such as GTest.
[0003] For example, the published document with authorization announcement number CN119807022B, application publication date of 2025-12-19, and patent title "Method, System, Apparatus, Device and Medium for Testing Software Products" discloses a method applied to the GTest engine, including: responding to a startup command sent by the software product under test, executing GTest initialization operations and loading operations for various test cases, wherein the software product under test is developed using Unreal Engine, and the various test cases include: various test cases related to C++ interface testing of the software product under test, and / or, various test cases related to UI testing of the software product under test; executing the various test cases loaded by the GTest engine through a test service to obtain the execution results of the various test cases; and returning the execution results of the various test cases to the software product under test. Similar software testing methods have the following problems and bottlenecks:
[0004] Consistency is difficult to guarantee: human misunderstanding can lead to test code that does not conform to the intent of the specification, resulting in false positives or false negatives.
[0005] Incomplete branch coverage: In complex state transition logic, it is very easy to overlook certain boundary conditions or abnormal branches by humans.
[0006] Inefficient and difficult to maintain: The entire process from specification to code is time-consuming and labor-intensive, and when the specification changes, the synchronous update of the test code also relies on manual intervention, which is prone to errors.
[0007] In recent years, although there have been attempts to use AI to generate code directly from natural language, the accuracy of the generated code is low due to the ambiguity and context dependence of natural language, and engineers find it difficult to directly and quickly verify whether the generated logic is correct. Summary of the Invention
[0008] The technical problem to be solved by the present invention is to realize a method and system for automatically generating unit test code that can ensure that the test code is strictly consistent with the functional specifications, achieve high branch coverage, and is easy for engineers to review and maintain.
[0009] To achieve the above objectives, the technical solution adopted by this invention is: a method for generating unit test code for automotive software, comprising the following steps:
[0010] Step 1: Standardize the functions and convert them into a Mermaid flowchart;
[0011] Step 2: Submit the generated flowchart to the AI model to generate Gtest test cases;
[0012] Step 3: Perform software functional testing using the generated Gtest test cases.
[0013] In step 1, the functional specification is parsed to extract logical elements, and the logical elements are converted into flowchart code in Mermaid format. The parsing is based on rule-based natural language processing technology.
[0014] In step 1, the Mermaid flowchart code precisely describes the control flow of the function, including but not limited to: all possible state nodes, the direction of transition between states, the condition judgment for triggering the transition, and the actions performed when a state enters or exits.
[0015] After step 1 is completed, a manual inspection is performed. This manual inspection involves visually displaying and manually correcting the Mermaid flowchart code through a user interface.
[0016] In step 2, the Gtest test cases are automatically generated source code that conforms to the unit testing framework. The generation is implemented using a template-based code generator.
[0017] In step 2, the instructions provided to the large language model explicitly require it to traverse each branch path in the flowchart and generate an independent test case for each path, so that the generated GTest code can achieve condition coverage and path coverage.
[0018] After step 2 is completed, manual inspection and adjustment are performed.
[0019] In step 3, after the functional test results are displayed, the test result data is shown, and the result data is compared with the preset qualified threshold range to determine and display the test conclusion.
[0020] A system for executing the automotive software unit test code generation method, the system comprising:
[0021] Flowchart modeling module: Used to receive or assist in generating Mermaid code that describes functional logic;
[0022] Visual verification module: used to render Mermaid code as graphics for user review;
[0023] AI code generation interface module: used to send the approved Mermaid code to the large language model and receive the returned GTest code;
[0024] Code output module: Used to display and export the final generated unit test code.
[0025] This invention has the following advantages:
[0026] 1. High fidelity and consistency: By using the key step of "engineers reviewing Mermaid flowcharts", the logical source of AI-generated test code is ensured to be correct, fundamentally solving the consistency problem caused by human comprehension bias.
[0027] 2. High branch coverage: Mermaid flowcharts naturally represent all branch paths. By instructing AI to generate tests based on the flowchart, all normal, abnormal, and boundary conditions can be systematically and comprehensively covered, overcoming the shortcomings of manually designed test cases that are prone to omissions.
[0028] 3. Highly efficient human-machine collaboration: Mermaid acts as an "engineering bridge," balancing automation and controllability. Engineers do not need to write complex test code; they only need to focus on the correctness of the logical model, greatly improving efficiency and making the maintenance of test code (as specifications change) simple and quick—simply modify the flowchart and regenerate it. Attached Figure Description
[0029] The following is a brief explanation of the content represented by each figure in this specification:
[0030] Figure 1 Flowchart for generating unit test code for automotive software;
[0031] Figure 2 A schematic diagram of the workflow for generating unit test code for automotive software.
[0032] Figure 3 This is an example of a test of the seat heating function. Detailed Implementation
[0033] The following description, with reference to the accompanying drawings, details the specific implementation of the present invention, including the shape and structure of each component, the relative positions and connections between the parts, the function and working principle of each part, the manufacturing process, and the operation and use methods, to help those skilled in the art to have a more complete, accurate, and in-depth understanding of the inventive concept and technical solution of the present invention.
[0034] This invention is an automotive software engineering and automated testing technology that solves the problems of technical bottlenecks and low efficiency in testing software by converting natural language functional specifications into unit test code through Mermaid flowcharts.
[0035] This invention provides a method for generating unit test code, comprising: parsing a functional specification to extract logical elements; converting the logical elements into flowchart code in Mermaid format; and automatically generating source code conforming to a unit test framework based on the flowchart code. A user interface is provided during the processing to visualize and manually correct the Mermaid flowchart code.
[0036] The parsing steps include: using rule-based natural language processing techniques;
[0037] The generation step is implemented using a template-based code generator;
[0038] Specifically, the core concept of this invention is to introduce Mermaid flowcharts as an "engineering bridge" connecting natural language functional specifications and AI code generation models. This bridge is a human-machine readable and verifiable structured representation. On the one hand, it allows engineers to convert natural language specifications into precise flowcharts at low cost; on the other hand, it provides AI large language models with perfectly structured input suitable for their reasoning, thereby generating high-quality, high-coverage test code.
[0039] The specific steps include:
[0040] 1. Specification Parsing and Flowchart Modeling: Receive automotive electronic function specifications (e.g., seat heating function specifications), and have engineers or auxiliary tools convert their key logic (states, conditions, transitions, actions) into flowchart code described by Mermaid syntax. This step engineers the ambiguous natural language specifications into an unambiguous structured logic model;
[0041] The Mermaid flowchart code precisely describes the control flow of the function, including but not limited to: all possible state nodes, the direction of transition between states, the condition for triggering the transition, and the actions performed when a state enters or exits.
[0042] 2. Flowchart Verification and Correction: The above code is visualized using the Mermaid rendering engine for engineers to review and confirm its logic. This manual intervention ensures the correctness of the logical model, which is the foundation for generating high-fidelity test code.
[0043] 3. AI-Driven Code Generation: The validated Mermaid flowchart code is input as a prompt into the large language model, which is then instructed to generate corresponding, complete GTest unit test code based on the structured logic of the flowchart. The large language model is a trained AI model capable of understanding Mermaid syntax and the GTest framework.
[0044] This step provides the large language model with explicit instructions to traverse every branch path in the flowchart and generate an independent test case for each path, thereby ensuring that the generated GTest code can achieve condition coverage and path coverage.
[0045] 4. Test code output and verification: Receive and output GTest code generated by the AI model, which engineers can compile and run to complete the final verification.
[0046] The present invention also provides a system for implementing the above method, comprising:
[0047] Flowchart Modeling Module: Used to receive or assist in generating Mermaid code that describes functional logic.
[0048] Visual verification module: used to render Mermaid code as graphics for user review.
[0049] AI code generation interface module: used to send the approved Mermaid code to the large language model and receive the returned GTest code.
[0050] Code output module: Used to display and export the final generated unit test code.
[0051] Terminology introduction:
[0052] Mermaid is a text-based diagramming tool that allows users to create flowcharts, sequence diagrams, Gantt charts, and other graphs using simple code syntax. In this document, Mermaid is used to visually represent system processes or structures, helping to understand the logical relationships within technical solutions.
[0053] GTest: GTest (Google Test) is an open-source C++ unit testing framework provided by Google for writing and running automated test cases. In this document, GTest is used to verify the functional correctness of software modules, ensuring that the system works as expected.
[0054] AI (Artificial Intelligence): AI refers to the technology of simulating human intelligent behavior through computer systems, including but not limited to machine learning, natural language processing, and image recognition. In this document, AI refers to the ability of a system to intelligently analyze, predict, or make decisions based on input data using algorithmic models.
[0055] The invention will be further described below with reference to an embodiment of the "seat heating function", such as... Figure 3 As shown:
[0056] Step 1, Flowchart Modeling: Based on the "Seat Heating Function Specification", the engineer wrote the following Mermaid code:
[0057] Step 2, Visual Verification: The system renders the above code as a flowchart, and the engineer checks and confirms that the logic is correct;
[0058] Step 3, AI code generation: The system combines the above Mermaid code with preset instructions and software variable names used in the flowchart, and sends it to large language model APIs such as DeepSeek.
[0059] For example: "Please generate GTest unit test code covering all branches according to the Mermaid process described above. There are 3 heating levels: 0 represents off, 1 represents 30℃, and 2 represents 35℃. The level command signal name is HeatCmd. The temperature feedback signal name is Temperature. The heating output variable is Heatoutput, where 1 indicates output is on and 0 indicates output is off."
[0060] Mermaid flowchart code:
[0061]
[0062] Step 4, Result Output: The AI model returns GTest code, which will contain multiple TEST_F test cases, such as testing all scenarios including "heating is turned on when the seat is occupied and the temperature is below the target", "heating is turned off when the seat is not occupied", and "heating is automatically turned off after the temperature reaches the target".
[0063] This test code has the following characteristics:
[0064] 1. Complete branch coverage:
[0065] (1) The system is powered on but there is no heating command;
[0066] (2) A heating command is received and the temperature is below the target;
[0067] (3) Received a heating command and the temperature reached / exceeded the target;
[0068] (4) Stop outputting once the target temperature is reached;
[0069] (5) Reheating begins after the temperature drops;
[0070] (6) Switch gears during the heating process;
[0071] (7) A shutdown command was received during the heating process;
[0072] 2. Boundary condition testing:
[0073] (1) The temperature is just below the target temperature;
[0074] (2) The temperature is exactly equal to the target temperature;
[0075] (3) The temperature is just higher than the target temperature;
[0076] 3. Multiple scenario tests:
[0077] (1) Various combinations of gear 1 and gear 2;
[0078] (2) Multiple gear shifts;
[0079] (3) Dynamic changes in temperature;
[0080] 4. Signal name correspondence:
[0081] (1) HeatCmd - Heating level command;
[0082] (2) Temperature feedback;
[0083] (3) Heatoutput - Heating output.
[0084] This method reads automotive electronic functional specifications through a parsing module, extracts logical elements such as states and conditions, and assists in identifying potential test requirements. Then, a generation module converts these elements into flowchart description code in Mermaid syntax; this process can be manually verified and adjusted. Finally, using this Mermaid code as input, a test code generation engine (intelligent model) is driven to automatically generate unit test source code for the Gtest framework. Throughout the process, necessary manual intervention and verification steps are introduced, including reviewing and correcting test requirements, flowcharts, and generated code to ensure the accuracy, logical correctness, and high coverage of the final test code.
[0085] This invention addresses the problems of low efficiency and error-proneness in traditional development modules, which rely on manual processes from requirements gathering to testing. By introducing Mermaid as an intermediate logic model and combining manual intervention with intelligent models, it ensures strict consistency and high quality in specifications, flowcharts, and test code. This significantly improves the automation level, quality, and efficiency of automotive software development.
[0086] The present invention has been described above by way of example with reference to the accompanying drawings. Obviously, the specific implementation of the present invention is not limited to the above-described manner. Any non-substantial improvements made using the inventive concept and technical solution of the present invention, or the direct application of the inventive concept and technical solution of the present invention to other occasions without modification, are all within the protection scope of the present invention.
Claims
1. A method for generating unit test code for automotive software, wherein the characteristic words... Yes, it includes the following steps: Step 1: Standardize the functions and convert them into a Mermaid flowchart; Step 2: Submit the generated flowchart to the AI model to generate Gtest test cases; Step 3: Perform software functional testing using the generated Gtest test cases.
2. The method for generating unit test code for automotive software according to claim 1, characterized in that: In step 1, the functional specification is parsed to extract logical elements, and the logical elements are converted into flowchart code in Mermaid format. The parsing is based on rule-based natural language processing technology.
3. The method for generating unit test code for automotive software according to claim 2, characterized in that: In step 1, the Mermaid flowchart code precisely describes the control flow of the function, including but not limited to: all possible state nodes, the direction of transition between states, the condition judgment for triggering the transition, and the actions performed when a state enters or exits.
4. The method for generating unit test code for automotive software according to claim 3, characterized in that: After step 1 is completed, a manual inspection is performed. This manual inspection involves visually displaying and manually correcting the Mermaid flowchart code through a user interface.
5. The method for generating automotive software unit test code according to any one of claims 1-4, characterized in that: In step 2, the Gtest test cases are automatically generated source code that conforms to the unit testing framework. The generation is implemented using a template-based code generator.
6. The method for generating unit test code for automotive software according to claim 5, characterized in that: In step 2, the instructions provided to the large language model explicitly require it to traverse each branch path in the flowchart and generate an independent test case for each path, so that the generated GTest code can achieve condition coverage and path coverage.
7. The method for generating unit test code for automotive software according to claim 6, characterized in that: After step 2 is completed, manual inspection and adjustment are performed.
8. The method for generating unit test code for automotive software according to claim 1 or 7, characterized in that: In step 3, after the functional test results are displayed, the test result data is shown, and the result data is compared with the preset qualified threshold range to determine and display the test conclusion.
9. A system for executing the automotive software unit test code generation method as described in any one of claims 1-8, characterized in that, The system includes: Flowchart modeling module: Used to receive or assist in generating Mermaid code that describes functional logic; Visual verification module: used to render Mermaid code as graphics for user review; AI code generation interface module: used to send the approved Mermaid code to the large language model and receive the returned GTest code; Code output module: Used to display and export the final generated unit test code.
Citation Information
Patent Citations
Methods, systems, apparatuses, devices, and media for testing software products
CN119807022B