Not sure which lesson to do first? Find your level and get a plan →

CS Thinking · Computational Thinking · Grade 6-8

Testing

Systematically running a program with known inputs to verify that its outputs are correct.

test passes  ⟺  oe=oa

Quick Answer

Systematically running a program with known inputs to verify that its outputs are correct. For each test you choose an input, work out the expected output before running anything, then compare it with what the program actually produces; a mismatch means a bug. Good test sets mix normal inputs, boundary values (0, the largest allowed value, an empty list) and invalid inputs, because bugs cluster at the edges. Passing tests shows those cases work, not that the program has no bugs.

Why This 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.

Intuitive Explanation

Say a program turns a test score into a grade: 90 or more is an A, 80 to 89 a B, below 80 a C. A test is a pair: an input and the answer you expect, written down before you run anything. Input 95, expect A. Input 85, expect B. Run the program, compare, and mark each test pass or fail. The expected answer has to come from the requirements, not from the program, or a wrong program would pass its own tests.

Which inputs? Normal ones like 95 and 85 show the main idea works. Boundary values sit exactly where the rule changes: 90, 89, 80, 79, plus the ends of the range, 0 and 100. Programmers often type > where they meant ≥, and only a boundary test such as 90 can catch that. Invalid inputs check what happens with nonsense: -5, 150 or 'ninety'. A dozen well-chosen inputs can reveal more than a hundred random ones.

Testing finds problems; it does not fix them. When a test fails, debugging takes over to find the cause. And passing every test proves only that those inputs work. A score of 101 or an empty input might still break a program that passed all of its tests, which is why testers keep asking which input they have not tried yet.

Think of it like this. 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.

Core idea. Testing can prove that bugs exist, but cannot prove that no bugs remain—exhaustive testing is impossible.


When to Use

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.

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

How to Recognize It

Check whether the job is to find out if something is wrong, before anyone tries to say what caused it.

  1. Is the expected output written down before the program runs?

    Yes is testing: a test needs a known right answer to compare against. If you are already looking at a wrong output and hunting for the line that caused it, that is Debugging.

  2. Are you choosing a set of inputs that mixes ordinary, boundary and invalid ones?

    Yes is designing tests. If the question is only which unusual inputs sit at the limits, such as an empty list, it is about Edge Cases.

  3. Is the whole program being checked, the way a user would use it?

    Yes is testing in general. If one function is checked on its own, cut off from the rest, the question is about Unit Testing.

  4. Is the result pass or fail, rather than a name for the kind of mistake?

    Yes is testing. If you must label a failure as a syntax, runtime or logic error, that is Error Types.

Testing vs Debugging vs Unit Testing vs Edge Cases

These four belong to the same cycle of checking code, so they are easy to swap. Ask whether you are checking for a problem, chasing one you found, checking a single piece, or choosing unusual inputs.

Testing

Meaning
Testing is running a program on chosen inputs and checking each output against an answer decided beforehand.
Key test
You have expected answers and get pass or fail; no cause is found yet.
Formula
pass  ⟺  oe=oa
Example
A grade calculator should turn 95 into 'A'; you type 95 and check the screen.

Debugging

Meaning
Debugging is finding the cause of a failure you already know about and correcting it.
Key test
A wrong output is already known; the job is to locate and fix it.
Example
A loop printing 1 to 9 instead of 1 to 10 is traced to range(1, 10) and changed to range(1, 11).

Unit Testing

Meaning
Unit testing checks one small piece of a program, usually a single function, on its own.
Key test
Only one function or module is under test, separated from the rest.
Formula
assert(f(x)=y)
Example
Checking that isEven returns true for 4 and false for 7, without running the whole app.

Edge Cases

Meaning
Edge cases are the unusual inputs at the limits of what a program should handle.
Key test
The question is which extreme inputs to try, not how to run or fix tests.
Example
For a function that averages a list: an empty list, a one-item list and a list of negative numbers.

Formula & Notation

test passes  ⟺  oe=oa
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 — Design boundary tests for a grader

Easy

