TLDR: A new study reveals that Large Language Models (LLMs) generate significantly better summaries for unit tests when prompted with assertion messages and their semantic meanings. This ‘assertion-aware’ approach helps LLMs understand the test’s true intent more effectively than providing the full method under test, leading to more concise and human-aligned summaries. Codex and Qwen-Coder were found to be the most reliable models in aligning with human preferences, while simply providing more context doesn’t always lead to better results.
Understanding the purpose of unit tests can often be a challenge, especially in large or automatically generated codebases where documentation might be sparse or non-existent. Developers frequently rely on concise summaries or comments to quickly grasp a test’s intent, but these are often missing or outdated. This is where Large Language Models (LLMs) come into play, offering a promising avenue for automatically generating these crucial summaries.
However, summarizing test code is distinct from summarizing general production code. Test methods are designed to validate expected behaviors through ‘assertions’ rather than implementing functionality. This unique characteristic means that LLMs need to reason about validation intent, not just implementation logic. Recognizing this challenge, a recent study titled Assertion-Aware Test Code Summarization with Large Language Models by Anamul Haque Mollah, Ahmed Aljohani, and Hyunsook Do, delves into how different components of test code influence the quality of LLM-generated summaries.
The Study’s Approach
The researchers developed a new benchmark dataset comprising 91 real-world Java test cases, each accompanied by a summary written by the original developer. They then conducted a controlled experiment, evaluating four prominent code LLMs: Codex, Codestral, DeepSeek, and Qwen-Coder. The core of their investigation involved testing seven different ways of ‘prompting’ these LLMs, varying the inclusion of elements like the raw test method, assertion messages, the semantic meaning of assertions, and the ‘Method Under Test’ (MUT) – the specific piece of code being tested.
A key innovation was the generation of ‘assertion semantics.’ Since assertion statements themselves can be technical, GPT-4o was used to translate each assertion into a concise, natural-language description of its intent. For example, an assertion like assertThat("Invalid age", user.getAge(), is(equalTo(18))); would be semantically interpreted as: “Checks that the user’s age equals 18.” This semantic information was then used to enrich the prompts given to the LLMs.
To assess the quality of the generated summaries, the study employed a range of metrics. These included traditional n-gram overlap metrics (like BLEU, ROUGE-L, METEOR) which measure word-level similarity, BERTScore for deeper semantic similarity, and a unique LLM-based evaluation (LLM-Eval). In LLM-Eval, GPT-4o acted as an independent judge, rating the quality of both developer-written and LLM-generated summaries based on criteria like accuracy, conciseness, and usefulness to a developer.
Key Findings: The Power of Assertions
The study yielded several significant insights into how LLMs perform in test code summarization:
-
Assertion-Enhanced Prompts Excel: Prompts that included both assertion messages and their semantic interpretations consistently led to the highest quality summaries across all models. This suggests that providing LLMs with a clear understanding of what the test is verifying, rather than just the raw code, is crucial.
-
Limited Value of Full MUT Context: While including the Method Under Test (MUT) sometimes improved lexical overlap (meaning more similar words were used), it often didn’t translate into higher human-aligned quality scores. This indicates that MUT can introduce verbosity without consistently improving the semantic alignment of the summary.
-
Less is Sometimes More: Surprisingly, providing LLMs with *all* available context – the test code, MUT, assertion messages, and semantics – did not always outperform simpler, more focused assertion-based prompts. In some cases, too much information seemed to distract the models, leading to diminishing returns.
-
Test-Only Prompts Underperform: Relying solely on the raw test method without any behavioral or structural signals consistently resulted in the weakest summaries, often vague and poorly aligned with developer expectations.
Model Performance Differences
When comparing the LLMs, Codex and Qwen-Coder emerged as the most reliable. They demonstrated strong alignment with human preferences, especially when guided by prompts rich in behavioral context. Codestral, while achieving high scores in word-level similarity, sometimes produced summaries that were textually similar but lacked the human-preferred clarity or conciseness. DeepSeek, despite showing competitive semantic scores, consistently underperformed in the human-aligned LLM evaluations, suggesting it struggled to distill behavioral intent into truly meaningful summaries.
Also Read:
- ContextCRBench: A New Benchmark for Detailed LLM Evaluation in Code Review
- Large Language Models and Loop Invariants: A Performance Review
Implications for AI in Software Development
This research provides practical guidance for developers and researchers working with LLMs for code summarization. It highlights that focusing on the behavioral context of unit tests, particularly through assertion semantics, is key to generating clear, useful, and human-aligned summaries. It also underscores the importance of thoughtful prompt design, where targeted, lightweight behavioral components can be more effective than simply providing all available information. Furthermore, the study emphasizes that evaluating LLM-generated code summaries requires more than just word-overlap metrics; semantic fidelity and human-aligned assessments are essential to avoid misleading conclusions about quality.


