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

利用少量示例提示的大语言模型自动测试套件增强

Automated Test Suite Enhancement Using Large Language Models with Few-shot Prompting

US Booking Services Ltd.(美国预订服务有限公司);University of Glasgow(格拉斯哥大学)

Alex Chudic, Gül Çalıklı

AI总结:

本文研究利用少量示例提示生成高质量单元测试,通过评估不同测试来源对测试质量的影响,提升混合人类-人工智能代码库的测试套件质量。

摘要:

单元测试对于验证代码模块(例如类、方法)的功能正确性至关重要,但手动编写单元测试往往耗费大量人力和时间。由传统方法(如基于搜索的软件测试SBST)生成的单元测试缺乏可读性、自然性和实用性。大语言模型(LLM)最近提供了有前景的结果,并已成为开发人员日常实践的一部分。因此,软件仓库现在包含人类编写、LLM生成以及传统方法(如SBST)生成的测试。虽然LLM的零样本能力已被广泛研究,但其在单元测试生成中的少量学习潜力仍被忽视。少量提示使LLM能够从提示中的示例中学习,自动检索此类示例可以增强测试套件。本文通过实证研究探讨不同测试工件来源(包括人类、SBST或LLM)的少量提示如何影响LLM生成的单元测试质量作为程序理解工件,以及其对通过评估正确性、覆盖率以及可读性、认知复杂性和可维护性在混合人类-人工智能代码库中的贡献。我们使用GPT-4o在HumanEval和ClassEval数据集上进行了实验,GPT-4o已集成到GitHub Copilot中,并被广泛使用。我们还评估了基于检索的方法选择相关示例。我们的结果表明,LLM可以通过少量提示生成高质量的测试,人类编写的示例产生最佳的覆盖率和正确性。此外,基于问题描述和代码的结合相似性选择示例,能够一致地产生最有效的少量提示。

英文摘要:

Unit testing is essential for verifying the functional correctness of code modules (e.g., classes, methods), but manually writing unit tests is often labor-intensive and time-consuming. Unit tests generated by tools that employ traditional approaches, such as search-based software testing (SBST), lack readability, naturalness, and practical usability. LLMs have recently provided promising results and become integral to developers' daily practices. Consequently, software repositories now include a mix of human-written tests, LLM-generated tests, and those from tools employing traditional approaches such as SBST. While LLMs' zero-shot capabilities have been widely studied, their few-shot learning potential for unit test generation remains underexplored. Few-shot prompting enables LLMs to learn from examples in the prompt, and automatically retrieving such examples could enhance test suites. This paper empirically investigates how few-shot prompting with different test artifact sources, comprising human, SBST, or LLM, affects the quality of LLM-generated unit tests as program comprehension artifacts and their contribution to improving existing test suites by evaluating not only correctness and coverage but also readability, cognitive complexity, and maintainability in hybrid human-AI codebases. We conducted experiments on HumanEval and ClassEval datasets using GPT-4o, which is integrated into GitHub Copilot and widely used among developers. We also assessed retrieval-based methods for selecting relevant examples. Our results show that LLMs can generate high-quality tests via few-shot prompting, with human-written examples producing the best coverage and correctness. Additionally, selecting examples based on the combined similarity of problem description and code consistently yields the most effective few-shot prompts.

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

↑