grade(score) should return 'A' for 90 to 100, 'B' for 80 to 89, 'C' for 0 to 79, and 'invalid' for anything else. Design a test set with the expected result for each test. Then suppose the code checks if score > 90 for an A. Which tests fail?
Show the steps and the answer
  1. Pick normal inputs from the middle of each band: 95 → A, 85 → B, 50 → C.

    These check that the main rule works at all.

  2. Add boundary inputs on both sides of each change: 90 → A, 89 → B, 80 → B, 79 → C, plus the ends 100 → A and 0 → C.

    Bugs gather where the rule switches, such as > typed where ≥ was meant.

  3. Add invalid inputs just outside the range: −1 → 'invalid' and 101 → 'invalid'.

    These check that the program rejects scores that cannot happen instead of grading them.

  4. Run all 11 tests on the code that uses score > 90: grade(90) returns 'B', so that one test fails; the other 10 pass.

    Only the boundary test exposes the bug. 95 and 100 still get an A, so normal tests alone would all have passed.

Answer: 11 tests: 95 → A, 85 → B, 50 → C; 90 → A, 89 → B, 80 → B, 79 → C, 100 → A, 0 → C; −1 and 101 → invalid. With > instead of ≥, only the test for 90 fails.

Takeaway: Test both sides of every boundary. A slip like > instead of ≥ is invisible to inputs from the middle of the range.

Example 2 — Expected values copied from the program

Standard

Omar's function area(w, h) should return the area of a rectangle. He runs area(3, 4), sees 7, and writes the test 'area(3, 4) should be 7'. He does the same for area(2, 5), which gives 7, and area(1, 1), which gives 2. All three tests pass. Is this testing? What should the tests say?
Show the steps and the answer
  1. Ask where each expected value came from: Omar copied it from the program's own output.

    A test compares the program with something independent. Copying the output compares the program with itself, so it can never fail.

  2. Work out the expected values from the requirement, area = width × height: 3 × 4 = 12, 2 × 5 = 10, 1 × 1 = 1.

    The requirement, not the code, decides what the right answer is.

  3. Compare: the program gave 7, 7 and 2, so all three tests fail.

    Each output equals w + h, which shows the function adds instead of multiplying.

  4. Notice that area(2, 2) would print 4 and pass even with the bug, since 2 + 2 = 2 × 2.

    Some inputs hide a bug by coincidence, another reason to test several different cases.

Answer: It was not real testing. The expected values should be 12, 10 and 1; the outputs 7, 7 and 2 fail all three tests, revealing that the function computes w + h.

Takeaway: Write the expected output from the requirements before you run the program, never from what the program prints.

Example 3 — Find the mistake: all tests pass, so no bugs?

Application

divide(a, b) should return a ÷ b, or the message 'cannot divide by 0' when b is 0. Its code is return a // b, which divides and drops any remainder. Rita tests (10, 2) → 5, (9, 3) → 3 and (8, 4) → 2. All pass, and she writes: "All my tests pass, so divide has no bugs." Find the mistake and correct it.
Show the steps and the answer
  1. Look at what her three tests have in common: positive numbers that divide exactly.

    Three tests of the same kind check one situation three times. They say nothing about remainders, zero or negative numbers.

  2. Add tests from the requirement: (7, 2) → 3.5, (5, 0) → 'cannot divide by 0', (0, 5) → 0, (−6, 3) → −2.

    Each new test probes a different situation the function must handle.

  3. Run them: (7, 2) returns 3 and (5, 0) crashes with a division-by-zero error, while (0, 5) and (−6, 3) pass.

    a // b drops the remainder, and nothing checks for b = 0. Both bugs were there all along; the first three tests simply never touched them.

  4. Fix the code: if b == 0, return 'cannot divide by 0'; otherwise return a / b. Re-run all 7 tests: all pass.

    Re-running the old tests with the new ones checks that the fix kept the earlier answers right.

Answer: Passing three similar tests does not prove the function is bug-free. The tests (7, 2) and (5, 0) fail; after the fix, all 7 tests pass and divide(7, 2) returns 3.5.

Takeaway: Passing tests show only that those inputs work. Vary the inputs: remainders, zero, negatives and the edges of the range.

