arXivDaily arXiv每日学术速递 周一至周五更新
arXiv 2409.16739cs.SE
收藏

自动化单元测试重构

Automated Unit Test Refactoring

Yi Gao, Xing Hu, Xiaohu Yang, Xin Xia

AI总结:

针对测试异味导致测试代码质量低、维护难及现有自动化重构方法灵活性不足的问题,提出基于LLM的上下文增强框架UTRefactor,通过思维链模拟手动重构并结合检查点机制,在6个开源Java项目测试中实现89%异味消除率,性能显著优于现有方法。

摘要:

测试异味源于不良的设计实践和不足的领域知识,会降低测试代码质量,使其更难维护和更新。手动重构测试异味耗时且易出错,凸显了自动化方法的必要性。当前基于规则的重构方法在预定义规则未覆盖的场景中常面临困难,且缺乏有效处理多样化案例所需的灵活性。本文提出一种名为UTRefactor的新方法,这是一个用于Java项目中自动测试重构的上下文增强型、基于LLM的框架。UTRefactor从测试代码中提取相关上下文,并利用包含测试异味定义、描述和基于DSL的重构规则的外部知识库。通过思维链方法模拟手动重构过程,UTRefactor引导LLM逐步消除测试异味,确保重构全程的准确性和一致性。此外,我们实现了一个检查点机制,以促进全面重构,尤其是在存在多种异味的情况下。我们在来自6个开源Java项目的879个测试上评估UTRefactor,将测试异味数量从2375个减少到265个,减少率达89%。UTRefactor在异味消除方面比直接基于LLM的重构方法性能高出61.82%,并显著优于基于规则的测试异味重构工具。我们的结果证明,UTRefactor在提升测试代码质量的同时能最大限度减少人工参与。

英文摘要:

Test smells arise from poor design practices and insufficient domain knowledge, which can lower the quality of test code and make it harder to maintain and update. Manually refactoring test smells is time-consuming and error-prone, highlighting the necessity for automated approaches. Current rule-based refactoring methods often struggle in scenarios not covered by predefined rules and lack the flexibility needed to handle diverse cases effectively. In this paper, we propose a novel approach called UTRefactor, a context-enhanced, LLM-based framework for automatic test refactoring in Java projects. UTRefactor extracts relevant context from test code and leverages an external knowledge base that includes test smell definitions, descriptions, and DSL-based refactoring rules. By simulating the manual refactoring process through a chain-of-thought approach, UTRefactor guides the LLM to eliminate test smells in a step-by-step process, ensuring both accuracy and consistency throughout the refactoring. Additionally, we implement a checkpoint mechanism to facilitate comprehensive refactoring, particularly when multiple smells are present. We evaluate UTRefactor on 879 tests from six open-source Java projects, reducing the number of test smells from 2,375 to 265, achieving an 89% reduction. UTRefactor outperforms direct LLM-based refactoring methods by 61.82% in smell elimination and significantly surpasses the performance of a rule-based test smell refactoring tool. Our results demonstrate the effectiveness of UTRefactor in enhancing test code quality while minimizing manual involvement.

机构、总结、中文摘要均由AI分析而来,可能存在错误,请以论文正文为准。

↑