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

Test What It Should Do

Testing 15 minChoose test cases yourself from the spec, covering normal, edge and wrong inputs, and watch each test fail once so you know it can catch a bug.

Test What It Should Do

Asking an assistant for tests is easy, and it will write them quickly. But if it looks at the code it just wrote and writes tests to match, the tests simply repeat the code's assumptions, bugs included. A test suite that agrees with a bug is worse than none, because it makes the bug look approved.

The rule

You pick the test cases from the spec; the assistant may type them.

A test is a small piece of code that runs part of your program and checks the result against what you expected. The important decision in any test is not the syntax but the case: which input, and which expected result. Those come from your spec and your understanding of the rules, not from the code under test.

Here are three tests for Kitty's split function, using pytest, a widely used Python testing library:

import pytest
from kitty.money import split_evenly

def test_even_split():
    assert split_evenly(90000, ["Asha", "Ravi", "Meera"]) == {
        "Asha": 30000, "Ravi": 30000, "Meera": 30000,
    }

def test_leftover_paise_are_not_lost():
    shares = split_evenly(100000, ["Asha", "Ravi", "Meera"])
    assert sum(shares.values()) == 100000

def test_empty_flat_is_refused():
    with pytest.raises(ValueError):
        split_evenly(5000, [])

Each one encodes a decision you made: even amounts split exactly, no paisa ever vanishes, and an empty flat is an error rather than a crash.

One more rule: watch every new test fail at least once. Break the code on purpose, or write the test before the fix, and check that it turns red. A test you have never seen fail may not be testing anything at all.

The shortcut

For each behaviour, list three cases: normal, edge, wrong. Normal is the everyday case. Edge sits on a boundary: a leftover, zero, empty, exactly at a limit. Wrong is input that must be refused. Write the expected result for each before running anything.

Test cases for "a payment cannot exceed what is owed"Step 1 of 4
  1. Normal

    Ravi owes Asha 30000 paise and pays 30000. Expected: both balances become 0.

The trap is letting the assistant choose the cases and write the code. Then the only thing being tested is whether the assistant agrees with itself.

Drills

Answer fast — trust the rule.

quick drill

Why should you, rather than the assistant, choose the test cases?

quick drill

For split_evenly, which of these is an edge case?

quick drill

What does sum(shares.values()) == 100000 protect against?

quick drill

A new test passed the first time you ran it, and you have never seen it fail. What is the risk?

Remember

  • Rule: you pick the test cases from the spec; the assistant may type them.
  • Shortcut: normal, edge, wrong, with expected results written first.
  • Trap: tests that only confirm the code agrees with itself. Watch every test fail once.

Practice — level up

0/1 solved

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

ExplainChoose the cases

Kitty has a rule: an expense amount must be at least ₹0.01 and at most ₹1,00,000. List normal, edge and wrong-input test cases for this rule with the expected result for each, and explain how you would confirm the tests can actually fail.