Humanizing Automatically Generated Unit Test Suites with LLM-Based Refactoring

Wendkûuni C. Ouédraogo, Yinghua Li, Xueqi Dang, Paweł Borsukiewicz, Liang Xiao, Lingfeng Bao, Anil Koyuncu, Jacques Klein, David Lo, Tegawendé F. BissyandéarXiv 2026

TL;DR

TestHumanizer is a hybrid framework that bridges the gap between high-coverage automated test generation and human-readable code. By using Large Language Models (LLMs) as a controlled refactoring layer over Search-Based Software Testing (SBST) outputs, it produces unit tests that maintain near-perfect coverage and compilation rates (88–98%) while significantly improving readability and developer adoption.

What Problem Does This Paper Address?

Automated test generation tools like EvoSuite are incredibly efficient at achieving high structural coverage, but they suffer from a major "usability gap." The tests they produce are often mechanical, cryptic, and riddled with "test smells"—like assertion roulette or complex conditional logic—making them a nightmare for developers to read or maintain.

On the other hand, LLMs can generate natural-sounding, human-like tests. However, when used to generate tests from scratch (one-shot generation), they are brittle. They frequently "hallucinate" APIs, fail to compile (rates as low as 51%), and often miss the deep structural coverage that specialized search-based tools provide. The authors seek to combine the robustness of SBST with the linguistic elegance of LLMs.

Methods

The authors introduce TestHumanizer, a post-processing pipeline that treats the LLM not as a creator, but as a sophisticated refactorer. The workflow consists of five key stages:

  1. Test Generation: Use a standard SBST tool (EvoSuite) to generate high-coverage, compilable test suites.
  2. Code Summarization: For the "summary-based" configuration, the LLM generates a natural-language summary of the class under test, filtered by a code-aware alignment metric (SIDE) to ensure accuracy.
  3. Contextual Refactoring: The LLM refactors the SBST suite using one of three context granularities:
  4. Tests-only: Just the generated tests.
  5. Code-centric: Tests plus the full source code of the class.
  6. Summary-based: Tests plus the generated class summary.
  7. Embedding-Based Similarity Check: A validation gate using CodeBERT to ensure the refactored test hasn't drifted too far semantically from the original.
  8. Composite Semantic Screening (CTSES): A final check integrating metrics like CodeBLEU and ROUGE-L to ensure behavioral faithfulness.
Overview of the TestHumanizer workflow, showing the multi-layered validation pipeline.

Datasets

The study evaluated TestHumanizer across two major Java benchmarks:

  • Defects4J: 147 classes from 15 open-source projects, known for containing real-world faults.
  • SF110: 203 classes from 69 projects, providing a large-scale, heterogeneous sample of Java code. In total, the researchers generated 31,500 refactorings using two state-of-the-art models: gpt-4o and mistral-large-2407.

Key Insights and Ideas

  • "Less is More" Context: Surprisingly, providing the full source code (code-centric) was less robust than providing a concise summary (summary-based). Long, repetitive prompts in the code-centric mode often triggered API-level rejections or "lost-in-the-middle" effects, leading to more hallucinations.
  • LLMs as Refactorers, Not Generators: The study confirms that LLMs are far more effective as validation-gated refinement layers than as standalone generators. By starting with a "correct" (compilable/high-coverage) base, the LLM's role is constrained to improving form rather than inventing function.
  • Hallucinations are Systemic: Even with explicit constraints, LLMs occasionally modified identifiers or imports incorrectly. This highlights that validation gates (compilation and semantic checks) are not optional extras but structural requirements for any reliable AI-driven refactoring pipeline.
  • Emergent Smell Reduction: While the prompts didn't explicitly ask to remove "test smells," the push for readability naturally reduced complex patterns. For instance, "Conditional Logic" smells dropped from ~37% to ~5% on Defects4J.

Key Results

  • Compilation Stability: TestHumanizer achieved compilation rates of 88–98%, vastly outperforming direct LLM generation (51–78%) and approaching the 100% baseline of EvoSuite.
  • Coverage Preservation: Structural coverage remained nearly identical to the original SBST suites, typically staying within 1–2 percentage points. Median method coverage remained at 100%.
  • Readability Boost: Refactored suites showed significant improvements in predicted readability (Scalabrino scores) and a reduction in cognitive complexity.
  • Developer Preference: A study with professional developers and graduate students confirmed that TestHumanizer tests were perceived as significantly more readable and clear in intent. Participants expressed a much higher willingness to adopt these tests compared to raw EvoSuite outputs ($p < 0.01$).

Limitations and Future Work

The authors acknowledge several areas for growth:

  • Language and Tool Scope: The study focused exclusively on Java and EvoSuite. Future work needs to explore if these findings generalize to other languages (like Python) or different SBST tools.
  • Deep Fixture Issues: While TestHumanizer cleans up test bodies and naming, it doesn't yet tackle deep architectural "smells" like General Fixture, which require redesigning how test data is set up.
  • Agentic Repair: The current pipeline is single-pass. The authors suggest that moving toward agentic "detect-repair" loops—where the LLM iteratively fixes its own compilation errors—could push success rates even closer to 100%.

Summary updated

BibTeX
@misc{ouedraogo2026humanizing,
  author = {Wendkûuni C. Ouédraogo and Yinghua Li and Xueqi Dang and Paweł Borsukiewicz and Liang Xiao and Lingfeng Bao and Anil Koyuncu and Jacques Klein and David Lo and Tegawendé F. Bissyandé},
  title = {{Humanizing Automatically Generated Unit Test Suites with LLM-Based Refactoring}},
  year = {2026},
  eprint = {2606.28229},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2606.28229},
}
Figure