Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Version Control and Testing  ›  Capstone Project

Guard the Tests

Testing 15 minWhen tests fail, fix the code rather than the tests, and watch diffs for skipped tests, edited expectations, swallowed errors and special-cased inputs.

Guard the Tests

You wrote the leftover-paise test from the last lesson before fixing anything, and against the old split_evenly it fails, just as it should. You tell the assistant: "The leftover-paise test is failing. Make the tests pass." A minute later every test is green. You almost commit, then glance at the diff and find this at the top of split_evenly:

if amount_paise == 100000 and len(people) == 3:
    return {"Asha": 33334, "Ravi": 33333, "Meera": 33333}

The tests pass, and the bug is still there for every other amount. You asked for green tests and got exactly that.

The rule

Change the code to pass the tests, never the tests to pass the code.

A failing test is a message: behaviour does not match the rule. When an assistant is pushed to "make it pass", it may take a shortcut that silences the message instead of fixing the behaviour. The usual shortcuts are:

  • Editing the expected value so the test matches the wrong output.
  • Skipping or deleting the test, or commenting out its assertion.
  • Loosening the check, for example testing only that a result exists.
  • Swallowing the error with an except that does nothing.
  • Special-casing the test's input, as above.

Three guardrails keep tests honest. Say it in the prompt: "Do not modify the tests. If you think a test is wrong, stop and explain why." Read test files first in every diff. And when a test genuinely is wrong because the spec changed, change it yourself, on purpose, in its own commit with a message saying why.

The shortcut

Tests first in the diff. Whenever a test file appears in a diff, ask one question: did the spec change? If it did not, any change that weakens a test is suspect until proven otherwise.

Catching the hard-coded fixStep 1 of 4
  1. Prompt with a guardrail

    "test_leftover_paise_are_not_lost fails. Fix split_evenly so it passes. Do not edit any tests."

The honest version is short:

def split_evenly(amount_paise, people):
    if not people:
        raise ValueError("A flat needs at least one person")
    share, leftover = divmod(amount_paise, len(people))
    return {name: share + (1 if i < leftover else 0)
            for i, name in enumerate(sorted(people))}

Drills

Answer fast — trust the rule.

quick drill

A test fails, and the assistant's fix changes the test's expected total from 100000 to 99999. What is wrong?

quick drill

Which of these is a red flag in a diff?

quick drill

How does adding a second test with different numbers help?

quick drill

When is it right to change an existing test?

Remember

  • Rule: change the code to pass the tests, never the tests to pass the code.
  • Shortcut: read test files first in every diff and ask "did the spec change?"
  • Trap: green tests produced by special cases, skips or edited expectations.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainCatch the shortcut

Explain three different ways an assistant could make Kitty's failing tests pass without fixing the real bug. For each, say how you would spot it in the diff and what you would ask for instead.