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 be True
  • Once cause x > 0 to be False

Example 2 - Compound predicate

if x > 0 and y < 10:
    do_something()

Predicates:

  1. x > 0
  2. 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 Casex > 0y < 10Compound Result
1TrueTrueTrue
2FalseTrueFalse
3TrueFalseFalse

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:

  1. n < 0
  2. 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:

  1. n < 0
  2. n == 0
  3. 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 CaseabWhy it’s needed
T1TrueTrueFull evaluation, result is True
T2TrueFalseFull evaluation, result is False
T3FalseanyShort-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 CaseabWhy it’s needed
T1FalseFalseFull evaluation, result is False
T2FalseTrueFull evaluation, result is True
T3TrueanyShort-circuited; b is not evaluated

Back to parent page: Software Testing

Web_and_App_Development Software_Testing Blackbox_Testing SOFT3202 Predicate_Coverage