一句话:大规模研究 Java PR 中为提升可测试性而进行的复合重构,归纳出十种模式并揭示其趋势。
论文关注为创建单元测试而对生产代码进行的可测试性重构,例如放宽方法访问或拆分类以便独立测试。作者收集了 621 个 GitHub Java 项目中的 346,841 个 PR,首先比较含测试-生产代码协同变更(test-pairs)与不含 test-pairs 的 PR 中原子重构的差异,发现前者显著包含更多如改变变量类型、改变参数类型等操作。其次人工分析 200 个显式提及 testability 或 refactor+test 的 PR,识别出十种可测试性重构模式;又分析 524 个 test-pairs PR。约 25% 的被分析 PR 实际包含这些模式,最常见的是为 override 或 invocation 提取方法、放宽方法访问以供调用、为调用提取类。论文还报告与这些模式共现的原子重构,并讨论对研究、实践与教育的启示。
To create unit tests, it may be necessary to refactor the production code, e.g. by widening access to specific methods or by decomposing classes into smaller units that are easier to test independently. We report on an extensive study to understand such composite refactoring procedures for the purpose of improving testability. We collected and studied 346,841 java pull requests from 621 GitHub projects. First, we compared the atomic refactorings in two populations: pull requests with changed test-pairs (i.e. with co-changes in production and test code and thus potentially including testability refactoring) and pull requests without test-pairs. We found significantly more atomic refactorings in test-pairs pull requests, such as Change Variable Type Operation or Change Parameter Type. Second, we manually analyzed the code changes of 200 pull requests, where developers explicitly mention the terms “testability” or “refactor + test”. We identified ten composite refactoring procedures for the purpose of testability, which we call testability refactoring patterns. Third, we manually analyzed additional 524 test-pairs pull requests: both randomly selected and where we assumed to find testability refactorings, e.g. in pull requests about dependency or concurrency issues. About 25% of all analyzed pull requests actually included testability refactoring patterns. The most frequent were extract a method for override or for invocation, widen access to a method for invocation, and extract a class for invocation. We also report on frequent atomic refactorings which co-occur with the patterns and discuss the implications of our findings for research, practice, and education.