DoAssignment study guide
B3.4 · Validate a program with test cases
Learn to validate a program with test cases through clear examples and targeted practice.
Ontario Grade 10 Computer Science
Introduction to Programming
ICS2O study topic B3.4
What you will learn
- Explain how test cases help check whether a program behaves as expected.
- Choose inputs that check typical values and important boundaries.
- Compare a program’s expected output with its output for a test input.
What a test case tells you
- A test case pairs a particular input with an expected output.
- Trace the instructions for the chosen input; do not guess what the program does.
- Validation checks selected cases. It does not prove that every possible input will work.
Choose useful inputs
- Test the value at a decision boundary, not only values on either side.
- Use the written rule to determine the expected output.
- A test case is useful when it checks a specific part of the program’s behaviour.
Trace and compare
- Separate the expected result from the result predicted by tracing the code.
- A mismatch identifies a case that needs attention; it does not by itself say where the mistake is.
Practise the validation routine
- Plan cases from the program’s intended behaviour.
- Record expectations before tracing so they are an independent comparison.
- Use the results to decide what needs further checking.
Test-case trace for delivery fee
| Input amount | Expected fee | Output predicted by tracing | Match? |
|---|---|---|---|
| 25 | 5 | 5 | Yes |
| 39 | 5 | 5 | Yes |
| 40 | 0 | 0 | Yes |
| 52 | 0 | 0 | Yes |
Worked example
Checking a simple calculation
Input a whole number, multiply by 2, and display the result.
Python 3
number = int(input())
result = number * 2
print(result)- Identify the partsThe input is a whole number. The process multiplies that number by 2. The output is the displayed result. The intended rule determines the expected output for each test.
- Trace both casesFor input 6, the process gives 12, which matches the expected output. For input 0, the process gives 0, which also matches. These cases check an ordinary positive input and zero.
Worked example
Testing a decision at its boundary
The Yes and No paths show the outputs for the decision.
Python 3
minutes = int(input())
if minutes >= 30:
print("Long")
else:
print("Short")- Set expected outputsThe rule says 30 minutes or more is long. Therefore 29 is expected to be short, while 30 and 31 are expected to be long. The case at exactly 30 checks the equality boundary.
- Trace the decisionFor 29, the condition minutes >= 30 is false, so the program takes the No path and prints Short. For 30 and 31, the condition is true, so each takes the Yes path and prints Long.
Worked example
Checking a free-delivery rule
An order of 40 or more has no delivery fee in this example.
Python 3
amount = int(input())
if amount >= 40:
print(0)
else:
print(5)- Choose cases from the ruleThe inputs 25 and 52 check an amount below and above the threshold. The inputs 39 and 40 check the values immediately around the change, including equality at 40.
- Trace and compareFor 25 and 39, the condition amount >= 40 is false, so the program displays a fee of 5. For 40 and 52, it is true, so the program displays 0. Each traced result agrees with the expected result from the stated rule.
Common mistakes and how to avoid them
Lesson summary
- Validation uses test cases to check whether a program behaves as expected.
- A test case includes a particular input and its expected output.
- Trace the program for that input and compare the predicted output with the expectation.
- Include useful ordinary and boundary cases, especially when a decision changes at a threshold.
Check your understanding
Question 1
- 17
- 18
- 25
- 10
Show answer and explanation
Question 2
- 3
- 4
- 7
- 12
Show answer and explanation
Question 3
- The program must work for every possible input.
- The program matches expectations for those two cases.
- The expected outputs must be wrong.
- No more test cases can be useful.
Show answer and explanation
Key terms
- Input
- Information supplied to a program.
- Process
- The work a program does with its input.
- Output
- Information or a result the program displays.
- Test case
- A particular input together with the output expected for that input.
- Boundary
- A value where a program’s decision or result may change.
- Trace
- Following program instructions for a chosen input to predict the output.
Continue through ICS2O
View the complete ICS2O Ontario Grade 10 Computer Science curriculum and lessons
Tutor support for B3.4 · Validate a program with test cases
Working on B3.4 · Validate a program with test cases in ICS2O? Bring the question and your own attempt. In a live session, you can trace a small input step by step, compare expected and actual output, and test an edge case. Ask about the first step that does not make sense, then try a similar question yourself.
Explore coding tutoring · Request an eligible free 30-minute introduction. Your request is matched by subject and language; a tutor must accept before a time is confirmed.
برای مبحث B3.4 · اعتبارسنجی برنامه با موردهای آزمون در درس ICS2O، سؤال و تلاش خود را همراه بیاورید. در جلسه میتوانید یک ورودی کوچک را گامبهگام دنبال کنید، خروجی مورد انتظار و واقعی را مقایسه کنید و یک حالت مرزی را بیازمایید. راهنمای معلم برنامهنویسی را ببینید یا جلسهٔ معارفهٔ رایگان ۳۰ دقیقهای درخواست کنید. زمان جلسه پس از پذیرش معلم قطعی میشود.
About this lesson and its review
Published by DoAssignment. This reviewed lesson follows Ontario Grade 10 Computer Science (ICS2O), expectation B3.4. It is a study resource, not an official curriculum publication.
Before publication, content is checked for structure, mathematical or chemical notation, calculations, course boundaries, and readability. Errors can still occur, so corrections are welcomed.