Skip to main content
QUICK REVIEW

[Paper Review] Unit Test Case Generation with Transformers and Focal Context

Michele Tufano, Dawn Drain|arXiv (Cornell University)|Sep 11, 2020
Software Engineering ResearchComputer Science29 references8 citations
TL;DR

This paper proposes AthenaTest, a sequence-to-sequence deep learning approach that generates human-readable unit test cases by fine-tuning a BART-based transformer on a large parallel corpus of real Java methods and their corresponding test cases. It achieves 43.7% coverage of focal methods with only 30 attempts and outperforms GPT-3 in test case correctness and developer preference, while matching EvoSuite’s test coverage.

ABSTRACT

Automated unit test case generation tools facilitate test-driven development and support developers by suggesting tests intended to identify flaws in their code. Existing approaches are usually guided by the test coverage criteria, generating synthetic test cases that are often difficult for developers to read or understand. In this paper we propose AthenaTest, an approach that aims to generate unit test cases by learning from real-world focal methods and developer-written testcases. We formulate unit test case generation as a sequence-to-sequence learning task, adopting a two-step training procedure consisting of denoising pretraining on a large unsupervised Java corpus, and supervised finetuning for a downstream translation task of generating unit tests. We investigate the impact of natural language and source code pretraining, as well as the focal context information surrounding the focal method. Both techniques provide improvements in terms of validation loss, with pretraining yielding 25% relative improvement and focal context providing additional 11.1% improvement. We also introduce Methods2Test, the largest publicly available supervised parallel corpus of unit test case methods and corresponding focal methods in Java, which comprises 780K test cases mined from 91K open-source repositories from GitHub. We evaluate AthenaTest on five defects4j projects, generating 25K passing test cases covering 43.7% of the focal methods with only 30 attempts. We execute the test cases, collect test coverage information, and compare them with test cases generated by EvoSuite and GPT-3, finding that our approach outperforms GPT-3 and has comparable coverage w.r.t. EvoSuite. Finally, we survey professional developers on their preference in terms of readability, understandability, and testing effectiveness of the generated tests, showing overwhelmingly preference towards AthenaTest.

Motivation & Objective

  • To address the poor readability and understandability of machine-generated unit tests in existing automated testing tools.
  • To improve test case generation by learning from real-world developer-written test cases and focal methods.
  • To investigate the impact of pretraining on natural language and source code, as well as focal context, on test generation quality.
  • To build and release Methods2Test, the largest publicly available supervised parallel corpus of Java test cases and their corresponding focal methods.
  • To evaluate the effectiveness, coverage, and developer preference of generated test cases compared to state-of-the-art tools like EvoSuite and GPT-3.

Proposed method

  • Formulate unit test generation as a sequence-to-sequence learning task using a transformer-based model.
  • Perform two-stage training: denoising pretraining on a large unsupervised corpus of Java source code and English text, followed by supervised fine-tuning on the test generation task.
  • Incorporate focal context—contextual code surrounding the focal method—to improve generation quality.
  • Utilize the Methods2Test dataset, comprising 780K test case pairs mined from 91K GitHub repositories, for supervised fine-tuning.
  • Apply transfer learning techniques to adapt a pre-trained BART model to the test case generation task.
  • Evaluate generated test cases via execution, measuring line and condition coverage, and conduct a developer survey on readability and understandability.

Experimental results

Research questions

  • RQ1How does pretraining on natural language and source code corpora impact the quality of generated unit test cases?
  • RQ2To what extent does incorporating focal context improve the quality and coverage of generated test cases?
  • RQ3How does the performance of AthenaTest compare to GPT-3 and EvoSuite in terms of test coverage and correctness?
  • RQ4How do professional developers perceive the readability and understandability of test cases generated by AthenaTest versus EvoSuite?
  • RQ5Can a fine-tuned transformer model generate test cases that are both correct and indistinguishable from human-written code?

Key findings

  • Pretraining on both natural language and source code reduced validation loss by 25% relative improvement compared to non-pretrained models.
  • Incorporating focal context provided an additional 11.1% improvement in validation loss over pretraining alone.
  • AthenaTest generated passing test cases for 43.7% of focal methods across five defects4j projects with only 30 attempts.
  • Test cases generated by AthenaTest achieved comparable test coverage to EvoSuite, with 51 lines and 41 conditions covered for the isNumber method, outperforming GPT-3 in correctness and coverage.
  • Developer surveys showed a strong preference for AthenaTest-generated test cases over EvoSuite’s, citing better readability and understandability.
  • AthenaTest successfully generated correct, idiomatic test cases that covered all conditions in the focal methods, such as properly asserting null returns and valid value creation.

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.