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.


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

0
下载
关闭预览

相关内容

大语言模型中的检索与结构化增强生成综述
专知会员服务
32+阅读 · 2025年9月17日
【ICML2025】大语言模型是自我示范预选择器
专知会员服务
12+阅读 · 2025年6月9日
定制化大型语言模型的图检索增强生成综述
专知会员服务
37+阅读 · 2025年1月28日
《大语言模型的数据合成与增强综述》
专知会员服务
43+阅读 · 2024年10月19日
《大型语言模型代码生成》综述
专知会员服务
68+阅读 · 2024年6月4日
「大型语言模型评测」综述
专知会员服务
70+阅读 · 2024年3月30日
大型语言模型在预测和异常检测中的应用综述
专知会员服务
70+阅读 · 2024年2月19日
自然语言处理中的语言模型预训练方法
PaperWeekly
14+阅读 · 2018年10月21日
NLP通用模型诞生?一个模型搞定十大自然语言常见任务
人工智能头条
10+阅读 · 2018年6月29日
国家自然科学基金
4+阅读 · 2017年12月31日
国家自然科学基金
1+阅读 · 2015年12月31日
国家自然科学基金
0+阅读 · 2015年12月31日
国家自然科学基金
0+阅读 · 2014年12月31日
国家自然科学基金
0+阅读 · 2014年12月31日
国家自然科学基金
0+阅读 · 2014年12月31日
国家自然科学基金
1+阅读 · 2014年12月31日
国家自然科学基金
0+阅读 · 2014年12月31日
国家自然科学基金
0+阅读 · 2014年12月31日
VIP会员
相关VIP内容
大语言模型中的检索与结构化增强生成综述
专知会员服务
32+阅读 · 2025年9月17日
【ICML2025】大语言模型是自我示范预选择器
专知会员服务
12+阅读 · 2025年6月9日
定制化大型语言模型的图检索增强生成综述
专知会员服务
37+阅读 · 2025年1月28日
《大语言模型的数据合成与增强综述》
专知会员服务
43+阅读 · 2024年10月19日
《大型语言模型代码生成》综述
专知会员服务
68+阅读 · 2024年6月4日
「大型语言模型评测」综述
专知会员服务
70+阅读 · 2024年3月30日
大型语言模型在预测和异常检测中的应用综述
专知会员服务
70+阅读 · 2024年2月19日
相关基金
国家自然科学基金
4+阅读 · 2017年12月31日
国家自然科学基金
1+阅读 · 2015年12月31日
国家自然科学基金
0+阅读 · 2015年12月31日
国家自然科学基金
0+阅读 · 2014年12月31日
国家自然科学基金
0+阅读 · 2014年12月31日
国家自然科学基金
0+阅读 · 2014年12月31日
国家自然科学基金
1+阅读 · 2014年12月31日
国家自然科学基金
0+阅读 · 2014年12月31日
国家自然科学基金
0+阅读 · 2014年12月31日
Top
微信扫码咨询专知VIP会员