AI large model-based unit test case rapid generation method

By combining AI large-scale models with static and dynamic analysis techniques to generate unit test cases, the problems of low efficiency and insufficient coverage of traditional methods are solved, achieving efficient and automated test case generation and improving software quality and coverage.

CN120994545APending Publication Date: 2025-11-21INSPUR TIANYUAN COMM INFORMATION SYST CO LTD
View PDF 0 Cites 10 Cited by

Patent Information

Application Number
CN202511065101.1
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-07-31
Publication Date
2025-11-21

AI Technical Summary

Technical Problem

Traditional unit test case writing is inefficient, has insufficient coverage, and is costly to maintain. Existing automation tools are inflexible and difficult to adapt to complex scenarios.

Method used

The system employs a large AI model combined with static and dynamic analysis techniques to parse code structure, generate test cases, record execution paths through instrumentation, generate natural language descriptions using the AI ​​model, automatically match test frameworks, optimize coverage, and iterate and improve based on historical test cases and user feedback.

Benefits of technology

It significantly improved the efficiency of test case generation, shortened the testing cycle, reduced development and maintenance costs, and improved software quality and coverage.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120994545A_ABST
    Figure CN120994545A_ABST
Patent Text Reader

Abstract

The invention particularly relates to a unit test case rapid generation method based on an AI large model. According to the unit test case rapid generation method based on the AI large model, structure information is extracted through a static AST, an execution path is tracked through dynamic instrumentation, and code analysis is achieved; an analysis result is converted into cue words through AI, a multi-scene use case is generated through reasoning, and the multi-scene use case is matched with a language framework; counting row / branch / path coverage, identifying gaps and optimizing loop logic; hot spots and paths are displayed through a visual report, IDE one-key repair and manual correction of a feedback model are supported, intelligent retrieval and reuse of historical cases are realized by relying on FAISS, and the generation accuracy is continuously improved. According to the unit test case rapid generation method based on the AI large model, high-coverage-rate test case generation is achieved through the AI large model, the test case generation efficiency is remarkably improved, the test period is shortened, the software quality is improved, the workload of manually compiling and maintaining the test case is reduced, and the development cost is reduced.
Need to check novelty before this filing date? Find Prior Art

Description

TECHNICAL FIELD

[0001] The present application relates to the technical field of software testing, in particular to a unit test case rapid generation method based on AI large model. BACKGROUND

[0002] Traditional unit test case writing mainly relies on manual completion by developers, which has the following problems:

[0003] (1) Low efficiency: manual test case writing is time-consuming and labor-intensive, especially for complex logic code modules.

[0004] (2) Inadequate coverage: manually written test cases are difficult to cover code branches and boundary conditions comprehensively.

[0005] (3) High maintenance cost: with code changes, test cases need to be updated frequently, increasing the maintenance burden.

[0006] Existing automated testing tools (such as JUnit, TestNG) provide some framework support, but still require manual definition of test logic. Some tools attempt to generate test cases through rule templates, but have poor flexibility and are difficult to adapt to complex scenarios.

[0007] Based on the above, the present application proposes a unit test case rapid generation method based on AI large model. SUMMARY

[0008] The present application provides a simple and efficient unit test case rapid generation method based on AI large model to overcome the shortcomings of the prior art.

[0009] The present application is achieved by the following technical solutions:

[0010] A unit test case rapid generation method based on AI large model, comprising the following steps:

[0011] Step S1, code parsing

[0012] Step S1.1, static analysis

[0013] Input the source code to be tested, extract code structure information through syntax tree (AST) parsing technology, identify function name, parameter type, return value type and modifier; analyze function parameters and return values, analyze function call chain, variable dependency and external dependency through control flow graph CFG and data flow graph DFG;

[0014] The code structure information includes class, method and variable definition;

[0015] The return value includes basic type, complex object and generic type;

[0016] In the step S1.1, the static analysis step is as follows:

[0017] Step S1.1.1, syntax tree parsing: using ANTLR parser generation tool to build a multi-language parser, converting source code into abstract syntax tree (AST), extracting structural information including class, method and variable definition;

[0018] For Java code, annotations can be recognized, including @Override and @Test;

[0019] For Python code, decorator syntax can be parsed;

