NeurIPS 2026Coming to NeurIPS?Explore papers, orals & directions

Automated Unit Test Improvement using Large Language Models at Meta

Nadia Alshahwan, Jubin Chheda, Anastasia Finogenova, Beliz Gokkaya, Mark Harman, Inna Harper, Alexandru Marginean, Shubho Sengupta, Eddy WangSigsoft fse Companion 2024

TL;DR

Meta has developed TestGen-LLM, a system that uses Large Language Models (LLMs) to automatically improve existing human-written unit tests. By employing a rigorous filtering process that guarantees improvements and prevents regressions, TestGen-LLM has been successfully deployed at Meta, demonstrating its potential for industrial-scale LLM-driven software engineering.

What Problem Does This Paper Address?

Writing comprehensive and effective unit tests is a crucial but often time-consuming aspect of software development. Existing human-written tests may miss edge cases, lack sufficient coverage, or become outdated as code evolves. While LLMs show promise in code generation, their application in a production environment for critical tasks like test improvement is hampered by concerns about reliability, hallucination, and the ability to guarantee actual improvements without introducing regressions. This paper addresses the challenge of reliably and scalably applying LLMs to enhance existing unit tests in a real-world industrial setting.

Methods

The core of TestGen-LLM is its "Assured LLM-based Software Engineering" (Assured LLMSE) approach. This involves a multi-stage filtering process applied to candidate test cases generated by LLMs:

  1. Buildability Filter: Ensures the generated test code compiles correctly within the existing project infrastructure.
  2. Passability Filter: Verifies that the generated test passes on its first execution. This is crucial for ensuring the test provides a reliable signal for regression testing.
  3. Reliability Filter (Anti-Flakiness): Uses repeated execution to discard "flaky" tests that pass inconsistently.
  4. Coverage Improvement Filter: Ensures that the generated test case actually increases the overall coverage of the test suite, preventing redundant tests.

TestGen-LLM also leverages an ensemble approach, combining outputs from multiple LLMs and prompt strategies to maximize the chances of finding effective test improvements. The system is designed to augment, not replace, human engineers, providing them with verifiable guarantees about the quality of the generated tests.

Datasets

The paper does not explicitly mention specific datasets used for training the LLMs themselves, as the focus is on the application of pre-existing internal Meta LLMs. However, the evaluation and deployment were conducted on real-world codebases from Meta's Instagram and Facebook platforms, specifically on components related to Reels and Stories products.

Key Insights and Ideas

  • "Assured" LLM-based Software Engineering is Key: The rigorous filtering process is paramount. Simply generating code with LLMs isn't enough; guarantees of improvement and non-regression are essential for industrial adoption.
  • LLMs as "Fashion Followers" Can Be Beneficial: TestGen-LLM's generated tests naturally adopt the coding style and conventions of existing human-written tests, which engineers found highly valuable and made acceptance easier.
  • Ensemble Approaches Enhance Robustness: Combining multiple LLMs and prompt strategies proved more effective than relying on a single configuration.
  • Human Oversight Remains Crucial, but LLMs Can Augment Significantly: TestGen-LLM is positioned as a tool to support engineers, not replace them, by handling the tedious aspects of test generation and providing verifiable quality assurances.
  • "Flaky" Tests are a Major Concern: The system's explicit filtering of flaky tests highlights a significant challenge in automated testing that LLMs must address.

Key Results

  • High Acceptance Rate: In production deployments, 73% of TestGen-LLM's test improvement recommendations were accepted by Meta software engineers.
  • Measurable Improvement:
    • 75% of generated test cases built correctly.
  • 57% passed reliably.
  • 25% increased coverage.
  • Production Impact: TestGen-LLM improved 10% of all test classes it was applied to during large-scale test-a-thons for Instagram and Facebook.
  • Human-Competitive Performance: During an Instagram test-a-thon, the TestGen-LLM tool ranked sixth overall among human engineers in terms of landed unit tests, demonstrating its significant added value.
  • Default Settings: Experiments led to the selection of LLM2 with a temperature of zero and the extend_coverage prompt as default settings for broader deployment.

Limitations and Future Work

  • Coverage as a Proxy: The paper acknowledges that line coverage is a proxy for "improvement" and suggests that more sophisticated criteria like mutation coverage could be explored, though they are computationally demanding.
  • Nuanced Coverage Reporting: The system could be improved to provide more detailed insights into where coverage is gained, especially when generated tests might cover units other than the direct class under test.
  • Re-prompting for Deeper Improvements: The authors suggest that TestGen-LLM could be enhanced to automatically re-prompt LLMs if a generated test only partially covers a method, aiming for more complete coverage.
  • Application-Aware Probability Distribution Resolution: Further research is needed to optimize how LLM probability distributions are resolved into code, beyond simple temperature settings.
  • Addressing "Fashion Following" of Deprecated Habits: While LLMs mimic coding styles well, they can also pick up deprecated practices. Future work will involve prompt engineering and static analysis to mitigate this.

Summary updated

BibTeX
@inproceedings{alshahwan2024automated,
  author = {Nadia Alshahwan and Jubin Chheda and Anastasia Finogenova and Beliz Gokkaya and Mark Harman and Inna Harper and Alexandru Marginean and Shubho Sengupta and Eddy Wang},
  title = {{Automated Unit Test Improvement using Large Language Models at Meta}},
  booktitle = {Sigsoft fse Companion},
  year = {2024},
  doi = {10.1145/3663529.3663839},
}
Figure