用大模型从Java文档自动生成测试断言,提升代码验证效率
Generating executable oracles to check conformance of client code to requirements of JDK Javadocs using LLMs
- 利用大模型解析Javadoc生成可执行的测试断言
- 生成的断言98.8%可编译,96.4%准确反映预期行为
- 适合需要自动化验证Java库使用合规性的开发者
软件测试仍是验证代码质量最常用的方法,但其有效性高度依赖测试用例的质量。测试用例由输入值和输出校验两部分构成,后者称为测试断言(test oracle)。尽管测试输入生成已取得进展,如模糊测试和符号执行,但测试断言的自动化仍较少被研究。本工作聚焦于广泛使用的Java库(如java.lang、java.util)客户端的测试断言生成。关键洞察是:核心Java库的Javadoc提供了丰富信息,包含对库行为及客户端正确用法的自然语言描述。我们利用大语言模型构建测试断言自动化框架,并进行实验评估。结果表明,LLMs能从Javadoc生成正常与异常行为的断言,其中98.8%可编译,96.4%准确反映预期属性。少数错误断言误差较小,可通过模型生成的附加注释轻松修正。
原文摘要 · Abstract (English)
Software testing remains the most widely used methodology for validating quality of code. However, effectiveness of testing critically depends on the quality of test suites used. Test cases in a test suite consist of two fundamental parts: (1) input values for the code under test, and (2) correct checks for the outputs it produces. These checks are commonly written as assertions, and termed test oracles. The last couple of decades have seen much progress in automated test input generation, e.g., using fuzzing and symbolic execution. However, automating test oracles remains a relatively less explored problem area. Indeed, a test oracle by its nature requires knowledge of expected behavior, which may only be known to the developer and may not not exist in a formal language that supports automated reasoning. Our focus in this paper is automation of test oracles for clients of widely used Java libraries, e.g., java.lang and java.util packages. Our key insight is that Javadocs that provide a rich source of information can enable automated generation of test oracles. Javadocs of the core Java libraries are fairly detailed documents that contain natural language descriptions of not only how the libraries behave but also how the clients must (not) use them. We use large language models as an enabling technology to embody our insight into a framework for test oracle automation, and evaluate it experimentally. Our experiments demonstrate that LLMs can generate oracles for checking normal and exceptional behaviors from Javadocs, with 98.8% of these oracles being compilable and 96.4% accurately reflecting intended properties. Even for the few incorrect oracles, errors are minor and can be easily corrected with the help of additional comment information generated by the LLMs.
Thank you to arXiv for use of its open access interoperability. PaperDance 不是 arXiv 官方产品;中文卡片由大模型生成,请以原文为准。