Test generation and defect prediction method and system based on heterogeneous program diagram

By constructing a heterogeneous dynamic program dependency graph and a four-layer, eight-head heterogeneous graph neural network, the separation problem between defect prediction and test generation in existing technologies is solved, and deep expression of multi-perspective semantics of the entire code cycle and efficient test case generation are achieved, thereby improving the accuracy of defect prediction and test coverage.

CN120670296APending Publication Date: 2025-09-19NO 15 INST OF CHINA ELECTRONICS TECH GRP
View PDF 0 Cites 3 Cited by

Patent Information

Application Number
CN202510702823.7
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-05-29
Publication Date
2025-09-19

AI Technical Summary

Technical Problem

Existing technologies fail to effectively integrate static, dynamic, and evolutionary information when generating test cases and defect predictions, resulting in insufficient sensitivity of defect location to the actual execution context and code history changes. In addition, test generation and defect prediction are separated and lack dynamic matching.

Method used

Construct a heterogeneous dynamic program dependency graph, combine static analysis and dynamic instrumentation to form a heterogeneous dynamic program dependency graph containing multiple types of nodes and edges, design a four-layer, eight-head heterogeneous graph neural network model, adopt type-aware attention mechanism and temporal coding injection, and optimize defect prediction and test generation through multi-task joint training.

Benefits of technology

It significantly improves the accuracy and explainability of defect prediction, enhances the pertinence and completeness of test cases, and realizes closed-loop optimization of test generation and defect prediction, making it suitable for large-scale rapid iterative development environments.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120670296A_ABST
    Figure CN120670296A_ABST
Patent Text Reader

Abstract

The invention discloses a test generation and defect prediction method and system based on a heterogeneous program diagram, and the method comprises the steps: constructing a heterogeneous dynamic program dependency diagram: extracting an abstract syntax tree, a control flow diagram and a program dependency diagram of a source code through static analysis, capturing an execution track during operation in combination with dynamic instrumentation, and integrating version evolution information, forming a heterogeneous dynamic program dependency graph containing various types of nodes and edges; heterogeneous graph neural network modeling: designing a four-layer eight-header heterogeneous graph neural network model based on the heterogeneous dynamic program dependency graph, and outputting a function level defect probability and a test case sequence by adopting a type awareness attention mechanism and time coding injection; and multi-task joint training and reasoning: adopting an end-to-end training strategy, jointly optimizing defect prediction loss and test generation loss, preferentially screening high-risk functions during reasoning, and generating a coverage test case. According to the method, the defect detection accuracy is remarkably improved, the test coverage rate is increased, and the method is suitable for enterprise-level complex software systems.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the technical field of software testing and defect prediction, and more particularly to a test generation and defect prediction method and system based on heterogeneous program graphs. Background Art

[0002] With the continuous expansion of software system scale and the increasing diversification of functional requirements, the software development process is becoming more complex and rapid. High-quality software development depends on a complete testing process. Unit testing, as an important cornerstone of quality assurance, can promptly discover and fix potential problems in the early stages of development, thereby effectively reducing the cost of later maintenance and repair. However, with the increase in software scale and complexity, the workload of traditional unit test writing and maintenance has also continued to rise. In order to improve test efficiency and coverage, researchers and developers have begun to explore methods for automatically generating unit test cases. The current mainstream automatic unit test generation technologies mainly include random testing, symbolic execution, and test generation based on search optimization. Although these methods have improved the efficiency of unit test writing to a certain extent, they often have the following limitations:

[0003] On the one hand, although random testing is simple to implement, the test cases it generates usually have low coverage, making it difficult to reach deep-level logic and abnormal branches in the code, and it also produces a large number of redundant or invalid test cases;

[0004] On the other hand, methods such as symbolic execution and search optimization can theoretically discover more complex defects or branches. However, in actual applications, since software systems generally have complex calling relationships, diverse third-party library dependencies, and frequent code iterations, symbolic execution faces problems such as path explosion and difficulty in environment simulation. Search optimization also finds it difficult to fully capture the deep semantic features of the code during the test case generation process, resulting in bottlenecks such as insufficient coverage of real business logic and difficulty in debugging.

[0005] Furthermore, current automated test case generation methods mostly focus on analyzing surface structural features, lacking a comprehensive understanding of the interrelationships between code complexity, semantic information, business logic, and execution paths. This makes it particularly difficult to detect defects hidden within deeper interactions or unusual branches. With the increasing adoption of agile development and continuous integration models, testing requirements are exploding, necessitating the automated generation of high-quality unit test cases within shorter development iterations to ensure software stability and reliability. Therefore, understanding the structural and semantic characteristics of code during test case generation, and thereby improving its relevance and completeness, has become a critical technical challenge urgently needed in software engineering.

