Skip to main content
QUICK REVIEW

[论文解读] Mimicking Production Behavior with Generated Mocks

Deepika Tiwari, Martin Monperrus|arXiv (Cornell University)|Aug 2, 2022
Software Engineering Research被引用 5
一句话总结

该论文提出 rick,一个开源工具,通过监控真实生产执行,生成基于模拟的断言的逼真单元测试。它对目标方法进行插桩,捕获包括输入、返回值和交互在内的上下文数据,并利用这些数据生成可执行测试,以52.4%的成功率复现完整的执行上下文。

ABSTRACT

Mocking allows testing program units in isolation. A developer who writes tests with mocks faces two challenges: design realistic interactions between a unit and its environment; and understand the expected impact of these interactions on the behavior of the unit. In this paper, we propose to monitor an application in production to generate tests that mimic realistic execution scenarios through mocks. Our approach operates in three phases. First, we instrument a set of target methods for which we want to generate tests, as well as the methods that they invoke, which we refer to as mockable method calls. Second, in production, we collect data about the context in which target methods are invoked, as well as the parameters and the returned value for each mockable method call. Third, offline, we analyze the production data to generate test cases with realistic inputs and mock interactions. The approach is automated and implemented in an open-source tool called RICK. We evaluate our approach with three real-world, open-source Java applications. RICK monitors the invocation of 128 methods in production across the three applications and captures their behavior. Based on this captured data, RICK generates test cases that include realistic initial states and test inputs, as well as mocks and stubs. All the generated test cases are executable, and 52.4% of them successfully mimic the complete execution context of the target methods observed in production. The mock-based oracles are also effective at detecting regressions within the target methods, complementing each other in their fault-finding ability. We interview 5 developers from the industry who confirm the relevance of using production observations to design mocks and stubs. Our experimental findings clearly demonstrate the feasibility and added value of generating mocks from production interactions.

研究动机与目标

  • 解决为单元测试创建逼真模拟和测试输入的挑战,这些通常基于假设而非真实系统行为。
  • 克服合成或手动设计的模拟所存在的局限性,这些模拟可能无法反映实际的生产交互。
  • 通过利用真实世界执行数据生成验证方法与外部依赖交互的断言,提升测试的有效性。
  • 实现自动化的可执行测试生成,同时保留生产环境中观察到的实际上下文和行为。
  • 为开发人员提供反映真实使用模式的测试用例,从而增强回归检测能力和测试可维护性。

提出的方法

  • 在生产环境中对目标方法及其依赖项(可模拟的方法调用)进行插桩,以收集运行时数据。
  • 在生产执行过程中监控并记录上下文数据,包括方法参数、返回值和调用序列。
  • 离线分析捕获的数据,生成具有逼真初始状态、输入和存根模拟的测试用例。
  • 构建三种类型的基于模拟的断言:输出断言(OO)、参数断言(PO)和调用顺序断言(CO),以验证交互。
  • 将生产环境中的复杂对象状态序列化,以在单元测试中重建逼真的测试环境。
  • 使用开源工具 rick 自动化从插桩到测试生成的完整流程。

实验结果

研究问题

  • RQ1RQ1:rick 是否能够成功监控并将其观察到的生产行为转换为可执行测试用例?
  • RQ2RQ2:捕获的生产数据在生成的测试中,能在多大程度上被表达为逼真的测试输入、模拟和断言?
  • RQ3RQ3:生成的测试在多大程度上能够复现生产环境中观察到的完整执行上下文?
  • RQ4RQ4:三种基于模拟的断言(OO、PO、CO)在检测回归方面的有效性如何,它们是否能够相互补充?
  • RQ5RQ5:专业开发人员是否认为生成的测试和断言具有相关性且易于理解?

主要发现

  • rick 为三个真实世界开源 Java 应用程序中的 128 个目标方法成功生成了 294 个测试用例。
  • 52.4% 的生成测试成功复现了生产环境中观察到的完整执行上下文,包括逼真的输入、模拟和断言。
  • 三种基于模拟的断言——输出(OO)、参数(PO)和调用顺序(CO)——在检测目标方法中的回归方面相互补充。
  • 通过定性调查,五名专业开发人员评价生成的测试具有相关性且易于理解。
  • 该方法能够自动创建带有显式断言的可执行测试,这些断言直接源自生产行为,与依赖隐式断言的录制-重放技术不同。
  • 本研究证明了从生产数据自动生成孤立且逼真的单元测试的可行性,弥合了开发假设与真实系统行为之间的差距。

更好的研究,从现在开始

从阅读论文到最终审阅,大幅缩短您的研究时间。

无需绑定信用卡

本解读由 AI 生成,并经人工编辑审核。