[0020] Step S1.1.2, type inference system: designing type inference algorithm for dynamic language, inferring variable types through variable assignment chain and function call context;

[0021] Step S1.1.3, control flow analysis: using Tarjan algorithm to automatically label loop structure and unreachable code blocks, generating control flow graph CFG; performing full path enumeration on switch-case statements to cover all theoretical branches;

[0022] Step S1.1.4, data flow analysis: when constructing data flow graph DFG, object reference relationship is handled through pointer analysis, sensitive data flow is tracked using taint propagation technology, and vulnerability patterns are identified.

[0023] Step S1.2, dynamic analysis

[0024] Through instrumentation technology or runtime monitoring, record function execution path, branch condition and loop count, combined with runtime variable value and exception capture and log information, dynamically supplement the blind area of static analysis, form a panoramic view of code behavior;

[0025] In the step S1.2, the dynamic analysis step is as follows:

[0026] Step S1.2.1, instrumentation scheme: using bytecode operation tool ASM to realize non-intrusive instrumentation, inserting probes at key positions including branch statements and loop entry, recording method call timing diagram, parameter value distribution statistics and exception throw stack at runtime;

[0027] Step S1.2.2, runtime monitoring: collecting JVM indicators through Java Agent technology, including:

[0028] Memory object snapshot, used to generate complex object test cases;

[0029] Thread contention state, used to diagnose concurrent conflict problems;

[0030] I / O call frequency, for locating external dependency bottlenecks.

[0031] Step S2, generating test cases

[0032] Step S2.1, AI large model formatting

[0033] Convert the code parsing result into a natural language description or structured prompt word, adapt to the requirements of the AI model;

[0034] In the step S2.1, the AI large model formatting process is as follows:

[0035] Step S2.1.1, function signature standardization: extract function name, parameter list, return value type and access modifier, type erasure or keep generic constraints;

[0036] Step S2.1.2, dependency extraction: identify external dependencies of method calls, mark context factors affecting testing, including global variables and static methods;

[0037] Step S2.1.3, natural language prompt generation: use a fixed template to fill in and dynamically optimize prompts to convert structured data into natural language descriptions;

[0038] Output natural language description in pre-set format, including function description, parameter constraints, exception scenarios and external dependency information;

[0039] In the step S2.1.3, the structured data is converted into natural language description by filling in the static code logic with a fixed template, and based on the control flow graph CFG to disassemble branch conditions, combined with data flow analysis DFG to define variable constraint rules.

[0040] Step S2.1.4, structured prompt word: provide configuration documents in JSON and XML formats to adapt to the requirements of the fine-tuned special AI model.

[0041] Step S2.2, AI logic reasoning

[0042] The model generates test cases based on code semantics, including normal scenarios, boundary conditions and exception handling;

[0043] Boundary conditions include numerical rationality, strings and collection classes;

[0044] Based on throws declaration and try-catch block, reverse deduce exception trigger condition, simulate exception case;

[0045] Step S2.3, AI supports multiple languages

[0046] Automatically match the target language test framework to ensure that the generated test code can be directly run;

[0047] Step S2.4, history case reuse

[0048] Retrieve historical test cases through a vector retrieval system, preferentially reuse high-coverage case templates, and fine-tune the model based on user-modified test cases to improve generation accuracy;

[0049] Vector retrieval system: use search library FAISS to build a test case vector library, feature vectors include code structure fingerprints, API call sequences, and exception patterns, similarity threshold is set to 0.85, and Top-K retrieval is supported;

[0050] Adaptive adjustment: user-modified cases update the model through contrastive learning, establish feedback factors, if the acceptance rate of case templates exceeds 90%, the corresponding template weight is increased by 20%, realizing continuous iterative improvement;

[0051] Step S3, coverage evaluation

[0052] Step S3.1, multi-dimensional coverage statistics

[0053] Statistical line coverage, and through static insertion and dynamic tracking technology, fine-grained analysis of code execution path is performed to ensure that test cases fully cover various scenarios, and code coverage is calculated through generated test cases;

[0054] In the step S3.1, the multi-dimensional coverage statistics process is as follows:

[0055] Line coverage: real-time calculation of the line execution proportion of the code, if it is less than 80%, an alarm mechanism is triggered, and the test case parameters are modified, and the test cases are automatically or manually regenerated;

