A Large-scale Empirical Study on Fine-tuning Large Language Models for Unit Testing
Ye Shang, Quanjun Zhang, Chunrong Fang, Siqi Gu, Jianyi Zhou, Zhenyu Chen
TL;DR
This paper presents the first large-scale empirical study on fine-tuning 37 LLMs across three unit testing tasks (test generation, assertion generation, test evolution), using five benchmarks and eight metrics. It systematically compares fine-tuning to prompt engineering, analyzes the impact of model series, architecture, and size, and provides practical guidelines for applying LLMs to unit testing tasks. The results show LLMs generally surpass prior state-of-the-art methods, with decoder-only models often performing best, while prompt-engineering approaches demonstrate strong potential in several tasks. The work also highlights ongoing challenges, including data leakage concerns and the limited bug-detection capability of generated tests, and emphasizes runtime-based metrics as more informative for evaluation. Overall, the study offers actionable insights and benchmarks to guide future research and practice in LLM-based unit testing.
Abstract
Unit testing plays a pivotal role in software development, improving software quality and reliability. However, generating effective test cases manually is time-consuming, prompting interest in unit testing research. Recently, Large Language Models (LLMs) have shown potential in various unit testing tasks, including test generation, assertion generation, and test evolution, but existing studies are limited in scope and lack a systematic evaluation of the effectiveness of LLMs. To bridge this gap, we present a large-scale empirical study on fine-tuning LLMs for unit testing. Our study involves three unit testing tasks, five benchmarks, eight evaluation metrics, and 37 popular LLMs across various architectures and sizes, consuming over 3,000 NVIDIA A100 GPU hours. We focus on three key research questions: (1) the performance of LLMs compared to state-of-the-art methods, (2) the impact of different factors on LLM performance, and (3) the effectiveness of fine-tuning versus prompt engineering. Our findings reveal that LLMs outperform existing state-of-the-art approaches on all three unit testing tasks across nearly all metrics, highlighting the potential of fine-tuning LLMs in unit testing tasks. Furthermore, large-scale, decoder-only models achieve the best results across tasks, while encoder-decoder models perform better under the same parameter scale. Additionally, the comparison of the performance between fine-tuning and prompt engineering approaches reveals the considerable potential capability of the prompt engineering approach in unit testing tasks. We then discuss the concerned issues on the test generation task, including data leakage issues, bug detection capabilities, and metrics comparisons. Finally, we further pinpoint carious practical guidelines for LLM-based approaches to unit testing tasks in the near future.
