A test generation method and device based on function intention and hierarchical knowledge base

By constructing a code graph and a hierarchical knowledge base through collaborative modeling, and combining execution feedback information, semantically reasonable and engineering-style consistent unit test cases are generated. This solves the problems of inaccurate test cases and low engineering usability in existing technologies, and improves generation efficiency and automated testing capabilities.

CN122285533APending Publication Date: 2026-06-26BEIHANG UNIV

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
BEIHANG UNIV
Filing Date
2026-05-29
Publication Date
2026-06-26

AI Technical Summary

Technical Problem

Existing technologies in unit test generation suffer from several problems: a disconnect between function intent understanding and test generation task modeling, a single dimension of knowledge base utilization, and a lack of systematic correlation modeling mechanisms between tasks. These issues result in inaccurate semantics of the generated test cases, inconsistent engineering styles, and low engineering usability.

Method used

By constructing a code graph to analyze the local context of the function under test, function intent information is generated. Based on a hierarchical knowledge base, filtering and collaborative modeling are performed, and execution feedback information is combined for iterative correction to generate semantically reasonable unit test cases with consistent engineering style.

Benefits of technology

It significantly improves the semantic accuracy and engineering consistency of test cases, reduces unnecessary computational overhead, increases generation efficiency and compilation pass rate, and enhances the throughput and resource utilization efficiency of automated testing.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN122285533A_ABST
    Figure CN122285533A_ABST
Patent Text Reader

Abstract

This invention relates to the field of software engineering technology, and in particular to a test generation method and apparatus based on function intent and a hierarchical knowledge base. The method includes: acquiring the function under test in the project to be tested; analyzing the local contextual relationships of the function under test by constructing a code graph to generate function intent information; filtering, scheduling, and coordinating the hierarchical knowledge base based on the features of the function under test and the function intent information to obtain reference test knowledge; co-modeling the function intent information with the test case generation task, using the function intent information to guide the test case generation task in conjunction with the reference test knowledge to generate initial unit test cases through constraints and alignment; and iteratively correcting the initial unit test cases based on the execution feedback information of the test cases until the target unit test cases meet the preset test standards and are output. This invention effectively solves the problems of existing technologies by combining semantic understanding of the function under test, hierarchical knowledge scheduling, collaborative modeling generation, and execution feedback repair.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the field of software engineering technology, and in particular to a test generation method and apparatus based on function intent and hierarchical knowledge base. Background Technology

[0002] As software size and complexity increase, unit testing has become a core component in ensuring software quality. However, manually writing test cases is not only costly but also difficult to cover frequently changing code in a timely manner. Therefore, how to automatically generate unit tests has become a key research focus in the field of software engineering.

[0003] Early automated test code generation techniques were primarily based on program analysis and randomized search. Because these methods aimed to achieve path or branch coverage, the generated test code often contained numerous mechanical variables and assertions, lacking an expression of the functional semantics of the function under test. Therefore, the test cases generated by these methods were poorly readable and failed to reflect the true testing intent.

[0004] In recent years, large language models have significantly improved the quality of generated test cases through large-scale pre-training. However, due to the "average" distribution of knowledge in these models, they often lack an understanding of the project's internal coding style and testing standards when applied to specific engineering projects. Because the generation process easily relies on surface pattern matching, test code often differs significantly from the project's existing code in terms of naming conventions and dependency usage, affecting project consistency.

[0005] Furthermore, existing technologies generally treat "function intent understanding" and "test generation" as independent tasks, typically relying solely on local code context for reasoning. Because this fragmented modeling approach makes it difficult to grasp the semantic intent of a function, the generated test cases often only cover superficial syntax, easily leading to unreasonable assertions or semantic deviations from actual requirements.

[0006] Furthermore, existing retrieval enhancement methods often employ a single, flat knowledge base structure, failing to distinguish between cross-project general experience and project-specific specifications. The lack of a hierarchical modeling mechanism for global and domain knowledge results in generated test cases that either overly rely on external examples and become out of touch with project style, or are limited to a finite number of samples within the project, making it difficult to cover complex scenarios. Current fine-tuning methods fail to systematically explore the correlation between intent summaries and test case generation, limiting the accuracy and engineering usability of the generated results.

[0007] In summary, due to the shortcomings of existing technologies in terms of semantic understanding depth, knowledge layering and utilization, and task collaborative modeling, they are not yet able to reliably generate high-quality unit test cases that are semantically reasonable, stylistically consistent, and adapted to specific engineering environments. Summary of the Invention

[0008] (a) Technical problems to be solved

[0009] In view of the above-mentioned shortcomings and deficiencies of the prior art, the present invention provides a test generation method based on function intent and hierarchical knowledge base. It solves the technical problems of the prior art, which is difficult to stably generate unit test cases with accurate semantic expression, consistent engineering style and high engineering usability due to the separation of function intent understanding and test generation task modeling, the single dimension of knowledge base utilization and the lack of systematic association modeling mechanism between tasks.

[0010] (II) Technical Solution

[0011] To achieve the above objectives, the main technical solutions adopted by the present invention include:

[0012] In a first aspect, embodiments of the present invention provide a test generation method based on function intent and a hierarchical knowledge base, comprising:

[0013] Obtain the function under test in the project to be tested, analyze the local context relationship of the function under test by constructing a code graph, and generate function intent information that reflects the functional goals, input and output characteristics and expected behavior of the function under test;

[0014] Based on the characteristics and intent information of the tested function, a hierarchical knowledge base obtained by hierarchically organizing test resources from different sources and applicable scopes is screened, scheduled, and coordinated in an orderly manner to obtain reference test knowledge.

[0015] The function intent information is co-modeled with the test case generation task. The function intent information is used to guide the test parameter initialization, external dependency simulation and assertion logic construction in the test case generation task. The reference test knowledge is combined to perform normative constraints and semantic alignment to generate initial unit test cases.

[0016] Execute initial unit test cases in the test environment of the project under test, and iteratively revise the initial unit test cases based on the obtained execution feedback information until the target unit test cases are output that meet the preset test standards.

[0017] Optionally, the function under test in the project to be tested is obtained, and the local context of the function under test is analyzed by constructing a code graph to generate function intent information that reflects the functional goals, input and output characteristics, and expected behavior of the function under test, including:

[0018] The project under test is analyzed to identify the objects under test. The entity objects in the project under test are used as nodes, and the relationships between entity objects are established as edges. A project-level code graph is constructed to represent the calling logic and dependency topology between entities within the project.

[0019] The function under test is identified as the focus node from the object under test. Taking the focus node as the origin, the adjacent nodes in the project-level code graph are traversed according to the preset subgraph exploration strategy to extract the local subgraph. The analysis scope is dynamically controlled by configuring the subgraph exploration depth. The subgraph exploration strategy is a depth-first or breadth-first traversal strategy along the edges where the relationships are located.

[0020] The function call relationships, parameter passing paths and test context contained in the local subgraph are analyzed to extract and identify contextual information related to the functional goals, input and output characteristics and expected behavior of the function under test.

[0021] The context information is structured using a predefined function intent summary template and mapped to the corresponding slots in the template for further structured organization.

