Analyse an existing program and make amendments to enhance functionality
12.3 Program Testing and Maintenance
What is Testing?
Testing is like checking your homework before handing it in. It helps you find mistakes (bugs) and ensures the program does exactly what it is supposed to do. 🧪
Maintenance: Keeping Your Code Healthy
After the program runs, you may need to update it to add new features or fix problems. Think of it as gardening – you prune, fertilise, and make sure the plant stays strong. 🌱
Key Steps in Program Testing
- Understand the Requirements – Know what the program should achieve. Write down the expected behaviour. 📚
- Write Test Cases – Create a list of inputs and the expected outputs. Think of each test case as a recipe ingredient. 🍴
- Run the Tests – Execute the program with each test case. Record the results.
- Debug – If a test fails, locate the bug (the “🐛”) and fix it.
- Refactor – Improve the code structure without changing behaviour. Make it cleaner and easier to read.
- Document – Update comments, README, or any documentation so future developers understand the changes.
Example: A Simple Calculator with a Bug
Consider this Python snippet that adds two numbers:
def add(a, b):
return a - b # ❌ Bug: should be a + b
Analysis:
- Requirement: Return the sum of
aandb. - Bug identified: The function uses subtraction instead of addition.
- Fix: Change
return a - btoreturn a + b.
After the fix, the program passes all test cases.
Test Case Table
| Test Case | Input (a, b) | Expected Output | Result |
|---|---|---|---|
| TC1 | (2, 3) | 5 | Pass |
| TC2 | (-1, 4) | 3 | Pass |
| TC3 | (0, 0) | 0 | Pass |
Exam Preparation Tips
🔍 Understand the process: Know each step of testing – from writing test cases to debugging.
🧩 Practice analysing code: Given a snippet, identify potential bugs and suggest fixes.
📄 Use diagrams: Flowcharts or tables can help explain your reasoning.
💡 Explain your reasoning: In written answers, show how you derived the test cases and why a particular fix works.
🕒 Time management: Allocate time to read the code, write tests, and document changes.
Analogy: The Debugging Detective
Imagine you are a detective 🕵️♂️ trying to solve a mystery. The program is the crime scene, the bug is the hidden clue, and your test cases are the questions you ask the witnesses. Each test that fails is a clue that leads you closer to the culprit (the bug). Once you catch the culprit, you can fix the crime and restore order.
Revision
Log in to practice.