Use Property-Based Testing to Bridge LLM Code Generation and Validation

Lehan He, Zeren Chen, Zhe Zhang, Jing Shao, Xiang Gao, Lu Sheng · OpenReview

Abstract

Large Language Models (LLMs) excel at code generation, but ensuring their outputs to be functionally correct, especially in complex programming tasks, is a persistent challenge. While traditional Test-Driven Development (TDD) offers a path for code refinement, its efficacy with LLMs is often undermined by the scarcity of high-quality test cases or the pitfalls of automated test generation, including biased tests or inaccurate output predictions that can misdirect the correction process. This paper introduces Property-Generated Solver, a novel framework that leverages Property-Based Testing (PBT) to validate high-level program properties or invariants, instead of relying on specific input-output examples. These properties are often simpler to define and verify than directly predicting exhaustive test oracles, breaking the "cycle of self-deception" where tests might share flaws with the code they are meant to validate. Property-Generated Solver employs two collaborative LLM-based agents: a Generator dedicated to code generation and iterative refinement, and a Tester that manages the PBT life-cycle and formulate semantically rich feedback from property violations. The resulting comprehensive and actionable feedback then guides the Generator in its refinement efforts. By establishing PBT as the core validation engine within this iterative, closed-loop paradigm, Property-Generated Solver provides a robust mechanism for steering LLMs towards more correct and generalizable code. Extensive experimental results on multiple code generation benchmarks demonstrate that Property-Generated Solver achieves substantial pass@1 improvements, ranging from 23.1% to 37.3% relative gains over established TDD methods.

Introduction: The Challenge of Automated Code Validation

While LLMs have shown remarkable proficiency in generating code, ensuring functional correctness remains a significant hurdle. The standard approach to automated refinement is Test-Driven Development (TDD), where a model generates code, runs it against test cases, and uses the resulting errors to iterate. However, this approach often falls into a "cycle of self-deception": if an LLM possesses a flawed understanding of a complex problem, it is likely to replicate that same logical error when generating test oracles. Consequently, the buggy code passes the buggy test, leaving errors undetected.

Figure 1: The cycle of self-deception in traditional TDD vs. the clarity of explicit properties.

Furthermore, for many complex problems—such as NP-hard tasks—generating a correct specific output is just as difficult as writing the solution itself. This makes traditional example-based testing a bottleneck for high-level reasoning tasks.

The Property-Generated Solver (PGS) Framework

To address these limitations, we introduce the Property-Generated Solver (PGS). Instead of relying on specific input-output examples, PGS utilizes Property-Based Testing (PBT) to validate high-level invariants. For instance, rather than checking if a sorting function produces a specific list, PBT verifies that the output is always non-decreasing and contains the same elements as the input, regardless of the numbers provided.

As shown in the architecture below, PGS operates through a collaborative two-agent system:

  1. The Tester: This agent analyzes the problem description to define abstract properties (invariants). It translates these into executable Python check functions and generates a diverse set of inputs to "stress test" the code.
  2. The Generator: This agent writes the initial code and iteratively refines it based on feedback from the Tester.
Figure 3: The iterative PGS workflow featuring the Generator and Tester agents.

A critical innovation in this loop is the use of minimized error-inducing inputs. Inspired by "delta debugging," the Tester identifies the simplest possible input that triggers a property violation. This prevents the Generator from getting lost in complex execution paths and provides a clear, unambiguous signal of where the logic failed.

Experimental Evaluation

PGS was evaluated across three prominent code generation benchmarks: HumanEval, MBPP (Mostly Basic Python Problems), and LiveCodeBench. The framework was tested using state-of-the-art models, including DeepSeek-Coder-V2, Qwen2.5-Coder, and DeepSeek-R1-Distilled-32B.

The results demonstrate that PGS consistently outperforms established TDD and self-debugging baselines. On HumanEval, PGS achieved an 89.0% pass@1 score using DeepSeek-Coder-V2, a significant jump from the 76.2% achieved via direct prompting. Overall, the framework yielded relative gains between 23.1% and 37.3% compared to standard TDD methods.

The advantage was most pronounced on difficult problems. On "Hard" LiveCodeBench tasks, PGS using DeepSeek-R1-Distilled-32B achieved a 40.7% pass@1, compared to the 28.1% achieved by direct prompting. This suggests that as problem complexity increases, the value of high-level property validation grows significantly.

Discussion and Insights

A key finding of this research is that validation is often easier than generation for LLMs. For "Hard" tasks, the gap was massive: a 1.1% direct pass rate vs. a 48.9% accuracy in generating valid properties. Because properties (like "the sum of the parts must equal the whole") are semantically simpler than full algorithms, the Tester is less likely to mirror the Generator's logical errors, effectively breaking the self-deception cycle.

However, the system's success depends on the Tester's ability to generate meaningful properties. If the LLM generates trivial or irrelevant properties, the validation becomes toothless. Additionally, the iterative nature of PBT requires more tokens and compute time than single-turn generation.

Conclusion

Property-Generated Solver establishes PBT as a robust validation engine for LLM code generation. By shifting the focus from specific examples to general properties, PGS provides a more reliable path toward functionally correct and generalizable code, particularly for complex competitive programming tasks.

Summary updated

BibTeX
@misc{he2025use,
  author = {Lehan He and Zeren Chen and Zhe Zhang and Jing Shao and Xiang Gao and Lu Sheng},
  title = {{Use Property-Based Testing to Bridge LLM Code Generation and Validation}},
  year = {2025},
  eprint = {2506.18315},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2506.18315},
}
Figure