AI-Assisted Coding · Module 3Lesson 6

Study explanation · Lesson 06 · 15 min

Test and debug from evidence

Part 3.5 returns to the backend. Brainstorm PATCH edge cases, write one pytest test at a time, prove each by breaking the source on purpose, and debug from the full failure output with fixes to the source, never to the test.

Listen to this lesson

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

Narrated by an AI voice.

Why the module returns to the backend

Every drag and every modal save is a PATCH, so the frontend now leans on the backend’s validation more than ever. The demo uses Copilot Chat to brainstorm PATCH edge cases that are not yet covered, then adds one focused test at a time, and proves each one by breaking the source it protects.

Key ideaAI can help write tests, but tests earn trust only when you prove they fail for the bug they claim to catch.

Candidate PATCH edge cases

The lecture’s list of candidates. Five have a definite expected result; the last depends on how your own model is written.

Candidate PATCH edge cases · Lecture notes pp. 10-11
ScenarioExpected result
Unsupported status such as "Archived"422 validation error.
Invalid priority such as "Urgent"422 validation error.
Invalid transition from InProgress back to ToDo422 invalid status transition.
Valid transition from InProgress to Done200 success and updated status.
PATCH to a task id that does not exist404 not found.
Empty JSON body or unsupported update shapeBackend-defined validation response; confirm against the actual current model and tests.

Read with careThe notes give no fixed answer for an empty or oddly shaped PATCH body. Read your model and existing tests before writing an expected status into a test.

The test verification loop

Seven steps, one test at a time. Steps 4 to 6 are the part people skip, and the part that tells you whether the test is real.

  1. Ask the assistant to brainstorm edge cases first. Do not request code yet.
  2. Pick one scenario and generate one pytest test in tests/test_tasks.py using the existing fixture pattern.
  3. Run the targeted test with pytest -k <test_name> -v. It should pass on correct source code.
  4. Deliberately break the source code that the test is supposed to protect.
  5. Run the test again. It must fail for the expected reason. If it still passes, the assertion is weak or wrong.
  6. Restore the source code and rerun the test. It should pass again.
  7. Repeat for each new edge-case test and then run the full suite.
Proving one generated test · Lecture notes p. 11
  1. E1BrainstormScenarios, no code
  2. E2One testExisting fixture pattern
  3. Passespytest -k <test_name> -v
  4. E3Break the sourceIt must fail, for the right reason
  5. RestorePasses again; then the full suite

The lecture’s example: a backward transition

The visible example creates a task, moves it from ToDo to InProgress, then tries to move it back to ToDo. The correct answer is a 422 with an invalid-transition message. To prove the test means something, the demo temporarily allows that transition in the source, watches the test fail because it now gets 200 instead of 422, and then restores the business rule. The notes print the behavior, not the code.

Behavior from the notes# Behavior being tested, not exact required code create task -> status is ToDo PATCH status to InProgress -> expect 200 PATCH status back to ToDo -> expect 422 assert the response body mentions an invalid status transition

Read with careDeliberate breakage is temporary by definition. Change the source, watch the failure, and restore it before you commit. The test itself never changes during the check.

Debug from failure output

For the debugging practice you introduce or meet a subtle bug, run pytest, and paste the full failure output into chat. Ask for four things: the root cause, the file and line likely involved, why the failure happens, and the fix in the source code. Do not ask the assistant to change the test unless the test itself is demonstrably wrong. Prompt E4 makes the assistant declare whether its fix is a cause fix or symptom suppression, and forbids weakening assertions or wrapping the failure in try/except.

Cause fix or symptom suppression

A test going green is not the goal; the behavior being right is. The notes pair each kind of suppression with the cause fix you should accept instead.

Two ways to make a red test green · Lecture notes pp. 11-12

Symptom suppression · reject

  • Edit the assertion to match the bug
  • Delete the body check
  • Wrap the call in try/except
  • Change tests without a root cause

Cause fix · accept

  • Restore the business rule
  • Fix the source to return the message
  • Fix the await, field, code or condition
  • Smallest source change, explained
Reject symptom suppression, accept cause fixes · Lecture notes pp. 11-12
Reject symptom suppressionAccept cause fixes
Changing an assertion from 422 to 200 just because the current code returned 200.Restoring the transition rule so invalid backward movement returns 422 again.
Deleting the assertion on the response body.Keeping the assertion and fixing the source so the expected message is returned.
Adding try/except around a failing call to hide the error.Fixing the missing await, wrong field name, wrong status code, or incorrect condition that caused the failure.
Changing tests broadly without explaining the root cause.Making the smallest source change and explaining why it addresses the failure.

The four-line debugging log

The deliverable is short on purpose. Four lines are enough to show that you used evidence and made a decision rather than accepting whatever came back.

Four-line debugging log · Lecture notes p. 12
LineWhat to write
1. Bug or failureWhat you broke or what failed, stated briefly.
2. EvidenceFailing test name plus the key status code, response body, or traceback line.
3. AI diagnosisThe assistant's stated root cause and proposed source fix.
4. DecisionAccept or reject, with one sentence explaining why it is a cause fix or symptom suppression.

Key takeaways

  • Brainstorm edge cases before asking for test code, then write one test at a time.
  • A test is trusted only after it fails for the bug it claims to catch, and passes again once restored.
  • Paste the full pytest output and ask for root cause, location, reason and a source fix.
  • Reject any fix that edits the expectation, deletes an assertion or hides the error.
  • Log the bug, the evidence, the AI’s diagnosis and your decision in four lines.

Built from the Module 3 lecture notes, prompt library and additional readings; the module videos and slides were not supplied.

Your study checkpoint

Ready to continue?

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

Next · Lesson 7