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 2 · Lesson 4 · Core

Testing decisions at their boundaries

Choose test values on each side of a boundary and predict the output before you run the program.

  • 50 minutes
  • Serves O3
  • Assumes M2.L3

Phase 1: Prepare

Step 1 Warm-up recall

From M2.L3: why does the order of elif checks matter?

Private: nothing you type here is sent or stored.

Step 2 Learning goal

By the end of this lesson you can:

  • identify the boundaries in a decision program (Foundation);
  • choose test values on and around each boundary (Core);
  • write a test table of inputs and expected outputs (Core).

Phase 2: Teach

Step 3 A familiar example

A ticket office charges by age. The two likeliest mistakes are at the edges: is a five-year-old a child or free, and is an 18-year-old an adult? Testing the edges finds them.

Step 4 Clear explanation

Most decision bugs sit at boundaries, the exact values where the answer changes. The common slip is using > where you meant >=, which changes the result for one value only.

17: Minor018: Adult119: Adult2The boundary is where bugs hide
Test the values on each side of a boundary. For age >= 18, test 17, 18 and 19.
age = int(input("Age: "))
if age < 5:
    print("Free")
elif age < 18:
    print("Child")
else:
    print("Adult")

The boundaries are 5 and 18. Test on each boundary and just below it. Write the expected output before you run the program, so you are checking the program, not copying it.

Age testedExpected outputWhy this test
4Freejust below the first boundary
5Childon the first boundary
17Childjust below the second boundary
18Adulton the second boundary

If the program prints something different from the expected column, you have found a bug while it is cheap to fix.

Step 5 Worked example (4 marks)

Using the ticket program above, what is printed for age 18 and for age 17?

  1. Age 18: 18 < 5 is False; 18 < 18 is False; so else runs. (1 mark)
  2. Output for 18: Adult. (1 mark)
  3. Age 17: 17 < 5 is False; 17 < 18 is True. (1 mark)
  4. Answer: Adult for 18 and Child for 17. (1 mark)

Go deeper: the professor's notes

Bugs sit on the boundary: test the value, the one below and the one above.
Below

n - 1.

On

n.

Above

n + 1.

FindlimitsPickbelow on abovePredictoutputRuncompare
Boundary

Tests

ValueExpected
365not leap day
366last day

At work: A year counter

Days left can be 365, 366 or 367.

  1. Write the three expected outputs.
  2. Run all three.

What a careful practitioner decides. Always test the exact limit value.

Phase 3: Practise

Step 6 Learner practice (instant-marked)

1. A boundary value is:

2. For age >= 18, the best values to test are:

3. When should you write the expected output?

4. Changing >= to > changes the result for:

Phase 4: Close

Step 7 Recap

  • Bugs hide at boundaries.
  • Test on and just below each boundary.
  • Write expected outputs before running.

Step 8 Exit ticket (ungraded)

List the test ages you would use for a program with boundaries at 12 and 65.

Private: nothing you type here is sent or stored.

Step 9 Go further (Extension)

Write the test table for a program that prints Pass at 50 or more.

In industry: Zune: day 366

On 31 December 2008 a clock routine in the 30GB Zune tested days > 366 inside a loop. On day 366 the days equalled 366, so the leap-year branch was skipped and the loop never ended. Microsoft said a clock driver handled the last day of a leap year badly.

Link to this lesson. The value that equals the limit is the one the code is most likely to get wrong. Test it first.

Source: Microsoft engineering blog, Hello w0rld0; TechCrunch, 31 Dec 2008

Common mistakes

MistakeWhy it fails
Testing only typical valuesAdd the boundary values.
Writing expected output after runningPredict first or you will copy the bug.
Mixing up > and >=Decide whether the boundary belongs to the upper or lower case.

Limits. Testing shows the presence of bugs; it cannot prove there are none.

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 · What is a boundary value?
17below18on19above
Boundary

A value where the answer changes, such as 18 for age >= 18. Bugs hide there because a single wrong symbol, > instead of >=, changes the result for that value only.

Card 2 of 3 · Why write the expected output before running a test?
PredictexpectedRunactualComparematch?
Predict first

If you run first and then write what you saw, you copy the bug. Predicting first checks the program against what it should do, as in any acceptance test at work.

Card 3 of 3 · How would you test a fee program with limits at 5 and 18?
4Free5Child18Adult
Test table

Test 4, 5, 17 and 18 at least. Write the expected result for each first, then run. This is how a team checks a pricing rule before release.

Review with flashcards

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

Open the module flashcards