11. Testing Your Code
1. Simple Definition & Meaning
Testing means writing small programs that check if your main program is working correctly. It is like an automatic spell-checker for your code.
2. Real-World Examples
- Example 1: Before driving a long distance, you check your car's tires and oil. That's testing!
- Example 2: Doing math homework and using a calculator to check if your answers are right.
3. Important Points
- Python has a built-in module called
unittest. - You write "test cases" to assert (prove) that something equals what you expect it to equal.
- If you change your code later, tests make sure you didn't accidentally break anything.
📝 Exam Notes (Quick Revision)
- unittest: The standard testing library in Python.
- assertEqual(a, b): Checks that a and b are equal.
- Test Case: A collection of unit tests that prove a function behaves as expected.