[0022] Based on the results of the structured processing, functional intent information that reflects the functional objectives, input and output characteristics, and expected behavior of the function under test is generated.

[0023] Optionally, based on the characteristics and intent information of the function under test, a hierarchical knowledge base obtained by hierarchically organizing test resources from different sources and applicable scopes is screened, scheduled, and coordinated in an orderly manner to obtain reference test knowledge, including:

[0024] The acquired test resources are identified and logically isolated according to their source attributes and applicable space dimensions, and a layered knowledge base is configured, including a global layer and a domain layer. The global layer is used to store functions outside the project under test, global unit test cases and their corresponding relationships, while the domain layer is used to store functions inside the project under test, domain unit test cases and their corresponding relationships.

[0025] Based on the features of the tested function and the function intent information, multi-dimensional retrieval features including functional semantics, call path and business logic constraints are extracted.

[0026] Using multidimensional retrieval features, semantic space similarity measurement and matching are performed in the global layer and domain layer of the hierarchical knowledge base, respectively, to filter out the corresponding global unit test cases and domain unit test cases;

[0027] Based on the engineering adaptation requirements of the function under test and the project under test, the selected global unit test cases and domain unit test cases are scheduled and coordinated in an orderly manner to obtain reference test knowledge that is aligned with the preset general logical constraints and engineering specifications.

[0028] Optionally, based on the engineering adaptation requirements of the function under test and the project under test, the selected global unit test cases and domain unit test cases are scheduled and coordinated in an orderly manner to obtain reference test knowledge aligned with preset general logical constraints and engineering specifications, including:

[0029] Based on the engineering adaptation requirements of the function under test and the project under test, priority is divided between global unit test cases and domain unit test cases in order to extract engineering style constraint information reflecting the coding style of the project under test, the usage of dependent objects, and the assertion expression form in the domain unit test cases, as well as test logic reference information reflecting the function verification logic and test input construction in the global unit test cases.

[0030] The global unit test cases and the domain unit test cases are respectively structured and parsed to be converted into global general test fragments and domain test fragments, or global general test patterns and domain test patterns;

[0031] Based on the function intent information and the calling relationship of the function under test in the project-level code graph, the global general test fragments and domain test fragments, or the global general test mode and domain test mode are matched and selected to identify the global general test fragments that have a functional semantic alignment relationship with the test logic reference information, and the structure is adapted in combination with the engineering style constraint information.

[0032] The adapted global general test fragments and domain-specific test fragments are collaboratively combined, including functional logic injection and test structure mapping, so as to fill the function verification logic and test input construction in the test logic reference information into the test structure form corresponding to the domain-specific test fragments, thereby obtaining reference test knowledge that is aligned with the preset general logic constraints and engineering specifications.

[0033] Optionally, function intent information is co-modeled with the test case generation task. The function intent information guides the initialization of test parameters, simulation of external dependencies, and construction of assertion logic in the test case generation task. Furthermore, it incorporates reference testing knowledge for normative constraints and semantic alignment to generate initial unit test cases, including:

[0034] The function intent information and the task of generating test cases for the function under test are mapped to a unified semantic space to build a collaborative modeling environment that couples function semantic constraints and test generation logic.

[0035] The test case generation task in the collaborative modeling environment is guided by the function intent information to initialize test parameters for the functional goals represented in the function intent information, build dependency simulation logic for external components for expected behavior, and build assertion logic for verifying the running results based on input and output characteristics.

[0036] By injecting reference test knowledge into the collaborative modeling environment, function intent information and reference test knowledge jointly drive joint inference. Initial unit test cases are generated by normatively constraining and semantically aligning the initial test parameters, external component dependency simulation logic, and assertion logic.

[0037] Optionally, after injecting reference test knowledge into the collaborative modeling environment, the following training optimization is performed based on the joint task loss:

[0038] Calculate the first loss value generated by the generation of function intent information to characterize the semantic extraction accuracy of the collaborative modeling environment for the function's functional goals and expected behavior;

[0039] Calculate the second loss value corresponding to the unit test case generation task to characterize the degree of alignment between the generated initial unit test cases and the function intent and reference test knowledge;

[0040] The first loss value and the second loss value are combined and calculated to construct the joint task loss;

[0041] Backpropagation is performed using joint task loss to iteratively update the model parameters involved in the collaborative modeling environment, thereby achieving synchronous optimization of intent understanding and test generation capabilities.

[0042] Optionally, initial unit test cases are executed in the test environment of the project under test, and the initial unit test cases are iteratively revised based on the obtained execution feedback information until the target unit test cases are output that meet the preset test criteria, including:

[0043] Configure an isolated test sandbox for executing initial unit test cases based on the dependency environment and resource configuration of the project under test;

[0044] Within the isolated test sandbox, the initial unit test cases are subjected to step-by-step verification in the order of compilation verification first and runtime verification last.

[0045] The initial unit test cases are compiled. If the compilation fails, the resulting compilation error information is used as the execution feedback information.

[0046] After the compilation is deemed successful, the function execution of the initial unit test cases is triggered. If an exception or assertion failure that does not conform to the preset test standards is found during the execution process, the generated runtime exception information or assertion failure information will be used as execution feedback information.

[0047] The execution feedback information is reintroduced into the collaborative modeling environment as a corrective constraint to resolve the syntax errors and semantic biases represented in the execution feedback information;

[0048] Within the collaborative modeling environment, the test input construction, dependency invocation methods, and assertion design involved in the initial unit test cases are adjusted based on the correction constraints to eliminate syntax errors and semantic deviations. The test generation, compilation, and execution verification process is executed cyclically until the initial unit test cases pass the preset test standards and the target unit test cases are output.

[0049] Secondly, embodiments of the present invention provide a test generation system based on function intents and a hierarchical knowledge base, comprising:

[0050] The Function Analysis and Intent Summary module is used to obtain the functions under test in the project under test, analyze the local context of the functions under test by constructing code graphs, and generate function intent information that reflects the functional goals, input and output characteristics and expected behavior of the functions under test.

[0051] The unit test case knowledge base management module is used to filter, schedule, and coordinate the hierarchical knowledge base obtained by organizing test resources from different sources and applicable scopes in an orderly manner based on the characteristics and intent information of the tested function, so as to obtain reference test knowledge.

[0052] The unit test case generation module is used to collaboratively model function intent information with test case generation tasks. It uses function intent information to guide the initialization of test parameters, simulation of external dependencies, and construction of assertion logic in the test case generation task, and combines reference test knowledge to perform normative constraints and semantic alignment to generate initial unit test cases.

[0053] The test case execution and feedback repair module is used to execute initial unit test cases in the test environment of the project under test, and iteratively correct the initial unit test cases based on the obtained execution feedback information until the target unit test cases are output in accordance with the preset test standards.

[0054] Thirdly, embodiments of the present invention provide a unit test generation device based on function intents and a hierarchical knowledge base, comprising: at least one processor; and a memory communicatively connected to the at least one processor; wherein the memory stores instructions executable by the at least one processor, the instructions being executed by the at least one processor to enable the at least one processor to execute the test generation method based on function intents and a hierarchical knowledge base as described above.

