Skip to content
SPM Tuition
Computer Science · Advanced programming practice

Creating a test table with expected outputs

Your program works on the one input you tried, but you cannot show it works on the others.

A test table lists the inputs you will try and the output you expect for each, worked out before you run the code. A program works only when every row matches, not when one run looks right.

This lesson is part of advanced programming practice. It builds on the test-data idea in testing normal, boundary and invalid inputs.

How do I choose test data?

Sort your inputs into three groups. Each group catches a different kind of fault.

Group What it is What it catches
Normal Typical valid values The main logic is wrong
Boundary The edges of each rule and the values either side Conditions with >, >=, < or <= in the wrong place
Invalid Values the rules reject Missing validation, crashes

Start from the specification, not from the code. The specification says where the boundaries are.

Worked example: a grading program

An original task: a program reads a mark from 0 to 100 and prints a grade. The rules are that 80 or above is A, 60 to 79 is B, 40 to 59 is C, and below 40 is F. A mark outside 0 to 100 prints “Invalid”.

The boundaries are 0, 39, 40, 59, 60, 79, 80 and 100. The invalid values just outside are −1 and 101.

Test Mark Type Expected output
1 75 Normal B
2 92 Normal A
3 80 Boundary A
4 79 Boundary B
5 40 Boundary C
6 39 Boundary F
7 0 Boundary F
8 100 Boundary A
9 −1 Invalid Invalid
10 101 Invalid Invalid

The table has ten rows, and each has a reason to be there. Remove a boundary row and one kind of error goes untested.

The mistake: trusting one sample run

A common slip is to run the program once with a mark like 75 and call it finished. The output B looks right, so the program seems to work.

Suppose the program contains this condition.

IF mark > 80 THEN
    PRINT "A"
ELSE IF mark >= 60 THEN
    PRINT "B"
Test Mark Expected Actual Result
1 75 B B Pass
2 92 A A Pass
3 80 A B Fail

Only test 3, the boundary, shows the fault. The code says > 80 where the rule says 80 or above, so it should be >= 80. A sample run at 75 would never have found it.

How to build a table in an exam

  1. Read the rules and underline every number.
  2. For each number, write that value and the values either side as boundary rows.
  3. Add one or two normal values in the middle of each range.
  4. Add invalid values beyond each end of the valid range.
  5. Work out the expected output for each row from the rules before looking at the code.

Check yourself

A youth ticket is sold to ages 13 to 17 inclusive. The program prints “Youth” for those ages and “Not youth” otherwise. Write a test table with at least six rows, including boundaries, and give the expected output of each.

Answer

Boundaries of the youth rule are 13 and 17, with the values just outside at 12 and 18.

Age Type Expected output
15 Normal Youth
13 Boundary Youth
17 Boundary Youth
12 Boundary Not youth
18 Boundary Not youth
40 Normal Not youth
−5 Invalid Invalid age message, if the specification says to reject it

If the specification does not mention invalid ages, say so in your answer and state what you assume. The four boundary rows are what expose a wrong > or >=.

What to study next

Try your tables on the advanced programming practice set. Log any boundary you missed in the mistake log and paper-error review.

If you want a teacher to build a test table with you for one of your own programs, see online one-to-one Computer Science tuition.

Common questions

Why write the expected output before running the program?

If you run first and then copy what the program printed, you are testing whether the program agrees with itself. The expected output comes from the specification, worked out by hand, so a difference shows a real fault.

What is boundary data?

Values at the edge of a rule, such as the smallest and largest valid values and the values just outside them. Errors in conditions like >= and > hide exactly at these edges.

What is invalid data and what should the program do?

Data that breaks the rules of the task, such as a mark of 120 when marks run from 0 to 100. The specification should say what the program does, such as printing a message, and the test table records that.

How many tests are enough?

Enough to cover each rule, each boundary and each kind of invalid input once. Extra tests of the same kind add nothing, so a small, well-chosen table beats a long one with repeats.

If you are unsure which inputs to test, one-to-one Computer Science lessons let a teacher read your program with you and show which value would break it before you run anything.

  • Online one-to-one lessons for your child with an experienced teacher.
  • Your first class is a one-hour trial, from RM50. The fee is agreed before you book.
  • Happy with the teacher? Continue with lessons of about 1.5 hours. If not, ask for another teacher.