利用生成的Mock模拟生产环境行为
Mimicking Production Behavior with Generated Mocks
AI总结:
针对Mock测试设计难的问题,提出通过监控生产环境方法调用生成带真实Mock交互测试的三阶段方法,实现为开源工具RICK,实验验证其可行性与实用价值。
摘要:
Mock技术支持对程序单元进行隔离测试。使用Mock编写测试的开发人员面临两大挑战:设计单元与其环境之间符合实际的交互,以及理解这些交互对单元行为的预期影响。本文提出通过监控生产环境中的应用,生成借助Mock模拟真实执行场景的测试。\n本方法分为三个阶段。第一,对一组待生成测试的目标方法,以及它们调用的方法(我们称之为可Mock方法调用)进行插桩。第二,在生产环境中收集目标方法被调用时的上下文数据,以及每次可Mock方法调用的参数与返回值。第三,离线分析生产数据,生成带有真实输入和Mock交互的测试用例。\n该方法已实现自动化,并集成在名为RICK的开源工具中。我们使用三个真实的开源Java应用评估了该方法:RICK在生产环境中监控了三个应用共128个方法的调用并捕获其行为;基于捕获的数据,RICK生成包含真实初始状态、测试输入以及Mock和桩(stub)的测试用例。所有生成的测试用例均可执行,其中52.4%的用例成功模拟了生产环境中观测到的目标方法的完整执行上下文。基于Mock的预言机还能有效检测目标方法中的回归问题,二者的故障检测能力互为补充。我们采访了5位行业开发者,他们证实了利用生产观测结果设计Mock和桩的实用性。实验结果清晰表明,从生产交互中生成Mock具备可行性与附加价值。
英文摘要:
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.
机构、总结、中文摘要均由AI分析而来,可能存在错误,请以论文正文为准。