[Paper Review] No More Manual Tests? Evaluating and Improving ChatGPT for Unit Test Generation
This paper empirically evaluates ChatGPT for Java unit test generation, analyzes its strengths and limitations, and introduces ChatTester to improve correctness via an initial test generator and iterative refiner.
Unit testing is essential in detecting bugs in functionally-discrete program units. Manually writing high-quality unit tests is time-consuming and laborious. Although traditional techniques can generate tests with reasonable coverage, they exhibit low readability and cannot be directly adopted by developers. Recent work has shown the large potential of large language models (LLMs) in unit test generation, which can generate more human-like and meaningful test code. ChatGPT, the latest LLM incorporating instruction tuning and reinforcement learning, has performed well in various domains. However, It remains unclear how effective ChatGPT is in unit test generation. In this work, we perform the first empirical study to evaluate ChatGPT's capability of unit test generation. Specifically, we conduct a quantitative analysis and a user study to systematically investigate the quality of its generated tests regarding the correctness, sufficiency, readability, and usability. The tests generated by ChatGPT still suffer from correctness issues, including diverse compilation errors and execution failures. Still, the passing tests generated by ChatGPT 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 propose ChatTESTER, a novel ChatGPT-based unit test generation approach, which leverages ChatGPT itself to improve the quality of its generated tests. ChatTESTER 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.
Motivation & Objective
- Assess ChatGPT's correctness, sufficiency, readability, and usability in generated unit tests.
- Identify common causes of compilation and execution errors in ChatGPT-generated tests.
- Compare ChatGPT with traditional Evosuite and learning-based AthenaTest baselines.
- Propose ChatTester to improve test correctness and demonstrate gains.
- Provide practical guidelines for leveraging ChatGPT in unit test generation.
Proposed method
- Construct a benchmark of 1,000 focal method–test method data pairs from 1,748 pairs across 185 Java projects.
- Design a two-part basic prompt (natural language task description and code context) and query ChatGPT (gpt-3.5-turbo).
- Evaluate correctness via syntactic, compilation, and execution checks using Java parsers and test execution; categorize error types.
- Assess sufficiency via statement and branch coverage and number of assertions using Jacoco.
- Conduct a user study to assess readability and usability against manually-written tests with practicing Java developers.
- Develop ChatTester with an intention-focused initial test generator and an iterative validate-and-fix refiner to improve test correctness.
Experimental results
Research questions
- RQ1RQ1: How correct are ChatGPT-generated tests (syntactic, compilation, execution) and what are common error types?
- RQ2RQ2: Are ChatGPT-generated tests sufficiently thorough in coverage and assertions?
- RQ3RQ3: How readable are ChatGPT-generated tests compared to manual tests?
- RQ4RQ4: Would developers directly use ChatGPT-generated tests in real projects?
- RQ5RQ5: Does ChatTester improve correctness over default ChatGPT, and what is the contribution of its components?
Key findings
- ChatGPT-generated tests show high syntactic correctness but low overall execution success due to compilation and assertion errors.
- Only 24.8% of ChatGPT tests pass execution, while 57.9% encounter compilation errors and 17.3% fail during execution due to incorrect assertions.
- For the tests that pass execution, ChatGPT achieves high statement coverage (82.3%) and branch coverage (65.6%), with readability and usability comparable to manual tests.
- ChatGPT-generated tests resemble manual tests in number of assertions and overall structure, suggesting practical promise if correctness issues are addressed.
- AthenaTest and Evosuite have lower correctness and coverage compared to ChatGPT, while Evosuite shows higher compilable and executable rates due to post-generation filtering.
- ChatTester—comprising an initial intention-based test generator and an iterative refiner—yields 34.3% more compilable tests and 18.7% more tests with correct assertions than default ChatGPT.
Read this paper
Open the full text in the Nubint reader and ask about any passage as you read.
Read this paper on NubintThis review was generated by AI from the original paper. Please check the paper before citing it.