Revisiting and Improving Retrieval-Augmented Deep Assertion Generation

Weifeng Sun, Hongyan Li, Meng Yan, Yan Lei, Hongyu Zhang · DOI

TL;DR

This paper introduces EDITAS, a novel approach for automatically generating assertions in unit tests. By analyzing the limitations of existing methods, EDITAS combines information retrieval with a deep learning-based editing mechanism to more effectively understand and adapt retrieved assertions, significantly outperforming prior state-of-the-art techniques.

What Problem Does This Paper Address?

Unit testing is crucial for software quality, but manually writing assertions (the "test oracles" that define expected behavior) is time-consuming. Previous research has explored automated assertion generation, notably an "Integration" approach that combines information retrieval (IR) with deep learning (DL). While promising, the "Integration" method had a knowledge gap: it wasn't clear why or where it succeeded or failed. This paper aims to bridge that gap by deeply analyzing "Integration," identifying its weaknesses, and proposing an improved method, EDITAS, to overcome them. The core challenge is that existing methods struggle to truly understand the semantic differences between a test's context (the "focal-test") and retrieved assertions, leading to incorrect modifications or an inability to handle complex edits like adding or deleting code elements.

Methods

The paper's core contribution is the EDITAS (Retrieve-and-Edit Assertion Generation) approach. It consists of two main components:

  1. Retrieval Component: Given an input "focal-test" (the test prefix and the method under test), this component searches a large corpus of existing test-assertion pairs (TAPs) to find the most similar focal-test. The assertion from this similar TAP is then used as a "prototype." This leverages the idea that similar code contexts often have similar expected outcomes.
  2. Edit Component: This is the novel part. Instead of generating assertions from scratch or just performing simple token replacements, EDITAS uses a sequence-to-sequence neural network. This network is trained to edit the prototype assertion. It learns "assertion edit patterns" by analyzing the semantic differences between the input focal-test and the retrieved similar focal-test. This allows EDITAS to understand how to modify the prototype assertion to match the specific context of the input focal-test, going beyond simple replacements to handle more complex edits.

Datasets

The experiments were conducted on two publicly available datasets:

  • Dataold: Derived from the original ATLAS dataset, this dataset was filtered to exclude test methods with unknown tokens (tokens not present in the focal-test or vocabulary).
  • Datanew: This dataset expands on Dataold by including cases with unknown tokens that were previously excluded. This makes Datanew more representative of realistic data distributions.

Both datasets were split into training, validation, and test sets with an 8:1:1 ratio.

Key Insights and Ideas

  • Retrieval is Key, but Not Enough: The analysis of the "Integration" method revealed that its success heavily relies on the initial retrieval step. Finding similar assertions is a good starting point, but adapting them correctly is the real challenge.
  • Semantic Understanding is Crucial: "Integration" struggled because it couldn't grasp the meaning behind code differences. This led to incorrect edits or a failure to edit when necessary. EDITAS addresses this by explicitly learning semantic differences.
  • Beyond Simple Replacements: Existing methods were limited to token replacement. EDITAS's ability to learn diverse edit patterns, including additions and deletions, makes it far more flexible and generalizable.
  • Assertions as Prototypes: Using retrieved assertions as editable prototypes is a powerful idea. It leverages existing knowledge while allowing for precise adaptation to new contexts.

Key Results

EDITAS significantly outperformed all baselines, including the "Integration" method, on both accuracy and BLEU scores.

  • Overall Improvement: EDITAS achieved an average improvement of 10.00%-87.48% in accuracy and 3.30%-42.65% in BLEU score across the two datasets.
  • Specific Gains: Compared to the "Integration" method, EDITAS showed an average accuracy improvement of 10.00%.
  • Assertion Type Effectiveness: EDITAS performed better than baselines across most assertion types, particularly for standard JUnit assertions, demonstrating its generality.

Limitations and Future Work

  • Dataset Specificity: The experiments were conducted on Java datasets. While the approach uses language-agnostic features (edit sequences), its performance on other languages is not yet demonstrated.
  • Lexical Similarity for Retrieval: The retrieval component relies on lexical similarity. This could lead to retrieving assertions that are lexically similar but semantically different, though EDITAS's edit component aims to mitigate this.
  • Compilability Not Evaluated: The authors acknowledge that they did not evaluate the compilability of generated assertions, as this is a challenging task dependent on external factors.

Future work includes:

  • Integrating contextual information and leveraging code language models for more effective code change handling.
  • Extending EDITAS to support other programming languages to enhance its generalizability.

Summary updated

BibTeX
@inproceedings{sun2023revisiting,
  author = {Weifeng Sun and Hongyan Li and Meng Yan and Yan Lei and Hongyu Zhang},
  title = {{Revisiting and Improving Retrieval-Augmented Deep Assertion Generation}},
  booktitle = {ASE},
  year = {2023},
  doi = {10.1109/ase56229.2023.00090},
}
Figure