Specialty Digest

Global English editionLearners’ Study Dashboard — in development
The Knowledge Library for All

Flashcards: open the module flashcards (end-of-module review with workplace questions).

Online Courses / University Track / Python from Zero

Module 8 · Lesson 2 · Core

Building in small, tested steps

Write one function at a time, test each alone and join them only when each works.

  • 50 minutes
  • Serves O6
  • Assumes M8.L1

Phase 1: Prepare

Step 1 Warm-up recall

From M8.L1: why work an example by hand before coding?

Private: nothing you type here is sent or stored.

Step 2 Learning goal

By the end of this lesson you can:

  • build one function and test it alone (Foundation);
  • join functions in a main program (Core);
  • keep a working copy as you go (Core).

Phase 2: Teach

Step 3 A familiar example

A builder checks each wall is level before adding the next storey. A program is built the same way, so a fault is found where it was made.

Step 4 Clear explanation

Follow the plan, one function at a time.

Writeone functionTestaloneJointo the programRunwhole program
Build one function at a time. Test it alone. Only then join it to the rest.
def needs_reorder(qty, level):
    """True when qty is at or below level (whole numbers)."""
    return qty <= level

assert needs_reorder(3, 5) == True
assert needs_reorder(5, 5) == True
assert needs_reorder(9, 5) == False
print("needs_reorder passes")
needs_reorder passes

The boundary case 5 and 5 is tested on purpose. Once each function passes alone, join them in a short main function that reads like the plan:

def main():
    rows, bad = load_rows("stock.csv")
    names = make_report(rows)
    print(len(names), "to reorder:", names)
    print(bad, "bad rows skipped")

main()

Keep a working copy each time something passes, so a later mistake can be undone. Add features one at a time, and rerun every earlier test after each change. If the whole program fails, return to the last working copy and add the new part again in a smaller step.

Step 5 Worked example (4 marks)

Your function make_report passes its tests. You add a sort of the names and the tests now fail. What is the sensible move?

  1. The sort is the only change since the last pass. (1 mark)
  2. Restore the working copy, or undo the sort. (1 mark)
  3. Add the sort again in a smaller step and rerun the tests. (1 mark)
  4. Answer: return to the working copy and retry in small steps. (1 mark)

Go deeper: the professor's notes

Build in small tested steps and keep a working copy.
Write

One function.

Test

Alone.

Join

Then main.

Writeone functionTestaloneJoinin mainSavea copy
Build

Order

StepRule
testbefore joining
copyafter each pass

At work: A new feature

You want to add sorting.

  1. Add it alone.
  2. Rerun all tests.

What a careful practitioner decides. Never add two things before a retest.

Phase 3: Practise

Step 6 Learner practice (instant-marked)

1. You should join functions together:

2. A main function should:

3. Keeping a working copy lets you:

4. After adding a feature you should rerun:

Phase 4: Close

Step 7 Recap

  • Build and test one function at a time.
  • Join them in a main function that reads like the plan.
  • Keep working copies and rerun all tests after each change.

Step 8 Exit ticket (ungraded)

Write needs_reorder for the stock plan and three asserts, including the boundary.

Private: nothing you type here is sent or stored.

Step 9 Go further (Extension)

Build the first two functions of your capstone and test each alone.

In industry: Heathrow Terminal 5: tested, but not the same

BAA's chairman told MPs the baggage system had been tested over six months and 20 times in a fully loaded condition. The Transport Committee still criticised British Airways for poor staff training and system testing.

Link to this lesson. Testing a system is not the same as testing the whole operation with real people, real timings and real conditions.

Source: Flight International, 2008; Building, 3 November 2008

Common mistakes

MistakeWhy it fails
Writing everything, then testingTest as you go.
No working copySave a copy after each pass.
Adding two features at onceOne at a time, then retest.

Limits. Teams use version control tools to keep working copies; the habit is the same.

Flashcards for this lesson (3)

Tap a card to reveal the answer. Read the question, answer it aloud or in writing first, then check yourself against the explanation.

Card 1 of 3 · How do you split a task into functions?
One jobeachClearnameTestalone
Functions

One job each, with a clear name: load the rows, decide for one row, make the report. Each can be tested alone.

Card 2 of 3 · Why build one function at a time?
WriteoneTestitThenjoin
Small steps

A fault is found where it was made. If you write everything and then test, you will not know which part is wrong.

Card 3 of 3 · What does a main function do?
Callsin orderReadslike the planShortand clear
main

It calls the other functions in order and reads like your plan: load the rows, make the report, print it.

Review with flashcards

Test yourself on this module with workplace flashcards and detailed answers.

Open the module flashcards