Skip to content
Tactiverge

School/△ Builder/Week 9

Build it, test it, break it

Your checker says OK for the real log. Will it still say the right thing when the logger glitches, the file is empty, or a reading is exactly 60.0?

About 60 min · 3 goals · Checked by automated tests

1.Why should I care?

Building in small, tested steps is how engineers keep a growing program under control. Each function you finish and test becomes something you can trust, so when the whole program misbehaves you know where not to look.

  • ◇ Implement the planned functions one at a time, testing each with assert before moving on.
  • ◇ Run the whole program on designed test logs and compare with hand-worked expected reports.
  • ◇ Record one real bug found during testing: symptom, hypothesis, test and fix.

2.Discover

In the Code Lab, start with decide() from this lesson and its four asserts: run them. Then add parse_reading, summarise and count_outside one at a time, each with its own asserts, and only then the main program. Paste each of your test logs into the lines list and compare with your expected reports.

Can't use the interactive? Read a description instead

The Code Lab exercise starts with decide(values, malformed): it returns INSPECT for an empty list, for any value below 10 or above 60, or when more than 2 lines are malformed; otherwise OK. Its tests: [20.0, 30.0] with 0 malformed → OK; [20.0, 61.0] → INSPECT; [10, 60] with 2 malformed → OK; [25.0] with 3 malformed → INSPECT; [] → INSPECT.

3.Checkpoint: guided practice

Check what you just discovered before going on. Hints appear after a wrong answer.

Practice 1 · a fresh version every time

A motor log has the lines ["40.0","","50.6","n/a","","59.7","59.4","45.6","52.3"] (°C). The checker skips any line that float() cannot convert and averages the rest. What mean does it report, in °C?

°C

Practice 2

In what order should you build the checker?

4.Apply it

Complete the project: the full checker with at least four tested functions, your test logs with expected and actual reports, a bug-diary entry for a real bug, and a one-page note explaining the algorithm so another learner can run it from the note alone.

5.Understand

Build one function at a time

SAFE_LOW_C = 10
SAFE_HIGH_C = 60

def decide(values, malformed):
    """Return "INSPECT" or "OK" following the brief."""
    if len(values) == 0:
        return "INSPECT"
    outside = 0
    for v in values:
        if v < SAFE_LOW_C or v > SAFE_HIGH_C:
            outside += 1
    if outside > 0 or malformed > 2:
        return "INSPECT"
    return "OK"

assert decide([20.0, 30.0], 0) == "OK"
assert decide([20.0, 61.0], 0) == "INSPECT"
assert decide([10, 60], 2) == "OK"       # band edges are safe
assert decide([25.0], 3) == "INSPECT"
assert decide([], 0) == "INSPECT"

Reading the log

In the Code Lab, paste the log into a list: lines = ["41.2", "ERR", ...]. On your own computer, read a file instead:

with open("motor_log.txt") as f:
    lines = f.read().splitlines()

Either way the rest of the program receives the same list of strings, which is why parsing lives in its own function.

Keep a bug diary

When a test fails, don't just fix it: write the symptom (decide([10, 60], 2) said INSPECT), the hypothesis (< versus <= on the band), the test that confirmed it (a probe showed outside = 2) and the fix. This record is a deliverable of the project, and the habit is worth more than the program.

Explain it another way

Like building a wall one course of bricks at a time and checking it with a level each time, instead of building the whole wall and hoping it's straight.

Worked example 1

A tester's log is 60.0, 60.0, 60.1. What should the report be?

All three convert; mean = 180.1 / 3 ≈ 60.03 °C, min 60.0, max 60.1. 60.1 is above 60, so outside = 1 and the decision is INSPECT. If your checker says OK, look at the comparison against SAFE_HIGH_C.

Common mix-ups

  • If the program works on the real log, it's finished.
    ✓ It's finished when it gives the right report for every designed test log, including the ones built to break it.
  • Tests slow you down.
    ✓ Tests make you faster: every later change is checked in seconds instead of by hand.

6.Practice on your own

Practice, not a test. Wrong answers get a hint and another try.

Practice 3

decide([10, 60], 0) returns INSPECT. Which line is most likely wrong?

Practice 4

Which is the best bug-diary entry?

7.Predict

Lines "41.2", "ERR", "54.0", "--". What mean should the checker report?

Guess first. Being wrong here is how the surprise works.

8.Reflect

9.Go further

Concepts: Functions and decomposition · Testing and debugging · Describing data