[0056] Branch coverage: check whether all if / else paths and switch paths are covered, if the coverage rate is less than 90%, an alarm message is issued;

[0057] Path coverage: for complex logic, including nested loops and recursion, path combination cases are generated, and a coverage alarm threshold of 75% is configured, if the threshold is lower, the test cases are regenerated, and loop / recursive combination cases are dynamically generated.

[0058] Step S3.2, branch coverage depth analysis

[0059] For conditional logic, the analysis is as follows:

[0060] Identify uncovered branches: Mark all conditional branches through the control flow graph (CFG), compare with the actual execution path, and give a report of the uncovered branches, and show all branch coverage in the report;

[0061] Generate supplementary use case suggestions: Generate supplementary test cases based on the identified uncovered conditions;

[0062] Boundary condition verification: Generate critical test data based on boundary values in conditions to ensure that extreme value scenarios are not missed;

[0063] Step S3.3, path coverage optimization strategy

[0064] For complex logic, including nested loops and recursion, the analysis is as follows:

[0065] Full path enumeration: Generate all theoretical execution paths through static analysis;

[0066] Symbolic execution combined with fuzz testing: Use symbolic execution to generate inputs that satisfy specific paths; for uncovered paths, generate and retain valid test cases randomly through fuzz query;

[0067] Loop invariant inference: Automatically infer loop termination conditions to generate special data sets that exit early or traverse completely;

[0068] According to the coverage results and user feedback, dynamically adjust the test cases, generate an interactive coverage report, and the report supports the following functions:

[0069] Hotspot map display: Mark the uncovered code lines and covered branches separately;

[0070] Path tracking: Graphically display the execution path;

[0071] Automatic repair suggestion: Directly associate with the integrated development environment (IDE), and recommend supplementary test cases when clicking on uncovered code;

[0072] Support user manual editing of test cases, and store the optimization results in the historical database for subsequent AI learning.

[0073] An AI-based large model unit test case rapid generation system for implementing the above method, comprising:

[0074] A code analysis module responsible for combining instrumentation technology and runtime monitoring, dynamically tracking execution paths, branch directions, and variable evolution, capturing exception logs to supplement static blind spots, and building a multi-dimensional code behavior portrait to provide a panoramic view for in-depth testing;

[0075] The test case generation module is responsible for converting the code parsing result into natural language or structured prompt words, driving the AI model to generate test schemes covering normal logic, boundary values and abnormal scenarios; automatically matching the test framework of the target language to ensure that the code is ready for use;

[0076] The coverage evaluation module is responsible for realizing fine-grained analysis of code execution paths through static insertion and dynamic tracking, and counting the coverage of lines, branches and paths.

[0077] For conditional logic, the CFG graph is used to identify uncovered branches and generate supplementary test case suggestions, and the boundary values are verified.

[0078] For complex structures, a combination strategy of full path enumeration, symbolic execution and fuzz testing is adopted to automatically infer loop invariants to generate special data sets, ensure that nested loops and recursive complete traversal or early exit scenarios are covered, and comprehensively improve the test sufficiency and defect detection rate.

[0079] The feedback optimization module is responsible for dynamically adjusting the test cases according to the coverage results and user feedback, and generating an interactive coverage report.

[0080] An AI large model-based unit test case rapid generation device, comprising a memory and a processor; the memory is used to store a computer program, and the processor is used to execute the computer program to realize the method steps described above.

[0081] A readable storage medium, the readable storage medium has a computer program stored thereon, and the computer program is executed by a processor to realize the method steps described above.

[0082] The beneficial effects of the present application are: the AI large model-based unit test case rapid generation method realizes high-coverage test case generation through AI large model, significantly improves test case generation efficiency, shortens test cycle, improves software quality, reduces the workload of manually writing and maintaining test cases, and reduces development cost. BRIEF DESCRIPTION OF DRAWINGS

[0083] In order to more clearly illustrate the technical solutions in the embodiments of the present application or the prior art, the drawings needed in the embodiment or prior art description will be briefly introduced below. Obviously, the drawings in the following description are some embodiments of the present application, and those skilled in the art can obtain other drawings according to these drawings without creative labor.

