RAG-Assert: Hybrid Retrieval-Augmented Assertion Generation for Automated Unit Testing
Test assertions are a key mechanism for validating program behavior in unit testing, and their quality determines the defect detection capability and engineering utility of automated tests. Existing assertion generation approaches fall into two main categories: deep learning-based methods, which are sensitive to training data scale and suffer from limited generalization, and retrieval-based methods, which rely on shallow lexical similarity and fail to capture deep code semantics. To address these limitations, this paper proposes Retrieval-Augmented Assertion Generation (RAG-Assert), a retrieval-augmented assertion generation method for automated unit testing. RAG-Assert constructs a hybrid retriever that combines token-level Jaccard similarity and CFG-derived basic-block semantic similarity to retrieve the most relevant historical assertion from the retrieval repository for a given focal test. The retrieved assertion is incorporated as external knowledge context during fine-tuning and generation of a pretrained code model. A beam-search decoding strategy is further adopted to select the reliable assertion from multiple candidates, reducing the probability of generating semantically inconsistent assertions. Experiments on the Methods2Test dataset demonstrate that RAG-Assert consistently outperforms five representative baseline methods across multiple metrics including Top-1 accuracy, Top-10 accuracy, and Bilingual Evaluation Understudy (BLEU) score. Evaluation on the Defects4J benchmark further confirms the practical defect detection capability of the proposed method. Ablation studies validate the complementary advantages of combining token-level similarity and CFG-derived basic-block semantic similarity in the hybrid retriever.
Authors
- Qiming Wu (ORCID: https://orcid.org/0009-0002-2567-7360)
- Tiegang Zhang
- Haibin Sui
- Weibiao Chen
- Chao Ma
- Chengwen Ji
Publication Details
- Journal
- International Journal of Pattern Recognition and Artificial Intelligence
- Published
- 2026-10-22
- DOI
- https://doi.org/10.1142/s0218001426590494
- Primary Topic
- Software Testing and Debugging Techniques
- Type
- article
- Field-Weighted Citation Impact
- 0.00