[0006] At the same time, software defect prediction and defect detection face similar challenges in practice. Defect prediction requires analyzing large-scale, multi-dimensional code data, comprehensively considering factors such as historical commits, code evolution, static syntax features, and dynamic behavioral characteristics to quickly locate and warn of potential defective modules early in the development process. Traditional defect prediction methods often rely on machine learning to perform classification or regression analysis on manually extracted feature vectors. However, manual feature engineering often relies on the developer's experience and accumulated knowledge, making it difficult to adapt to today's massive and diverse code scenarios, and it is difficult to extract high-quality features for complex software logic.

[0007] In recent years, graph neural networks (GNNs), thanks to their efficient modeling capabilities for graph-structured data, have been widely used in tasks such as vulnerability detection and code search and completion. A common approach is to convert source code into an abstract syntax tree (AST), control flow graph (CFG), or data flow graph (DFG), and then use GNNs for feature aggregation and representation learning. Related research has shown that GNNs can better capture program structural dependencies and local contextual information than sequential models, thereby improving the performance of downstream tasks.

[0008] However, current work on applying GNN to test generation and defect prediction is still at the following level:

[0009] Only using the static Program Dependency Graph (PDG): This ignores dynamic information such as runtime branch hits, call frequency, and version evolution, making the model less sensitive to the real execution environment and code evolution trends.

[0010] Test generation and defect prediction are separated: Test cases are often generated independently after defect prediction, without forming a feedback loop. As a result, the test budget and defect risk cannot be dynamically matched.

[0011] In this context, the industry urgently needs a new technical solution for large-scale, rapidly iterative development environments that can integrate static, dynamic, and evolutionary information, have incremental scalability and cross-language generalization capabilities, and simultaneously provide explainable defect prediction and risk-driven test generation closed loops to meet the dual needs of modern software engineering for quality and efficiency. Summary of the Invention

[0012] In view of this, the present invention provides a test generation and defect prediction method and system based on heterogeneous program graphs, which can solve the problem that the existing technology only relies on static program graphs and cannot simultaneously characterize runtime trajectories and version evolution information, resulting in insufficient sensitivity of defect location to the actual execution context and code history changes.

[0013] In order to achieve the above object, the present invention adopts the following technical solutions:

[0014] The present invention provides a test generation and defect prediction method based on heterogeneous program graphs, comprising the following steps:

[0015] Constructing a heterogeneous dynamic program dependency graph: Static analysis extracts the source code's abstract syntax tree, control flow graph, and program dependency graph. Dynamic instrumentation is used to capture runtime execution traces and integrate version evolution information to form a heterogeneous dynamic program dependency graph containing multiple types of nodes and edges.

[0016] Heterogeneous graph neural network modeling: Based on the heterogeneous dynamic program dependency graph, a four-layer, eight-head heterogeneous graph neural network model is designed. It uses a type-aware attention mechanism and temporal encoding injection to output function-level defect probabilities and test case sequences.

[0017] Multi-task joint training and inference: Adopt an end-to-end training strategy, jointly optimize the defect prediction loss and test generation loss, prioritize high-risk functions during inference, and generate comprehensive test cases.

[0018] Furthermore, the step of constructing a heterogeneous dynamic program dependency graph includes:

[0019] JavaParser and Soot tools are used to jointly extract abstract syntax trees, control flow graphs, and program dependency graphs, perform static source parsing, and generate various types of nodes and edges.

[0020] Dynamic execution information captures timestamp sequences and test coverage relationships through code instrumentation;

[0021] All node and edge information is uniformly serialized into a heterogeneous dynamic graph format for use in graph neural network models.

[0022] Furthermore, dynamic execution information is captured through code instrumentation, including timestamp sequences and test coverage relationships, including:

[0023] Dynamically instrument the source code program by capturing the timestamps and call chains of statements or functions in real time during test execution, establishing OCCURS_BEFORE edges for adjacent events in chronological order, and dynamically linking TEST_COVERS edges during test execution to represent the coverage relationship.

[0024] Furthermore, the heterogeneous graph neural network model has a four-layer eight-head structure with a hidden layer dimension of 256, and independently learns the Query, Key, and Value projection matrices for different meta-relationships, including: (source node type, edge type, target node type).

[0025] Furthermore, the heterogeneous graph neural network model injects relative time coding into the key value calculation, and the calculation formula is:

[0026] T enc (Δt) = [sin! (Δt / 10 0 ),cos! (Δt / 10 0 ),…,sin! (Δt / 10 3 ),cos! (Δt / 10 3 )]