[0084] Figure 1 The present application is based on an AI large model-based unit test case rapid generation system architecture diagram.

[0085] Figure 2A flowchart for generating test cases for the present application is shown.

[0086] Figure 3 A flowchart for coverage optimization of the present application is shown. DETAILED DESCRIPTION

[0087] In order for those skilled in the art to better understand the technical solutions in the present application, the technical solutions in the embodiments of the present application will be described clearly and completely below in conjunction with the embodiments of the present application. Obviously, the described embodiments are only some of the embodiments of the present application, not all. Based on the embodiments in the present application, all other embodiments obtained by those skilled in the art without creative labor should fall within the scope of protection of the present application.

[0088] The method for quickly generating unit test cases based on an AI large model comprises the following steps:

[0089] Step S1, code analysis

[0090] In combination with the patching technology and runtime monitoring, the execution path, branch direction and variable evolution are dynamically tracked, the abnormal log is captured to supplement the static blind area, and a multi-dimensional code behavior portrait is constructed to provide a panoramic view for deep testing;

[0091] Step S1.1, static analysis

[0092] The source code to be tested is input, the code structure information is extracted through the syntax tree (AST) analysis technology, the function name, parameter type, return value type and modifier (such as public / private) are identified, the function call chain, variable dependency and external dependency (such as database, API call) are analyzed through the control flow graph CFG and data flow graph DFG by analyzing the function parameters and return values;

[0093] The code structure information includes class, method and variable definition;

[0094] The return value includes basic type, complex object and generic type;

[0095] In the step S1.1, the static analysis step is as follows:

[0096] Step S1.1.1, syntax tree analysis: a multi-language parser is constructed by using the ANTLR parser generator tool to convert the source code into an abstract syntax tree (AST), and the structure information such as class, method and variable definition is accurately extracted;

[0097] For Java code, annotations can be recognized, including @Override and @Test;

[0098] For Python code, decorator syntax can be parsed;

[0099] Step S1.1.2, type inference system: a type inference algorithm is designed for dynamic languages such as Python, which infers variable types through variable assignment chains and function call contexts, with an accuracy of over 85%;

[0100] Step S1.1.3, control flow analysis: Tarjan algorithm is used to automatically label loop structures and unreachable code blocks, and generate control flow graph CFG; all theoretical branches are covered by full path enumeration of switch-case statements;

[0101] Step S1.1.4, data flow analysis: when constructing data flow graph DFG, object reference relationship is handled through pointer analysis, and sensitive data flow is tracked using taint propagation technology to identify vulnerability patterns such as SQL injection.

[0102] Step S1.2, dynamic analysis

[0103] Through instrumentation technology or runtime monitoring, record function execution path, branch condition and loop times, combined with runtime variable value and exception capture and log information, dynamically supplement the blind area of static analysis, form a panoramic view of code behavior, enhance the understanding of code behavior;

[0104] In the step S1.2, the dynamic analysis step is as follows:

[0105] Step S1.2.1, instrumentation scheme: use bytecode operation tool ASM to realize non-intrusive instrumentation, insert probes at key positions including branch statements and loop entries, record method call timing diagram (support distributed tracking), parameter value distribution statistics (automatic identification of boundary values) and exception throw stack (including nested exception chain) at runtime;

[0106] Step S1.2.2, runtime monitoring: collect JVM indicators through Java Agent technology, including:

[0107] Memory object snapshot, used to generate complex object test cases;

[0108] Thread contention state, used to diagnose concurrent conflict problems;

[0109] I / O call frequency, used to locate external dependency bottlenecks.

[0110] Step S2, generate test cases

[0111] Step S2.1, AI large model formatting

[0112] Convert code parsing results (such as function signature, dependency relationship) into natural language description or structured prompt word, adapt to the requirements of AI model (such as DeepSeek, Qwen);

[0113] In the step S2.1, the AI large model formatting process is as follows:

[0114] Step S2.1.1, function signature standardization: extract function name, parameter list (including type), return value type and access modifier (public / private, etc.), and deal with generics (such as List <t>) type erasure or preserving generic constraints are performed;

[0115] Step S2.1.2, dependency extraction: identify external dependencies of method calls (such as database queries, API calls, file IO), label contextual factors that affect testing, including global variables and static methods;

