PythonpytestAdvanced~7hPart 3 of 6

Python Confidence

Prove it, or find out why not

Anyone can run a program and watch it not crash. This is the part where you decide what it was supposed to do, design the cases that would catch it lying, read what the failure actually says, and repair it — with a test that would have caught the bug the first time.

Modules
7
Drills
15
Build steps
15
Time
~7h

Walk out with

A repaired program, the tests that exposed its four defects, and a process you run instead of guessing when something breaks.

What you'll do

01

Decide what correct means

The specification, the oracle, and the expected failure.

02

Design the cases

Partitions, boundaries, edges — then one parametrized test.

03

Judge a test, read a failure

Brittle or not, and what the runner is telling you.

04

Repair it, and prove it

Six steps, four real defects, a regression test each.

The pitch

What you practise, and what you leave with

A repaired timesheet program: four real defects found by testing, fixed at the cause, and each one guarded by a test that failed first.

You will practise

  • Name the value a rule should be tested at
  • Spot the partition a suite never touches
  • Say which test survives a refactor
  • Pick the fix that is the actual cause

Afterwards you can

  • Design tests that expose a failure
  • Read a failing run back to a cause
  • Judge whether a test is worth keeping
  • Prove a fix instead of claiming one

Modules

7 modules, 39 items

Lessons explain one idea. Drills repeat it until it sticks. Build steps make something that exists afterwards.

  1. 1

    Say what it should do

    ~39 min

    Decide what correct means, and where the expected answer comes from.

    • LessonWhat this project hands you5 min
    • LessonWhere the expected answer comes from12 min
    • DrillWork out the answer first12 min
    • DrillWhere the answer came from10 min
  2. 2

    Write the test

    ~51 min

    Install pytest, write a test file, and read what the first red run says.

    • Build stepInstall pytest and watch it choose7 min
    • Build stepGet a red run on the board9 min
    • LessonWhat a test is made of13 min
    • DrillWhat will this suite say12 min
    • Build stepTest the failure too10 min
  3. 3

    The cases worth testing

    ~34 min

    Find the inputs where a rule changes its mind, and the ones nobody tested.

    • DrillName the value you would test10 min
    • DrillFind what the suite misses12 min
    • Build stepCover the classes yourself12 min
  4. 4

    One test, many cases

    ~48 min

    Turn a list of cases into one test, count what ran, and spell it from memory.

    • LessonThe case list becomes the test14 min
    • DrillCount what actually ran10 min
    • Build stepCollapse your suite into one test12 min
    • DrillSay it in pytest12 min
  5. 5

    Is this test any good?

    ~51 min

    Judge a test the way you judge code: by what it will do to you later.

    • LessonWhat a test owes you15 min
    • DrillName the defect in the test12 min
    • DrillWhich test survives the change12 min
    • Build stepSay why, in your own words12 min
  6. 6

    Read what it says

    ~56 min

    Turn a red run into a located defect: which case, which frame, which line.

    • LessonThe report is the evidence14 min
    • DrillRead the run10 min
    • DrillFind your own frame10 min
    • DrillFind the earliest divergence12 min
    • Build stepNarrow a red run to one case10 min
  7. 7

    The repair lab

    ~2h

    Six steps, four real defects, and a test behind every fix.

    • LessonSix steps, and why the order holds12 min
    • LessonLook without disturbing the run7 min
    • DrillChoose the next thing to look at12 min
    • DrillFix the cause, not the symptom12 min
    • DrillCut the failing input in half8 min
    • Build stepStep 1: make it fail on demand14 min
    • Build stepStep 3: find the line that disagrees8 min
    • Build stepSteps 5 and 6: fix it, and guard it10 min
    • Build stepThe hour nobody worked14 min
    • Build stepSteps 1 and 2: make the drift happen8 min
    • Build stepStep 4: one run, two hypotheses10 min
    • Build stepThe week with nobody in it12 min
    • Build stepWrite down what happened12 min
    • LessonWhat you can now say8 min

Somebody else wrote it. Find out whether it works.

The first item is free. ~7h of focused work, at your own pace.

Advanced~7h7 modules