Improving Deep Assertion Generation via Fine-Tuning Retrieval-Augmented Pre-trained Language Models

Quanjun Zhang, Chunrong Fang, Yi Zheng, Yaxin Zhang, Yuan Zhao, Rubing Huang, Jianyi Zhou, Yun Yang, Tao Zheng, Zhenyu Chen

TL;DR

RetriGen is a novel framework that significantly improves automated test assertion generation by pairing a hybrid (lexical + semantic) codebase retriever with a fine-tuned pre-trained language model (PLM). By combining the literal matching of traditional search with the deep code understanding of models like CodeT5, RetriGen achieves an average accuracy of 57.66%, vastly outperforming existing state-of-the-art techniques and successfully uncovering real-world software bugs.

What Problem Does This Paper Address?

Unit testing is vital for software reliability, yet writing tests manually consumes upwards of 15% of a developer's time. While automated tools (like EvoSuite) excel at generating the setup code (test prefixes) to achieve high code coverage, they notoriously struggle to write meaningful assertions—the actual test oracles that verify if a program behaved correctly.

Existing automated Assertion Generation (AG) tools fall into two traps:

  1. Deep Learning (DL) approaches try to generate assertions from scratch but are severely bottlenecked by small, specialized training datasets.
  2. Retrieval-based approaches look for similar code snippets in external repositories to copy/adapt their assertions. However, they rely purely on surface-level lexical matching (keyword similarity). If a developer uses different variable names for the same logic, lexical retrieval fails completely.

The authors introduce RetriGen to bridge this gap by combining semantic code understanding with retrieval-augmented generation.

Methods

RetriGen treats assertion generation as a sequence-to-sequence translation task, broken down into three distinct phases:

Fig. 1. Framework Overview of RetriGen
  1. Assertion Retrieval Phase: Given a "focal-test" (the method under test combined with its test prefix), RetriGen searches an external codebase using a hybrid retriever. This retriever combines a sparse token-based retriever (using Jaccard similarity for literal matches) and a dense embedding-based retriever (using an off-the-shelf CodeLlama model to calculate Cosine similarity for semantic meaning).
  2. Assertion Generation Phase: The top-1 retrieved test-assert pair is concatenated with the original input. This augmented text is fed into an encoder-decoder PLM. While the framework is model-agnostic, the authors primarily implement it using CodeT5 (220 million parameters), fine-tuning it to map the combined context into a correct assertion.
  3. Assertion Inference Phase: During deployment, the fine-tuned model uses beam search to output a ranked list of candidate assertions, selecting the one with the highest probability.

Datasets

The approach was evaluated on two widely recognized, large-scale Java benchmarks:

  • $Data_{old}$ (ATLAS dataset): Contains 156,760 samples extracted from GitHub. It filters out long methods and assertions containing "unknown" tokens to simplify the task.
  • $Data_{new}$: An extended dataset containing 265,420 samples. It retains the real-world "unknown" tokens omitted by $Data_{old}$ to better reflect actual, messy production code distributions.

Both datasets were split using a standard 8:1:1 ratio for training, validation, and testing.

Key Insights and Ideas

  • The Plastic Surgery Hypothesis Meets PLMs: The paper successfully automates the software engineering concept that new code can often be synthesized from existing code snippets in a repository. By feeding these snippets directly into a PLM's context window during fine-tuning, the model learns how to adapt old assertions to new contexts.
  • Hybrid Retrieval is Essential: Relying solely on embeddings can miss exact syntax matches, while relying solely on tokens misses structural duplicates with different naming conventions. Merging them provides a significantly cleaner ground-truth anchor for the generator.
  • Smaller and Fine-Tuned Beats Big and Blind: To avoid the data leakage issues common in commercial, black-box LLMs (like ChatGPT or Codex), the authors focused on fine-tuning a smaller, open-source model (CodeT5). Despite its compact size, fine-tuning it on domain-specific retrieval data allowed it to outperform much larger baselines.

Key Results

RetriGen set new records in the assertion generation field across both datasets using two metrics: exact-match Accuracy and CodeBLEU (a syntax-aware variant of the BLEU metric).

  • Overall Performance: RetriGen achieved an average accuracy of 57.66% and a CodeBLEU score of 73.24%, outperforming all six baselines by an average margin of 50.66% (accuracy) and 14.14% (CodeBLEU).
  • Complementarity: RetriGen generated 1,598 ($Data_{old}$) and 1,818 ($Data_{new}$) unique, correct assertions that every single baseline failed to predict. This represents a 3.71X to 4.58X improvement over EditAS, the previous state-of-the-art.
  • Fault Detection: In a practical test using the Defects4J bug database, assertions generated by RetriGen successfully detected 33 real-world bugs, outperforming EditAS (which found 21).
  • Generalizability: Swapping CodeT5 for other PLMs (GraphCodeBERT, UniXcoder, CodeBERT, CodeGPT) still yielded results that comfortably beat the state-of-the-art baselines.

Limitations and Future Work

While powerful, RetriGen's fine-tuning paradigm requires notable computational resources, making the fine-tuning of billion-level parameter models expensive for average development teams. Additionally, the current implementation only utilizes the top-1 retrieved assertion.

The authors suggest that future work should explore Parameter-Efficient Fine-Tuning (PEFT) strategies to lower hardware barriers, expand the framework to support multi-shot retrieval (feeding multiple relevant assertions into the prompt), and test RetriGen's capabilities on programming languages beyond Java.

Summary updated

BibTeX
@article{zhang2025improving,
  author = {Quanjun Zhang and Chunrong Fang and Yi Zheng and Yaxin Zhang and Yuan Zhao and Rubing Huang and Jianyi Zhou and Yun Yang and Tao Zheng and Zhenyu Chen},
  title = {{Improving Deep Assertion Generation via Fine-Tuning Retrieval-Augmented Pre-trained Language Models}},
  journal = {ACM Trans. Softw. Eng. Methodol.},
  year = {2025},
  doi = {10.1145/3721128},
}
Figure