[0116] Step S2.1.3, natural language prompt generation: use fixed template filling combined with dynamic optimization prompts to convert structured data into natural language descriptions to improve the understanding ability of AI models;

[0117] Output natural language description in preset format, including function description, parameter constraints, exception scenarios and external dependency information;

[0118] In step S2.1.3, fixed template filling is used to fill static code logic, and branch conditions are disassembled based on control flow graph CFG, and variable constraint rules are defined based on data flow analysis DFG to convert structured data into natural language description.

[0119] Fixed template filling (applicable to regular code logic), for example:

[0120] Please generate unit test cases for the following method:

[0121] Method function: [function description, such as "get username list according to user ID"];

[0122] Input parameters: [parameter 1 type and constraints], [parameter 2 type and constraints];

[0123] Return value: [return value type and possible values];

[0124] Exception: [possible exceptions and triggering conditions];

[0125] External dependencies: [such as databases, network requests, etc.];

[0126] Dynamic prompt optimization (applicable to complex logic):

[0127] Generate branch condition description combined with control flow graph (CFG), for example:

[0128] This method contains the following branch logic:

[0129] 1). When parameter b = 0, throw parameter exception;

[0130] 2). When a is negative, the return value is negative;

[0131] 3). When a is a power of 2, the calculation efficiency may be optimized.

[0132] Variable constraints are generated based on data flow analysis (DFG), as follows:

[0133] The variable `user_input` must satisfy

[0134] - Length ≤ 100

[0135] - Contains only letters and numbers

[0136] - If empty, use the default value "guest".

[0137] Step S2.1.4, structured prompt words (JSON / XML format): Provide configuration documents in JSON and XML formats to adapt to the requirements of fine-tuned specialized AI models (such as specialized test generation models).

[0138] Step S2.2, AI logical reasoning

[0139] The model generates test cases based on code semantics, including normal scenarios, boundary conditions, and exception handling;

[0140] Normal scenarios: Cover typical inputs and expected outputs;

[0141] Boundary conditions include numerical rationality, strings, and collection types;

[0142] Numerical type: Automatically identify int boundaries (MAX_VALUE / MIN_VALUE);

[0143] String: Generate empty string, super long string (256 characters), Unicode special characters;

[0144] Collection type: Generate empty set, single-element set, full-capacity set;

[0145] Based on the throws declaration and try-catch block, the exception triggering condition is deduced reversely, and the exception case is simulated; exception cases include data operation connection timeout and primary key conflict.

[0146] Step S2.3, AI supports multiple languages

[0147] Automatically match the test framework of the target language (Java→JUnit, Python→pytest, C++→GoogleTest) to ensure that the generated test code can be directly run;

[0148] Step S2.4, historical case reuse

[0149] Retrieve historical test cases through vector retrieval system, preferentially reuse high-coverage case templates, and fine-tune the model based on user-corrected test cases to improve generation accuracy;

[0150] Vector Retrieval System: Use search library FAISS to build test case vector library, feature vector contains code structure fingerprint (SimHash), API call sequence and exception pattern, similarity threshold is set to 0.85, and Top-K retrieval is supported;

[0151] Adaptive adjustment: User corrected use cases update the model through contrastive learning, establish feedback factors, if the acceptance rate of use case templates exceeds 90%, the corresponding template weight is increased by 20%, realizing continuous iterative improvement;

[0152] Step S3, coverage evaluation

[0153] Step S3.1, multi-dimensional coverage statistics

[0154] Statistical basis line coverage, and through static insertion and dynamic tracking technology, fine-grained analysis of code execution path is carried out to ensure that test cases fully cover various scenarios, and code coverage is counted through generated test cases;

[0155] In the step S3.1, the multi-dimensional coverage statistics process is as follows:

[0156] Line coverage: Real-time calculation of line execution proportion of code, if less than 80%, trigger alarm mechanism, and modify test case parameters, and manually or automatically regenerate test cases;

[0157] Branch coverage: Check whether all if / else paths and switch paths are covered, if the coverage rate is less than 90%, send alarm information to decide whether to regenerate test cases;

