Testing Formula

Testing is systematically running a program with known inputs to verify that its outputs are correct.

The Formula

test passes  ⟺  oe=oa

Ask yourself: Are you running a program on chosen inputs and comparing its output with answers worked out in advance?

Quick example

Test divide function with: (10, 2), (0, 5), (-6, 3), (5, 0) [error case].

What this formula means

Systematically running a program with known inputs to verify that its outputs are correct. Testing involves designing test cases that cover normal inputs, boundary values, and error conditions, then comparing actual results against expected results.

Testing a program is like a fire drill: you create the situation on purpose, with a plan for what should happen, and watch for anything that goes differently. A drill that only ever uses the main exit would never discover the blocked side door, just as tests with only normal inputs miss the edge-case bugs.

Formal view

A test case is a triple (i,oe,oa) where i is the input, oe is the expected output, and oa is the actual output. The test passes iff oe=oa. A test suite {T1,T2,…,Tm} aims to cover all execution paths.

Worked examples

Example 1

easy
A function isEven(n) should return TRUE if n is even and FALSE otherwise. Suggest three test cases, including a normal, boundary, and erroneous value.
Show solution

Answer

Normal: isEven(4) → TRUE. Boundary: isEven(0) → TRUE. Erroneous: isEven('hello') → error/FALSE.

First step

1.
Step 1: Normal test: isEven(4) should return TRUE. This tests typical valid input.

Full solution

  1. 2.
    Step 2: Boundary test: isEven(0) should return TRUE. Zero is an edge case that is technically even.
  2. 3.
    Step 3: Erroneous test: isEven('hello') should handle the invalid input gracefully (return an error or FALSE). This tests that the function does not crash on bad input.
Good testing uses normal data (typical valid input), boundary data (edge cases at limits), and erroneous data (invalid input). This ensures the program works correctly in all situations.

Example 2

medium
A program grades students: 90-100 = A, 80-89 = B, 70-79 = C, 60-69 = D, below 60 = F. Design a complete set of test data including boundary values.

Example 3

medium
A function daysInMonth(m) takes m∈{1,…,12}. List one boundary, one normal, and one error input.

Common mistakes

  • Only testing the happy path (normal inputs) and neglecting edge cases and error conditions
  • Writing tests that depend on each other's execution order instead of being independent
  • Assuming that passing all tests means the program is bug-free—testing can only show the presence of bugs, not their absence

Common mistakes guide

If this formula keeps breaking in real problems, read the most common errors before you practice again.

Why this formula matters

Testing is how you find bugs before users do, and how you know a fix did not break something else. A grading program tested only with 85 may hand a perfect 100 the wrong letter. Writing the expected answer first matters too: if you just run the program and accept what it prints, you are checking nothing.

Frequently asked questions

What is the Testing formula?

Systematically running a program with known inputs to verify that its outputs are correct. Testing involves designing test cases that cover normal inputs, boundary values, and error conditions, then comparing actual results against expected results.

How do you use the Testing formula?

Test when you need evidence that a program does what it should: after writing it, after every fix, and before anyone else uses it. Signals are phrases like expected output, test case, or a list of inputs to try. Pick inputs at the boundaries as well as normal ones. If a test has already failed and you need to find why, that is Debugging. If you are still designing the steps, that is the Algorithm.

Why is the Testing formula important in CS Thinking?

Testing is how you find bugs before users do, and how you know a fix did not break something else. A grading program tested only with 85 may hand a perfect 100 the wrong letter. Writing the expected answer first matters too: if you just run the program and accept what it prints, you are checking nothing.

What do students get wrong about Testing?

Test edge cases: empty input, zero, maximum values, unexpected types.

What should I learn before the Testing formula?

Before studying the Testing formula, you should understand: algorithm, debugging.