Skip to main content
QUICK REVIEW

[Paper Review] An Empirical Evaluation of Using Large Language Models for Automated Unit Test Generation

Max Schäfer, Sarah Nadi|arXiv (Cornell University)|Feb 13, 2023
Software Testing and Debugging TechniquesComputer Science53 citations
TL;DR

The paper presents TestPilot, an adaptive LLM-based tool for generating JavaScript unit tests without additional training, achieving high coverage and promoting diverse, non-copy tests across 25 npm packages. It also compares against Nessie and explores effects of different prompt components and LLMs.

ABSTRACT

Unit tests play a key role in ensuring the correctness of software. However, manually creating unit tests is a laborious task, motivating the need for automation. Large Language Models (LLMs) have recently been applied to this problem, utilizing additional training or few-shot learning on examples of existing tests. This paper presents a large-scale empirical evaluation on the effectiveness of LLMs for automated unit test generation without additional training or manual effort, providing the LLM with the signature and implementation of the function under test, along with usage examples extracted from documentation. We also attempt to repair failed generated tests by re-prompting the model with the failing test and error message. We implement our approach in TestPilot, a test generation tool for JavaScript that automatically generates unit tests for all API functions in an npm package. We evaluate TestPilot using OpenAI's gpt3.5-turbo LLM on 25 npm packages with a total of 1,684 API functions. The generated tests achieve a median statement coverage of 70.2% and branch coverage of 52.8%, significantly improving on Nessie, a recent feedback-directed JavaScript test generation technique, which achieves only 51.3% statement coverage and 25.6% branch coverage. We also find that 92.8% of TestPilot's generated tests have no more than 50% similarity with existing tests (as measured by normalized edit distance), with none of them being exact copies. Finally, we run TestPilot with two additional LLMs, OpenAI's older code-cushman-002 LLM and the open LLM StarCoder. Overall, we observed similar results with the former (68.2% median statement coverage), and somewhat worse results with the latter (54.0% median statement coverage), suggesting that the effectiveness of the approach is influenced by the size and training set of the LLM, but does not fundamentally depend on the specific model.

Motivation & Objective

  • Motivate the automation of unit test creation to reduce developer effort.
  • Evaluate whether off-the-shelf LLMs can generate effective unit tests without fine-tuning.
  • Assess coverage and test quality (assertions, non-trivial assertions) of LLM-generated tests.
  • Analyze the impact of prompt components on test generation effectiveness.
  • Compare TestPilot against existing test-generation techniques and across multiple LLMs.

Proposed method

  • Prompt-based test generation using an LLM (gpt3.5-turbo) with prompts that include function signatures, documentation, and usage examples.
  • Adaptive re-prompting: if a generated test fails, re-prompt with the failure and error message to fix the test.
  • Five-part TestPilot architecture: API Explorer, Documentation Miner, Prompt Generator, Test Validator, and Prompt Refiner.
  • Dynamic API discovery for JavaScript by inspecting the package at runtime to identify testable functions.
  • Mocha-based test generation and execution to validate and refine generated tests.
  • Comparison experiments with Nessie and with alternative LLMs (code-cushman-002 and StarCoder).

Experimental results

Research questions

  • RQ1RQ1 How much statement coverage and branch coverage do tests generated by TestPilot achieve?
  • RQ2RQ2 How effective are TestPilot’s prompts when removing or including different information components (body, usage examples, doc comments)?
  • RQ3RQ3 How does TestPilot perform across different LLMs (GPT-3.5-turbo, code-cushman-002, StarCoder)?
  • RQ4RQ4 How similar are the generated tests to existing tests (i.e., are they memorized or copied from training data)?
  • RQ5RQ5 Do the generated tests contain non-trivial assertions that exercise functionality?

Key findings

  • Median statement coverage of 70.2% and branch coverage of 52.8% achieved on 25 npm packages.
  • Nessie achieves 51.3% statement coverage and 25.6% branch coverage for comparison.
  • 92.8% of TestPilot tests have <= 50% similarity to existing tests (none are exact copies).
  • 60.0% of tests have <= 40% similarity to existing tests (and 92.8% <= 50%).
  • Adaptive re-prompting fixes about 15.6% of failing tests.
  • Results are qualitatively similar when using code-cushman-002 (68.2% stat, 51.2% branch) and StarCoder (54.0% stat, 37.5% branch).
  • All five prompt components are essential for high-quality test generation; removing any component reduces effectiveness.

Read this paper

Open the full text in the Nubint reader and ask about any passage as you read.

Read this paper on Nubint

This review was generated by AI from the original paper. Please check the paper before citing it.