[0027] Where Δt represents the relative time interval between two adjacent events; T enc (Δt) represents the temporal encoding result, which is used as the continuous feature of the temporal edge OCCURS_BEFORE, concatenated with the one-hot edge type vector, and used as the input of the heterogeneous graph neural network model.

[0028] Furthermore, in the joint optimization of defect prediction loss and test generation loss:

[0029] The defect prediction loss is expressed as follows:

[0030]

[0031] N represents the total number of function nodes involved in the calculation in the current training batch; y i Indicates the true defect label: 1 = defective, 0 = no defect; z i represents defect logits, which is the unnormalized score of the function node being defective; σ represents the Sigmoid activation function;

[0032] The test generation loss is expressed as follows:

[0033]

[0034] M represents the length of the target sequence; represents the generation probability of step t: generating from the vocabulary or copying from the graph; P vocab (y t ) represents the decoding softmax of token y in the whole vocabulary t probability; represents the attention weight of the pointer network to the source node or word unit v;

[0035] Overall training goal

[0036]

[0037] Among them, the test generation loss The weight is the defect prediction loss twice as much.

[0038] In a second aspect, an embodiment of the present invention further provides a test generation and defect prediction system based on a heterogeneous program graph, using the test generation and defect prediction method based on a heterogeneous program graph as described in any of the above embodiments, the system comprising:

[0039] Build a heterogeneous dynamic program dependency graph module. This module extracts the source code's abstract syntax tree, control flow graph, and program dependency graph through static analysis. It then uses dynamic instrumentation to capture runtime execution traces and integrates version evolution information to form a heterogeneous dynamic program dependency graph containing multiple types of nodes and edges.

[0040] The heterogeneous graph neural network modeling module designs a four-layer, eight-head heterogeneous graph neural network model based on the heterogeneous dynamic program dependency graph. It uses a type-aware attention mechanism and temporal encoding injection to output function-level defect probabilities and test case sequences.

[0041] The multi-task joint training and inference module adopts an end-to-end training strategy to jointly optimize the defect prediction loss and test generation loss. During inference, it prioritizes high-risk functions and generates coverage test cases.

[0042] It can be seen from the above technical solutions that compared with the prior art, the present invention has the following technical advantages:

[0043] By constructing a heterogeneous dynamic program dependency graph, this paper unifies three layers of information—static dependencies, dynamic execution trajectories, and Git evolutionary edges—into a single graph model, enabling a deep expression of the program's full-cycle, multi-perspective semantics. Furthermore, the paper constructs an interpretable heterogeneous graph neural network, introducing dynamic edge Δt embedding within the Heterogeneous Graph Transformer to enhance the model's perception of the causal chain of execution sequences. Furthermore, a gating mechanism is designed on evolutionary edges to explicitly measure the contribution of version changes to defect probability, further improving the accuracy and interpretability of defect predictions. BRIEF DESCRIPTION OF THE DRAWINGS

[0044] In order to more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, the following briefly introduces the drawings required for use in the embodiments or the description of the prior art. Obviously, the drawings described below are merely embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on the provided drawings without paying any creative work.

[0045] Figure 1 Schematic diagram of the test generation and defect prediction process based on heterogeneous program graphs provided by the present invention;

[0046] Figure 2 A diagram showing the construction process of the heterogeneous dynamic program dependency graph HD-PDG provided by the present invention;

[0047] Figure 3 This is a block diagram of the test generation and defect prediction system based on heterogeneous program graphs provided by the present invention. DETAILED DESCRIPTION

[0048] The following will clearly and completely describe the technical solutions in the embodiments of the present invention in conjunction with the accompanying drawings. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without making creative efforts are within the scope of protection of the present invention.

[0049] The embodiment of the present invention discloses a test generation and defect prediction method based on heterogeneous program graphs. The overall architecture is as follows: Figure 1 As shown, it includes the following 3 steps:

[0050] Step 1: Construct a heterogeneous dynamic program dependency graph: Extract the source code's abstract syntax tree, control flow graph, and program dependency graph through static analysis, capture runtime execution traces with dynamic instrumentation, and integrate version evolution information to form a heterogeneous dynamic program dependency graph containing multiple types of nodes and edges.

[0051] Reference Figure 2 Figure 2 illustrates the construction process of a heterogeneous dynamic program dependency graph (HD-PDG). First, static analysis is performed on the source code using JavaParser in conjunction with Soot. This process extracts the abstract syntax tree (AST), control flow graph (CFG), and static program dependency graph (PDG), respectively. Six core node types (Function, BasicBlock, Statement, Variable, API, and TestCase) are generated based on these six types of nodes and their corresponding nine semantic edge types (AST_PARENT, CFG_NEXT, DATA_DEP, CONTROL_DEP, CALLS, WRITES, READS, OCCURS_BEFORE, and TEST_COVERS).

