[论文解读] Effective Test Generation Using Pre-trained Large Language Models and Mutation Testing
本文提出 MuTAP,一种通过集成变异测试来识别并修复测试预言(test oracle)弱点,从而提升由预训练大语言模型(LLM)生成的测试用例有效性的框架。通过利用存活变异体作为反馈,迭代优化提示(prompt),MuTAP 提升了错误检测能力,在使用 Llama-2-chat 时对真实存在缺陷的代码实现了 94.06% 的错误检测率,优于 Pynguin 及零样本/少样本 LLM 方法。
One of the critical phases in software development is software testing. Testing helps with identifying potential bugs and reducing maintenance costs. The goal of automated test generation tools is to ease the development of tests by suggesting efficient bug-revealing tests. Recently, researchers have leveraged Large Language Models (LLMs) of code to generate unit tests. While the code coverage of generated tests was usually assessed, the literature has acknowledged that the coverage is weakly correlated with the efficiency of tests in bug detection. To improve over this limitation, in this paper, we introduce MuTAP for improving the effectiveness of test cases generated by LLMs in terms of revealing bugs by leveraging mutation testing. Our goal is achieved by augmenting prompts with surviving mutants, as those mutants highlight the limitations of test cases in detecting bugs. MuTAP is capable of generating effective test cases in the absence of natural language descriptions of the Program Under Test (PUTs). We employ different LLMs within MuTAP and evaluate their performance on different benchmarks. Our results show that our proposed method is able to detect up to 28% more faulty human-written code snippets. Among these, 17% remained undetected by both the current state-of-the-art fully automated test generation tool (i.e., Pynguin) and zero-shot/few-shot learning approaches on LLMs. Furthermore, MuTAP achieves a Mutation Score (MS) of 93.57% on synthetic buggy code, outperforming all other approaches in our evaluation. Our findings suggest that although LLMs can serve as a useful tool to generate test cases, they require specific post-processing steps to enhance the effectiveness of the generated test cases which may suffer from syntactic or functional errors and may be ineffective in detecting certain types of bugs and testing corner cases PUTs.
研究动机与目标
- 为解决现有基于 LLM 的测试生成工具的局限性,这些工具虽关注代码覆盖率,但常无法检测真实错误。
- 通过将变异测试作为反馈机制,提升 LLM 生成测试用例的故障揭示能力。
- 实现在无需程序UnderTest(PUT)自然语言描述的情况下,仍能有效生成测试用例。
- 通过利用变异体反馈进行迭代优化,减少因语法或功能错误导致的无效测试用例。
- 证明变异测试可作为 LLM 基于测试生成的强大评估与优化机制。
提出的方法
- MuTAP 首先基于 PUT 和指令,使用零样本或少样本提示方式,通过预训练 LLM 生成初始测试用例。
- 对生成的测试用例进行语法和返回值正确性验证,过滤掉无效用例。
- 对 PUT 应用变异测试以生成变异体,并识别出存活变异体——即未被初始测试套件杀死的变异体。
- 将存活变异体用于增强原始提示,生成包含 PUT、初始测试用例和变异体特异性失败模式的优化提示。
- 使用增强后的提示重新提示 LLM,生成能杀死先前存活变异体的改进测试用例。
- 最终对测试用例进行验证,并根据其正确性和杀死变异体的有效性进行排序。
实验结果
研究问题
- RQ1变异测试能否有效用于提升 LLM 生成测试用例的故障检测能力?
- RQ2将存活变异体整合到提示中,如何影响测试用例生成的有效性?
- RQ3MuTAP 是否能在无需 PUT 自然语言描述的情况下生成有效测试用例?
- RQ4MuTAP 在检测真实错误方面,与最先进的自动化测试生成工具(如 Pynguin)及零样本/少样本 LLM 方法相比表现如何?
- RQ5经变异反馈增强的 LLM 能在多大程度上检测边界情况和细微错误?
主要发现
- MuTAP 在合成有缺陷的代码上实现了 93.57% 的变异分数(MS),优于所有评估方法。
- 使用 Codex 时,MuTAP 在基准测试中检测到 86.72% 的真实有缺陷代码片段,显著优于基线方法。
- 使用 Llama-2-chat 时,MuTAP 检测到 94.06% 的有缺陷代码,超越了 Pynguin 和零样本/少样本 LLM 技术。
- MuTAP 比现有方法多检测出最多 28% 的有缺陷人工编写的代码片段,其中 17% 的缺陷同时未被 Pynguin 和基于 LLM 的方法检测到。
- 将存活变异体整合到提示中显著提升了测试用例质量,尤其在检测细微和边界情况错误方面表现突出。
- MuTAP 的迭代优化过程有效修正了初始 LLM 生成测试用例中的语法和功能错误,提升了其有效性和正确性。
更好的研究,从现在开始
从阅读论文到最终审阅,大幅缩短您的研究时间。
无需绑定信用卡
本解读由 AI 生成,并经人工编辑审核。