[0055] Fourthly, embodiments of the present invention provide a computer-readable storage medium storing computer-executable instructions, which, when executed by a processor, implement the test generation method based on function intent and hierarchical knowledge base as described above.

[0056] (III) Beneficial Effects

[0057] The beneficial effects of this invention are: by organically combining semantic understanding of the function under test, hierarchical knowledge scheduling, collaborative modeling generation, and execution feedback repair, this invention effectively solves the technical problems of low engineering usability caused by the fragmented modeling logic and single knowledge dimension in the prior art.

[0058] First, this invention constructs a code graph to analyze the local context of the function under test and extract the function intent information. This invention achieves a deeper semantic characterization than the surface code structure, enabling the generation process to directly revolve around the functional goal, input and output characteristics, and expected behavior. Compared with traditional path coverage or code surface structure testing methods, this invention significantly improves the semantic accuracy of the generated test cases as executable instruction sequences and reduces the invalid computational overhead caused by the computer processing logically conflicting code.

[0059] At the same time, by utilizing a hierarchical knowledge base to systematically coordinate general experience across projects with specific practices within a project, this invention can balance general verification patterns with project-specific coding standards. This makes the generated test cases more aligned with the actual needs of the target project in terms of assertion expression and engineering consistency, overcoming the limitations of existing methods that struggle to balance generality and project characteristics.

[0060] Furthermore, by co-modeling the function intent with the generation task, this invention ensures that the function semantic constraints continuously guide the underlying logic synthesis process, such as test parameter initialization, dependency simulation, and assertion construction. This ensures that the generated test code is highly consistent with the actual test intent, effectively avoiding logical deviations caused by task fragmentation, and thus significantly improving the logic convergence speed and generation efficiency of computers in the automated production of software components.

[0061] Furthermore, the iterative self-healing mechanism based on execution feedback established in this invention automatically verifies initial test cases in a real engineering environment. By monitoring underlying system interrupt signals, compilation errors, and runtime exception feedback in real time, this invention transforms the hardware execution state into dynamically corrective constraints, driving the system to perform targeted self-healing repairs on the test instruction flow. Compared to existing one-time generation strategies, this closed-loop verification mode significantly improves the compilation pass rate and runtime stability of test cases, and greatly reduces the waste of computing resources during repeated compilation and resetting of the execution environment.

[0062] Ultimately, through the synergistic effect of the above mechanisms, this invention can stably produce target unit test cases that are semantically reasonable, structurally standardized, and highly maintainable. This invention not only achieves precise control over the internal execution state of the computer, but also significantly improves the automated throughput and resource utilization efficiency of the computer system when handling complex software quality assurance tasks, providing a feasible technical means for automated testing in large-scale software engineering environments. Attached Figure Description

[0063] Figure 1 This is a schematic diagram of the overall process of the method provided in the embodiments of the present invention;

[0064] Figure 2This is a schematic diagram illustrating the specific process of step S1 of the method provided in this embodiment of the invention;

[0065] Figure 3 Function analysis and intent summary architecture diagram of the method provided in the embodiments of the present invention;

[0066] Figure 4 This is a detailed flowchart illustrating step S2 of the method provided in this embodiment of the invention;

[0067] Figure 5 A functional architecture diagram of the unit test case knowledge base management system for the method provided in this embodiment of the invention;

[0068] Figure 6 This is a schematic flowchart illustrating step S24 of the method provided in this embodiment of the invention.

[0069] Figure 7 This is a detailed flowchart illustrating step S3 of the method provided in this embodiment of the invention;

[0070] Figure 8 A training and inference architecture diagram for the unit test case generation module of the method provided in the embodiments of the present invention;

[0071] Figure 9 This is a detailed flowchart illustrating step S31 of the method provided in this embodiment of the invention;

[0072] Figure 10 This is a detailed flowchart illustrating step S4 of the method provided in this embodiment of the invention;

[0073] Figure 11 A diagram illustrating the test execution and feedback repair architecture of the method provided in this embodiment of the invention;

[0074] Figure 12 This is a schematic diagram of the overall composition of the system provided in an embodiment of the present invention. Detailed Implementation

[0075] To better explain and facilitate understanding of the present invention, the present invention will be described in detail below with reference to the accompanying drawings and specific embodiments.

[0076] like Figure 1As shown in the figure, an embodiment of the present invention proposes a test generation method based on function intent and a hierarchical knowledge base, comprising: acquiring the function under test in the project to be tested; analyzing the local context relationship of the function under test by constructing a code graph to generate function intent information reflecting the functional goals, input and output characteristics, and expected behavior of the function under test; based on the characteristics of the function under test and the function intent information, filtering, scheduling, and orderly coordinating the hierarchical knowledge base obtained by hierarchically organizing test resources from different sources and applicable scopes to obtain reference test knowledge; co-modeling the function intent information and the test case generation task, using the function intent information to guide the test parameter initialization, external dependency simulation, and assertion logic construction in the test case generation task, and combining the reference test knowledge to perform normative constraints and semantic alignment to generate initial unit test cases; executing the initial unit test cases in the test environment of the project to be tested, and iteratively correcting the initial unit test cases based on the obtained execution feedback information until the preset test standards are met and the target unit test cases are output.

[0077] This invention effectively solves the technical problems of low engineering usability caused by the fragmented modeling logic and single knowledge dimension in the prior art by organically combining semantic understanding of the function under test, hierarchical knowledge scheduling, collaborative modeling generation and execution feedback repair.

[0078] First, this invention constructs a code graph to analyze the local context of the function under test and extract the function intent information. This invention achieves a deeper semantic characterization than the surface code structure, enabling the generation process to directly revolve around the functional goal, input and output characteristics, and expected behavior. Compared with traditional path coverage or code surface structure testing methods, this invention significantly improves the semantic accuracy of the generated test cases as executable instruction sequences and reduces the invalid computational overhead caused by the computer processing logically conflicting code.

[0079] At the same time, by utilizing a hierarchical knowledge base to systematically coordinate general experience across projects with specific practices within a project, this invention can balance general verification patterns with project-specific coding standards. This makes the generated test cases more aligned with the actual needs of the target project in terms of assertion expression and engineering consistency, overcoming the limitations of existing methods that struggle to balance generality and project characteristics.

[0080] Furthermore, by co-modeling the function intent with the generation task, this invention ensures that the function semantic constraints continuously guide the underlying logic synthesis process, such as test parameter initialization, dependency simulation, and assertion construction. This ensures that the generated test code is highly consistent with the actual test intent, effectively avoiding logical deviations caused by task fragmentation, and thus significantly improving the logic convergence speed and generation efficiency of computers in the automated production of software components.

[0081] Furthermore, the iterative self-healing mechanism based on execution feedback established in this invention automatically verifies initial test cases in a real engineering environment. By monitoring underlying system interrupt signals, compilation errors, and runtime exception feedback in real time, this invention transforms the hardware execution state into dynamically corrective constraints, driving the system to perform targeted self-healing repairs on the test instruction flow. Compared to existing one-time generation strategies, this closed-loop verification mode significantly improves the compilation pass rate and runtime stability of test cases, and greatly reduces the waste of computing resources during repeated compilation and resetting of the execution environment.