[0052] The explanation of the six types of core nodes is shown in Table 1:

[0053] Table 1

[0054]

[0055] The explanations of the nine types of semantic edges are shown in Table 2:

[0056] Table 2

[0057]

[0058]

[0059] Subsequently, the target program is instrumented during runtime, capturing the timestamps and call chains of statements or functions in real time. OCCURS_BEFORE edges are established for adjacent events in chronological order, and TEST_COVERS edges are dynamically linked during test execution to represent coverage relationships. All node and edge information is uniformly archived in JSON / Parquet. Node features consist of word embeddings, position encodings, and file identifiers, while edge features include one-hot vectors (the simplest encoding method for discrete categories (such as edge types) to dense vectors) and relative time encodings. Finally, it is serialized into a heterograph binary file using DGL (Deep Graph Library, a PyTorch / TensorFlow-based graph neural network development framework open sourced by AWS, NYU, and other institutions), forming a heterogeneous dynamic program dependency graph containing temporal information as input to the Heterogeneous Graph Transformer (HGT).

[0060] For example, it is divided into the following two sub-steps:

[0061] 1) Static parsing

[0062] The goal of static parsing is to convert the source file into a basic skeleton of "nodes" and "semantic edges". First, use the JavaParser CLI (Java Parser Command Line Tool):

[0063] javaparser-cli dump--format=json src / main / java / Foo.java>Foo.ast.json

[0064] Get the kind, line number, column number and source code snippet of each Node; then introduce Soot:

[0065] soot-pp-cp target / classes-process-dirtarget / classes-w\

[0066] -p cg.spark enabled:true

[0067] Soot writes the Jimple file (the intermediate code generated by Soot) in the sootOutput directory. All call edges can be obtained from Scene.v().getCallGraph() through cg.spark. UnitGraph (the control flow graph class in Soot) can be used to traverse the basic block successor and directly map CFG_NEXT. SimpleLocalDefs / Uses (Soot's data flow analysis tool) is then used to obtain Use-Def and construct DATA_DEP. If you want to control dependencies, use Control Dependence Graph (the control dependency graph generator) to analyze the edge from the if / while condition to the controlled statement, namely CONTROL_DEP. The line number comes from JavaParser, and Jimple Unit also carries a line number attribute. You can use (file, line) as the primary key to align two events to the same node. Then assign a unified ID to all generated entities:

[0068] Function ID format F_ <abs-path>:<start_line> ;

[0069] Basic Block ID Format B_ <func-id> : <bb-index>;

[0070] Statement ID format S_ <abs-path> : <line>;

[0071] Variable ID format V_ <abs-path> : <line> : <name>;

[0072] The API ID directly takes the fully qualified name;

[0073] Node attributes are written to nodes_static.json, with line numbers and file paths to ensure that the source code can be traced back later. The tokens field is encoded using the same SentencePiece-BPE model, converting string lists into integer sequences. All relationships collected above are traversed, and (src-id, dst-id, etype) are appended to edges_static.json.

[0074] The static portion includes five semantic edges and four node types: AST_PARENT, CFG_NEXT, DATA_DEP, CONTROL_DEP, and CALLS. Files and line numbers are locked, and dynamic timing and test coverage information are incorporated to complete the HD-PDG skeleton.

[0075] 2) Test coverage mapping

[0076] Use Junit to map each test case to a TestCase node, connect each TestCase node to all hit statements, and record them as TEST_COVERS edge information.

[0077] Through the above steps, HD-PDG eventually becomes a global program representation of dynamic and static information fusion, providing a data basis for subsequent defect prediction and test generation.

[0078] In step 1 of the present invention, a heterogeneous dynamic program dependency graph (HD-PDG) is used to model the source code, unifying control flow, data flow, call relationships, and temporal evolution in a single graph. Compared to traditional homogeneous or single-relationship graphs, this method explicitly distinguishes semantic types at the node and edge levels, enabling the model to capture code logic, context, and execution sequence at a finer granularity, significantly improving the richness and accuracy of structural and semantic information.

[0079] Step 2: Heterogeneous Graph Neural Network Modeling: Based on the heterogeneous dynamic program dependency graph, a four-layer, eight-head heterogeneous graph neural network model is designed. This model uses a type-aware attention mechanism and temporal encoding injection to output function-level defect probabilities and test case sequences.

[0080] This step constructs a heterogeneous graph neural network (HGT). A four-layer, eight-head Heterogeneous Graph Transformer with 256 hidden dimensions is defined in the Deep Graph Library (DGL). The model automatically learns independent query, key, and value projection matrices for each source node type, edge type, and target node type. It also embeds relative time encodings in the key vector, enabling attention to simultaneously perceive structural heterogeneity and temporal order. The encoder output is followed by two decoding heads: a defect prediction head using a two-layer MLP + Sigmoid to output defect probabilities, and a test generation head using a Pointer-Generator network combined with Beam Search to generate input-assertion-expectation sequences.

