paperbot · PL 论文追踪

全部论文 待读 收藏

P2 MockSniffer

MockSniffer:面向单元测试的 mock 推荐技术
ASE Proceedings of the 35th IEEE/ACM International Conference on Automated Software Engineering2020匹配度 2/10 · 论文用机器学习做单元测试 mock 推荐,属于 AI 辅助软件测试,与用户关注的静态分析、Agentic/LLM 程序、浮点程序分析、贝叶斯程序分析等核心方向无直接交集;也未涉及 LLM/Agent 改进静态分析或形式化验证。引用 19 inbox
Hengcheng Zhu, Lili Wei, Ming Wen, Yepang Liu, Shing-Chi Cheung, Qin Sheng, Cui Zhou
crossref: 2026-09-12

AI 速览

一句话:通过实证研究刻画被 mock 依赖的特征,并用机器学习实现全自动的单元测试 mock 推荐工具 MockSniffer。

单元测试中常用 mock 替换真实依赖以降低测试成本、减少 flakiness 并提升覆盖率,但哪些依赖应当被 mock 并无明确标准:mock 不足无法将被测类与其依赖隔离,mock 过度则加重维护负担并可能引发虚假失败;已有研究还显示 17% 的 mock 对象是测试创建后才引入,13% 的原有 mock 最终被取消。本文分析了四个活跃开源项目中的 10,846 个测试脚本,刻画被 mock 依赖的实践特征,进而设计并实现了全自动工具 MockSniffer:只需输入被测类及其依赖,即综合多种影响开发者决策的因素,借助机器学习给出 mock 推荐。在十个开源项目上的评估表明,MockSniffer 优于三种基线方法,并在两个潜在应用场景中表现良好。

问题
单元测试中缺乏判断哪些依赖应被 mock 的明确标准,不当的 mock 决策会导致被测类无法隔离或产生难以维护的冗余 mock 与虚假测试失败,且开发者的 mock 决策会随时间变化。
方法
先对四个使用 mock 的开源项目中 10,846 个测试脚本进行实证研究,刻画被 mock 依赖的特征;据此设计并实现全自动工具 MockSniffer,仅以被测类及其依赖为输入,利用机器学习综合多种影响 mock 决策的因素给出推荐。
评估
在十个开源项目上评估,MockSniffer 的性能优于三种基线方法,并在两个潜在应用场景中取得良好表现。
原文摘要(Abstract)

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.

链接与引用

DOI 原文 · PDF(开放获取) ·

BibTeX
@inproceedings{paperbot5978,
  title = {MockSniffer},
  author = {Hengcheng Zhu and Lili Wei and Ming Wen and Yepang Liu and Shing-Chi Cheung and Qin Sheng and Cui Zhou},
  booktitle = {ASE},
  year = {2020},
  doi = {10.1145/3324884.3416539}
}