[0082] Ultimately, through the synergistic effect of the above mechanisms, this invention can stably produce target unit test cases that are semantically reasonable, structurally standardized, and highly maintainable. This invention not only achieves precise control over the internal execution state of the computer, but also significantly improves the automated throughput and resource utilization efficiency of the computer system when handling complex software quality assurance tasks, providing a feasible technical means for automated testing in large-scale software engineering environments.

[0083] Ultimately, through the synergistic effect of the above mechanisms, this invention can stably produce target unit test cases that are semantically reasonable, structurally standardized, and highly maintainable, providing a reliable technical solution for the in-depth application of automated testing in complex software development and maintenance scenarios.

[0084] To better understand the above technical solutions, exemplary embodiments of the present invention will be described in more detail below with reference to the accompanying drawings. Although exemplary embodiments of the present invention are shown in the drawings, it should be understood that the present invention can be implemented in various forms and should not be limited to the embodiments set forth herein. Rather, these embodiments are provided so that the present invention can be understood more clearly and thoroughly, and that the scope of the present invention can be fully conveyed to those skilled in the art.

[0085] Specifically, embodiments of the present invention provide a test generation method based on function intent and a hierarchical knowledge base, including:

[0086] S1. Obtain the function under test in the project under test, and analyze the local context of the function under test by constructing a code graph to generate function intent information that reflects the functional goals, input and output characteristics, and expected behavior of the function under test. This step is used to perform structured analysis on the function under test in the project under test and form an intent description that can reflect the functional goals and behavioral semantics of the function, serving as the semantic basis and interpretability constraints for the subsequent test generation process.

[0087] Furthermore, such as Figure 2 and Figure 3 As shown, step S1 includes:

[0088] S11. The test project is parsed to identify the test objects. Entity objects within the test project are defined as nodes, and the relationships between these entities are established as edges, constructing a project-level code graph representing the inter-entity call logic and dependency topology within the project. The test project, stored in computer memory, is parsed to identify the test objects. Entity objects such as functions, classes, variables, interfaces, and existing test cases within the project are defined as nodes in the graph, and the call relationships, dependencies, inheritance relationships, and data interaction relationships between these entities are established as edges, thereby constructing a project-level code graph representing the inter-entity call logic, data flow, and dependency topology within the project. This graph provides global physical structural support for subsequent semantic analysis.

[0089] S12. Identify the function under test as the focus node from the object under test. Using the focus node as the origin, traverse adjacent nodes in the project-level code graph according to a preset subgraph exploration strategy to extract local subgraphs. During this process, configurable subgraph exploration depth parameters enable dynamic control of the analysis scope, achieving an optimal balance between analysis accuracy and computational overhead, effectively filtering redundant global information and focusing on key dependency logic. The subgraph exploration strategy is either depth-first or breadth-first traversal along the edges containing the relationships.

[0090] S13. Analyze the function call relationships, parameter passing paths, and test context contained in the local subgraph, extracting and identifying contextual information related to the functional goals, input / output characteristics, and expected behavior of the function under test. This process not only focuses on the definition of the function under test itself, but also includes its externally dependent calling protocols, internal private state change logic, and exception triggering boundaries, thereby identifying deep contextual information reflecting the function's functional goals, input / output characteristics, and expected behavior, thus achieving a preliminary transformation from the physical structure of the code to logical semantic features.

[0091] S14. The context information is structured and mapped to the corresponding slots in the template using a pre-defined function intent summary template. This template standardizes the expression of intent, accurately mapping the analyzed feature information to the corresponding semantic slots. This process ensures that the generated intent summary clearly and consistently describes the function's functional goals, input / output characteristics, and expected behavior, significantly improving its engineering readability and semantic consistency, and avoiding ambiguity caused by natural language descriptions.

[0092] S15. Based on the results of the structured processing, generate function intent information reflecting the functional goals, input / output characteristics, and expected behavior of the function under test. Based on the results of the structured processing and template mapping, generate the final function intent information. This intent information achieves automatic extraction from project-level code structure to function-level semantic intent, providing stable and interpretable semantic constraints for the subsequent unit test case generation module, ensuring that the generated test cases can closely follow the actual logic of the function under test.

[0093] S2. Based on the characteristics and intent information of the function under test, the hierarchical knowledge base obtained by hierarchically organizing test resources from different sources and applicable scopes is screened, scheduled and coordinated in an orderly manner to obtain reference test knowledge.

[0094] Furthermore, such as Figure 4 and Figure 5 As shown, step S2 includes:

[0095] S21. The acquired test resources are identified and logically isolated according to their source attributes and applicable scope, and a layered knowledge base containing a global layer and a domain-specific layer is configured. This step adopts a layered knowledge base structure, dividing test case knowledge into a global layer and a domain-specific layer according to their source and applicable scope to support the differentiated utilization of testing experience in different engineering scenarios. The global layer stores functions from outside the project under test, global unit test cases, and the correspondence between functions from outside the project under test and global unit test cases. Its content reflects common software design patterns, common interface usage methods, and universal test verification logic, providing cross-project basic testing experience support for test generation. The domain-specific layer stores the correspondence between functions extracted from the project under test, actually written and maintained by developers, and domain-specific unit test cases. This layer of knowledge accurately reflects the coding standards, testing styles, and specific business logic constraints of the target project, ensuring that the generated results have high engineering adaptability.

[0096] S22. Based on the features and intent information of the function under test, extract multi-dimensional retrieval features including functional semantics, call paths, and business logic constraints. The features of the function under test refer to a set of basic information that describes the structural attributes, behavioral semantics, and contextual relationships of the function within the project, used to characterize the function's functional form and dependent environment in the program. The features of the function under test include at least one or more of the following aspects:

[0097] (1) Function structure features, including function name, function signature, input parameter type and number, return value type and exception throwing information, used to describe the function interface structure;

[0098] (2) Function call relationship characteristics, including other methods called inside the function, dependent classes or modules, and possible call paths, to reflect the function's dependency relationship in the system;

[0099] (3) Project context features, including the class, module or package path where the function is located, as well as its hierarchical structure and business component in the project, which are used to describe the organizational position of the function in the project;

[0100] (4) Control and data processing characteristics, including the main control structure, data processing method and key operation type inside the function, which are used to reflect the basic execution logic of the function.

[0101] Based on the aforementioned features and function intent information of the currently tested function, multi-dimensional retrieval features, including functional semantics, call paths, and business logic constraints, are extracted through semantic mapping and feature fusion techniques. These features constitute a retrieval operator for precise positioning in a high-dimensional knowledge space, ensuring that highly relevant knowledge items can be identified from a massive knowledge base.

[0102] S23. Using multi-dimensional retrieval features, semantic space similarity measurement and matching are performed in both the global and domain-specific layers of the hierarchical knowledge base to filter out corresponding global unit test cases and domain-specific unit test cases. Concurrent or sequential retrieval and matching are performed in both the global and domain-specific layers of the hierarchical knowledge base using multi-dimensional retrieval features. By calculating the similarity between retrieval features and knowledge base entries, corresponding global unit test cases (from the global layer) and domain-specific unit test cases (from the domain-specific layer) are filtered out. This process enables the simultaneous acquisition of cross-project basic experience and project-specific practices.

