一句话:提出全自动重构框架,识别单元测试中手写的继承式 mock 并改写为 Mockito 调用。
单元测试需要隔离被测单元,但软件单元之间高度耦合,开发者常用 mocking 以「伪造」对象替换依赖。尽管已有成熟的 mocking 框架,实践中开发者仍偏好「手写」方案——继承:创建依赖类的子类并通过方法覆写来模拟行为。这种做法实现繁琐,并损害单元测试的设计质量。本文提出一个全自动重构框架,自动识别这类继承式 mock 的使用并将其替换为 Mockito 机制;框架的设计建立在五个使用继承进行 mocking 的开源项目的经验之上。作者在另外四个项目上评估该方法,结果表明框架高效、对新的数据集普遍适用,在检测缺陷(以变异体形式度量)方面基本保持测试用例行为,并使测试代码与生产代码解耦。有经验的开发者所做的定性评估表明,框架生成的自动重构结果从多个方面提升了单元测试质量,例如使测试条件更显式,并改善测试用例的内聚性、可读性、可理解性与可维护性。
Unit testing focuses on verifying the functions of individual units of a software system. It is challenging due to the high inter-dependencies among software units. Developers address this by mocking-replacing the dependency by a "faked" object. Despite the existence of powerful, dedicated mocking frameworks, developers often turn to a "hand-rolled" approach-inheritance. That is, they create a subclass of the dependent class and mock its behavior through method overriding. However, this requires tedious implementation and compromises the design quality of unit tests. This work contributes a fully automated refactoring framework to identify and replace the usage of inheritance by using Mockito-a well received mocking framework. Our approach is built upon the empirical experience from five open source projects that use inheritance for mocking. We evaluate our approach on four other projects. Results show that our framework is efficient, generally applicable to new datasets, mostly preserves test case behaviors in detecting defects (in the form of mutants), and decouples test code from production code. The qualitative evaluation by experienced developers suggests that the auto-refactoring solutions generated by our framework improve the quality of the unit test cases in various aspects, such as making test conditions more explicit, as well as improved cohesion, readability, understandability, and maintainability with test cases.
DOI 原文 ·
@inproceedings{paperbot6421,
title = {An automatic refactoring framework for replacing test-production inheritance by mocking mechanism},
author = {Xiao Wang and Lu Xiao and Tingting Yu and Anne Woepse and Sunny Wong},
booktitle = {FSE},
year = {2021},
doi = {10.1145/3468264.3468590}
}