Computer Science
Grade 5
20 min
Testing Your Code: Finding the Problems
Students will learn the importance of testing code to identify potential bugs.
Tutorial Preview
1
Introduction & Learning Objectives
Learning Objectives
Define a 'bug', 'test case', and 'edge case' in the context of a computer program.
Create a set of at least three test cases (including an edge case) for a program that uses loops and variables.
Identify the 'expected output' for a given input before running the code.
Compare the 'actual output' of a program to the 'expected output' to confirm if a bug exists.
Debug a simple 'off-by-one' error in a loop.
Test a program with boundary conditions, such as the first and last numbers in a range.
Have you ever played a video game where your character got stuck in a wall? 🧱 That's a bug! How do we find those problems before anyone else does?
In this lesson, we will become code detective...
2
Key Concepts & Vocabulary
TermDefinitionExample
BugAn error or mistake in a computer program that causes it to produce an incorrect or unexpected result.A calculator program where `2 + 2` gives you `5`.
DebuggingThe process of finding and fixing bugs in your code. It's like being a detective for your program.Finding the line of code that says `2 + 3` instead of `2 + 2` and fixing it.
Test CaseA specific set of inputs and conditions you use to check if your program works as you expect.For a program that adds two numbers, a test case would be: Input 1 = `5`, Input 2 = `10`, Expected Output = `15`.
Expected OutputThe result you predict your program *should* produce for a given test case.If your code is supposed to count to 3, your expected output is `1, 2, 3`.
Actual OutputThe result your program *actually* prod...
3
Core Syntax & Patterns
The Predict-Run-Compare Cycle
1. PREDICT what the code will do. 2. RUN the code with your test case. 3. COMPARE your prediction to the actual result.
Use this cycle for every test case. If the prediction and the actual result don't match, you've found a bug!
The Boundary Checker Pattern
Always test the smallest value, the largest value, and a value in the middle.
Bugs often hide at the 'boundaries' or 'edges' of a range. For a loop that should run from 1 to 10, you must test what happens at 1 and 10, not just 5.
4 more steps in this tutorial
Sign up free to access the complete tutorial with worked examples and practice.
Sign Up Free to ContinueSample Practice Questions
Challenging
You code a game where a user enters a number from 1 to 10. If the number is even, they get 5 points. If it's odd, they lose 2 points. Which of the following is the BEST test plan?
A.Test with 4 and 7
B.Test with 5, 6, 7, 8
C.Test with 0, 1, 2, 9, 10, 11, and the word 'five'
D.Test with all numbers from 1 to 10
Challenging
A program should draw a square if `size` is between 10 and 50 (inclusive). Which buggy condition would be the hardest to find with only a few random tests?
A.`if (size > 50)`
B.`if (size == 10)`
C.`if (size > 10 and size < 50)`
D.`if (size > 10 or size < 50)`
Challenging
To test a function that finds the largest of three numbers, a student tests `(5, 2, 1)`, `(3, 8, 4)`, and `(6, 9, 10)`. What important type of test case is missing?
A.Cases with very large numbers
B.Cases where numbers are repeated, like (7, 7, 4)
C.Cases with fractions
D.Cases where the first number is the largest
Want to practice and check your answers?
Sign up to access all questions with instant feedback, explanations, and progress tracking.
Start Practicing Free