[0081] For example, it is divided into the following three sub-steps:

[0082] 1) Graph data preparation and preprocessing

[0083] After static parsing and runtime instrumentation, we get HD-PDG, which contains six types of nodes: Function, BasicBlock, Statement, Variable, API, and TestCase, and nine types of edges: AST_PARENT, CFG_NEXT, DATA_DEP, CONTROL_DEP, CALLS, WRITES, READS, OCCURS_BEFORE, and TEST_COVERS. The original representation of each node is represented by the word embedding E tok (v)

[0084] Position code E pos (l v ) and file-language embedding E file (f v ) are cascaded.

[0085]

[0086] Where v represents any node in HD-PDG (Function, BasicBlock, Statement, Variable, API and TestCase). v Represents the location information of the node in the source file, such as line number, column number, AST depth, etc. v Represents the source file information (file ID, file path, hash, language tag) to which the node belongs. tok (v) indicates that the node location information l v Perform position encoding, such as absolute or relative line number normalization, sine-cosine position encoding. file (f v ) indicates that the source file information f v Mapped into file-language embeddings to indicate cross-file or cross-language differences.

[0087] Here represents the initial node, represents the vector The real vector space in which it is located has dimension d0.

[0088] make

[0089] dim(E tok (v))=d tok

[0090] dim(E pos (l v ))=d pos

[0091] dim(E file (f v ))=d file

[0092] The dimensions of the vector after concatenation:

[0093] d0=d tok +d pos +d file

[0094] It is the space composed of all the initial node representation vectors formed by the embedding and splicing of these three parts.

[0095] [E tok (v)|E pos (l v )|E file (f v )] means concatenating the above three vectors along the feature dimension to obtain a complete feature vector. It is the initial representation of node v (0-layer hidden state), which serves as the input of the subsequent heterogeneous graph neural network.

[0096] For the time edge, use logarithmic piecewise discretization and then apply sin / cos function to obtain an 8-dimensional time vector

[0097] T enc (Δt)=[sin(Δt / 10 0 ),cos(Δt / 10 0 ),…,sin(Δt / 10 3 ),cos(Δt / 10 3 )]

[0098] All nodes, edges, and features are serialized into a DGL heterograph, and a meta-relationship-frequency dictionary is output for subsequent sparse weight sharing.

[0099] Where Δt represents the relative time interval between two adjacent events. enc (Δt) represents the temporal encoding result, which is used as the continuous feature of the temporal edge OCCURS_BEFORE and is concatenated with the one-hot edge type vector and input into the GNN.

[0100] For example:

[0101] If event e1 occurs at 123ms and event e2 occurs at 128ms, then Δt = 5ms. Substituting into the above formula, we get T enc (Δt) = [-0.959, 0.284, 0.479, 0.878, 0.050, 0.999, 0.005, 1.000]. This function converts scalar time intervals into fixed 8-dimensional continuous vectors and feeds them into the graph neural network. By applying this to the OCCURS_BEFORE edge, the trained graph neural network considers both execution order and intervals, prioritizing high-risk paths. This helps prioritize test cases. It also allows for more accurate defect localization by focusing on the last few steps before an error.

[0102] 2) Heterogeneous Neighborhood Sampling and Batch Generation

[0103] During training, a type-aware budget s is used for each layer l∈{1,2,3} l ={20,15,10}. The neighbor sampling probability of a certain element relation r is written as

[0104]

[0105] where deg r (u) is the out-degree of node u under relation r, s l,r ≤s l The sampling results are heterogeneous subgraphs with controllable size, which carry the original temporal features and can be directly fed into the GPU.

[0106] P l ((u,r,v)) represents the probability of taking edge (u,r,v) into the batch at layer l. (u,r,v) represents a heterogeneous edge triplet, with source node u (6 types of core nodes), relation type r (9 types of semantic edges), and target node v. deg r (u) represents the number of outgoing edges of node u under relation r. l,r Indicates the maximum number of neighbors allowed to be retained in the relationship r at level l (if exceeded, they will be randomly pruned).

[0107] 3) HGT encoder and time key embedding

[0108] A Heterogeneous GraphTransformer with four layers, eight heads and hidden dimension d = 256 is used. For any element relation r = (τ s ,e,τ t ), whose single-head attention score is defined as

[0109]

[0110] τ s : Source node type, specifically the node name in Table 1. e: Semantic edge type, specifically the edge type in Table 2. τ t : Target node type, specifically the node name in Table 1. e is used in the following, and all subsequent parameterized projections of r are dependent on the source node (τ s ), semantic edge (e), target node (τ t )of.

