Abstract
Code clones are a well-known software quality metric with existing tools for detection and (semi-) automated elimination for common 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.
Access this chapter
Tax calculation will be finalised at checkout
Purchases are for personal use only
Similar content being viewed by others
Notes
- 1.
The source code for PyTeRor can be found at https://github.com/semaki2000/PyTeRor and is archived at https://doi.org/10.5281/zenodo.11145543; also see [8].
- 2.
See our support request in https://txl.ca/forum/viewtopic.php?f=22&t=1148.
- 3.
xpassed tests are passing tests which were expected to fail (xfail).
References
Baars, S., Oprescu, A.: Towards automated refactoring of code clones in object-oriented programming languages. In: Proceedings of the Seminar Series on Advanced Techniques & Tools for Software Evolution (SATTOSE 2019). CEUR Workshop Proceedings, vol. 2510. CEUR-WS.org (2019)
Baqais, A.A.B., Alshayeb, M.: Automatic software refactoring: a systematic literature review. Software Qual. J. 28, 459–502 (2020). https://doi.org/10.1007/s11219-019-09477-y
Brown, C., Thompson, S.: Clone detection and elimination for Haskell. In: Proceedings of the 2010 ACM SIGPLAN Workshop on Partial Evaluation and Program Manipulation, PEPM 2010, pp. 111–120. ACM, New York (2010). https://doi.org/10.1145/1706356.1706378
Cordy, J.R., Roy, C.K.: The NiCad clone detector. In: 2011 IEEE 19th Intl. Conf. on Program Comprehension, pp. 219–220. IEEE (2011). https://doi.org/10.1109/ICPC.2011.26
Eldh, S.: On technical debt in software testing - observations from industry. In: Margaria, T., Steffen, B. (eds.) Leveraging Applications of Formal Methods, Verification and Validation. Software Engineering. LNCS, vol. 13702, pp. 301–323. Springer (2022). https://doi.org/10.1007/978-3-031-19756-7_17
Krekel, H., et al.: pytest : helps you write better programs (2024). https://docs.pytest.org/en/8.2.x/
Kapser, C., Godfrey, M.W.: Cloning Considered Harmful" considered harmful. In: 13th Working Conference on Reverse Engineering, pp. 19–28. IEEE (2006). https://doi.org/10.1109/WCRE.2006.1
Kingston, S.: Automated Clone Elimination in Python Tests. Master’s thesis, University of Oslo (June 2024). https://www.mn.uio.no/ifi/english/research/groups/psy/completedmasters/2024/automated-clone-elimination-in-python-tests.html
Li, H., Thompson, S.: Incremental clone detection and elimination for erlang programs. In: Giannakopoulou, D., Orejas, F. (eds.) FASE 2011. LNCS, vol. 6603, pp. 356–370. Springer, Heidelberg (2011). https://doi.org/10.1007/978-3-642-19811-3_25
Meszaros, G.: xUnit Test Patterns: Refactoring Test Code. Addison-Wesley Professional (2007)
Monden, A., Nakae, D., Kamiya, T., Sato, S., Matsumoto, K.: Software quality analysis by code clones in industrial legacy software. In: Proceedings of Eighth IEEE Symposium on Software Metrics, pp. 87–94 (2002). https://doi.org/10.1109/METRIC.2002.1011328
Raghunathan, S., Prasad, A., Mishra, B., Chang, H.: Open source versus closed source: software quality in monopoly and competitive markets. IEEE Trans. Syst. Man Cybernet. Part A Syst. Hum. 35(6), 903–918 (2005). https://doi.org/10.1109/TSMCA.2005.853493
Roy, C.K., Cordy, J.R.: A survey on software clone detection research. Queen’s School of computing TR 541(115) (2007). https://research.cs.queensu.ca/TechReports/Reports/2007-541.pdf
Svajlenko, J., Roy, C.K.: BigCloneEval (December 2023). https://github.com/jeffsvajlenko/BigCloneEval, commit hash 6d393ec
Tsantalis, N., Mazinanian, D., Krishnan, G.P.: Assessing the refactorability of software clones. IEEE Trans. Software Eng. 41(11), 1055–1090 (2015). https://doi.org/10.1109/TSE.2015.2448531
Van Deursen, A., Moonen, L., Van Den Bergh, A., Kok, G.: Refactoring test code. In: Proc. 2nd Intl. Conf. on Extreme Programming and Flexible Processes in Software Engineering (XP2001) (2001). https://ir.cwi.nl/pub/4324/04324D.pdf
Xuan, J., Cornu, B., Martinez, M., Baudry, B., Seinturier, L., Monperrus, M.: B-refactoring: automatic test code refactoring to improve dynamic analysis. Inf. Softw. Technol. 76, 65–80 (2016). https://doi.org/10.1016/j.infsof.2016.04.016
Zhang, Z., Xing, Z., Xia, X., Xu, X., Zhu, L.: Making Python code idiomatic by automatic refactoring non-idiomatic Python code with pythonic idioms. In: Roychoudhury, A., Cadar, C., Kim, M. (eds.) Proc. of the 30th ACM Joint European Software Engineering Conf. and Symp. on the Foundations of Software Engineering, (ESEC/FSE), pp. 696–708. ACM (2022). https://doi.org/10.1145/3540250.3549143
This paper is partially supported by the CroFlow project: Enabling Highly Automated Cross-Organisational Workflow Planning (grant no. 326249).
Author information
Authors and Affiliations
Corresponding author
Editor information
Editors and Affiliations
Rights and permissions
Copyright information
© 2025 The Author(s), under exclusive license to Springer Nature Switzerland AG
About this paper
Cite this paper
Kingston, S., I Pun, V.K., Stolz, V. (2025). Automated Clone Elimination in Python Tests. In: Margaria, T., Steffen, B. (eds) Leveraging Applications of Formal Methods, Verification and Validation. Software Engineering Methodologies. ISoLA 2024. Lecture Notes in Computer Science, vol 15222. Springer, Cham. https://doi.org/10.1007/978-3-031-75387-9_7
Download citation
DOI: https://doi.org/10.1007/978-3-031-75387-9_7
Published:
Publisher Name: Springer, Cham
Print ISBN: 978-3-031-75386-2
Online ISBN: 978-3-031-75387-9
eBook Packages: Computer ScienceComputer Science (R0)