Where You'll See It

  • Trying to break a new game level before release

    A tester deliberately walks into walls, presses buttons in odd orders, and enters weird inputs to find bugs the way a player might, covering edge cases instead of only the intended path.

  • Checking a calculator app

    You test normal inputs (2+2), boundary inputs (very large numbers), and error inputs (divide by zero) and compare each actual result to what you expected, which is exactly designing test cases.

  • Form on a website

    Quality testers try submitting an empty form, a 200-character name, and symbols in the phone field to make sure the form rejects bad input gracefully rather than crashing.

Common Mistakes

  1. The slip: If my program passes all my tests, it has no bugs.

    Why it's tempting: All green checks feel like complete proof of correctness.

    Instead: Testing can show the presence of bugs but never their absence; passing tests only means the cases you tried worked. Testing every possible input is almost never practical.

  2. The slip: Testing the normal, expected inputs is enough.

    Why it's tempting: The happy path is what users 'should' do, so it seems like the important case.

    Instead: Good test sets cover normal inputs, boundary values, and error conditions, because real bugs usually hide in the edge and error cases.

  3. The slip: It's fine if tests have to run in a certain order to pass.

    Why it's tempting: If they all pass when run together, the order feels harmless.

    Instead: Each test should be independent and set up its own state; tests that depend on each other's order hide bugs and break when one changes.


Mini Practice

Try these on your own. Tap Reveal when you want to check.

  1. discount(total) takes 10% off when the total is over 100 points; otherwise it changes nothing. Write the expected outputs for total = 80, 100 and 200.

    Hint: Check carefully whether 100 counts as 'over 100'.

  2. A form accepts ages 13 to 19. Name four boundary inputs worth testing and the expected result for each.

    Hint: Test each edge of the range and the value just outside it.

  3. Three tests of max3(a, b, c): max3(1, 2, 3) expected 3, got 3; max3(3, 2, 1) expected 3, got 1; max3(2, 3, 1) expected 3, got 3. Which tests pass, and what does the failure suggest?

    Hint: A test passes only when the expected and actual outputs match.

  4. isPassing(score) should return true for scores of 50 or more, on a 0 to 100 scale. Choose one typical test, one boundary test and one error test, each with its expected result.

    Hint: Pick a normal input, an input at the edge, and an input that should be refused.

  5. Your program prints the wrong total. You already know which input causes it, and now you step through the code line by line to find the bad line. Is this testing or debugging?

    Hint: Testing finds that something is wrong. What finds where it is wrong?

  6. A student says: 'All 5 of my tests pass, so my program has no bugs.' What is wrong with this claim?

    Hint: Think about the inputs those 5 tests never tried.

Want the full set?

50 practice questions for this concept — free to try, every one with a complete worked solution showing the why, not just the answer.

Frequently Asked Questions

Why work out the expected answer before running the program?

Because once you see the program's output, it is easy to convince yourself it looks right. If a tip calculator shows $4.50 for a $30 bill at 15%, you might just nod, but working it out first gives 30×0.15=4.530 \times 0.15 = 4.5, so it matches. Had the program shown $4.05, you would only notice the mistake if your own answer was already written down.

Who should test a program, the person who wrote it or someone else?

Both, but someone else usually finds more. The writer tends to try the inputs they had in mind while coding, which are exactly the ones the code already handles. A fresh tester tries things the writer never imagined, like typing a name into an age box. Game studios hire whole teams for this, and many companies pay outside people a reward for every serious bug they report.

Can a computer run the tests for you?

Yes. Programmers write tests as small pieces of code that call the program with an input and check the result automatically. A project can then run hundreds of tests in a few seconds every time someone changes the code, which catches a change that breaks something that used to work. Re-running the old tests after every change like this is called regression testing.

Learning Path

← Before

AlgorithmDebugging
Testing

You are here

You need Algorithm (what the program is meant to do) and Debugging (how to fix a fault once found) first. Testing adds a planned way to find faults: chosen inputs, expected outputs written in advance, and a pass or fail for each. Next, Unit Testing tests one function at a time, and Edge Cases looks closely at the boundary inputs where bugs hide.

See Also