Chat-like Asserts Prediction with the Support of Large Language Model

Han Wang, Han Hu, Chunyang Chen, Burak TurhanarXiv 2024 · DOI

Introduction

In the realm of software engineering, unit testing is a critical yet labor-intensive task. While automated tools have made significant strides in generating test inputs, the creation of the "test oracle"—the assert statement that verifies the correctness of an execution—remains a formidable challenge. This is particularly true for Python, a language whose dynamic typing and flexible syntax complicate the task of inferring developer intent.

To address this, researchers have introduced CLAP (Chat-like execution-based Asserts Prediction). CLAP leverages the capabilities of Large Language Models (LLMs) and integrates them with a real-time execution environment to automatically generate meaningful and accurate test assertions.

The CLAP Framework

The core of CLAP is a pipeline that treats assertion generation as a multi-turn dialogue between an LLM and a Python interpreter. This approach moves beyond static one-pass generation, allowing the system to refine its output based on actual execution results.

Figure 1: The CLAP pipeline overview, illustrating the interaction between the Prompt Dialog Manager, LLM, and Python Interpreter.

The process begins with the Prompt Dialog Manager, which constructs sophisticated queries for the LLM. The system is designed to handle both single and multi-assertion scenarios, maintaining logical consistency across complex test cases.

Prompt Engineering and Feedback

CLAP utilizes three primary prompting strategies to maximize LLM performance:

  • Persona-based Prompting: The LLM is instructed to adopt the persona of a "unit testing expert," setting a high standard for the generated code.
  • One-shot Learning: By providing a single, relevant example from the target project, the model learns to adapt to local coding conventions and styles.
  • Chain-of-Thought (CoT): The model is prompted to reason through the problem step-by-step, identifying the focal method and parameters before generating the final assertion.
Figure 2: Detailed structure of Greeting, Query, and Feedback prompts used to guide the LLM.

A defining feature of CLAP is its "Chat-like" Feedback Loop. When the LLM generates a predicted assertion, it is immediately executed by a Python interpreter. If the assertion fails or causes an error, the resulting error message is fed back into the LLM. This allows the model to self-correct, much like a human developer debugging a test case.

Dataset and Evaluation

To evaluate CLAP, the authors compiled a new benchmark consisting of 3,696 test methods across 14 popular open-source Python projects, including scrapy, keras, and sanic. To ensure the validity of the results, the researchers meticulously filtered the data to prevent "data leakage," ensuring that the test cases were not part of the LLMs' original training sets.

Key Results and Insights

CLAP demonstrates a significant leap in performance over existing baselines:

  • Unprecedented Accuracy: CLAP achieved a 64.7% Accurate Match (AM) for single assertions, vastly outperforming traditional tools like ATLAS (7.8%) and surpassing previous LLM-based approaches like CEDAR (54.7%).
  • The Power of Feedback: The study revealed that the interpreter-driven feedback loop is the most critical component for success. While LLMs excel at logic, they often struggle with precise value calculations; the interpreter provides the necessary ground truth for correction.
  • Contextual Efficiency: Interestingly, the researchers found that "one-shot" prompting was more effective than "few-shot" prompting. A single, highly relevant example provided better guidance than multiple examples, which tended to introduce noise.
  • Real-World Validation: The practical utility of CLAP was confirmed when 7 out of 9 pull requests containing CLAP-generated assertions were accepted and merged by open-source project maintainers.

Conclusion

CLAP represents a major advancement in automated test oracle generation for Python. By combining advanced prompt engineering with an execution-based feedback loop, it achieves high accuracy and generates assertions that are often superior to those written by humans. While challenges remain—such as improving focal method identification and reducing the computational cost of multi-turn interactions—CLAP paves the way for more robust and automated software testing workflows. Future developments may see these capabilities integrated directly into developer environments as real-time coding assistants.

Summary updated

BibTeX
@misc{wang2024chat,
  author = {Han Wang and Han Hu and Chunyang Chen and Burak Turhan},
  title = {{Chat-like Asserts Prediction with the Support of Large Language Model}},
  year = {2024},
  eprint = {2407.21429},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2407.21429},
}
Figure