[0103] S24. Based on the engineering adaptation requirements of the function under test and the project under test, the selected global unit test cases and domain unit test cases are scheduled and coordinated in an orderly manner to obtain reference test knowledge that aligns with general logical constraints and specific engineering specifications.

[0104] Furthermore, such as Figure 6 As shown, step S24 includes:

[0105] S241. Based on the engineering adaptation requirements of the function under test and the project under test, priority is assigned to global unit test cases and domain-specific unit test cases to extract engineering style constraint information reflecting the coding style of the project under test, the usage of dependent objects, and the assertion expression form from the domain-specific unit test cases, and test logic reference information reflecting the function's functional verification logic and test input construction from the global unit test cases. First, priority is assigned to the two types of test cases according to the engineering adaptation requirements. Specifically, for the domain-specific unit test cases selected at the domain level, the test structure information reflecting the target project's coding style, the usage of dependent objects, and the assertion expression form is extracted first and used as engineering style constraints in the test generation process; for the global unit test cases selected at the global level, the test patterns and assertion relationships reflecting the function's functional verification logic are extracted as references for test logic construction.

[0106] S242. Perform structured parsing on global unit test cases and domain-specific unit test cases respectively, to convert them into globally common test fragments and domain-specific test fragments, or globally common test patterns and domain-specific test patterns. Specifically, this includes structured parsing of the test input construction methods, function call paths, dependency object usage methods, and assertion expressions in the two types of test examples, converting them into structured test fragments or test patterns that can be used for test generation, so as to facilitate subsequent unified scheduling and combination.

[0107] S243. Based on the function intent information and the call relationship between the function under test and the project-level code graph, match and select globally common test segments and domain-specific test segments, or globally common test patterns and domain-specific test patterns. Identify globally common test segments that have functional semantic alignment with the test logic reference information, and perform structural adaptation based on engineering style constraints. Specifically, match and select the parsed test segments based on the functional semantics and call relationship of the function under test. In this process, prioritize the selection of common test segments that reflect the core functional verification logic of the function under test. Simultaneously, adapt the test structure by considering the dependency call methods, naming conventions, and assertion expressions in the domain-specific test segments that are consistent with the target project.

[0108] S244. The adapted global general test fragments and domain-specific test fragments are collaboratively combined, including functional logic injection and test structure mapping. This involves filling the function verification logic and test input construction from the test logic reference information into the corresponding test structure form of the domain-specific test fragments, thereby obtaining reference test knowledge aligned with preset general logical constraints and engineering specifications. The adapted general test fragments and domain-specific test fragments are collaboratively combined to form unified reference test knowledge. Global unit test cases primarily provide logical constraints and test input construction for function verification, while domain-specific unit test cases primarily provide test structure forms that conform to the target project's engineering specifications. Through this method, the test logic and engineering specifications are collaboratively integrated, thereby generating reference test knowledge that can both verify function semantics and maintain consistency with the target project's test code style.

[0109] S3. Co-modeling is performed between function intent information and the test case generation task. The function intent information guides the initialization of test parameters, simulation of external dependencies, and construction of assertion logic within the test case generation task. It also incorporates reference test knowledge for normative constraints and semantic alignment, generating initial unit test cases. This step is the core execution stage for test generation. The unit test case generation module, as a crucial hub connecting semantic understanding, knowledge utilization, and execution verification, aims to automatically construct unit test cases for the function under test based on function semantic constraints and test knowledge support.

[0110] Furthermore, such as Figure 7 and Figure 8 Step S3 includes:

[0111] S31. Function intent information and the task of generating test cases for the function under test are mapped to a unified semantic space to construct a collaborative modeling environment that couples function semantic constraints with test generation logic. During execution, both function intent information and layered test knowledge are received simultaneously, serving as unified semantic and structural constraints. Furthermore, function intent information and unit test case generation tasks are mapped to a unified semantic space. Through a multimodal feature fusion mechanism, a collaborative modeling environment capable of deep coupling between function semantic constraints and test generation logic is constructed. This environment ensures that the generation task no longer relies solely on surface-level syntactic information but is built upon an understanding of the underlying behavioral logic of the function under test.

[0112] Furthermore, such as Figure 9 As shown, after injecting reference test knowledge into the collaborative modeling environment, the following training optimization is performed based on the task joint loss to achieve the synchronous evolution of intent understanding ability and test generation ability:

[0113] S311. Calculate the first loss value generated by the generation of function intent information to characterize the semantic extraction accuracy of the collaborative modeling environment for the function's functional goals and expected behavior.

[0114] S312. Calculate the second loss value corresponding to the unit test case generation task to characterize the degree of alignment between the generated initial unit test cases and the function intent and reference test knowledge.

[0115] S313. Combine the first loss value and the second loss value to calculate the joint task loss.

[0116] S314. Utilize joint task loss to perform backpropagation and iteratively update the model parameters involved in the collaborative modeling environment to achieve synchronous optimization of intent understanding capability and test generation capability.

[0117] S32. The test case generation task in the collaborative modeling environment is guided by function intent information. This involves initializing test parameters for the functional goals represented in the function intent information, constructing dependency simulation logic for external components based on expected behavior, and building assertion logic for verifying the execution results based on input and output characteristics. During test generation, code generation is not performed in isolation; instead, function intent information and the test generation task are collaboratively modeled. Then, the function intent information is used as a semantic guidance operator to guide the generation task in the collaborative modeling environment. Specifically, automated initialization of test parameters is performed for the functional goals in the function intent information, dependency simulation logic for external components is constructed for expected behavior, and assertion logic for verifying the execution results is built based on input and output characteristics. This ensures that the test input construction, dependency invocation methods, and assertion design are all consistent with the function intent.

[0118] S33. Inject reference test knowledge into the collaborative modeling environment, and jointly drive joint reasoning based on function intent information and reference test knowledge. By guiding, constraining and aligning the initial test parameters, external component dependency simulation logic and assertion logic, initial unit test cases are generated.

[0119] After the collaborative modeling environment is built, the process of injecting reference test knowledge and generating initial unit test cases is executed. Considering the relevant test examples retrieved from the hierarchical knowledge base, mandatory normative constraints can be imposed on the overall organization structure of the test code, variable naming conventions, and assertion writing style. Driven by both function semantic constraints and reference test knowledge, this invention performs end-to-end joint inference within the collaborative modeling environment. During this inference process, function intent information is used to guide the generation of task-aligned functional goals, and based on reference test knowledge, normative constraints and semantic alignment are applied to the initialized test parameters, external component dependency simulation logic, and assertion logic, achieving deep coupling in both physical structure and logical semantics.

