2. • You are given a Python function that
calculates the factorial of a number using
recursion.
• Perform White Box Testing to analyze its
control flow, loops, and data flow.
– Identify all execution paths in the function.
– Write test cases to ensure 100% statement
and branch coverage.
– Implement the test cases in Python using
unittest.
Lab Question: White Box Testing on a Python Function
4. • Step 1: Identify Execution Paths
– The function has three main paths:
• If n < 0, return "Invalid input".
• If n == 0 or n == 1, return 1.
• If n > 1, recursively compute n * factorial(n - 1).
• We need to create test cases covering all
these paths.
Solution: White Box Testing Approach
6. • Statement Coverage:
– Every line of code is executed at least once.
• Branch Coverage:
– Each condition (if, elif, else) is tested with
different inputs.
• Loop Coverage:
– The recursive function calls itself until n == 1,
ensuring all iterations are tested.
Step 3: Validate Coverage
8. • Step 1: Introduce an Error in the Code
• What is Wrong?
– Instead of decrementing n (n - 1), the function increments n (n
+ 1).
– This results in infinite recursion, causing a RecursionError
Simulating an Error in the Factorial Function and Fixing It
9. • Expected Output (with the error)
• E means an error occurred.
• RecursionError shows that the function never stops
calling itself, leading to a crash.
Step 2: Run the Test Cases and Observe the Error
10. • We correct the recursion step by changing
(n + 1) back to (n - 1):
Step 3: Fix the Error