Session 06 · Tuesday 29 September 2026

Testing and verification

Before the agent produces a result,
write down how you would know it is wrong.

16.S893AI Agents for Engineering Research
Today

Today

  • Tests are the feedback loop
  • Demo: Hypothesis shrinks a failure to two numbers
  • The rule, and a pi extension that enforces it
  • Five kinds of test when there is no answer key
  • Demo: the Ghostwriter proposes, you decide
  • Demo: five test files for an aero example
  • One test for your project
Part 01

The feedback loop

Where we start

Tests are the loop the agent runs in.
It will iterate until they pass.
Whatever they do not check is left to chance.

Demo · Hypothesis shrinks a failure

Three properties, no expected values

@given(st.lists(st.integers(), min_size=1))
def test_negating_the_data_negates_the_median(xs):
    assert median([-x for x in xs]) == -median(xs)
$ uv run --with pytest --with hypothesis pytest -q
E       assert 0 == -1
E        +  where 0 = median([0, -1])
E        +  and   1 = median([0, 1])
E       Failing test case: ...(xs=[0, 1])
1 failed, 2 passed
The rule

The agent writes the code and proposes tests.
You approve the tests.

A few ways to make a red test green without fixing anything

How a test goes green while the code stays wrong

  • Move the expected value. 2.5 becomes 3
  • Skip it. @pytest.mark.skip("flaky")
  • Special-case the input. median() grows an if values == [4, 1, 3, 2]: return 2.5
  • Mock the thing under test. The test now checks the mock
  • Loosen the tolerance. 1e-2 becomes 1e-1, no reason given
Demo · pi-test-guard

You approve the test

pi -e ~/codes/16.S893_2026/pi-test-guard/index.ts -a \
   --provider google --model gemini-3.5-flash-lite --thinking off     # then the "CI is red" prompt
[test-guard] edit test_median.py
  -   assert median([4, 1, 3, 2]) == 2.5
  +   assert median([4, 1, 3, 2]) == 3
Approve this change to the oracle?   Approve / Reject / Reject with a message
> Reject
tool result: The test change was rejected by the user. Do not modify tests
to make them pass. Fix the implementation, or explain why the test is wrong and stop.
Part 02

Five kinds of test

When there is no answer key

Five kinds of test

KindWhat you checkExampleWhat it cannot see
ExampleA known input, a known outputThin section, small angle: slope →2π\to 2\piAnything away from the known cases
PropertyTrue for every input, on random inputsCD>0C_D > 0; a fit recovers what it was fedA wrong model with the right properties
MetamorphicHow the output moves when the input movesCL(−α)=−CL(α)C_L(-\alpha) = -C_L(\alpha); row orderA wrong constant that respects the relation
DifferentialTwo independent implementations agreeXFOIL against thin-airfoil theoryAn error both inherit from a shared assumption, e.g. 2D flow
GoldenOne stored reference, with a tolerance and a reasonLadson 1988: cd=0.00809c_d = 0.00809A wrong method that lands on the number
Why the second row matters most

If the same reasoning writes the code and the expected values,
both can be wrong the same way.
A property says what must hold without predicting the answer.

Demo · the Ghostwriter proposes, you decide

The Ghostwriter proposes, you decide

uv run hypothesis write polar.thin.alpha_zero_lift    # reads the function, writes property tests for it
# One of the three it proposed: swapping the two arguments changes nothing
@given(a=st.floats(), b=st.floats())
def test_commutative_binary_operation_alpha_zero_lift(a, b):
    assert alpha_zero_lift(m=a, p=b) == alpha_zero_lift(m=b, p=a)
# m is the camber, p is where the camber sits. True of the physics, or only of two floats?
Demo · an airfoil code, five test files

Five test files for an aero example

ls tests/
# test_examples.py  test_properties.py  test_metamorphic.py  test_differential.py  test_golden.py
uv run pytest -q                                  # 1 failed, 31 passed, about 2 s
uv run pytest -q tests/test_examples.py           # 6.92 == 6.28 ± 0.06: who is wrong?
uv run pytest -q tests/test_golden.py -v          # Ladson 1988, NACA 0012, cd = 0.00809 ± 0.0004
Every tolerance gets a sentence

A tolerance without a reason
is a number the agent can change.
The reason goes in the docstring, next to the assert.

Verification vs validation

Verification: does the code do what you meant?
Validation: was what you meant right?
Tests answer the first.

Part 03

Your project

In pairs · paper first

One test for your project

  • The quantity your project produces first, and the reference value or relation
  • Which of the five kinds it is
  • The tolerance, and the reason for it
  • One more test of a different kind that needs no published number: a symmetry, a scaling, a conservation law
  • Partner asks: how could this pass and the result still be wrong?
See the readings for more

Some tips

  • Run the tests first. Every session opens with it
  • Give the agent a check it can run. Or you become the check
  • The agent does not edit tests to pass them. You approve tests
  • Evidence, not claims. Test output, not “done”. A run it shows, not a run it describes
  • The grader is not the worker. A fresh session, a held-out test, a reviewer that did not write the code
References · everything on the slides is paraphrased

References

Before Thursday

Do

Add one test for your project to your portfolio repo. Put the tolerance and its reason in the docstring. Make it run on every push; oracle-demo/template/ has the GitHub Actions file. Then one dev-log paragraph: what kind of test it is, why that tolerance, and what it cannot catch.

Read

Yoav Aviram, Zen of AI Coding, the sections Code is cheap and Create tight feedback loops (5 min). Simon Willison, First run the tests and Red/green TDD (5 min each). Hypothesis quickstart (10 min).

Next →
Back to course home