[0120] Compared to traditional methods, this joint generation approach effectively avoids the semantic bias issues that arise from generating test cases solely based on function signatures or surface-level syntax information. Driven by both function semantic constraints and reference test knowledge, end-to-end joint inference is performed within a collaborative modeling environment. This process achieves deep alignment in both physical structure and logical semantics by combining and semantically refactoring the initial test parameters, external component dependency simulation logic, and assertion logic. The final output is initial unit test cases that perfectly match the function's intended functional goals, expected behavior, and pre-defined project engineering specifications, thus completing the transformation from abstract intent to concrete engineering implementation.

[0121] S4. Execute initial unit test cases in the test environment of the project under test, and iteratively revise the initial unit test cases based on the obtained execution feedback information until the target unit test cases are output that meet the preset test standards. This step aims to verify and optimize the generated initial unit test cases and is an important part of realizing a closed-loop process from generation, verification to revision. By constructing an iterative self-repair mechanism based on execution feedback, this invention can effectively improve the stability, reliability, and engineering usability of test cases, and avoid the uncontrollable failure problems caused by a one-time generation strategy.

[0122] Furthermore, such as Figure 10 and Figure 11 As shown, step S4 includes:

[0123] S41. Based on the dependencies and resource configuration of the project under test, configure an isolated test sandbox for executing initial unit test cases. This process involves independently encapsulating and automatically integrating physical resources and software dependencies at the operating system level to ensure that the test code is executable and secure in a real engineering environment, without interfering with the normal operation of the host system.

[0124] S42. Within the isolated test sandbox, perform step-by-step verification on the initial unit test cases in the order of compilation verification first, followed by runtime verification. This progressive verification logic aims to prioritize solving the underlying physical construction problems before addressing the deeper logical semantic problems, thereby achieving a balance between verification accuracy and repair convergence speed.

[0125] S43. Perform compilation processing on the initial unit test cases. If the compilation fails, the generated compilation error information will be used as the execution feedback information.

[0126] S44. After the compilation is deemed successful, the function execution of the initial unit test case is triggered. If an exception or assertion failure that does not conform to the preset test standard is found during the execution process, the generated runtime exception information or assertion failure information will be used as execution feedback information.

[0127] S45. The execution feedback information is reintroduced into the collaborative modeling environment as a corrective constraint to analyze the syntax errors and semantic biases represented in the execution feedback information. This execution feedback information is then reintroduced into the collaborative modeling environment as a dynamic corrective constraint. Through structured analysis of the feedback information, the syntax error features and semantic bias features represented within it are analyzed and extracted, transforming them into negative feedback operators recognizable by the generative model, serving as an important basis for correcting test cases.

[0128] S46. Within the collaborative modeling environment, based on corrective constraints, targeted adjustments are made to the test input construction, dependency invocation methods, and assertion design involved in the initial unit test cases to eliminate syntax errors and semantic deviations. The test generation, compilation, and execution verification processes are then iteratively executed until the initial unit test cases pass the preset test standards, at which point the target unit test cases are output. This feedback and correction process can be repeated in multiple iterations until the generated test cases can stably pass test execution or meet the preset quality requirements.

[0129] Through the aforementioned automatic execution and feedback repair mechanism, this invention forms a closed-loop process from generation, verification to correction in the unit test generation process, effectively improving the stability, reliability and engineering usability of generated test cases, and enabling automatically generated unit tests to better meet the usage needs in actual software development and maintenance scenarios.

[0130] Furthermore, this invention also provides a test generation system based on function intent and a hierarchical knowledge base, comprising: a function analysis and intent summary module, used to obtain the function under test in the project under test, analyze the local context relationship of the function under test by constructing a code graph, and generate function intent information reflecting the functional goals, input and output characteristics, and expected behavior of the function under test; a unit test case knowledge base management module, used to filter, schedule, and coordinate hierarchical knowledge bases obtained by hierarchically organizing test resources from different sources and applicable scopes based on the characteristics of the function under test and the function intent information, and obtain reference test knowledge; a unit test case generation module, used to co-model the function intent information and the test case generation task, use the function intent information to guide the initialization of test parameters, simulation of external dependencies, and construction of assertion logic in the test case generation task, and combine the reference test knowledge to perform normative constraints and semantic alignment, and generate initial unit test cases; and a test case execution and feedback repair module, used to execute the initial unit test cases in the test environment of the project under test, and iteratively correct the initial unit test cases based on the obtained execution feedback information until the preset test standards are met and the target unit test cases are output.

[0131] refer to Figure 12 The technical solution of this invention is implemented with a systematic structure. The overall system consists of a function analysis and intent summary module, a unit test case knowledge base management module, a unit test case generation module, and a test case execution and feedback repair module. The modules work together through data flow and control flow to form a complete closed loop for automatic generation and optimization of unit tests. The system first receives the code of the function under test from the project under test through the function analysis and intent summary module, parses its structural information and contextual relationships, forms an intent representation of the function's functional goals and expected behavior, and passes this intent information as a unified semantic constraint to the subsequent modules.

[0132] The unit test case knowledge base management module is used for unified storage and scheduling of test-related knowledge. Internally, it organizes test resources hierarchically according to knowledge source and applicable scope, and provides the unit test case generation module with test examples and verification modes related to the function under test. After receiving function intent information, the code of the function under test, and knowledge support from the test knowledge management module, the unit test case generation module comprehensively constructs unit test cases that meet the test objectives and outputs the initially generated test code. The test case execution and feedback repair module is responsible for automatically compiling and executing the generated test cases, collecting exception information and failure results generated during execution, and returning relevant feedback to the unit test case generation module to drive the correction and optimization of test cases. This multi-round interaction gradually improves the executability and stability of the generated tests.

[0133] Meanwhile, embodiments of the present invention provide a unit test generation device based on function intents and a hierarchical knowledge base, comprising: at least one processor; and a memory communicatively connected to the at least one processor; wherein the memory stores instructions executable by the at least one processor, the instructions being executed by the at least one processor to enable the at least one processor to execute the unit test generation method based on function intents and a hierarchical knowledge base as described above.

[0134] Furthermore, embodiments of the present invention provide a computer-readable storage medium storing computer-executable instructions, which, when executed by a processor, implement the unit test generation method based on function intent and hierarchical knowledge base as described above.

[0135] In summary, this invention provides a test generation method, system, device, and medium based on function intent and hierarchical knowledge base. By organically combining semantic understanding of the function under test, hierarchical knowledge scheduling, collaborative modeling generation, and execution feedback repair, this invention constructs an end-to-end closed-loop automated test generation system for software engineering scenarios.

[0136] Specifically, this invention first constructs a code graph of the project under test, models functions, classes and their calling dependencies as a structured semantic network, and performs configurable depth subgraph exploration and semantic extraction centered on the focus function. Combined with a standardized intent summary template, it forms a structured intent description of the function's functional goals and expected behavior, thereby providing stable and interpretable semantic constraints for the subsequent generation process.

[0137] Building upon this foundation, this invention introduces a layered unit test case knowledge base management mechanism. It divides knowledge resources into a global layer storing cross-project general design patterns and verification logic, and a domain-specific layer storing the target project's internal coding standards, testing styles, and domain characteristics. Through the orderly retrieval and fusion of these two layers of knowledge, this invention can balance general verification patterns with project-specific business logic. This ensures that the generated test cases, while maintaining general correctness, are more closely aligned with the actual engineering environment of the target project, effectively improving code readability, maintainability, and reusability.

