AI-Assisted Coding · Midcourse projectPart 4

Study explanation · Part 04 · 11 min

Tests, Break Tests and the behavior contract

Keep every existing test and add at least four new pytest tests. Prove at least two of them with a Break Test: pass, break the source on purpose, see the right failure, restore, pass again. Run a behavior contract before and after your refactor.

Listen to this lesson

Hear the lesson read aloud, with each sentence highlighted as you go.

Narrated by an AI voice.

What the brief requires

The test requirements are spread across the brief. Together they are short.

  • All existing tests stay in the suite and still pass.
  • At least 4 new pytest tests across your two features.
  • Backend test results recorded in verification.md.
  • Break Test evidence for at least two tests: at least one important Feature 1 test, and the same loop for Feature 2.
  • The full pytest suite and the behavior contract run before your refactor, and the contract again after it.

Meaningful tests

Four is a floor, not a target. Use the “good tests” column of your chosen options as a starting list. A meaningful test checks behavior, not just that the server answered: the right status code and the right body, the unchanged fields after an update, the exact set a filter returns, the empty list with a 200 when nothing matches. Follow your existing fixtures, including the storage reset, so tests do not share state.

  • Assert status plus content: ids, fields, flags, the returned set.
  • Cover the boundary: due today, a blank value, a missing id.
  • Cover the failure: invalid input returns the validation response.
  • Cover the regression: omitting the new parameter keeps old behavior.

Read with careModule 2 lists common AI mistakes in generated tests: asserting 200 when the route returns 201 or 204, a test name that does not match its body, calling json() on a 204, and a reset fixture without autouse so tests share state. Read every AI-written test before you trust it.

The Break Test

A green suite tells you the code and the tests agree. It does not tell you the tests would notice if the code were wrong. The Break Test checks that. You take one important passing test, make one small temporary change to the application source that should break the behavior it protects, and run the test again. It should fail, for the reason you expected. Then you restore the source exactly and see it pass again.

A valid Break Test · Module 2 lecture notes p. 10 · Midcourse brief p. 3
  1. 1PassThe selected test passes on correct source.
  2. 2Mutate sourceOne small, temporary change. Never the test.
  3. 3Expected failureFails on the rule it protects, not a syntax error.
  4. 4RestoreExact restoration, e.g. from the checkpoint.
  5. 5Pass + cleanPasses again; git shows no leftover change.

Key ideaIf the test does not fail when the behavior is broken, fix the test, not the production code. A test that passes while the behavior is broken is protecting nothing.

What makes the evidence convincing

Record the Break Test as steps, not as a claim. The reviewer should be able to see the passing command, the exact mutation, the failure message, why that failure is about the rule and not a typo, the restoration, the passing re-run, and proof that no temporary change is left behind.

Break Test evidence, one row per step · Study-guide synthesis of the brief and Module 2
StepRecord
Selected testIts name, and the behavior it protects.
Correct sourceThe targeted command and its passing result.
Temporary mutationFile, function, and the one change made. Test file unchanged.
Mutated resultThe same command failing, with the assertion message.
Why it is semanticOne sentence linking the failure to the rule.
RestorationHow the source was restored, then the passing re-run and full suite.
Clean stategit diff and git status showing nothing left over.

The behavior contract

The brief asks for a behavior contract before and after your refactor, but it is a Module 3 idea: a short checklist of observable behaviors, each marked PASS before you refactor and re-checked after. For the midcourse project, extend your Module 3 contract with the new features’ behaviors: the new field round-trips, the filter returns only what it should, the empty state is valid, errors stay visible. Browser items cannot be marked PASS from reading code; they need an observation.

Key takeaways

  • Keep every existing test; add at least four meaningful new ones.
  • Assert status and content, boundaries, failures and regressions.
  • A Break Test mutates source, never the test, and must fail for the semantic reason.
  • Restore exactly and prove a clean working tree.
  • Run the behavior contract before and after the refactor, with real observations.

From the midcourse project brief (4 pages), with links back to the Module 1-3 habits it reuses.

Your study checkpoint

Ready to continue?

Mark the lesson complete once you have read it and tried its idea on something real.

Next · Part 5