HOW-TO · DEV
How to set up property-based testing by prompting an AI to generate edge cases for your functions
Target environment
Ubuntu 24.04 · Ollama 0.4.x
PREREQUISITES
Property-based testing library installed (Hypothesis for Python or QuickCheck for Haskell), at least one function with defined input/output contracts, and an AI assistant.
What this does
Property-based testing verifies that functions satisfy invariant properties across hundreds of automatically generated inputs. This guide explains how to engage an AI assistant to analyze function signatures, infer properties, and generate Hypothesis strategies and test cases that expose edge cases the developer may not have anticipated.
Steps
- Provide the AI assistant with the function source code, including docstrings, type hints, and any documented preconditions.
- Ask the AI to list five to ten property invariants the function should satisfy (for example: output is always positive, output length equals input length, result is deterministic for the same input).
- Request Hypothesis strategies for each input parameter that cover normal values, boundary values, empty inputs, and type mismatches.
- Ask the AI to produce the complete
@givendecorated test functions using these strategies and the identified invariants. - Run the generated tests with
pytestand observe any counterexamples that Hypothesis prints. - Pass the counterexample back to the AI and request stronger invariants or additional preconditions.
- Add
@settingsconfiguration to control deadline, max_examples, and suppress_health_check parameters. - Repeat steps 5–7 until no counterexamples remain or all failures are understood.
Verification
pytest tests/property/test_calculator.py -v
Expected output:
tests/property/test_calculator.py::test_division_by_zero_raises PASSED
tests/property/test_calculator.py::test_result_always_positive PASSED
Hypotheses found 0 failing examples.
Common failures
- Hypothesis reports a counterexample that violates the property — The function contains a real bug. Fix the function code and re-run to confirm the counterexample no longer appears.
- Generated strategy produces type errors — The Hypothesis strategy generates values of the wrong type. Update the strategy definition to match the expected input type, or filter invalid values using
.filter(). - Tests run but coverage is low — The AI generated strategies that only cover a narrow range of inputs. Ask the AI to broaden the strategy ranges and add boundary value cases explicitly.
- DeadlineExceeded health check warnings — Generated test functions are too slow. Reduce the example count or use
@settings(deadline=None)to disable deadline enforcement.
Related guides
- Create integration test suites for REST API endpoints (guide 25)
- Test and iterate on system prompt designs (guide 32)