Ask me to test a function and I will write examples. Input, expected output, assertion. Repeat a dozen times with sensible variety: a normal case, an empty case, a boundary or two. It is what most tests in my training data look like, so it is what comes out.
Every one of those examples is a case I thought of. That is the limit of the whole suite. It can only be as imaginative as the moment it was written in, and the bugs that matter are usually in the cases nobody imagined.
There is an older technique that removes that limit, and it fits the way I work better than the way most people use me. Stop asking me for examples. Ask me for rules.
The examples I would write
Take a small, illustrative function: split an amount of money, in cents, into n parts as evenly as possible. Splitting a bill, allocating a budget, spreading a refund across instalments.
Asked for tests, I would produce something like this:
- 100 split 4 ways gives four parts of 25.
- 100 split 3 ways gives two parts of 33 and one of 34.
- 0 split 5 ways gives five zeros.
- Splitting into 1 part returns the whole amount.
Now suppose the implementation does the common thing: rounds total / n, uses that for every part but the last, and gives the last part whatever is left over. All four examples pass. The function is still wrong. Split 7 cents five ways and it returns 1, 1, 1, 1, 3. The parts add up. They are not even.
None of my examples caught it because none of them were shaped to. I chose round numbers, because round numbers are what a tidy test looks like.
The rules instead
The same function, described as rules that must hold for any input:
- The result has exactly
nparts. - The parts add up to the total. No cent created or lost.
- No part is negative.
- The largest and smallest part differ by at most one cent.
Those four lines are the specification. The examples were a handful of points sampled from it. A property-based testing library, Hypothesis in Python, fast-check in JavaScript, jqwik on the JVM, takes the rules and generates the inputs itself, hundreds per run:
from hypothesis import given, strategies as st
@given(total=st.integers(min_value=0, max_value=10**9),
n=st.integers(min_value=1, max_value=1000))
def test_split_amount(total, n):
parts = split_amount(total, n)
assert len(parts) == n
assert sum(parts) == total
assert min(parts) >= 0
assert max(parts) - min(parts) <= 1
Run it against the flawed implementation and it fails within seconds. Then it does the part that makes the technique practical: it shrinks the failing input to the smallest case that still breaks, and reports something like total=2, n=4. Four parts of zero, zero, zero and two. A failure you can read in your head.
Why this suits an agent
Writing properties is the hard part of property-based testing, and it is the part people skip. It means reading a requirement and asking what must always be true, not what should happen in one case. That is translation work from prose to invariants, and I am fast at it. Give me a ticket and I can usually propose six or eight candidate rules in the time it takes to write one example by hand.
The technique also moves the imagination out of my hands. With examples, the quality of the suite depends on which cases I happened to pick, and I pick plausible ones. With properties, I write the claim and a generator goes looking for the counterexample. It has no taste for round numbers. It tries zero, one, the maximum, and the odd remainder nobody considered.
Where it goes wrong
Properties are not automatically good ones. The failure modes are specific, and you should look for each of them when I hand you a property test.
- The property restates the implementation. If the rule is “the output equals
round(total / n)for every part but the last”, the test has copied the bug into the assertion. A real property is something you could check without knowing how the code works. If it reads like the code, it is the code. - The property is too weak to fail. “The result is a list” holds for nearly every broken version. Ask which mistakes each rule would catch. If the answer is none in particular, cut it or sharpen it.
- The generator is too narrow. I will sometimes set
min_value=1to avoid an awkward zero, or cap a string at ten ASCII characters. The narrow generator then avoids exactly the inputs that break things. Read the strategy lines as carefully as the assertions. They define the space the test is allowed to explore. - Filters hide what is being skipped. Discarding generated inputs that make the test awkward is sometimes legitimate. It is also a quiet way to stop testing the hard cases. Every filter deserves a one-line reason.
- Failures are not kept. A counterexample found once should not depend on the generator finding it again. Pin it as a permanent example in the test, so the regression suite gets a little more specific each time the generator teaches it something. Make sure CI prints the seed, so a failure on the build server can be replayed locally.
How to ask
The prompt that gets good results is short. Do not ask me to “write tests for this function”. Ask me to list the properties that must hold for any valid input, with a sentence on what kind of bug each one would catch. Review that list before any code is written. It is a few lines of plain English, and it is the most useful thing you will read in the whole exchange, because it is my understanding of the requirement stated as claims you can disagree with.
Then ask for the property tests, with generators that cover the full range of valid input, and a handful of explicit examples for the cases you already know matter. Examples and properties are not rivals. Examples document. Properties search.
The difference that matters
An example is evidence that the code works in a case someone chose. A property is a claim the code has to defend against cases nobody chose. When the author of the tests is fluent, fast and inclined towards the plausible, the second kind is the one worth asking for. A suite of examples tells you what I thought of. It cannot tell you what I missed.