[0158] Path coverage: For complex logic, including nested loops and recursion, path combination cases are generated, and coverage alarm threshold is configured as 75%, if less than the threshold, regenerate test cases, and dynamically generate loop / recursive combination cases.

[0159] Step S3.2, branch coverage depth analysis

[0160] For conditional logic (such as if-else, switch), the analysis is as follows:

[0161] Identify uncovered branches: Mark all conditional branches through control flow graph CFG, compare actual execution paths, give a report of unrecognized branches, and display all branch coverage in the report, for example: Branch a (score >= 60) is not covered.

[0162] Generate supplementary test case suggestions: Generate supplementary test case suggestions according to the identified uncovered conditions;

[0163] Boundary condition verification: generate critical test data based on boundary values in conditions (e.g., score >= 90) (score = 89, 90, 91) to ensure that extreme scenarios are not missed;

[0164] Step S3.3, path coverage optimization strategy

[0165] For complex logic, including nested loops and recursion, the analysis is as follows:

[0166] Full path enumeration: generate all theoretical execution paths (e.g., loop 0 times, 1 time, multiple times) through static analysis;

[0167] Symbolic execution combined with fuzz testing: use symbolic execution (e.g., KLEE) to generate inputs that satisfy specific paths; for paths that are not covered, use fuzz query (e.g., AFL) to randomly generate and retain valid use cases;

[0168] Loop invariant inference: automatically infer loop termination conditions (e.g., i < array.length) to generate special data sets that exit early or traverse completely;

[0169] Step S4, feedback optimization

[0170] According to the coverage results and user feedback, dynamically adjust the test cases, generate interactive coverage reports, and the reports support the following functions:

[0171] Hotspot map display: mark the uncovered code lines and covered branches separately (e.g., red mark for uncovered code lines, green mark for covered branches);

[0172] Path tracking: graphically display the execution path (e.g., highlight the actual execution flow in the control flow graph CFG);

[0173] Automatic repair suggestion: directly associate with the integrated development environment IDE, and recommend additional use cases when clicking on the uncovered code;

[0174] Support user manual editing of test cases, and store the optimization results in the historical database for subsequent AI learning.

[0175] The unit test case rapid generation system based on AI large model is used to implement the above method, comprising:

[0176] Code analysis module, responsible for combining with the insertion technology and runtime monitoring, dynamically tracking the execution path, branch direction and variable evolution, capturing exception logs to supplement the static blind area, and building a multi-dimensional code behavior portrait to provide a panoramic view for deep testing;

[0177] The test case generation module is responsible for converting the code analysis result into natural language or structured prompt words, driving the AI model (such as DeepSeek) to generate test schemes covering normal logic, boundary values and abnormal scenarios; automatically matching the test framework of the target language to ensure that the code is ready for use;

[0178] The coverage evaluation module is responsible for realizing fine-grained analysis of code execution paths through static insertion and dynamic tracking, and counting the coverage of lines, branches and paths.

[0179] For conditional logic, the CFG graph is used to identify uncovered branches and generate supplementary test case suggestions, and the boundary values are verified;

[0180] For complex structures, a combination strategy of full path enumeration, symbolic execution and fuzz testing is adopted to automatically infer loop invariants to generate special data sets, ensuring that nested loops and recursive complete traversal or early exit scenarios are covered, and the test sufficiency and defect detection rate are improved;

[0181] The feedback optimization module is responsible for dynamically adjusting the test cases according to the coverage results and user feedback, and generating an interactive coverage report.

[0182] The AI-based large model unit test case rapid generation device includes a memory and a processor; the memory is used to store a computer program, and the processor is used to execute the computer program to realize the method steps described above.

[0183] The computer program is stored on the readable storage medium, and the computer program is executed by the processor to realize the method steps described above.

[0184] The above-described embodiments are only one of the specific embodiments of the present application, and the usual changes and replacements made by those skilled in the art within the scope of the technical solutions of the present application should be included in the protection scope of the present application.< / t>

Claims