[0111] Represents the single-head attention weight: the importance of u→v under the relationship r, u, v are six types of important nodes, and r is nine types of edges. Represents a query projection, which depends only on the source node type. It is a key projection that depends on the target node type and meta-relationship. The purpose of this is to allow different edges to have independent parameters. Represents the time chain projection, injecting time code into the key. Represents the hidden vector of node u at layer l. Represents the hidden vector of node v at layer l. enc (Δt u,v ) represents an 8-dimensional multi-frequency sin / cos time vector, which has been explained with examples above. It is used to prevent the dot product from being too large.

[0112] Update the node vector after aggregating weighted messages

[0113]

[0114] Here, σ represents layer normalization and ReLU. The self-loop relation r = SELF_LOOP is used to preserve local information, and the global node r = AGGREGATES aggregates file-level context. AGGREGATES is an artificially added "global aggregation" relation used to inject file-level context information into each core node. AGGREGATES bridges the information pathway from "file to each node," allowing the model to not only see local function / statement dependencies but also capture the macro-context of the entire file, enabling a more comprehensive assessment of defect risks.

[0115] Indicates output mapping, which concatenates multiple results and then maps them. Indicates value projection and dependent meta-relationship.

[0116] This step utilizes heterogeneous graph neural networks (HGT) to aggregate features and model relationships within the HD-PDG, building a function-level defect probability prediction model. By leveraging type-time dual attention, the model effectively focuses on high-risk nodes and quantifies defect probabilities in complex dependency environments. This provides reliable risk ranking and coverage prioritization for subsequent test case generation, significantly improving defect detection rates and test resource utilization efficiency.

[0117] Step 3: Multi-task joint training and inference: Adopt an end-to-end training strategy, jointly optimize the defect prediction loss and test generation loss, prioritize high-risk functions during inference, and generate comprehensive test cases.

[0118] It specifically includes the following two sub-steps:

[0119] 1) Multi-task decoding and joint loss design:

[0120] For each function node F i Representation (L=4) Defect prediction first

[0121]

[0122] z i Represents defect logits, which is the function F i is the unnormalized fraction of defects. is the output function representation of the L=4th layer of the HGT. W1 is the feature matrix of the hidden layer. W2 is the feature matrix of the output layer.

[0123]

[0124] L Defect represents the defect prediction loss; N represents the total number of function nodes involved in the calculation in the current training batch; σ represents the Sigmoid activation function; the test generation head uses Pointer-Generator; given the target sequence Its negative log-likelihood is

[0125] y i Indicates the true defect label: 1 = defective, 0 = no defect. i Represents defect logits, which is the function F i is the unnormalized fraction of defects.

[0126]

[0127] represents the test generation loss; M represents the length of the target sequence; P is the generation probability of the tth step: generating from the vocabulary or copying from the graph. vocab (y t ) represents the decoding softmax of token y in the whole vocabulary t probability. It refers to the attention weight of the pointer network to the source node or word unit v. Refers to the Pointer-Generator negative log-likelihood (NLL) loss.

[0128] Overall training goal:

[0129]

[0130] The gradient uses AdamW, learning rate η, and gradient norm clipping And enable mixed precision to stabilize training on large images.

[0131] 2) Inference pipeline and adaptation

[0132] Calculate the defect detection F1 and AUROC on the validation set:

[0133]

[0134] TP stands for true positive, FP stands for false positive, FN stands for false negative, P stands for precision, R stands for recall, and F1 stands for the balanced precision and recall. AUROC stands for receiver operating characteristic (ROC) curve, which plots the true positive rate (TPR) and false positive rate (FPR) curves by traversing all classification thresholds.

[0135] Execute generated tests simultaneously and measure branch coverage delta

[0136]

[0137] Cov base+gen :

[0138] After adding new test cases generated by the "Test Generation" module to the basic test set, the new branch coverage rate; that is, the ratio of the number of branches touched by all tests (original + new) to the total number of code branches.

[0139] Cov base :

[0140] After adding new test cases generated by the "Test Generation" module to the basic test set, the new branch coverage rate; that is, the ratio of the number of branches touched by all tests (original + new) to the total number of code branches.

[0141] ΔCov:

[0142] Coverage delta indicates the relative coverage improvement brought about by test generation. It measures the percentage of branch coverage improvement contributed by the newly added test cases.

[0143] That is, an adaptive early stopping strategy is adopted in the inference stage. When the verification loss does not decrease or the defect detection F1 value does not improve for three consecutive times, the training is terminated, and the model weights are solidified when the defect detection F1 ≥ 0.8 and the coverage increment ΔCov ≥ 10%.

