Welcome to the World of Testing!
Ever played a video game that crashed right when you were about to win? Or used an app that gave you a weird error message? That happens when programs aren't tested properly. In this chapter, we are going to learn how programmers find and fix those "bugs" to create robust programs—programs that are strong, reliable, and hard to break!
Don't worry if some of this seems like a lot to take in. We’ll break it down into small, bite-sized pieces with plenty of examples from everyday life.
1. Why Do We Test?
The main purpose of testing is simple: to make sure the program works exactly how it is supposed to! Specifically, testing helps us:
• Find and fix errors (bugs) in the code.
• Ensure the program meets the requirements (does what the user asked for).
• Check that the program can handle unexpected inputs without crashing.
Quick Review: Testing isn't about proving a program is perfect; it’s about finding as many mistakes as possible before the user sees them!
2. Types of Testing
There are two main ways programmers test their work. Think of it like a chef making a new soup.
Iterative Testing
Iterative testing happens during the development process. A programmer writes a small piece of code (a module), tests it, fixes any bugs, and then moves on to the next part.
Analogy: A chef tasting the soup while they are still cooking it, adding a bit more salt or water as they go.
Final / Terminal Testing
Final testing (also called terminal testing) happens at the very end of the process, once all the pieces of the program have been put together. This checks if the whole system works correctly as a single unit.
Analogy: Serving the completed bowl of soup to a customer for the final verdict.
Key Takeaway: Iterative = During development. Final = After development is finished.
3. Identifying Errors
In Computer Science, not all mistakes are the same. You need to know the difference between the two main types of errors.
Syntax Errors
A syntax error is a "grammar" mistake in the code. Programming languages have very strict rules (syntax). If you misspell a word like prnt() instead of print(), or forget a bracket, the computer won't understand what to do. The program will not run at all if there is a syntax error.
Logic Errors
A logic error is a mistake in the "thinking" of the program. The code is written perfectly fine (no grammar mistakes), so the program will run, but it will give the wrong result.
Example: You want to add two numbers \(2 + 2\), but you accidentally typed \(2 - 2\). The computer does the math correctly, but gives you 0 instead of 4. That’s a logic error!
Did you know? The term "bug" comes from the 1940s when a real moth got stuck inside a computer and caused it to stop working!
4. Selecting Suitable Test Data
To test a program properly, you can't just type in random stuff. You need a plan! Let’s imagine we have a program that asks for a student's test score, which must be between 0 and 100.
We use four specific types of test data:
1. Normal Data
This is data that is within the range and should be accepted.
Example: 45, 82, or 10.
2. Boundary Data
This is data at the very edges of the allowed range. These are the most common places for logic errors to hide!
Example: 0 and 100. (We should also test -1 and 101 to see if they are rejected).
3. Invalid Data
This is data of the correct type (a number) but it is outside the range.
Example: -50 or 150. The program should reject these.
4. Erroneous Data
This is data of the wrong type entirely. The computer is expecting a number, but you give it something else.
Example: Writing "eighty" instead of 80, or typing "banana". The program should handle this without crashing.
Memory Aid - The "NIBE" Trick:
Normal
Invalid
Boundary
Erroneous
5. Test Plans and Refining
A test plan is a document (usually a table) where the programmer lists everything they are going to test before they start. It usually includes:
• Test Number: To keep track.
• Description: What are we testing?
• Test Data: What value are we typing in?
• Expected Result: What should happen?
• Actual Result: What actually happened?
• Pass/Fail: Did it work?
Refining Algorithms
Once you have tested your program and found errors, you must refine it. This simply means fixing the mistakes and improving the code so that it is more robust. If your test plan shows a "Fail," you go back to the code, fix the logic, and test it again!
Common Mistake to Avoid: Students often confuse Invalid and Erroneous data. Remember: Invalid is a "bad value" of the right type (e.g., -5 for age). Erroneous is the wrong type of data altogether (e.g., "Hello" for age).
Summary: The Quick Review
1. Testing purpose: To find bugs and ensure requirements are met.
2. Iterative: Testing while you code. Final: Testing at the very end.
3. Syntax Error: Code won't run (grammar mistake). Logic Error: Code runs but gives wrong answer.
4. Test Data: Normal (standard), Boundary (edges), Invalid (wrong value), Erroneous (wrong type).
5. Test Plan: A table used to record and check results to refine the program.