1. An AI large model-based unit test case rapid generation method, characterized in that: Comprising the following steps: Step S1, code parsing Step S1.1, static analysis Input the source code to be tested, extract the code structure information through syntax tree parsing technology, identify the function name, parameter type, return value type and modifier; analyze the function parameters and return value, analyze the function call chain, variable dependency and external dependency through control flow graph CFG and data flow graph DFG; The code structure information includes class, method and variable definition; The return value includes basic type, complex object and generic type; Step S1.2, dynamic analysis Through the insertion technology or runtime monitoring, record the function execution path, branch condition and loop times, combine the runtime variable value and exception capture and log information, dynamically supplement the blind area of static analysis, form the panoramic view of code behavior; Step S2, generating test cases Step S2.1, AI large model formatting Convert the code parsing result into natural language description or structured prompt word, adapt to the requirements of AI model; Step S2.2, AI logic reasoning The model generates test cases based on code semantics, including normal scenarios, boundary conditions and exception handling; Boundary conditions include numerical rationality, strings and collection classes; Based on the throws declaration and try-catch block, the exception triggering condition is deduced reversely, and the exception case is simulated; Step S2.3, AI supports multiple languages Automatically match the test framework of the target language to ensure that the generated test code can be directly run; Step S2.4, historical case reuse Retrieve historical test cases through vector retrieval system, preferentially reuse high-coverage case templates, fine-tune the model based on user-modified test cases to improve accuracy; Vector retrieval system: use search library FAISS to build test case vector library, feature vector includes code structure fingerprint, API call sequence and exception mode, similarity threshold is set to 0.85, supports Top-K retrieval; Adaptive adjustment: user-modified cases update the model through contrast learning, establish feedback factors, if the acceptance rate of more than 90% of the case templates is more than 90%, the corresponding template weight is increased by 20%, realizing continuous iterative improvement; Step S3, coverage evaluation Step S3.1, multi-dimensional coverage statistics Statistical basic line coverage, and through static insertion and dynamic tracking technology, carry out fine-grained analysis on code execution path, ensure that test cases fully cover various scenarios, and generate test cases to count code coverage; Step S3.2, branch coverage depth analysis For conditional logic, analyze as follows: Identify uncovered branches: mark all conditional branches through control flow graph CFG, compare actual execution path, give report of unrecognized branches, and display all branch coverage in the report; Generate supplementary test case suggestions: generate supplementary test case suggestions according to the identified uncovered conditions; Boundary condition verification: generate critical test data based on boundary values in conditions to ensure that extreme value scenarios are not missed; Step S3.3, path coverage optimization strategy For complex logic, including nested loops and recursion, analyze as follows: Full path enumeration: generate all theoretical execution paths through static analysis; Symbolic execution combined with fuzz testing: generate inputs that satisfy specific paths using symbolic execution; for un-covered paths, randomly generate and retain valid use cases through fuzz queries; Loop invariant inference: automatically infer loop termination conditions, generate special data sets for early exit or complete traversal; Step S4, feedback optimization According to the coverage results and user feedback, dynamically adjust the test cases, generate interactive coverage reports, and the reports support the following functions: Hotspot map display: mark the uncovered code lines and covered branches respectively; Path tracking: graphically display the execution path; Automatic repair suggestion: directly associate with the integrated development environment (IDE), recommend supplementary use cases when clicking on the uncovered code; Support user manual editing of test cases, and store the optimization results in the historical database for subsequent AI learning. 2.The AI large model-based unit test case rapid generation method of claim 1, wherein: In step S1.1, the static analysis steps are as follows: Step S1.1.1, syntax tree parsing: use ANTLR parser generator tool to build a multi-language parser, convert source code into abstract syntax tree, extract structural information, including class, method and variable definition; For Java code, it can recognize annotations, including @Override and @Test; For Python code, it can parse decorator syntax; Step S1.1.2, type inference system: design type inference algorithm for dynamic language, derive variable type through variable assignment chain and function call context; Step S1.1.3, control flow analysis: use Tarjan algorithm to automatically mark loop structure and unreachable code blocks, generate control flow graph (CFG); perform full path enumeration on switch-case statements to cover all theoretical branches; Step S1.1.4, data flow analysis: when constructing data flow graph (DFG), handle object reference relationship through pointer analysis, track sensitive data flow using taint propagation technology, and identify vulnerability patterns. 3.The AI large model-based unit test case rapid generation method of claim 1, wherein: In step S1.2, the dynamic analysis steps are as follows: Step S1.2.1, instrumentation scheme: use bytecode manipulation tool ASM to implement non-intrusive instrumentation, insert probes at key locations, including branch statements and loop entries, record method call sequence diagram, parameter value distribution statistics and exception throw stack at runtime; Step S1.2.2, runtime monitoring: collect JVM indicators through Java Agent technology, including: Memory object snapshot, used to generate complex object test cases; Thread contention state, used to diagnose concurrency conflict problems; I / O call frequency, used to locate external dependency bottlenecks. 4.The AI large model-based unit test case rapid generation method of claim 1, wherein: In step S2.1, the AI large model formatting process is as follows: Step S2.1.1, function signature standardization: extract function name, parameter list, return value type and access modifier, perform type erasure or retain generic constraints for generics; Step S2.1.2, dependency relationship extraction: identify external dependencies of method calls, mark context factors that affect testing, including global variables and static methods; Step S2.1.3, natural language prompt word generation: use a combination of fixed template filling and dynamic optimization prompts to convert structured data into natural language descriptions; Output natural language description in pre-set format, including function description, parameter constraints, exception scenarios, and external dependency information. Step S2.1.4, structured prompt word: provide configuration documents in JSON and XML formats to adapt to the requirements of fine-tuned special AI models. 5.The AI big model-based unit test case rapid generation method according to claim 4, characterized in that: In step S2.1.3, fixed templates are used to fill in static code logic, and based on the control flow graph (CFG), branch conditions are disassembled, variable constraint rules are defined based on data flow analysis (DFG), and structured data is converted into natural language description. 6.The AI large model-based unit test case rapid generation method of claim 1, wherein: In step S3.1, the multi-dimensional coverage statistical process is as follows: Line coverage: real-time calculation of the line execution percentage of the code, if it is less than 80%, an alarm mechanism is triggered, and the test case parameters are revised, and the test case is automatically or manually regenerated; Branch coverage: check if all if / else paths and switch paths are covered, if the coverage rate is less than 90%, an alarm message is sent; Path coverage: for complex logic, including nested loops and recursion, path combination test cases are generated, and a coverage alarm threshold of 75% is configured, if the threshold is exceeded, the test case is regenerated, and loop / recursive combination test cases are dynamically generated.

