Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to main content

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.

This is a preview of subscription content, log in via an institution to check access.

Access this chapter

Subscribe and save

Springer+ Basic
$34.99 /Month
  • Get 10 units per month
  • Download Article/Chapter or eBook
  • 1 Unit = 1 Article or 1 Chapter
  • Cancel anytime
Subscribe now

Buy Now

Chapter
USD 29.95
Price excludes VAT (USA)
  • Available as PDF
  • Read on any device
  • Instant download
  • Own it forever
eBook
USD 59.99
Price excludes VAT (USA)
  • Available as EPUB and PDF
  • Read on any device
  • Instant download
  • Own it forever
Softcover Book
USD 74.99
Price excludes VAT (USA)
  • Compact, lightweight edition
  • Dispatched in 3 to 5 business days
  • Free shipping worldwide - see info

Tax calculation will be finalised at checkout

Purchases are for personal use only

Institutional subscriptions

Similar content being viewed by others

Notes

  1. 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. 2.

    See our support request in https://txl.ca/forum/viewtopic.php?f=22&t=1148.

  3. 3.

    xpassed tests are passing tests which were expected to fail (xfail).

References

  1. 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)

    Google Scholar 

  2. 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

    Article  Google Scholar 

  3. 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

  4. 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

  5. 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

  6. Krekel, H., et al.: pytest : helps you write better programs (2024). https://docs.pytest.org/en/8.2.x/

  7. 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

  8. 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

  9. 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

    Chapter  Google Scholar 

  10. Meszaros, G.: xUnit Test Patterns: Refactoring Test Code. Addison-Wesley Professional (2007)

    Google Scholar 

  11. 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

  12. 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

    Article  Google Scholar 

  13. 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

  14. Svajlenko, J., Roy, C.K.: BigCloneEval (December 2023). https://github.com/jeffsvajlenko/BigCloneEval, commit hash 6d393ec

  15. 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

    Article  Google Scholar 

  16. 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

  17. 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

    Article  Google Scholar 

  18. 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

Download references

This paper is partially supported by the CroFlow project: Enabling Highly Automated Cross-Organisational Workflow Planning (grant no. 326249).

Author information

Authors and Affiliations

Authors

Corresponding author

Correspondence to Violet Ka I Pun .

Editor information

Editors and Affiliations

Rights and permissions

Reprints and permissions

Copyright information

© 2025 The Author(s), under exclusive license to Springer Nature Switzerland AG

About this paper

Check for updates. Verify currency and authenticity via CrossMark

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)

Publish with us

Policies and ethics