Predicate coverage or condition coverage is used in white-box testing to check if all the basic decisions (conditions) in the code have been evaluated to both True
and False
at least once during testing.
A predicate
A predicate is a boolean expression, sometimes that evaluates to True
or False
.
It can be:
x > 0
n == 10
not is_valid
- Or even part of a compound condition like
(x > 0 and y < 5)
Examples
Example 1
if x > 0:
do_something()
Here:
- The predicate is
x > 0
To achieve 100% predicate coverage, your test cases must:
- Once cause
x > 0
to beTrue
- Once cause
x > 0
to beFalse
Example 2 - Compound predicate
if x > 0 and y < 10:
do_something()
Predicates:
x > 0
y < 10
Here to achieve 100% predicate coverage, it requires each individual predicate to evaluate to both True and False at least once, regardless of the outcome of the entire if condition.
Test Case | x > 0 | y < 10 | Compound Result |
---|---|---|---|
1 | True | True | True |
2 | False | True | False |
3 | True | False | False |
Example 3 - Loops
def factorial(n):
if n < 0:
return None # Predicate 1: n < 0
if n == 0:
return 1 # Predicate 2: n == 0
result = 1
for i in range(1, n + 1): # Implicit condition in loop (range controls it)
result *= i
return result
Predicates:
n < 0
n == 0
These are explicit Boolean expressions inside if
statements.
What about the for loop
The for loop:
for i in range(1, n + 1):
This is not a predicate by itself — it uses the range
function to determine how many times the loop executes, but there’s no explicit Boolean condition we test directly (like in while
or if
). So we do not count this as a predicate for predicate coverage.
Example 4 - While loop
def factorial(n):
if n < 0:
return None # Predicate 1
if n == 0:
return 1 # Predicate 2
result = 1
i = 1
while i <= n: # Predicate 3
result *= i
i += 1
return result
There are 3 predicates in this code:
n < 0
n == 0
i <= n
i <= n
in the while loop is a simple boolean expression.
Short-circuit evaluation
In Python, logical operators like and and or use a shortcut: If the result of the full expression is already known after evaluating the first part (a), Python skips evaluating the second part (b). This is called short-circuit evaluation.
and
operator
if a and b:
...
In Python, evaluation of a boolean expression may be stopped if the truth value of the entire expression has been determined from part of the expression.
If a
is False
, there’s no need to check b
— the result will be False no matter what. Python stops early, this is short-circuiting.
Therefore, to test a and b
, we would need the following cases:
Test Case | a | b | Why it’s needed |
---|---|---|---|
T1 | True | True | Full evaluation, result is True |
T2 | True | False | Full evaluation, result is False |
T3 | False | any | Short-circuited; b is not evaluated |
or
operator
if a or b:
...
To evaluate as True
, only one of a
or b
needs to be True
: If a
is True, Python doesn’t evaluate b
— the result is already True
.
Therefore, to test a or b
, we would need the following cases:
Test Case | a | b | Why it’s needed |
---|---|---|---|
T1 | False | False | Full evaluation, result is False |
T2 | False | True | Full evaluation, result is True |
T3 | True | any | Short-circuited; b is not evaluated |
Back to parent page: Software Testing
Web_and_App_Development Software_Testing Blackbox_Testing SOFT3202 Predicate_Coverage