Testing and Debugging
Testing and Debugging
Objective:
To understand the importance of software testing by identifying
bugs, writing test cases, and fixing errors in a small program. This
activity will enhance debugging and test-writing skills.
Activity Steps:
1. Setup:
Provide or create a small, buggy program (Python or Java).
Example: A Calculator program with intentional errors such as:
o Incorrect mathematical operations.
o Missing or improperly handled edge cases (e.g., division
by zero).
o Syntax errors or logic errors.
Example Program: Buggy Python Calculator
python
Copy code
def add(a, b):
return a - b # Intentional bug
calculator()
2. Assign Tasks:
Task 1: Identify Bugs
Run the program with test inputs to uncover issues.
Document the bugs (e.g., incorrect addition, missing error
handling).
Task 2: Write Test Cases
Write a set of test cases for each function in the program using
a testing framework (e.g., unittest in Python or JUnit in Java).
Example Python Test Cases:
Python
import unittest
from calculator import add, divide
class TestCalculator(unittest.TestCase):
def test_add(self):
self.assertEqual(add(3, 2), 5) # This will fail due to the bug
def test_divide(self):
self.assertEqual(divide(6, 2), 3)
def test_divide_by_zero(self):
with self.assertRaises(ZeroDivisionError):
divide(6, 0)
if __name__ == '__main__':
unittest.main()
Task 3: Fix Errors
Correct the identified bugs in the program:
o Fix the addition function: return a + b
o Add error handling for division by zero.
o Refactor the program for better readability.
Task 4: Validate with Tests
Rerun the test cases to ensure all bugs are fixed and edge cases
are handled properly.
Outcome:
By completing this activity, participants will:
Gain practical experience in identifying and fixing software
bugs.
Learn how to write and automate test cases.
Understand the importance of thorough testing and debugging
in software development.