Show understanding of ways of exposing and avoiding faults in programs
12.3 Program Testing and Maintenance
Testing Techniques
Think of testing as a quality‑check list for a new gadget. Each technique looks at a different part of the gadget to make sure it works. Below are the main types of tests you’ll use in A‑Level CS.
| Test Type | Purpose | Example | Analogy |
|---|---|---|---|
| Unit Testing | Check a single function or method. | Test add(a,b) with many inputs. |
🔧 Test each tool in a toolbox separately. |
| Integration Testing | Check interactions between modules. | Test login() with database.connect(). |
🚧 Test how a bridge supports traffic. |
| System Testing | Test the whole system as a user would. | Run the full e‑commerce site. | 🏠 Test the entire house before moving in. |
| Acceptance Testing | Confirm the system meets user requirements. | Client tests the final product. | 🛒 Test a product before buying it. |
| Regression Testing | Ensure new changes don’t break existing features. | Run all tests after a bug fix. | 🔁 Test the same recipe after adding a new ingredient. |
Fault Exposure
Faults are hidden bugs that can surface only when the program runs. Here’s how you can expose them early:
- Test‑Driven Development (TDD) – Write tests before code. The tests act like a safety net.
- Boundary Value Analysis – Test values at the edges of input ranges. Example:
age = 0orage = 120. - Equivalence Partitioning – Divide inputs into valid/invalid groups and test one from each.
- Static Analysis Tools – Scan code for potential errors (e.g.,
undefined variable). - Code Coverage Metrics – Aim for high coverage (e.g., 80%+). Use tools like
gcovorJaCoCo.
Fault Avoidance
Avoiding faults is like building a sturdy bridge. Here are the best practices:
- Clear Requirements – Write precise, unambiguous specifications.
- Design by Contract – Define pre‑conditions, post‑conditions, and invariants.
- Code Reviews – Peer review catches mistakes early.
- Consistent Coding Standards – Use naming conventions and style guides.
- Assertions – Embed checks in code:
assert(x > 0); - Version Control – Commit small, logical changes with clear messages.
- Automated Build & Test Pipelines – Use CI/CD to run tests on every commit.
Maintenance Types
After a program is released, it still needs care. Maintenance is split into four main types:
| Maintenance Type | Purpose | Example |
|---|---|---|
| Corrective | Fix bugs found after release. | Patch a security vulnerability. |
| Adaptive | Adjust to new hardware or OS. | Update drivers for a new GPU. |
| Perfective | Improve performance or usability. | Refactor code to reduce runtime from $O(n^2)$ to $O(n\log n)$. |
| Preventive | Prevent future faults. | Add unit tests for a new feature. |
Best Practices for Students
1️⃣ Write tests first. Think of tests as a safety net that lets you experiment without fear.
2️⃣ Keep code small. Small functions are easier to test and debug.
3️⃣ Use descriptive names. A function called calculateTotal() is clearer than calc().
4️⃣ Document assumptions. Write comments that explain why a piece of code exists.
5️⃣ Review and refactor. Regularly revisit old code; it’s like cleaning a room to avoid clutter.
6️⃣ Celebrate small wins. Every bug fixed is a step toward mastery. 🎉
Remember: Testing is not a one‑off event but a continuous practice. Treat it like a game where you’re always looking for hidden bugs (🐞) and fixing them before they cause trouble. Happy coding! 🚀
Revision
Log in to practice.