0
0
PyTesttesting~10 mins

Branch coverage in PyTest - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test checks a simple function with an if-else branch. It verifies that both branches are tested to achieve full branch coverage.

Test Code - pytest
PyTest
def is_even(num):
    if num % 2 == 0:
        return True
    else:
        return False

import pytest

def test_is_even():
    assert is_even(4) == True
    assert is_even(5) == False
Execution Trace - 6 Steps
StepActionSystem StateAssertionResult
1Test startspytest test runner initialized-PASS
2Calls is_even(4)Function receives input 4Check if 4 % 2 == 0 branch is takenPASS
3Returns True for input 4Function returns TrueAssert returned value is TruePASS
4Calls is_even(5)Function receives input 5Check if else branch is taken for 5PASS
5Returns False for input 5Function returns FalseAssert returned value is FalsePASS
6Test endsAll assertions passedBranch coverage achieved for both if and elsePASS
Failure Scenario
Failing Condition: Only one branch of the if-else is tested, e.g., only is_even(4) is tested
Execution Trace Quiz - 3 Questions
Test your understanding
What does branch coverage ensure in this test?
AOnly the if branch is tested
BOnly the else branch is tested
CBoth the if and else branches are executed during testing
DNo branches are tested
Key Result
To achieve full branch coverage, tests must include inputs that cause every branch of conditional statements to execute.