[0138] Furthermore, in the model training and inference stages, this invention performs joint modeling and collaborative optimization of the function intent summarization task and the unit test case generation task, so that the model can be subject to the dual constraints of function semantics and hierarchical knowledge during the generation process. This guides the construction of test inputs, dependency calls and assertion design to be strictly centered around the real functional semantics, effectively avoiding the semantic bias problem caused by relying only on the surface code structure or random exploration.

[0139] Furthermore, after test cases are generated, the generated unit test code is automatically executed, and execution feedback information such as compilation errors, runtime exceptions, and assertion failures is collected in real time. By reintroducing the above feedback as corrective constraints into the test generation process, this invention constructs an iterative self-repair mechanism based on execution feedback. Through the cyclical processing flow of generation, execution, feedback, and repair, test cases gradually eliminate syntax errors and semantic deviations while maintaining the existing effective test structure. This significantly improves the executability, stability, and engineering usability of automatically generated unit tests, and effectively avoids the uncontrollable failure problem caused by a one-time generation strategy.

[0140] Compared with existing technologies, this invention exhibits significant technical advantages. First, it provides a clearer and more systematic semantic understanding of the function under test. Compared to existing technologies that rely on multi-dependency path coverage or code surface structures, this invention uses intent-summarized modeling to directly revolve the generation process around the function's functional goals and expected behavior, which is more conducive to accurately characterizing semantics and significantly improves the clarity and understandability of test cases. Second, the joint modeling approach of this invention demonstrates stronger consistency, solving the problem of lack of coordination between function analysis and test generation in existing technologies. This allows function semantic information to continuously exert a constraining effect throughout the process, more effectively preventing test cases from deviating from the true testing intent. Third, in terms of engineering adaptability, this invention performs better. By managing cross-project general experience and project-specific test assets through a layered knowledge base, it overcomes the limitations of existing methods that struggle to balance generality and project characteristics, making the generated test cases more aligned with the target project's needs in terms of coding standards, assertion expressions, and engineering consistency.

[0141] More importantly, due to the introduction of a continuous verification and feedback repair mechanism, the test cases generated by this invention have a higher executability rate and stronger stability, significantly reducing the probability of generating unexecutable tests. This provides a comprehensive and practical technical solution for stably generating semantically reasonable, structurally standardized, and highly engineering-usable unit test cases in complex software development and maintenance scenarios.

[0142] Those skilled in the art will understand that embodiments of the present invention can be provided as methods, systems, or computer program products. Therefore, the present invention can take the form of a completely hardware embodiment, a completely software embodiment, or an embodiment combining software and hardware aspects. Furthermore, the present invention can take the form of a computer program product embodied on one or more computer-usable storage media (including, but not limited to, disk storage, CD-ROM, optical storage, etc.) containing computer-usable program code.

[0143] This invention is described with reference to flowchart illustrations and / or block diagrams of methods, apparatus (systems), and computer program products according to embodiments of the invention. It will be understood that each block of the flowchart illustrations and / or block diagrams, as well as combinations of blocks in the flowchart illustrations and / or block diagrams, can be implemented by computer program instructions.

[0144] It should be noted that any reference numerals placed between parentheses in the claims should not be construed as limiting the claims. The word "comprising" does not exclude the presence of components or steps not listed in the claims. The word "a" or "an" preceding a component does not exclude the presence of a plurality of such components. The invention can be implemented by means of hardware comprising several different components and by means of a suitably programmed computer. In claims that enumerate several means, several of these means may be embodied by the same hardware. The use of the terms first, second, third, etc., is merely for convenience of expression and does not indicate any order. These terms can be understood as part of the component names.

[0145] Furthermore, it should be noted that in the description of this specification, the terms "one embodiment," "some embodiments," "embodiment," "example," "specific example," or "some examples," etc., refer to specific features, structures, materials, or characteristics described in connection with that embodiment or example, which are included in at least one embodiment or example of the present invention. In this specification, the illustrative expressions of the above terms do not necessarily refer to the same embodiment or example. Moreover, the specific features, structures, materials, or characteristics described may be combined in any suitable manner in one or more embodiments or examples. Furthermore, without contradiction, those skilled in the art can combine and integrate the different embodiments or examples described in this specification, as well as the features of different embodiments or examples.

[0146] Although preferred embodiments of the invention have been described, those skilled in the art, upon learning the basic inventive concept, can make other changes and modifications to these embodiments. Therefore, the claims should be interpreted to include both the preferred embodiments and all changes and modifications falling within the scope of the invention.

[0147] Obviously, those skilled in the art can make various modifications and variations to this invention without departing from its spirit and scope. Therefore, if these modifications and variations fall within the scope of the claims of this invention and their equivalents, then this invention should also include these modifications and variations.

Claims

1. A test generation method based on function intent and hierarchical knowledge base, characterized in that, include: Obtain the function under test in the project to be tested, analyze the local context relationship of the function under test by constructing a code graph, and generate function intent information that reflects the functional goals, input and output characteristics and expected behavior of the function under test; Based on the characteristics and intent information of the tested function, a hierarchical knowledge base obtained by hierarchically organizing test resources from different sources and applicable scopes is screened, scheduled, and coordinated in an orderly manner to obtain reference test knowledge. The function intent information is co-modeled with the test case generation task. The function intent information is used to guide the test parameter initialization, external dependency simulation and assertion logic construction in the test case generation task. The reference test knowledge is combined to perform normative constraints and semantic alignment to generate initial unit test cases. Execute initial unit test cases in the test environment of the project under test, and iteratively revise the initial unit test cases based on the obtained execution feedback information until the target unit test cases are output that meet the preset test standards.

2. The test generation method based on function intent and hierarchical knowledge base as described in claim 1, characterized in that, Obtain the function under test from the project under test, analyze the local context of the function under test by constructing a code graph, and generate function intent information that reflects the functional goals, input and output characteristics, and expected behavior of the function under test, including: The project under test is analyzed to identify the objects under test. The entity objects in the project under test are used as nodes, and the relationships between entity objects are established as edges. A project-level code graph is constructed to represent the calling logic and dependency topology between entities within the project. The function under test is identified as the focus node from the object under test. Taking the focus node as the origin, the adjacent nodes in the project-level code graph are traversed according to the preset subgraph exploration strategy to extract the local subgraph. The analysis scope is dynamically controlled by configuring the subgraph exploration depth. The subgraph exploration strategy is a depth-first or breadth-first traversal strategy along the edges where the relationships are located. The function call relationships, parameter passing paths and test context contained in the local subgraph are analyzed to extract and identify contextual information related to the functional goals, input and output characteristics and expected behavior of the function under test. The context information is structured using a predefined function intent summary template and mapped to the corresponding slots in the template for further structured organization. Based on the results of the structured processing, functional intent information that reflects the functional objectives, input and output characteristics, and expected behavior of the function under test is generated.

