一句话:通过实证研究刻画被 mock 依赖的特征,并用机器学习实现全自动的单元测试 mock 推荐工具 MockSniffer。
单元测试中常用 mock 替换真实依赖以降低测试成本、减少 flakiness 并提升覆盖率,但哪些依赖应当被 mock 并无明确标准:mock 不足无法将被测类与其依赖隔离,mock 过度则加重维护负担并可能引发虚假失败;已有研究还显示 17% 的 mock 对象是测试创建后才引入,13% 的原有 mock 最终被取消。本文分析了四个活跃开源项目中的 10,846 个测试脚本,刻画被 mock 依赖的实践特征,进而设计并实现了全自动工具 MockSniffer:只需输入被测类及其依赖,即综合多种影响开发者决策的因素,借助机器学习给出 mock 推荐。在十个开源项目上的评估表明,MockSniffer 优于三种基线方法,并在两个潜在应用场景中表现良好。
In unit testing, mocking is popularly used to ease test effort, reduce test flakiness, and increase test coverage by replacing the actual dependencies with simple implementations. However, there are no clear criteria to determine which dependencies in a unit test should be mocked. Inappropriate mocking can have undesirable consequences: under-mocking could result in the inability to isolate the class under test (CUT) from its dependencies while over-mocking increases the developers' burden on maintaining the mocked objects and may lead to spurious test failures. According to existing work, various factors can determine whether a dependency should be mocked. As a result, mocking decisions are often difficult to make in practice. Studies on the evolution of mocked objects also showed that developers tend to change their mocking decisions: 17% of the studied mocked objects were introduced sometime after the test scripts were created and another 13% of the originally mocked objects eventually became unmocked. In this work, we are motivated to develop an automated technique to make mocking recommendations to facilitate unit testing. We studied 10,846 test scripts in four actively maintained open-source projects that use mocked objects, aiming to characterize the dependencies that are mocked in unit testing. Based on our observations on mocking practices, we designed and implemented a tool, MockSniffer, to identify and recommend mocks for unit tests. The tool is fully automated and requires only the CUT and its dependencies as input. It leverages machine learning techniques to make mocking recommendations by holistically considering multiple factors that can affect developers' mocking decisions. Our evaluation of MockSniffer on ten open-source projects showed that it outperformed three baseline approaches, and achieved good performance in two potential application scenarios.