Consider a simple function with two if statements in sequence. How many unique execution paths should be tested to achieve 100% path coverage?
def example(x, y): if x > 0: print("X positive") if y > 0: print("Y positive")
Each if statement can be true or false independently.
Each if statement has two possible outcomes (true or false). With two independent ifs, total paths = 2 * 2 = 4.
What is the output when calling test_func(3, -1)?
def test_func(a, b): if a > 0: print("A positive") else: print("A non-positive") if b > 0: print("B positive") else: print("B non-positive") # Call: test_func(3, -1)
Check each if condition with the given inputs.
a=3 > 0 so prints 'A positive'. b=-1 <= 0 so prints 'B non-positive'.
Which assertion best verifies that all paths in the following function have been executed at least once?
def check_values(x, y): if x > 10: if y < 5: return "Path 1" else: return "Path 2" else: return "Path 3"
All unique paths must be covered regardless of order.
Using a set comparison ensures all unique paths are covered, order does not matter.
Given this code, a tester claims there are 3 paths. Is this correct? If not, what is the correct number of paths?
def sample(x, y): if x > 0: if y > 0: return 1 return 0
Consider when the second if is evaluated.
The second if is inside the first if, creating three distinct paths: 1. x <= 0 (first false) -> return 0; 2. x > 0 and y > 0 (both true) -> return 1; 3. x > 0 and y <= 0 (first true, second false) -> return 0. These are unique sequences of branches despite sharing a return statement.
You have a function with three nested if statements. How many test cases are needed to guarantee 100% path coverage?
Function structure:
if A:
if B:
if C:
do X
else:
do Y
else:
do Z
else:
do WCount unique paths from root to leaves in the nested if structure.
Paths are:
1) A true, B true, C true
2) A true, B true, C false
3) A true, B false
4) A false
So total 4 unique paths.