In API testing, deriving logical constraints on API response bodies to be used as oracles is crucial for generating test cases and performing automated testing of RESTful APIs. However, existing approaches are restricted to dynamic analysis, in which oracles are extracted via the execution of APIs as part of the system under test. In this paper, we propose a complementary LLM-based static approach in which constraints for API response bodies are mined from API specifications. We leverage large language models (LLMs) to comprehend API specifications, mine constraints for response bodies, and generate test cases. To reduce LLM hallucination, we apply an Observation-Confirmation (OC) scheme that uses initial prompts to contextualize constraints, allowing subsequent prompts to more accurately confirm their presence. Our empirical results show that RBCTest with OC prompting achieves high precision in constraint mining, with averages ranging from 85.1% to 93.6%. It also performs well in generating test cases from mined constraints, with precision ranging from 86.4% to 91.7%. We further use test cases generated by RBCTest to detect 46 mismatches between API specifications and actual response data across 19 real-world APIs. Four of these mismatches were reported in developers' forums.
翻译:在API测试中,推导API响应体的逻辑约束作为预言机制,对于生成测试用例和执行RESTful API的自动化测试至关重要。然而,现有方法局限于动态分析,即通过执行被测系统中的API来提取预言机制。本文提出一种基于大型语言模型(LLM)的互补性静态方法,从API规范中挖掘响应体约束。我们利用大型语言模型理解API规范、挖掘响应体约束并生成测试用例。为减少LLM幻觉,我们采用观察-确认(OC)方案:通过初始提示对约束进行语境化处理,使后续提示能更准确地确认其存在性。实验结果表明,采用OC提示的RBCTest在约束挖掘中实现了高精度,平均精度范围达85.1%至93.6%。在基于挖掘约束生成测试用例方面也表现优异,精度范围达86.4%至91.7%。我们进一步使用RBCTest生成的测试用例,在19个真实API中检测出46处API规范与实际响应数据的不匹配现象,其中4处不匹配已在开发者论坛中被报告。