3. The test generation method based on function intent and hierarchical knowledge base as described in claim 2, characterized in that, Based on the characteristics and intent information of the function under test, a hierarchical knowledge base, obtained by organizing test resources from different sources and with different scopes of application, is filtered, scheduled, and coordinated in an orderly manner to obtain reference test knowledge, including: The acquired test resources are identified and logically isolated according to their source attributes and applicable space dimensions, and a layered knowledge base is configured, including a global layer and a domain layer. The global layer is used to store functions outside the project under test, global unit test cases and their corresponding relationships, while the domain layer is used to store functions inside the project under test, domain unit test cases and their corresponding relationships. Based on the features of the tested function and the function intent information, multi-dimensional retrieval features including functional semantics, call path and business logic constraints are extracted. Using multidimensional retrieval features, semantic space similarity measurement and matching are performed in the global layer and domain layer of the hierarchical knowledge base, respectively, to filter out the corresponding global unit test cases and domain unit test cases; Based on the engineering adaptation requirements of the function under test and the project under test, the selected global unit test cases and domain unit test cases are scheduled and coordinated in an orderly manner to obtain reference test knowledge that is aligned with the preset general logical constraints and engineering specifications.

4. The test generation method based on function intent and hierarchical knowledge base as described in claim 3, characterized in that, Based on the engineering adaptation requirements of the function under test and the project under test, the selected global unit test cases and domain unit test cases are scheduled and coordinated in an orderly manner to obtain reference test knowledge aligned with preset general logical constraints and engineering specifications, including: Based on the engineering adaptation requirements of the function under test and the project under test, priority is divided between global unit test cases and domain unit test cases in order to extract engineering style constraint information reflecting the coding style of the project under test, the usage of dependent objects, and the assertion expression form in the domain unit test cases, as well as test logic reference information reflecting the function verification logic and test input construction in the global unit test cases. The global unit test cases and the domain unit test cases are respectively structured and parsed to be converted into global general test fragments and domain test fragments, or global general test patterns and domain test patterns; Based on the function intent information and the calling relationship of the function under test in the project-level code graph, the global general test fragments and domain test fragments, or the global general test mode and domain test mode are matched and selected to identify the global general test fragments that have a functional semantic alignment relationship with the test logic reference information, and the structure is adapted in combination with the engineering style constraint information. The adapted global general test fragments and domain-specific test fragments are collaboratively combined, including functional logic injection and test structure mapping, so as to fill the function verification logic and test input construction in the test logic reference information into the test structure form corresponding to the domain-specific test fragments, thereby obtaining reference test knowledge that is aligned with the preset general logic constraints and engineering specifications.

5. The test generation method based on function intent and hierarchical knowledge base as described in claim 2, characterized in that, The function intent information is co-modeled with the test case generation task. The function intent information guides the initialization of test parameters, simulation of external dependencies, and construction of assertion logic within the test case generation task. Furthermore, it incorporates reference testing knowledge for normative constraints and semantic alignment, generating initial unit test cases, including: The function intent information and the task of generating test cases for the function under test are mapped to a unified semantic space to build a collaborative modeling environment that couples function semantic constraints and test generation logic. The test case generation task in the collaborative modeling environment is guided by the function intent information to initialize test parameters for the functional goals represented in the function intent information, build dependency simulation logic for external components for expected behavior, and build assertion logic for verifying the running results based on input and output characteristics. By injecting reference test knowledge into the collaborative modeling environment, function intent information and reference test knowledge jointly drive joint inference. Initial unit test cases are generated by normatively constraining and semantically aligning the initial test parameters, external component dependency simulation logic, and assertion logic.

6. The test generation method based on function intent and hierarchical knowledge base as described in claim 5, characterized in that, After incorporating reference test knowledge into the collaborative modeling environment, the following training optimization is performed based on the joint task loss: Calculate the first loss value generated by the generation of function intent information to characterize the semantic extraction accuracy of the collaborative modeling environment for the function's functional goals and expected behavior; Calculate the second loss value corresponding to the unit test case generation task to characterize the degree of alignment between the generated initial unit test cases and the function intent and reference test knowledge; The first loss value and the second loss value are combined and calculated to construct the joint task loss; Backpropagation is performed using joint task loss to iteratively update the model parameters involved in the collaborative modeling environment, thereby achieving synchronous optimization of intent understanding and test generation capabilities.

7. A test generation method based on function intent and hierarchical knowledge base as described in claim 5 or 6, characterized in that, Execute initial unit test cases in the test environment of the project under test, and iteratively revise the initial unit test cases based on the obtained execution feedback information until the target unit test cases are output that meet the preset test criteria, including: Configure an isolated test sandbox for executing initial unit test cases based on the dependency environment and resource configuration of the project under test; Within the isolated test sandbox, the initial unit test cases are subjected to step-by-step verification in the order of compilation verification first and runtime verification last. The initial unit test cases are compiled. If the compilation fails, the resulting compilation error information is used as the execution feedback information. After the compilation is deemed successful, the function execution of the initial unit test cases is triggered. If an exception or assertion failure that does not conform to the preset test standards is found during the execution process, the generated runtime exception information or assertion failure information will be used as execution feedback information. The execution feedback information is reintroduced into the collaborative modeling environment as a corrective constraint to resolve the syntax errors and semantic biases represented in the execution feedback information; Within the collaborative modeling environment, the test input construction, dependency invocation methods, and assertion design involved in the initial unit test cases are adjusted based on the correction constraints to eliminate syntax errors and semantic deviations. The test generation, compilation, and execution verification process is executed cyclically until the initial unit test cases pass the preset test standards and the target unit test cases are output.

8. A test generation system based on function intent and hierarchical knowledge base, characterized in that, include: The Function Analysis and Intent Summary module is used to obtain the functions under test in the project under test, analyze the local context of the functions under test by constructing code graphs, and generate function intent information that reflects the functional goals, input and output characteristics and expected behavior of the functions under test. The unit test case knowledge base management module is used to filter, schedule, and coordinate the hierarchical knowledge base obtained by organizing test resources from different sources and applicable scopes in an orderly manner based on the characteristics and intent information of the tested function, so as to obtain reference test knowledge. The unit test case generation module is used to collaboratively model function intent information with test case generation tasks. It uses function intent information to guide the initialization of test parameters, simulation of external dependencies, and construction of assertion logic in the test case generation task, and combines reference test knowledge to perform normative constraints and semantic alignment to generate initial unit test cases. The test case execution and feedback repair module is used to execute initial unit test cases in the test environment of the project under test, and iteratively correct the initial unit test cases based on the obtained execution feedback information until the target unit test cases are output in accordance with the preset test standards.

9. A unit test generation device based on function intent and hierarchical knowledge base, characterized in that, include: At least one processor; and memory that is communicatively connected to at least one processor; The memory stores instructions that can be executed by at least one processor, which enables the at least one processor to perform a test generation method based on function intent and hierarchical knowledge base as described in any one of claims 1-7.

10. A computer-readable storage medium storing computer-executable instructions thereon, characterized in that, When the executable instructions are executed by the processor, they implement a test generation method based on function intent and hierarchical knowledge base as described in any one of claims 1-7.