[0144] Neighborhood sampling (e.g. [20, 15, 10]) and mixed precision are used during training to control video memory and joint loss. Perform end-to-end fine-tuning. During the inference phase, use the defect header to screen high-risk functions, then call the test header to write automatic single tests, completing the integrated "bug detection + supplementary testing" process.

[0145] This step couples the defect prediction head with the Pointer-Generator test generation head, using the same graph representation and GNN encoder, to form a closed-loop "detection-generation" toolchain. The system automatically identifies high-risk functions and instantly generates comprehensive unit tests within the CI / CD pipeline, enabling automated and intelligent test completion and quality assessment, providing a new paradigm for efficient and low-intervention enterprise software engineering.

[0146] The test generation and defect prediction method based on heterogeneous program graphs provided by the present invention is again illustrated by an example as follows:

[0147] Taking the user login module as an example, the source code is first parsed into a heterogeneous program graph containing AST nodes (such as IfStatement and MethodInvocation), CFG nodes (such as entry, branch, and merge), and data flow nodes (such as variable definition and use). The AST subgraph, CFG subgraph, and data flow subgraph are represented and learned through a graph neural network, respectively, to obtain a vector embedding for each node and subgraph. Then, the reachable execution path is analyzed based on CFG path enumeration, and specific test cases (such as (user=null,pwd="abc") and (user="bob",pwd="wrong") are generated by combining data flow information and boundary value strategies. After execution, input and output pairs are obtained. Finally, the vector embeddings of the entire graph are aggregated and input into a defect prediction model (such as an MLP classifier). The defect probability of each path or function is output, and the presence of defects is determined based on a set threshold. Attention weights can also be used to locate high-risk code areas.

[0148] When constructing the HD-PDG of the present invention, JavaParser and Soot tools are used to extract static code structures, test coverage relationships are captured through JUnit instrumentation, and evolutionary edges are generated by integrating Git commit history. An HGT model is constructed for training, a heterogeneous subgraph sampling strategy is defined in the DGL framework, mixed precision training is used to optimize video memory usage, and a joint loss function is used to balance defect prediction and test generation tasks. During inference deployment, the trained model is integrated into the CI / CD pipeline, and a list of high-risk functions and automatically generated JUnit test cases are output in real time, and the branch coverage increment is evaluated. This significantly improves defect detection accuracy, increases test coverage, and supports cross-language and cross-version code analysis, making it suitable for enterprise-level complex software systems; it achieves closed-loop optimization of test generation and defect prediction, significantly reducing the cost of manual intervention.

[0149] Based on the same inventive concept, the present invention also provides a test generation and defect prediction system based on heterogeneous program graphs, using the test generation and defect prediction method based on heterogeneous program graphs as described in the above embodiment, referring to Figure 3 As shown, the system includes:

[0150] Build a heterogeneous dynamic program dependency graph module. This module extracts the source code's abstract syntax tree, control flow graph, and program dependency graph through static analysis. It then uses dynamic instrumentation to capture runtime execution traces and integrates version evolution information to form a heterogeneous dynamic program dependency graph containing multiple types of nodes and edges.

[0151] The heterogeneous graph neural network modeling module designs a four-layer, eight-head heterogeneous graph neural network model based on the heterogeneous dynamic program dependency graph. It uses a type-aware attention mechanism and temporal encoding injection to output function-level defect probabilities and test case sequences.

[0152] The multi-task joint training and inference module adopts an end-to-end training strategy to jointly optimize the defect prediction loss and test generation loss. During inference, it prioritizes high-risk functions and generates coverage test cases.

[0153] Through deep modeling of heterogeneous program graphs and multi-task collaborative reasoning, the system achieves precise, automated, and efficient defect detection and test generation, providing a complete solution for software quality assurance from code analysis to test implementation, and is particularly suitable for complex systems with high reliability requirements (such as finance, the Internet of Things, and aerospace).

[0154] The various embodiments in this specification are described in a progressive manner, with each embodiment focusing on the differences from other embodiments. Reference can be made to the common and similar parts between the various embodiments. For the devices disclosed in the embodiments, since they correspond to the methods disclosed in the embodiments, the description is relatively simple, and the relevant parts can be referred to the method description.

[0155] The above description of the disclosed embodiments is intended to enable one skilled in the art to implement or use the present invention. Various modifications to these embodiments will be readily apparent to one skilled in the art, and the general principles defined herein may be implemented in other embodiments without departing from the spirit or scope of the present invention. Therefore, the present invention is not limited to the embodiments shown herein but is intended to conform to the widest scope consistent with the principles and novel features disclosed herein.< / name> < / line> < / abs-path> < / line> < / abs-path> < / func-id>

Claims

1. A test generation and defect prediction method based on heterogeneous program graphs, characterized in that: The following steps are involved: Constructing a heterogeneous dynamic program dependency graph: Static analysis extracts the source code's abstract syntax tree, control flow graph, and program dependency graph. Dynamic instrumentation is used to capture runtime execution traces and integrate version evolution information to form a heterogeneous dynamic program dependency graph containing multiple types of nodes and edges. Heterogeneous graph neural network modeling: Based on the heterogeneous dynamic program dependency graph, a four-layer, eight-head heterogeneous graph neural network model is designed. It uses a type-aware attention mechanism and temporal encoding injection to output function-level defect probabilities and test case sequences. Multi-task joint training and inference: Adopt an end-to-end training strategy, jointly optimize the defect prediction loss and test generation loss, prioritize high-risk functions during inference, and generate comprehensive test cases.

2. A test generation and defect prediction method based on heterogeneous program graphs according to claim 1, characterized in that: The steps of constructing a heterogeneous dynamic program dependency graph include: JavaParser and Soot tools are used to jointly extract abstract syntax trees, control flow graphs, and program dependency graphs, perform static source parsing, and generate various types of nodes and edges. Dynamic execution information captures timestamp sequences and test coverage relationships through code instrumentation; All node and edge information is uniformly serialized into a heterogeneous dynamic graph format for use in graph neural network models.

3. The test generation and defect prediction method based on heterogeneous program graph according to claim 1, characterized in that: Dynamic execution information is captured through code instrumentation, including timestamp sequences and test coverage relationships, including: Dynamically instrument the source code program by capturing the timestamps and call chains of statements or functions in real time during test execution, establishing OCCURS_BEFORE edges for adjacent events in chronological order, and dynamically linking TEST_COVERS edges during test execution to represent the coverage relationship.

4. The method for test generation and defect prediction based on heterogeneous program graphs according to claim 1, characterized in that: The heterogeneous graph neural network model has a four-layer eight-head structure with a hidden layer dimension of 256, and independently learns the Query, Key, and Value projection matrices for different meta-relationships, including: (source node type, edge type, target node type).

5. The method for test generation and defect prediction based on heterogeneous program graphs according to claim 1, characterized in that: The heterogeneous graph neural network model injects relative time coding into the key value calculation, and the calculation formula is: T enc (Δt)=[sin!(Δt / 10 0 ),cos!(Δt / 10 0 ),…,sin!(Δt / 10 3 ),cos!(Δt / 10 3 )] Where Δt represents the relative time interval between two adjacent events; T enc (Δt) represents the temporal encoding result, which is used as the continuous feature of the temporal edge OCCURS_BEFORE, concatenated with the one-hot edge type vector, and used as the input of the heterogeneous graph neural network model.

6. The method for test generation and defect prediction based on heterogeneous program graphs according to claim 1, characterized in that: In the joint optimization of defect prediction loss and test generation loss: The defect prediction loss is expressed as follows: N represents the total number of function nodes involved in the calculation in the current training batch; y i Indicates the true defect label: 1 = defective, 0 = no defect; z i represents defect logits, which is the unnormalized score of the function node being defective; σ represents the Sigmoid activation function; The test generation loss is expressed as follows: M represents the length of the target sequence; represents the generation probability of step t: generating from the vocabulary or copying from the graph; P vocab (y t ) represents the decoding softmax of token y in the whole vocabulary t probability; represents the attention weight of the pointer network to the source node or word unit v; Overall training goal Among them, the test generation loss The weight is the defect prediction loss twice as much.

7. A test generation and defect prediction system based on heterogeneous program graphs, characterized in that: Using the test generation and defect prediction method based on heterogeneous program graph according to any one of claims 1 to 6, the system includes: Build a heterogeneous dynamic program dependency graph module. This module extracts the source code's abstract syntax tree, control flow graph, and program dependency graph through static analysis. It then uses dynamic instrumentation to capture runtime execution traces and integrates version evolution information to form a heterogeneous dynamic program dependency graph containing multiple types of nodes and edges. The heterogeneous graph neural network modeling module designs a four-layer, eight-head heterogeneous graph neural network model based on the heterogeneous dynamic program dependency graph. It uses a type-aware attention mechanism and temporal encoding injection to output function-level defect probabilities and test case sequences. The multi-task joint training and inference module adopts an end-to-end training strategy to jointly optimize the defect prediction loss and test generation loss. During inference, it prioritizes high-risk functions and generates coverage test cases.

Citation Information

Cited By

  • Test case priority ranking method and system based on test heterogeneous graph

    CN120872847A

  • A test case priority ordering method and system based on test heterogeneous graphs

    CN120872847B

  • Variation test method and device based on intelligent automation script

    CN120929389A