← Back to course
Code Crew G5 / Week 12 / Friday
5/6
Week 12 Β· Ship it

Friday

Run it all
// Build, test and graduate
⏱ about 15 min

Friday: Run it all

It is the last build day. Every part is ready. Rocket's finger hovers over the run button.

"What if something breaks?" he whispers.

Raven points at the test list. "What do you notice we wrote first, last week?"

"The tests!" Rocket says. "If they all pass, we know each part works."

Nova glows her brightest. "Here is a hint. Run the tests first. Then run the report."

Part 6: tests

# Part 6: tests
def check(label, got, want):
    if got == ____:
        print("PASS", label)
    else:
        print("FAIL", label, "got", got, "want", want)

def run_tests():
    check("total", total_items({"a": 2, "b": 3}), 5)
    check("total of nothing", total_items({}), 0)
    check("low", low_stock({"b": 9, "c": 0, "a": 1}, 5), ["a", "c"])
    check("find", find_item(["lamps", "maps", "rope"], "maps"), 1)
    check("find missing", find_item(["lamps", "maps"], "tea"), -1)
    check("crew", count_crew(fleet), 8)
What goes in the blank in check?
WHY THIS EXERCISEcheck compares the real answer with the expected answer we worked out by hand.

Part 7: the report

# Part 7: the report
def report(stock, tree):
    crew = ____(tree)
    print("Crew:", crew)
    print("Items:", total_items(stock))
    print("Water each:", stock["water"] // crew)
    print("Low:", ", ".join(low_stock(stock, 5)))
    names = sorted(stock)
    print("maps is at spot", find_item(names, "maps"))

run_tests()
restock(inventory, "rope", 10)
restock(inventory, "kites", 4)
report(inventory, fleet)
Which helper goes in the blank to count the crew?
WHY THIS EXERCISEThe report reuses the helpers instead of doing the work again.

All seven parts together make one 96-line program. Here is what it prints when it runs.

Output:
PASS total
PASS total of nothing
PASS low
PASS find
PASS find missing
PASS crew
Crew: 8
Items: 93
Water each: 5
Low: kites, lamps
maps is at spot 2
  • Every test passes, so each helper works on its test cases.
  • Rope went from 2 to 12, so it is no longer low.
  • Kites is new, with 4, so it shows up as low.
  • Water each is 40 // 8, which is 5 for each crew member.
READ THE OUTPUT.
  • Read the question.
  • Tap your answer.
Why is maps at spot 2 now, not spot 1?
If we also ran restock(inventory, "lamps", 5), what would the Low line show?
The Items line says 93. Rope gained 10 and kites added 4. What was the total before restocking?
WHY THIS EXERCISEMonday's test drive showed 79, so the program and the hand math agree.
At the computer
With a grown-up, type all seven parts into one file in IDLE. Fill each blank as you go.
Run it. If a test says FAIL, read its got and want, and find the bug.

Practice lab: when a test is wrong

def check(label, got, want):
    if got == want:
        print("PASS", label)
    else:
        print("FAIL", label, "got", got, "want", want)

def total_items(stock):
    total = 0
    for item in stock:
        total = total + stock[item]
    return total

check("two", total_items({"a": 1, "b": 1}), 2)
check("one", total_items({"a": 4}), 5)
How many tests PASS?
In the FAIL line, what is the got value?
What should the want be for the test labelled one?
How many lines does the program print?
TEST CHECK
  • Read the question.
  • Tap your answer.
For the test labelled one, what is wrong?
What should you do when a test fails?
Why does the report reuse total_items?
StatementTrue or false?
The test labelled two passes.?
A FAIL always means the code has a bug.?
check prints the got and want values on a FAIL.?
All six tracker tests pass in the full program.?
WHY THIS EXERCISEGood engineers check the test and the code when something fails.

You shipped it! A 96-line program, built one tested part at a time. Tomorrow is graduation.

← Thursday