Evaluating and Improving ChatGPT for Unit Test Generation
Top Cited Papers
Open Access
- 12 July 2024
- journal article
- research article
- Published by Association for Computing Machinery (ACM) in Proceedings of the ACM on Software Engineering
- Vol. 1 (FSE) , 1703-1726
Abstract
Unit testing plays an essential role in detecting bugs in functionally-discrete program units ( e.g. , methods). Manually writing high-quality unit tests is time-consuming and laborious. Although the traditional techniques are able to generate tests with reasonable coverage, they are shown to exhibit low readability and still cannot be directly adopted by developers in practice. Recent work has shown the large potential of large language models (LLMs) in unit test generation. By being pre-trained on a massive developer-written code corpus, the models are capable of generating more human-like and meaningful test code. In this work, we perform the first empirical study to evaluate the capability of ChatGPT ( i.e ., one of the most representative LLMs with outstanding performance in code generation and comprehension) in unit test generation. In particular, we conduct both a quantitative analysis and a user study to systematically investigate the quality of its generated tests in terms of correctness, sufficiency, readability, and usability. We find that the tests generated by ChatGPT still suffer from correctness issues, including diverse compilation errors and execution failures (mostly caused by incorrect assertions); but the passing tests generated by ChatGPT almost resemble manually-written tests by achieving comparable coverage, readability, and even sometimes developers’ preference. Our findings indicate that generating unit tests with ChatGPT could be very promising if the correctness of its generated tests could be further improved. Inspired by our findings above, we further propose ChatTester , a novel ChatGPT-based unit test generation approach, which leverages ChatGPT itself to improve the quality of its generated tests. Chat Tester incorporates an initial test generator and an iterative test refiner. Our evaluation demonstrates the effectiveness of ChatTester by generating 34.3 % more compilable tests and 18.7 % more tests with correct assertions than the default ChatGPT. In addition to ChatGPT, we further investigate the generalization capabilities of ChatTester by applying it to two recent open-source LLMs ( i.e. , CodeLlama-Instruct and CodeFuse) and our results show that ChatTester can also improve the quality of tests generated by these LLMs.Keywords
This publication has 50 references indexed in Scilit:
- An Analysis of the Automatic Bug Fixing Performance of ChatGPT2023
- ChatGPT and Software Testing Education: Promises & Perils2023
- Automatically generating test cases for safety-critical software via symbolic executionJournal of Systems and Software, 2023
- An empirical study of automated unit test generation for PythonEmpirical Software Engineering, 2023
- An empirical investigation on the readability of manual and generated test cases2018
- Modeling readability to improve unit tests2015
- A Survey on Unit Testing Practices and Problems2014
- The Daikon system for dynamic detection of likely invariantsScience of Computer Programming, 2007
- A survey of unit testing practicesIEEE Software, 2006
- Software unit test coverage and adequacyACM Computing Surveys, 1997