7. An AI large model-based unit test case rapid generation system, characterized in that: For implementing the method of any one of claims 1-6, comprising: A code analysis module is responsible for combining instrumentation technology and runtime monitoring, dynamically tracking execution paths, branch directions, and variable evolution, capturing exception logs to supplement static blind spots, and building multi-dimensional code behavior portraits to provide a panoramic view for in-depth testing. A test case generation module is responsible for converting code analysis results into natural language or structured prompt words to drive AI models to generate test plans that cover normal logic, boundary values, and exception scenarios; automatically match the target language test framework to ensure that the code is ready for use; A coverage evaluation module is responsible for implementing fine-grained analysis of code execution paths through static instrumentation and dynamic tracking, and calculating line, branch, and path coverage rates. For conditional logic, based on the CFG graph, un-covered branches are identified and supplementary test case suggestions are generated, and boundary values are verified; For complex structures, use a combination strategy of full path enumeration, symbolic execution, and fuzz testing to automatically infer loop invariants to generate special data sets, ensuring that nested loops and recursive complete traversal or early exit scenarios are covered, and overall test sufficiency and defect detection rate are improved; A feedback optimization module is responsible for dynamically adjusting test cases based on coverage results and user feedback, and generating interactive coverage reports.

8. An AI large model-based unit test case rapid generation device, characterized in that: The memory is used to store a computer program, and the processor is used to execute the computer program to implement the method of any one of claims 1-6.

9. A readable storage medium characterized by: The computer program stored on the readable storage medium is executed by the processor to implement the method of any one of claims 1-6.

Citation Information

Cited By

  • Unit test code generation method, system, equipment and medium

    CN121255659A

  • Industrial controller automatic test system and method based on artificial intelligence

    CN121300335A

  • An artificial intelligence-based industrial controller automatic test system and method

    CN121300335B

  • Intelligent optimization method for unit test coverage rate guided by using control flow

    CN121349900A

  • Big model-based rule generation method, system and equipment and storage medium

    CN121390324A