Automated Unit Test Refactoring
Open Access
- 19 June 2025
- journal article
- Published by Association for Computing Machinery (ACM) in Proceedings of the ACM on Software Engineering
- Vol. 2 (FSE) , 713-733
Abstract
Test smells arise from poor design practices and insufficient domain knowledge, which can lower the quality of test code and make it harder to maintain and update. Manually refactoring of test smells is time-consuming and error-prone, highlighting the necessity for automated approaches. Current rule-based refactoring methods often struggle in scenarios not covered by predefined rules and lack the flexibility needed to handle diverse cases effectively. In this paper, we propose a novel approach called UTRefactor, a context-enhanced, LLM-based framework for automatic test refactoring in Java projects. UTRefactor extracts relevant context from test code and leverages an external knowledge base that includes test smell definitions, descriptions, and DSL-based refactoring rules. By simulating the manual refactoring process through a chain-of-thought approach, UTRefactor guides the LLM to eliminate test smells in a step-by-step process, ensuring both accuracy and consistency throughout the refactoring. Additionally, we implement a checkpoint mechanism to facilitate comprehensive refactoring, particularly when multiple smells are present. We evaluate UTRefactor on 879 tests from six open-source Java projects, reducing the number of test smells from 2,375 to 265, achieving an 89% reduction. UTRefactor outperforms direct LLM-based refactoring methods by 61.82% in smell elimination and significantly surpasses the performance of a rule-based test smell refactoring tool. Our results demonstrate the effectiveness of UTRefactor in enhancing test code quality while minimizing manual involvement.Keywords
All Related Versions
This publication has 26 references indexed in Scilit:
- A comprehensive catalog of refactoring strategies to handle test smells in Java-based systemsSoftware Quality Journal, 2024
- Machine learning-based test smell detectionEmpirical Software Engineering, 2024
- An empirical evaluation of RAIDE: A semi-automated approach for test smells detection and refactoringScience of Computer Programming, 2023
- Empirical analysis of security vulnerabilities in Python packagesEmpirical Software Engineering, 2023
- On the co-occurrence of refactoring of test and source code2022
- Refactoring Test Smells With JUnit 5: Why Should Developers Keep Up-to-Date?IEEE Transactions on Software Engineering, 2022
- Test Smell Detection Tools: A Systematic Mapping Study2021
- tsDetect: an open source test smells detection tool2020
- Refactoring Test Smells2020
- Code Smell Detecting Tool and Code Smell-Structure Bug Relationship2012