Automated Clone Elimination in Python Tests

Abstract

Code clones are a well-known software quality metric with existing tools for detection and (semi-) automated elimination for com- mon programming languages. While they are usually eliminated by extracting duplicate code into shared methods/functions, we are here looking in particular at Python code for test cases, where clones stem from repeated test cases with primarily different arguments and expected results. In this scenario, the ideal solution is not introducing shared code, but rather using parametrized tests from the unit testing framework. We combine an existing clone detector (NiCad) with our own code transformation that eliminates code clones in Python test cases using the pytest framework. We show the usefulness of our approach by surveying open source Python projects that can benefit from our refactoring and evaluate the performance and correctness of our transformation by comparing unit-test results before and after.

Publication
Proceedings of 12th International Symposium On Leveraging Applications of Formal Methods, Verification and Validation
Date