AI-Assisted Coding · Module 2Lesson 5

Study explanation · Lesson 05 · 15 min

Tests that can fail: pytest and the Break Test

Cursor can generate tests with good names and plausible assertions, but a green suite proves nothing on its own. You review the generated tests, run them, then deliberately break production code and confirm the right tests fail.

Listen to this lesson

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

Narrated by an AI voice.

A passing suite is not enough

The notes call testing the climax of the module. Cursor can generate tests with good names and plausible assertions, but a passing suite is not enough. A test is only useful if it fails when the behavior it protects is broken.

Key ideaA test that passes when the code is broken is lying.

Prompt D1: fixtures and named tests

Prompt D1 generates two files. tests/conftest.py holds a TestClient fixture, an autouse reset fixture so every test starts with empty storage, and a created_task fixture. tests/test_tasks.py holds named tests covering create, list, get, patch, transitions and delete. The notes ask for 16 or more; D1 names 17 and forbids skipping or renaming any of them. It also forbids AsyncClient, mocking storage, and calling r.json() on a 204 response.

Expected test coverage · Lecture notes p. 9
AreaExamples
CreateValid task returns 201; missing title and blank title return 422.
ListEmpty list returns 200; filters by status or priority return 200.
Get by idExisting task returns 200; missing id returns 404.
PatchValid updates return 200; missing id returns 404; invalid payloads return 422.
Status transitionsValid transition returns 200; invalid ToDo -> Done returns 422.
DeleteExisting task returns 204 with no body; missing id returns 404.

Review before running pytest

Read the generated tests before you run them. Each of these points is a place where a test can pass for the wrong reason or fail for the wrong reason.

  • Missing title test sends {}, not a blank title string.
  • Blank title test sends spaces, such as {"title": " "}.
  • Create task test expects 201.
  • Delete existing task test expects 204 and asserts r.content == b"" instead of calling r.json().
  • Invalid transition test checks an actually invalid transition, such as ToDo -> Done.
  • The storage reset fixture has autouse=True so tests do not leak state.

Commands from the notespip install "pytest>=8" "httpx>=0.27" pytest tests/ -v # Expected: 16 or more tests, all PASSED.

The Break Test

After the suite is green, deliberately break production code and rerun pytest. This proves the tests are actually protecting important behavior. The notes give two breaks: remove the transition check, then disable the blank-title check. Each break should make a specific test fail, and each restore should bring the suite back to green.

  1. Comment out the validate_status_transition call in the PATCH route.
  2. Run pytest. The invalid-transition test should fail, typically because it expected 422 but got 200.
  3. Restore the validator call and rerun pytest. The suite should pass again.
  4. Break the title validator temporarily, for example by making the blank-title check ineffective.
  5. Run pytest. The blank-title test should fail. Restore the validator and rerun the full suite.
One Break Test cycle · Lecture notes p. 10
  1. Green suitepytest tests/ -v passes.
  2. Break on purposeComment out the validator call.
  3. RerunThe test that protects it must fail.
  4. RestorePut the code back.
  5. Green againThe full suite passes.

If nothing fails at step 3, the test is not protecting that behavior. Fix the test, not the production code.

Read with careWith D1’s suite, the same-status test also depends on the transition validator, so removing it can fail two tests, not one. The author’s own run saw exactly that. The notes say “at least” the invalid-transition test should fail.

If the Break Test does not fail

Fix the test, not the production code. A test that passes while the relevant behavior is broken is not protecting anything. Prompt D3 is built for this moment: you paste the break you introduced, the tests you expected to fail and the actual output, and the prompt forbids the assistant from repairing the deliberate bug. If an expected test passed, it asks for the missing or weak assertion and a test-only fix. Prompt D2 is its everyday partner: for ordinary pytest failures, it makes the assistant state for each one whether it is fixing the TEST or the PRODUCTION CODE, and forbids weakening assertions to get green.

Key idea“Green by weakening the test” undoes the whole point of the Break Test.

Common AI mistakes in generated tests

The notes list six. Several of them make a test pass when it shouldn’t, which is exactly what the Break Test exposes.

  • Asserts 200 when the endpoint correctly returns 201 or 204.
  • Test name says missing_title but the body still contains a title key.
  • Calls r.json() on a 204 response.
  • Uses async test patterns when the suite is synchronous.
  • Forgets autouse=True on the reset fixture, causing tests to share state.
  • Uses the wrong id type or a hard-coded id that does not exist.

What you leave Part 2.4 with

Part 2.4 ends with a suite you have proved, not just run.

  • tests/conftest.py with storage reset, TestClient, and helper fixtures.
  • tests/test_tasks.py with 16 or more named tests.
  • A green pytest run.
  • Break Test proof that transition and title validation tests fail when the corresponding production code is broken.
  • A short note describing what the tests caught and what, if anything, you had to correct.

Key takeaways

  • A test is useful only if it fails when the behavior it protects is broken.
  • Review generated tests before running them: {} for missing title, spaces for blank, 204 with empty content, autouse reset.
  • Break the transition check and the title check on purpose; the right tests must fail.
  • If a Break Test doesn’t fail, fix the test, never the production code.

Built from the Module 2 lecture notes and prompt library; page references are in the source notes below.

Your study checkpoint

Ready to continue?

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

Next · Lesson 6