[论文解读] TestART: Improving LLM-based Unit Testing via Co-evolution of Automated Generation and Repair Iteration
TestART 提出了一种协同进化框架,通过模板驱动的修复和反馈驱动的生成,迭代地生成和修复基于大语言模型(LLM)的单元测试,显著提升了测试质量。其测试通过率达到 78.55%,行覆盖率高达 90.96%,较先前方法如 ChatGPT-4.0 和 EvoSuite 分别高出 18% 和 3.4%。
Unit testing is crucial for detecting bugs in individual program units but consumes time and effort. Recently, large language models (LLMs) have demonstrated remarkable capabilities in generating unit test cases. However, several problems limit their ability to generate high-quality unit test cases: (1) compilation and runtime errors caused by the hallucination of LLMs; (2) lack of testing and coverage feedback information restricting the increase of code coverage;(3) the repetitive suppression problem causing invalid LLM-based repair and generation attempts. To address these limitations, we propose TestART, a novel unit test generation method. TestART improves LLM-based unit testing via co-evolution of automated generation and repair iteration, representing a significant advancement in automated unit test generation. TestART leverages the template-based repair strategy to effectively fix bugs in LLM-generated test cases for the first time. Meanwhile, TestART extracts coverage information from successful test cases and uses it as coverage-guided testing feedback. It also incorporates positive prompt injection to prevent repetition suppression, thereby enhancing the sufficiency of the final test case. This synergy between generation and repair elevates the correctness and sufficiency of the produced test cases significantly beyond previous methods. In comparative experiments, TestART demonstrates an 18% improvement in pass rate and a 20% enhancement in coverage across three types of datasets compared to baseline models. Additionally, it achieves better coverage rates than EvoSuite with only half the number of test cases. These results demonstrate TestART's superior ability to produce high-quality unit test cases by harnessing the power of LLMs while overcoming their inherent flaws.
研究动机与目标
- 解决 LLM 生成的单元测试因代码无效、缺乏反馈以及重复抑制而导致的通过率低和覆盖率差的问题。
- 通过集成自动化修复和反馈循环,克服 LLM 在处理编译错误和运行时错误方面的局限性。
- 通过利用测试执行反馈和基于模板的修复,提升测试的可读性、正确性和覆盖率。
- 通过使用提示注入和迭代优化而非微调,降低对昂贵微调的依赖。
- 生成高质量、人类可读的测试用例,确保其语法正确且语义有效。
提出的方法
- 采用交替进行自动化测试生成与自动化修复的协同进化循环,迭代优化测试用例。
- 使用基于模板的修复方法,修正 LLM 生成测试中的编译错误和逻辑错误,确保语法正确性。
- 将测试执行反馈(如覆盖率指标、错误信息)注入提示中,以指导下一步的生成过程。
- 利用提示注入技术,防止重复抑制,避免冗余的重新生成或修复尝试。
- 整合通过测试用例的覆盖率反馈,提升测试套件的完备性,改善行覆盖率。
- 采用反馈驱动的生成策略,基于先前执行结果对 LLM 进行条件化,实现自适应优化。

实验结果
研究问题
- RQ1与单步生成相比,生成与修复的协同进化框架是否能提升 LLM 生成单元测试的通过率?
- RQ2基于模板的修复在纠正 LLM 生成测试用例中的编译错误和逻辑错误方面效果如何?
- RQ3测试执行反馈(如覆盖率、运行时错误)在多大程度上能提升生成测试的质量和覆盖率?
- RQ4反馈注入能否防止重复抑制并减少基于 LLM 的测试生成中的冗余重新生成尝试?
- RQ5在测试通过率和代码覆盖率方面,TestART 与当前最先进的方法(如 ChatGPT-4 和 EvoSuite)相比表现如何?
主要发现
- TestART 在单元测试生成任务上实现了 78.55% 的通过率,较 ChatGPT-4.0 和基于 ChatGPT-3.5 的方法 ChatUniTest 分别高出约 18%。
- 该方法在通过的焦点方法上实现了 90.96% 的行覆盖率,较 EvoSuite 的 87.56% 提高了 3.4 个百分点。
- TestART 的反馈驱动生成显著提升了测试用例的充分性,表现为更高的覆盖率以及更少的冗余或无效测试用例。
- 基于模板的修复能有效解决编译和逻辑错误,减少对完整重新生成的需求,同时最小化重复抑制现象。
- 将执行反馈整合到提示工程中,显著增强了 LLM 生成语义正确且高效的测试用例的能力。
- 协同进化循环实现了稳定、迭代式的优化,生成的测试用例比单次生成更易读、更易维护且更可靠。

更好的研究,从现在开始
从阅读论文到最终审阅,大幅缩短您的研究时间。
无需绑定信用卡
本解读由 AI 生成,并经人工编辑审核。