Course map
Stage 1: Foundation
Stage 2: Intermediate
Stage 3: Advanced
Stage 4: DSA in Python
Stage 5: Specialization

Part 11 / Topic 6 of 12

What to test

Like a cricket coach who tests a new bowler on easy balls, tricky balls and no-balls.

Kind Meaning apply_discount example
Happy path Normal, expected input 10% off ₹1,000 → 900
Edge cases Limits and unusual but valid input 0%, 100%, an empty cart
Errors Bad input must fail clearly -5% or 120% → ValueError

Boundaries are where most bugs hide. If a rule changes at 18, test 17 and 18.

Arrange-Act-Assert (AAA)

Write each test in three short steps:

from shop import cart_total

def test_cart_total_adds_all_lines():
    # Arrange: prepare the data
    cart = [
        {"name": "Pen", "price": 20, "qty": 3},
        {"name": "Bag", "price": 500, "qty": 1},
    ]
    # Act: call the code once
    total = cart_total(cart)
    # Assert: check the result
    assert total == 560

One idea per test

  • Test one behaviour per test. When it fails, the name tells you what broke.
  • Good names read like a sentence: test_empty_cart_costs_nothing, test_line_total_rejects_zero_qty.
  • Tests must not depend on each other or on the order they run in.

Done here? The practice set for Part 11 — 7 exercises with solutions — is on the part page.

Search the guide