[论文解读] exLong: Generating Exceptional Behavior Tests with Large Language Models
exLóng 是基于 CodeLlama 微调的大型语言模型,通过推理执行轨迹、保护条件以及类似但非异常行为测试(non-EBTs)来生成高质量的异常行为测试(EBTs)。其性能优于最先进模型(CAT-LM、GPT-3.5)和基于静态分析的工具(Randoop、EvoSuite),已有 23 个 EBT 由 exLóng 生成并被开源项目采纳。
Many popular programming languages, including C#, Java, and Python, support exceptions. Exceptions are thrown during program execution if an unwanted event happens, e.g., a method is invoked with an illegal argument value. Software developers write exceptional behavior tests (EBTs) to check that their code detects unwanted events and throws appropriate exceptions. Prior research studies have shown the importance of EBTs, but those studies also highlighted that developers put most of their efforts on "happy paths", e.g., paths without unwanted events. To help developers fill the gap, we present the first framework, dubbed exLong, that automatically generates EBTs. exLong is a large language model instruction fine-tuned from CodeLlama and embeds reasoning about traces that lead to throw statements, conditional expressions that guard throw statements, and non-exceptional behavior tests that execute similar traces. We compare exLong with the state-of-the-art models for test generation (CAT-LM) and one of the strongest foundation models (GPT-4o), as well as with analysis-based tools for test generation (Randoop and EvoSuite). Our results show that exLong outperforms existing models and tools. Furthermore, we contributed several pull requests to open-source projects and 23 EBTs generated by exLong were already accepted.
研究动机与目标
- 解决自动化测试中的关键缺口:开发者因过度关注‘正常路径’而忽视异常行为的测试。
- 克服现有大型语言模型和工具在生成有意义、可读且有效的异常行为测试(EBTs)方面的局限性。
- 提供一个面向特定目标的指令微调大型语言模型框架,用于为代码中的特定 throw 语句生成 EBTs。
- 通过自动化生成高覆盖率、语义有意义的 EBTs,提升代码质量保障水平。
- 通过向真实开源项目贡献被采纳的 EBTs,展示其实际应用价值。
提出的方法
- 使用新型任务指令对 CodeLlama 进行指令微调,该指令旨在嵌入对通向目标 throw 语句的执行轨迹的推理能力。
- 整合保护表达式(条件逻辑),以控制 throw 语句的执行时机,确保语义正确性。
- 整合来自执行相似代码路径的非异常行为测试(non-EBTs)的上下文,以提升生成测试的真实性与结构合理性。
- 利用 non-EBTs 的堆栈跟踪,指导生成 EBT 中的测试设置与调用逻辑。
- 设计输入提示结构,包含方法签名、目标 throw 语句、相关保护条件以及示例 non-EBTs 以提供上下文。
- 生成的 EBT 包含适当的测试设置(如输入构造)、方法调用以及使用 JUnit 风格注解的预期异常断言。

实验结果
研究问题
- RQ1指令微调的大型语言模型能否生成语义正确且可执行的异常行为测试(EBTs)?
- RQ2exLóng 在生成 EBTs 方面与最先进基于大型语言模型的测试生成器(如 CAT-LM)及基础模型(如 GPT-3.5)相比表现如何?
- RQ3exLóng 是否能在为特定 throw 语句生成 EBTs 方面超越基于静态分析的测试生成工具(如 Randoop、EvoSuite)?
- RQ4exLóng 能在多大程度上生成被真实世界开源项目采纳的 EBTs?
- RQ5整合对执行轨迹、保护条件以及类似 non-EBTs 的推理是否能提升 EBT 的质量与覆盖率?
主要发现
- exLóng 在生成可执行且正确的 EBTs 方面优于 CAT-LM 和 GPT-3.5,尤其在精准定位特定 throw 语句方面表现更优。
- 与 Randoop、EvoSuite 等基于分析的工具相比,exLóng 生成的 EBTs 具有更高的可读性与结构正确性。
- exLóng 实现了高于基线工具的 throw 语句覆盖率,证明其在有效识别异常行为方面的优势。
- exLóng 生成的 23 个 EBT 已被开源项目采纳,验证了其在实际应用中的价值与对代码质量的积极影响。
- 整合执行轨迹、保护表达式以及 non-EBT 上下文显著提升了生成 EBT 的质量与正确性。
- exLóng 通过支持针对特定方法与 throw 语句的 EBT 生成,实现了面向开发者的实用场景,优于全程序随机或搜索驱动的测试生成方式。

阅读本论文
在 Nubint 阅读器中打开原文,边读边就任意段落提问。
在 Nubint 中阅读本论文本解读由 AI 根据论文原文生成,引用前请核对原文。