Why Testing Matters
Why Testing Matters
Writing code is only part of programming.
You also need to know if your code works correctly.
That is where testing helps.
What Is Testing?
Testing means checking that your program gives the expected result.
You compare:
- expected output
- actual output
If they match, the code works for that case.
Why Testing Is Important
Testing helps you:
- catch bugs early
- prevent broken features
- save debugging time
- improve confidence
- make changes safely
Example Without Testing
def add(a, b):
return a - b
The function should add, but it subtracts.
Without testing, the bug may go unnoticed.
Manual Test
def add(a, b):
return a - b
print(add(2, 3))
Output
-1
Expected result:
5
The test shows the function is wrong.
Fix the Code
def add(a, b):
return a + b
print(add(2, 3))
Output
5
Test Many Cases
Good code should work for different inputs.
print(add(1, 1))
print(add(0, 5))
print(add(-2, 3))
Output
2
5
1
Real Example
If a payment function fails, users may be charged incorrectly.
Testing helps prevent serious problems.
Testing Types (Simple View)
- Manual testing = run code yourself
- Automated testing = code checks code
You will learn automated testing soon.
Code Along
Create a function:
def square(n):
return n * n
Test it with:
- 2
- 3
- 5
Mini Challenge
This function is wrong:
def double(n):
return n + n + 1
Tasks:
- Test with
4 - Find the bug
- Fix it
Expected output after fix:
8
Real World Use Case
Companies test login systems, payments, reports, APIs, and mobile apps every day.
Quiz
- What is testing?
- Why is testing useful?
- What is the difference between expected and actual output?
- What can happen if code is not tested?
Assignment
Create a cube(n) function and test it with three different values.
Summary
You learned why testing matters and how tests help find bugs before users do.