[Paper Review] Can Large Language Models Write Good Property-Based Tests?
The paper investigates using GPT-4-based prompts to synthesize property-based tests (PBT) from API documentation, introducing PBT-GPT with three prompting strategies and an evaluation methodology for generator and property quality.
Property-based testing (PBT), while an established technique in the software testing research community, is still relatively underused in real-world software. Pain points in writing property-based tests include implementing diverse random input generators and thinking of meaningful properties to test. Developers, however, are more amenable to writing documentation; plenty of library API documentation is available and can be used as natural language specifications for PBTs. As large language models (LLMs) have recently shown promise in a variety of coding tasks, we investigate using modern LLMs to automatically synthesize PBTs using two prompting techniques. A key challenge is to rigorously evaluate the LLM-synthesized PBTs. We propose a methodology to do so considering several properties of the generated tests: (1) validity, (2) soundness, and (3) property coverage, a novel metric that measures the ability of the PBT to detect property violations through generation of property mutants. In our evaluation on 40 Python library API methods across three models (GPT-4, Gemini-1.5-Pro, Claude-3-Opus), we find that with the best model and prompting approach, a valid and sound PBT can be synthesized in 2.4 samples on average. We additionally find that our metric for determining soundness of a PBT is aligned with human judgment of property assertions, achieving a precision of 100% and recall of 97%. Finally, we evaluate the property coverage of LLMs across all API methods and find that the best model (GPT-4) is able to automatically synthesize correct PBTs for 21% of properties extractable from API documentation.
Motivation & Objective
- Motivate the underuse of property-based testing (PBT) in real-world software and identify pain points in generating generators and meaningful properties.
- Propose a method (PBT-GPT) to synthesize PBT components from API documentation using LLMs.
- Introduce three prompting strategies (independent, consecutive, and joint) for generator and property synthesis.
- Develop an evaluation methodology to assess generator validity/diversity and property validity/soundness/strength.
- Provide preliminary results on Python library APIs (numpy, networkx, datetime) to illustrate potential benefits and limitations.
Proposed method
- Design a prompt template that includes API documentation, system/user instructions, and a specified output format for either a generator, properties, or both.
- Define three prompting methods—independently prompting for generator and properties, prompting consecutively with context, and prompting together to produce a combined test.
- Characterize failure modes of PBT-GPT and propose an evaluation methodology focused on generator quality (validity and diversity) and property quality (validity, soundness, and strength).
- Implement and evaluate PBT-GPT on sample Python APIs using Hypothesis as the PBT framework.
- Discuss mitigation strategies and human-in-the-loop approaches to improve generator validity, diversity, and property soundness.

Experimental results
Research questions
- RQ1Can LLMs synthesize usable property-based tests from API documentation?
- RQ2How do different prompting strategies affect the quality of generated PBT components (generators and properties)?
- RQ3What are the common failure modes in LLM-generated PBT, and how can they be mitigated?
- RQ4How can we evaluate generator validity/diversity and property validity/soundness/strength for synthesized PBT?
- RQ5What preliminary results can be observed when applying PBT-GPT to numpy, networkx, and datetime APIs?
Key findings
- PBT-GPT produces promising preliminary results with generators and properties derived from API docs for numpy, networkx, and datetime.
- Three prompting strategies (independent, consecutive, joint) offer different trade-offs between generator and property synthesis.
- Generators may exhibit validity and diversity issues; properties can be invalid, unsound, or weak, necessitating mitigation or human-in-the-loop refinement.
- An evaluation methodology focusing on generator validity, generator diversity, property validity, property soundness, and property strength is proposed and demonstrated with examples.
- Mitigation strategies include continued prompting to fix invalid generators/properties and using examples to improve soundness and strength.
- Early results indicate that LLM-synthesized PBT can serve as a useful starting point for developers to iterate on tests.

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.