基于LLM重构的自动生成单元测试套件人性化
Humanizing Automatically Generated Unit Test Suites with LLM-Based Refactoring
AI总结:
提出TestHumanizer混合方法,利用LLM对SBST生成的测试套件进行受控重构,提升可读性和可维护性,同时保持编译和覆盖率,实验表明重构后编译率接近100%,可读性显著提升。
摘要:
基于搜索的测试生成工具(如EvoSuite)能够大规模生成可编译且高覆盖率的单元测试,但其套件往往难以阅读和维护。LLM可以生成更自然的测试,但直接生成仍然脆弱,在我们的研究中编译率仅为51-78%。我们引入了TestHumanizer,一种混合SBST+LLM方法,将LLM作为可编译SBST套件上的受控重构层,以改进命名、结构和面向开发者的清晰度,同时保持行为和编译有效性。我们在Defects4J和SF110的350个类上评估了TestHumanizer。EvoSuite为每个类生成15个套件,每个套件在三种上下文配置下使用gpt-4o和mistral-large-2407进行重构,产生了31,500次重构。TestHumanizer的编译率达到88-98%,接近EvoSuite的100%基线,明显高于直接LLM生成。结构覆盖率基本保持不变,通常在1-2个百分点内,86-95%的重构满足复合忠实重构阈值。重构后的套件还改善了预测可读性,降低了控制流和认知复杂度,并减轻了结构坏味。基于摘要的设置提供了最稳健的权衡,而长代码为中心的提示更容易出现幻觉导致的失败。对30个类和444个测试方法的开发者研究证实,感知可读性和采用意愿显著提升,Wilcoxon p值小于0.01,且评分者间一致性较高。总体而言,LLM最有效的不是作为独立的生成器,而是作为稳健SBST输出上的验证门控精化层。
英文摘要:
Search-based test generation tools such as EvoSuite produce compilable and high-coverage unit tests at scale, but their suites are often hard to read and maintain. LLMs can generate more natural tests, yet direct generation remains brittle, with compilation rates of only 51-78% in our study. We introduce TestHumanizer, a hybrid SBST+LLM approach that uses LLMs as controlled refactoring layers over compilable SBST suites to improve naming, structure, and developer-oriented clarity while preserving behavior and compilation validity. We evaluate TestHumanizer on 350 classes from Defects4J and SF110. EvoSuite generates 15 suites per class, and each suite is refactored under three context configurations using gpt-4o and mistral-large-2407, yielding 31,500 refactorings. TestHumanizer reaches 88-98% compilation rates, close to EvoSuite's 100% baseline and clearly above direct LLM generation. Structural coverage is largely preserved, typically within 1-2 percentage points, and 86-95% of refactorings satisfy a composite faithful-refactoring threshold. Refactored suites also improve predicted readability, reduce control-flow and cognitive complexity, and mitigate structural smells. The summary-based setting offers the most robust trade-off, while long code-centric prompts are more prone to hallucination-induced failures. A developer study on 30 classes and 444 test methods confirms significant gains in perceived readability and willingness to adopt, with Wilcoxon p less than 0.01 and substantial inter-rater agreement. Overall, LLMs are most effective not as standalone generators but as validation-gated refinement layers over robust SBST outputs.
机构、总结、中文摘要均由AI分析而来,可能存在错误,请以论文正文为准。